use super::*;
fn catalog_document(default_key: Option<&str>) -> OValue {
use serde_json::json;
let mut api_key = json!({
"type": "string",
"label": "API Key",
"description": [{"type": "subHeading", "text": "The API key of the provider"}],
"encrypted": true,
});
if let Some(value) = default_key {
api_key["default"] = json!(value);
}
let doc = json!({
"evals": [{
"description": [{"type": "subHeading", "text": "Checks prompts with a vendor scanner"}],
"guardrailCategory": "security",
"id": "vendor.scan",
"name": "Vendor scan",
"parameters": {
"properties": {
"apiKey": api_key,
"credentials": {
"type": "object",
"properties": {
"client_secret": {"type": "string", "label": "Client secret"},
"region": {"type": "string", "default": "us-east-1"},
},
"required": ["client_secret"],
},
"token": {"type": ["string", "null"], "description": [{"type": "subHeading", "text": "Optional token"}]},
"headers": {"type": "object", "description": [{"type": "subHeading", "text": "Extra headers"}]},
"categories": {"type": "array", "items": {"type": "string", "enum": ["prompt", "response"]}},
"webhookURL": {"type": "string", "format": "uri"},
},
"required": ["apiKey"],
"type": "object",
},
"supportedHooks": ["beforeRequestHook"],
"type": "guardrail",
}],
"object": "list",
});
parse(&doc.to_string()).unwrap()
}
#[test]
fn d238_a_credential_named_schema_withholds_and_registers_no_words() {
let catalog = catalog_document(None);
let received = to_compact(&catalog).unwrap();
let secrets = SecretSet::default();
let mut doc = catalog.clone();
apply(&mut doc, &PLAIN, &secrets);
assert_eq!(to_compact(&doc).unwrap(), received);
for word in [
"string",
"object",
"subHeading",
"list",
"API Key",
"us-east-1",
"The API key of the provider",
] {
assert!(!secrets.carried_by(word), "{word} registered");
}
let catalog = catalog_document(Some("FAKE-GW-SECRET-51"));
let secrets = SecretSet::default();
let mut doc = catalog.clone();
apply(&mut doc, &PLAIN, &secrets);
let text = to_compact(&doc).unwrap();
assert!(!text.contains("FAKE-GW-SECRET-51"), "{text}");
assert!(text.contains(r#""default":"[withheld]""#), "{text}");
assert!(text.contains(r#""object":"list""#), "{text}");
assert!(text.contains(r#""label":"API Key""#), "{text}");
assert!(secrets.carried_by("FAKE-GW-SECRET-51"));
assert!(!secrets.carried_by("string"));
let (text, secrets) = applied(
r#"{"properties":{"api_key":"FAKE-GW-SECRET-52","credentials":{"type":"basic","password":"pw-short3"},"region":"us"}}"#,
&PLAIN,
);
assert_eq!(
text,
r#"{"properties":{"api_key":"[withheld]","credentials":"[withheld]","region":"us"}}"#
);
assert!(secrets.carried_by("FAKE-GW-SECRET-52"));
assert!(secrets.carried_by("pw-short3"));
assert!(!secrets.carried_by("basic"));
}
#[test]
fn d238_a_credential_named_inside_a_schema_is_withheld() {
for (doc, policy, secret) in [
(
r#"{"properties":{"db":{"type":"object","password":"PROBE-PW-001x"}},"note":"PROBE-PW-001x"}"#,
&PLAIN,
"PROBE-PW-001x",
),
(
r#"{"properties":{"openai":{"type":"string","api_key":"FAKE-GW-SECRET-0060"}},"note":"FAKE-GW-SECRET-0060"}"#,
&PLAIN,
"FAKE-GW-SECRET-0060",
),
(
r#"{"properties":{"openai":{"type":"string","key":"sk-0123456789abcdef"}},"note":"sk-0123456789abcdef"}"#,
&PLAIN,
"sk-0123456789abcdef",
),
(
r#"{"properties":{"db":{"type":"string","passwd":"pw-short60"}},"note":"pw-short60"}"#,
&PLAIN,
"pw-short60",
),
(
r#"{"properties":{"svc":{"type":"object","oauth_metadata":{"x":"pw-short61"}}},"note":"pw-short61"}"#,
&PLAIN,
"pw-short61",
),
(
r#"{"configurations":{"properties":{"auth":{"type":"object","client_secret":"pw-short62"}}},"note":"pw-short62"}"#,
&INTEGRATIONS,
"pw-short62",
),
(
r#"{"config":"{\"properties\":{\"t\":{\"type\":\"string\",\"secret\":\"pw-short63\"}}}","note":"pw-short63"}"#,
&PLAIN,
"pw-short63",
),
(
r#"{"properties":{"list":{"type":"array","items":{"type":"object","token":"FAKE-GW-SECRET-0064"}}},"note":"FAKE-GW-SECRET-0064"}"#,
&PLAIN,
"FAKE-GW-SECRET-0064",
),
(
r#"{"properties":{"api_key":{"type":"object","key":"pw-short65"}},"note":"pw-short65"}"#,
&PLAIN,
"pw-short65",
),
] {
let (text, secrets) = applied(doc, policy);
assert!(!text.contains(secret), "{text}");
assert!(text.contains(r#""note":"[withheld]""#), "{text}");
assert!(secrets.carried_by(secret), "{doc}");
}
let (text, secrets) = applied(
r#"{"properties":{"svc":{"type":"object","description":"Service","headers":{"x-api":"pw-short66","accept":"*/*"}}}}"#,
&PLAIN,
);
assert_eq!(
text,
r#"{"properties":{"svc":{"type":"object","description":"Service","headers":{"x-api":"[withheld]","accept":"*/*"}}}}"#
);
assert!(!secrets.carried_by("Service"));
}
#[test]
fn d238_a_credential_in_a_credential_property_description_is_withheld() {
let (text, secrets) = applied(
r#"{"properties":{"api_key":{"type":"string","description":"For tests use FAKE-GW-SECRET-0068.","title":"Bearer abcdefgh12345678","label":"API Key","pattern":"^sk-[a-z0-9]{16}$"},"note":{"type":"string","description":"For tests use FAKE-GW-SECRET-0068."}}}"#,
&PLAIN,
);
assert_eq!(
text,
r#"{"properties":{"api_key":{"type":"string","description":"[withheld]","title":"Bearer [withheld]","label":"API Key","pattern":"^sk-[a-z0-9]{16}$"},"note":{"type":"string","description":"For tests use FAKE-GW-SECRET-0068."}}}"#
);
assert!(!secrets.carried_by("FAKE-GW-SECRET-0068"));
let ordinary = r#"{"properties":{"apiKey":{"type":"string","description":[{"type":"subHeading","text":"Sent as x-portkey-api-key, see https://docs.example.test/guides/api-keys/rotation"}],"title":"Bearer token","label":"API Key"}}}"#;
let (text, _) = applied(ordinary, &PLAIN);
assert_eq!(text, ordinary);
}
#[test]
fn d238_an_empty_schema_value_prints() {
let empty = r#"{"properties":{"client_secret":{"type":"string","default":"","const":"","enum":[],"examples":[],"label":"Client secret"},"apiKey":{"type":"object","default":{}}}}"#;
let (text, secrets) = applied(empty, &PLAIN);
assert_eq!(text, empty);
assert!(checked(&text));
assert_eq!(secrets.form_count(), 0);
let (text, _) = applied(
r#"{"properties":{"client_secret":{"type":"string","default":" ","examples":[""],"enum":["PIN-REVIEW-0188x"],"x-sample":""}}}"#,
&PLAIN,
);
assert_eq!(
text,
r#"{"properties":{"client_secret":{"type":"string","default":"[withheld]","examples":"[withheld]","enum":"[withheld]","x-sample":"[withheld]"}}}"#
);
assert!(checked(&text));
assert!(!checked(
r#"{"properties":{"client_secret":{"type":"string","default":" "}}}"#
));
}