feat(import): bind OpenAPI security schemes safely
This commit is contained in:
1 parent
c562d5368d
commit
41a0e46110
38 files changed
+4651
-590
No files matched your search
@@ -0,0 +1,780 @@
|
||||
mod security_schemes {
|
||||
use crank_import::rest::{
|
||||
ImportFindingSeverity, ImportParseError, NormalizationConfig, NormalizedSecuritySchemeKind,
|
||||
SecurityParameterLocation, SecurityRequirementsSource, normalize_verified_document,
|
||||
preview_from_ir, scan_secret_literals, validate_normalized_ir,
|
||||
};
|
||||
|
||||
fn normalize_document(
|
||||
document: &str,
|
||||
config: &NormalizationConfig,
|
||||
) -> Result<crank_import::rest::NormalizedIr, ImportParseError> {
|
||||
normalize_verified_document(
|
||||
document,
|
||||
crank_import::rest::SourceDigest::parse("b".repeat(64)).unwrap(),
|
||||
config,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ac_1_projects_openapi_security_scheme_identity_onto_operation_metadata() {
|
||||
let document = r#"
|
||||
openapi: 3.1.0
|
||||
info: { title: Security metadata }
|
||||
components:
|
||||
securitySchemes:
|
||||
WorkspaceBearer:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: JWT
|
||||
paths:
|
||||
/private:
|
||||
get:
|
||||
operationId: private
|
||||
security: [{ WorkspaceBearer: [] }]
|
||||
responses: { '200': { description: ok } }
|
||||
"#;
|
||||
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let operation = ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| operation.path == "/private")
|
||||
.unwrap();
|
||||
let operation_contract = serde_json::to_string(operation).unwrap();
|
||||
|
||||
// This assertion deliberately inspects the operation projection, not the
|
||||
// canonical source tree, so copying the original document is insufficient.
|
||||
assert!(operation_contract.contains("WorkspaceBearer"));
|
||||
assert!(operation_contract.contains("bearer"));
|
||||
assert!(operation_contract.contains("JWT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ac_6_never_copies_literal_secret_canaries_into_normalized_ir() {
|
||||
const CANARY: &str = "atdd-secret-canary-2-10";
|
||||
let document = format!(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {{ title: Secret canary }}
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer:
|
||||
type: http
|
||||
scheme: bearer
|
||||
x-example-token: {CANARY}
|
||||
x-credentials: {CANARY}
|
||||
x-ciphertext: {CANARY}
|
||||
paths:
|
||||
/private:
|
||||
get:
|
||||
security: [{{ Bearer: [] }}]
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
example: {{ token: {CANARY} }}
|
||||
"#
|
||||
);
|
||||
|
||||
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
|
||||
let normalized = serde_json::to_string(&ir).unwrap();
|
||||
|
||||
assert!(
|
||||
!normalized.contains(CANARY),
|
||||
"normalized IR must not retain literal credential canaries"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn security_requirements_inherit_and_operation_empty_array_disables_auth() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Requirements }
|
||||
components:
|
||||
securitySchemes:
|
||||
Key: { type: apiKey, in: header, name: X-Workspace-Key }
|
||||
security: [{ Key: [] }]
|
||||
paths:
|
||||
/inherited:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
/public:
|
||||
get:
|
||||
security: []
|
||||
responses: { '200': { description: ok } }
|
||||
/and:
|
||||
get:
|
||||
security: [{ Key: [], Other: [] }]
|
||||
responses: { '200': { description: ok } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let inherited = ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| operation.path == "/inherited")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
inherited.security_requirements.source,
|
||||
SecurityRequirementsSource::Document
|
||||
);
|
||||
assert_eq!(
|
||||
inherited.security_requirements.alternatives[0].schemes,
|
||||
["Key"]
|
||||
);
|
||||
let public = ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| operation.path == "/public")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
public.security_requirements.source,
|
||||
SecurityRequirementsSource::Operation
|
||||
);
|
||||
assert!(public.security_requirements.alternatives.is_empty());
|
||||
let and = ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| operation.path == "/and")
|
||||
.unwrap();
|
||||
assert!(
|
||||
and.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_security_and_requirement")
|
||||
);
|
||||
assert_eq!(
|
||||
and.security_requirements.alternatives[0].schemes,
|
||||
["Key", "Other"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes() {
|
||||
let document = r#"
|
||||
openapi: 3.1.0
|
||||
info: { title: Alternatives }
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: JWT
|
||||
User:
|
||||
type: apiKey
|
||||
in: query
|
||||
name: access_token
|
||||
CookieKey:
|
||||
type: apiKey
|
||||
in: cookie
|
||||
name: session
|
||||
security:
|
||||
- Bearer: []
|
||||
- User: []
|
||||
- CookieKey: []
|
||||
paths:
|
||||
/private:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let first = preview_from_ir(&ir);
|
||||
let second = preview_from_ir(&ir);
|
||||
assert_eq!(
|
||||
serde_json::to_vec(&first).unwrap(),
|
||||
serde_json::to_vec(&second).unwrap()
|
||||
);
|
||||
let candidate = &first.groups[0].operations[0];
|
||||
assert_eq!(candidate.security_requirements.alternatives.len(), 3);
|
||||
assert!(
|
||||
matches!(candidate.security_schemes["Bearer"].kind, NormalizedSecuritySchemeKind::Http { ref format_hint, .. } if format_hint.as_deref() == Some("JWT"))
|
||||
);
|
||||
assert!(
|
||||
matches!(candidate.security_schemes["User"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } if parameter_name == "access_token")
|
||||
);
|
||||
assert!(
|
||||
matches!(candidate.security_schemes["CookieKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Cookie, ref parameter_name, .. } if parameter_name == "session")
|
||||
);
|
||||
assert!(!candidate.security_requirements.alternatives[2].supported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger2_security_definitions_project_basic_and_api_key_header_and_query() {
|
||||
let document = r#"
|
||||
swagger: '2.0'
|
||||
info: { title: Swagger security }
|
||||
securityDefinitions:
|
||||
BasicAuth: { type: basic }
|
||||
HeaderKey: { type: apiKey, in: header, name: X-Api-Key }
|
||||
QueryKey: { type: apiKey, in: query, name: api_key }
|
||||
security: [{ BasicAuth: [] }, { HeaderKey: [] }]
|
||||
paths:
|
||||
/private:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
assert!(
|
||||
matches!(ir.security_schemes["BasicAuth"].kind, NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic")
|
||||
);
|
||||
assert!(
|
||||
matches!(ir.security_schemes["HeaderKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Header, ref parameter_name, .. } if parameter_name == "X-Api-Key")
|
||||
);
|
||||
assert!(
|
||||
matches!(ir.security_schemes["QueryKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } if parameter_name == "api_key")
|
||||
);
|
||||
assert_eq!(ir.operations[0].security_requirements.alternatives.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_security_schemes_create_blockers_without_copying_credentials() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Unsupported }
|
||||
components:
|
||||
securitySchemes:
|
||||
OAuth: { type: oauth2, flows: {} }
|
||||
Oidc: { type: openIdConnect, openIdConnectUrl: https://issuer.test }
|
||||
Mutual: { type: mutualTLS }
|
||||
paths:
|
||||
/private:
|
||||
get:
|
||||
security: [{ OAuth: [read] }, { Oidc: [] }, { Mutual: [] }, { Unknown: [] }]
|
||||
responses: { '200': { description: ok } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let codes = ir.operations[0]
|
||||
.findings
|
||||
.iter()
|
||||
.map(|finding| finding.code.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(codes.contains(&"unsupported_security_scheme"));
|
||||
assert!(codes.contains(&"unresolved_security_scheme"));
|
||||
assert!(!codes.contains(&"invalid_security_scopes"));
|
||||
assert!(!codes.contains(&"unexpected_security_scopes"));
|
||||
assert!(ir.operations[0].findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirements"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}));
|
||||
assert!(
|
||||
ir.operations[0]
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.iter()
|
||||
.all(|alternative| !alternative.supported)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalized_ir_rejects_unrelated_security_findings_and_accepts_valid_security_ir() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Validation }
|
||||
components:
|
||||
securitySchemes:
|
||||
OAuth: { type: oauth2, flows: {} }
|
||||
paths:
|
||||
/private:
|
||||
get:
|
||||
security: [{ OAuth: [] }]
|
||||
responses: { '200': { description: ok } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
validate_normalized_ir(&ir).unwrap();
|
||||
|
||||
let mut tampered = ir;
|
||||
let finding = tampered.operations[0]
|
||||
.findings
|
||||
.iter_mut()
|
||||
.find(|finding| finding.code == "unsupported_security_scheme")
|
||||
.unwrap();
|
||||
finding.location.pointer = "/info/title".to_owned();
|
||||
finding.construct_id = "3.0.3:/info/title".to_owned();
|
||||
assert_eq!(
|
||||
validate_normalized_ir(&tampered),
|
||||
Err(ImportParseError::InvalidDocument)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_or_alternative_does_not_block_a_supported_choice() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Mixed alternatives }
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer: { type: http, scheme: bearer }
|
||||
OAuth: { type: oauth2, flows: {} }
|
||||
security: [{ OAuth: [] }, { Bearer: [] }]
|
||||
paths:
|
||||
/private:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let operation = &ir.operations[0];
|
||||
assert_eq!(operation.security_requirements.alternatives.len(), 2);
|
||||
assert!(!operation.security_requirements.alternatives[0].supported);
|
||||
assert!(operation.security_requirements.alternatives[1].supported);
|
||||
assert!(
|
||||
!operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.code == "unsupported_security_requirements")
|
||||
);
|
||||
assert!(operation.findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_scheme"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openapi_invalid_http_scopes_are_selection_aware_and_valid_or_sibling_survives() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Invalid HTTP scopes }
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer: { type: http, scheme: bearer }
|
||||
security:
|
||||
- Bearer: [read]
|
||||
- Bearer: "read"
|
||||
- Bearer: [1]
|
||||
- Bearer: []
|
||||
paths:
|
||||
/private:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let operation = &ir.operations[0];
|
||||
let alternatives = &operation.security_requirements.alternatives;
|
||||
assert_eq!(alternatives.len(), 4);
|
||||
assert!(
|
||||
alternatives[..3]
|
||||
.iter()
|
||||
.all(|alternative| !alternative.supported)
|
||||
);
|
||||
assert!(alternatives[3].supported);
|
||||
assert!(operation.findings.iter().any(|finding| {
|
||||
finding.code == "unexpected_security_scopes"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
}));
|
||||
assert!(operation.findings.iter().any(|finding| {
|
||||
finding.code == "invalid_security_scopes"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
}));
|
||||
assert!(
|
||||
!operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| { finding.code == "unsupported_security_requirements" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swagger_invalid_api_key_scopes_do_not_hide_valid_or_sibling() {
|
||||
let document = r#"
|
||||
swagger: '2.0'
|
||||
info: { title: Invalid Swagger scopes }
|
||||
securityDefinitions:
|
||||
HeaderKey: { type: apiKey, in: header, name: X-Api-Key }
|
||||
BasicAuth: { type: basic }
|
||||
security:
|
||||
- HeaderKey: [read]
|
||||
- BasicAuth: []
|
||||
paths:
|
||||
/private:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let operation = &ir.operations[0];
|
||||
assert!(!operation.security_requirements.alternatives[0].supported);
|
||||
assert!(operation.security_requirements.alternatives[1].supported);
|
||||
assert!(operation.findings.iter().any(|finding| {
|
||||
finding.code == "unexpected_security_scopes"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
}));
|
||||
assert!(
|
||||
!operation
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| { finding.code == "unsupported_security_requirements" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scanner_finds_examples_defaults_extensions_and_url_userinfo_without_values() {
|
||||
const CANARIES: [&str; 5] = [
|
||||
"example-token-secret",
|
||||
"default-password-secret",
|
||||
"extension-api-key-secret",
|
||||
"url-userinfo-secret",
|
||||
"header-example-secret",
|
||||
];
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Redaction }
|
||||
servers: [{ url: 'https://operator:url-userinfo-secret@example.test' }]
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer: { type: http, scheme: bearer, x-example-token: extension-api-key-secret }
|
||||
paths:
|
||||
/private:
|
||||
get:
|
||||
parameters:
|
||||
- name: token
|
||||
in: header
|
||||
example: header-example-secret
|
||||
schema: { type: string, default: default-password-secret }
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
application/json:
|
||||
example: { token: example-token-secret }
|
||||
"#;
|
||||
let findings = scan_secret_literals(document.as_bytes()).unwrap();
|
||||
assert!(findings.len() >= CANARIES.len());
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let serialized = serde_json::to_string(&ir).unwrap();
|
||||
assert!(CANARIES.iter().all(|canary| !serialized.contains(canary)));
|
||||
let report = serde_json::to_string(&findings).unwrap();
|
||||
assert!(CANARIES.iter().all(|canary| !report.contains(canary)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scanner_does_not_mistake_password_or_token_schema_properties_for_credentials() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Password fields }
|
||||
paths:
|
||||
/login:
|
||||
post:
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
password: { type: string, format: password }
|
||||
access_token: { type: string }
|
||||
responses: { '204': { description: ok } }
|
||||
"#;
|
||||
assert!(
|
||||
scan_secret_literals(document.as_bytes())
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scanner_keeps_version_examples_and_null_credential_defaults() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Benign examples }
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer: { type: http, scheme: bearer, clientSecret: null }
|
||||
paths:
|
||||
/version:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
content:
|
||||
text/plain:
|
||||
example: 1.2.3
|
||||
"#;
|
||||
assert!(
|
||||
scan_secret_literals(document.as_bytes())
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scanner_allows_oauth_endpoint_urls_but_redacts_secret_query_parameters() {
|
||||
let safe_document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: OAuth URLs }
|
||||
components:
|
||||
securitySchemes:
|
||||
OAuth:
|
||||
type: oauth2
|
||||
flows:
|
||||
authorizationCode:
|
||||
authorizationUrl: https://auth.example.test/authorize
|
||||
tokenUrl: https://auth.example.test/token
|
||||
scopes: {}
|
||||
password:
|
||||
tokenUrl: https://auth.example.test/password-token
|
||||
scopes: {}
|
||||
clientCredentials:
|
||||
tokenUrl: https://auth.example.test/client-token
|
||||
scopes: {}
|
||||
"#;
|
||||
assert!(
|
||||
scan_secret_literals(safe_document.as_bytes())
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let secret_document = safe_document
|
||||
.replace(
|
||||
"https://auth.example.test/token",
|
||||
"https://auth.example.test/token?access_token=oauth-url-canary",
|
||||
)
|
||||
.replace(
|
||||
"https://auth.example.test/password-token",
|
||||
"https://auth.example.test/password-token?access_token=password-flow-canary",
|
||||
)
|
||||
.replace(
|
||||
"https://auth.example.test/client-token",
|
||||
"https://auth.example.test/client-token?access_token=client-flow-canary",
|
||||
);
|
||||
let findings = scan_secret_literals(secret_document.as_bytes()).unwrap();
|
||||
assert!(findings.iter().any(|finding| {
|
||||
finding.location.pointer
|
||||
== "/components/securitySchemes/OAuth/flows/authorizationCode/tokenUrl"
|
||||
}));
|
||||
assert!(findings.iter().any(|finding| {
|
||||
finding.location.pointer == "/components/securitySchemes/OAuth/flows/password/tokenUrl"
|
||||
}));
|
||||
assert!(findings.iter().any(|finding| {
|
||||
finding.location.pointer
|
||||
== "/components/securitySchemes/OAuth/flows/clientCredentials/tokenUrl"
|
||||
}));
|
||||
let report = serde_json::to_string(&findings).unwrap();
|
||||
assert!(!report.contains("oauth-url-canary"));
|
||||
assert!(!report.contains("password-flow-canary"));
|
||||
assert!(!report.contains("client-flow-canary"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples() {
|
||||
const CANARIES: [&str; 8] = [
|
||||
"982734651",
|
||||
"true",
|
||||
"opaque-query-canary",
|
||||
"cookie-session-canary",
|
||||
"set-cookie-canary",
|
||||
"query-param-canary",
|
||||
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl",
|
||||
"opaque-jwt-object-key-canary",
|
||||
];
|
||||
const JWT_KEY: &str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl";
|
||||
let document = format!(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {{ title: Contextual redaction }}
|
||||
servers: [{{ url: 'https://api.test/items?access_token=opaque-query-canary' }}]
|
||||
components:
|
||||
securitySchemes:
|
||||
Custom: {{ type: http, scheme: bearer, x-clientSecret: 982734651, x-accessToken: true }}
|
||||
paths:
|
||||
/login:
|
||||
post:
|
||||
parameters:
|
||||
- name: api_key
|
||||
in: query
|
||||
schema: {{ type: string }}
|
||||
example: query-param-canary
|
||||
- name: Cookie
|
||||
in: header
|
||||
schema: {{ type: string }}
|
||||
example: cookie-session-canary
|
||||
responses:
|
||||
'200':
|
||||
description: ok
|
||||
headers:
|
||||
Set-Cookie:
|
||||
schema: {{ type: string }}
|
||||
example: set-cookie-canary
|
||||
content:
|
||||
application/json:
|
||||
example: {{{JWT_KEY}: opaque-jwt-object-key-canary}}
|
||||
examples:
|
||||
compact:
|
||||
value: {{{JWT_KEY}}}
|
||||
"#
|
||||
);
|
||||
let findings = scan_secret_literals(document.as_bytes()).unwrap();
|
||||
let location_pointers: Vec<_> = findings
|
||||
.iter()
|
||||
.map(|finding| finding.location.pointer.as_str())
|
||||
.collect();
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/x-clientSecret"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/x-accessToken"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/servers/0/url"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/parameters/0/example"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/parameters/1/example"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/headers/Set-Cookie/example"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/content/application~1json/example"))
|
||||
);
|
||||
assert!(
|
||||
location_pointers
|
||||
.iter()
|
||||
.any(|pointer| pointer
|
||||
.ends_with("/content/application~1json/examples/compact/value"))
|
||||
);
|
||||
|
||||
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
|
||||
let serialized = serde_json::to_string(&ir).unwrap();
|
||||
for canary in CANARIES {
|
||||
assert!(!serialized.contains(canary), "IR retained canary {canary}");
|
||||
}
|
||||
let redacted_locations: Vec<_> = ir
|
||||
.findings
|
||||
.iter()
|
||||
.filter(|finding| finding.code == "secret_literal_redacted")
|
||||
.map(|finding| finding.location.pointer.as_str())
|
||||
.collect();
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/x-clientSecret"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/x-accessToken"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/servers/0/url"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/parameters/0/example"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/parameters/1/example"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/headers/Set-Cookie/example"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer.ends_with("/content/application~1json/example"))
|
||||
);
|
||||
assert!(
|
||||
redacted_locations
|
||||
.iter()
|
||||
.any(|pointer| pointer
|
||||
.ends_with("/content/application~1json/examples/compact/value"))
|
||||
);
|
||||
let report = serde_json::to_string(&ir.findings).unwrap();
|
||||
assert!(report.contains("secret_literal_redacted"));
|
||||
for canary in CANARIES {
|
||||
assert!(
|
||||
!report.contains(canary),
|
||||
"finding report retained canary {canary}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsafe_bearer_format_canary_is_redacted_and_not_projected() {
|
||||
const CANARY: &str = "Bearer sk-format-canary";
|
||||
let document = format!(
|
||||
r#"
|
||||
openapi: 3.0.3
|
||||
info: {{ title: Format redaction }}
|
||||
components:
|
||||
securitySchemes:
|
||||
Bearer:
|
||||
type: http
|
||||
scheme: bearer
|
||||
bearerFormat: "{CANARY}"
|
||||
paths:
|
||||
/private:
|
||||
get: {{ security: [{{ Bearer: [] }}], responses: {{ '200': {{ description: ok }} }} }}
|
||||
"#
|
||||
);
|
||||
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
|
||||
let serialized = serde_json::to_string(&ir).unwrap();
|
||||
assert!(!serialized.contains(CANARY));
|
||||
let scan_findings = scan_secret_literals(document.as_bytes()).unwrap();
|
||||
assert!(scan_findings.iter().any(|finding| {
|
||||
finding.location.pointer == "/components/securitySchemes/Bearer/bearerFormat"
|
||||
}));
|
||||
let redaction_findings: Vec<_> = ir
|
||||
.findings
|
||||
.iter()
|
||||
.filter(|finding| finding.code == "secret_literal_redacted")
|
||||
.collect();
|
||||
assert_eq!(redaction_findings.len(), 1);
|
||||
assert_eq!(
|
||||
redaction_findings[0].location.pointer,
|
||||
"/components/securitySchemes/Bearer/bearerFormat"
|
||||
);
|
||||
let report = serde_json::to_string(&ir.findings).unwrap();
|
||||
assert!(report.contains("secret_literal_redacted"));
|
||||
assert!(report.contains("/components/securitySchemes/Bearer/bearerFormat"));
|
||||
assert!(!report.contains(CANARY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn historical_v4_ir_without_security_fields_remains_valid() {
|
||||
let document = r#"
|
||||
openapi: 3.0.3
|
||||
info: { title: Replay }
|
||||
paths:
|
||||
/old:
|
||||
get: { responses: { '200': { description: ok } } }
|
||||
"#;
|
||||
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
|
||||
let mut value = serde_json::to_value(ir).unwrap();
|
||||
value["normalizer_version"] = serde_json::json!("normalized-ir-v4");
|
||||
value["projection_version"] = serde_json::json!("preview-v4");
|
||||
value.as_object_mut().unwrap().remove("security_schemes");
|
||||
for operation in value["operations"].as_array_mut().unwrap() {
|
||||
operation
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("security_schemes");
|
||||
operation
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("security_requirements");
|
||||
}
|
||||
let replay: crank_import::rest::NormalizedIr = serde_json::from_value(value).unwrap();
|
||||
validate_normalized_ir(&replay).unwrap();
|
||||
assert!(
|
||||
replay.operations[0]
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user