story 2-10-security-schemes-secrets: implemented and reviewed via bmad-loop
This commit is contained in:
1 parent
f5f40cb167
commit
ecfb4e788f
45 files changed
+6025
-829
No files matched your search
@@ -4,7 +4,7 @@
|
||||
//! - an OR array with an empty requirement object `{}` exposes the public
|
||||
//! alternative and must keep the affected operation actionable, without
|
||||
//! touching other operations;
|
||||
//! - a single non-empty requirement must remain a blocker (regression anchor);
|
||||
//! - supported non-empty requirements remain resolvable for profile binding;
|
||||
//! - malformed security shapes (non-array value, non-object array item) must
|
||||
//! produce a source-located operation blocker instead of silently
|
||||
//! disappearing.
|
||||
@@ -50,19 +50,30 @@ fn candidate<'a>(
|
||||
}
|
||||
|
||||
fn security_findings<'a>(
|
||||
findings: &'a [NormalizedFinding],
|
||||
ir: &'a crank_import::rest::NormalizedIr,
|
||||
key: &str,
|
||||
) -> Vec<&'a NormalizedFinding> {
|
||||
findings
|
||||
.iter()
|
||||
all_findings(ir)
|
||||
.into_iter()
|
||||
.filter(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
finding.code == "invalid_security_requirements"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
&& finding.operation_key.as_deref() == Some(key)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn all_findings(ir: &crank_import::rest::NormalizedIr) -> Vec<&NormalizedFinding> {
|
||||
ir.findings
|
||||
.iter()
|
||||
.chain(
|
||||
ir.operations
|
||||
.iter()
|
||||
.flat_map(|operation| &operation.findings),
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_or_alternative_makes_the_operation_public() {
|
||||
// Document-level OR array: [{"probe": []}, {}] offers an explicit public
|
||||
@@ -91,7 +102,7 @@ fn empty_or_alternative_makes_the_operation_public() {
|
||||
public.findings
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /public").is_empty(),
|
||||
security_findings(&ir, "GET /public").is_empty(),
|
||||
"no security blocker may belong to the public operation: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
@@ -129,33 +140,28 @@ fn or_alternative_keeps_only_that_operation_public() {
|
||||
);
|
||||
let closed = candidate(&preview, "GET /closed");
|
||||
assert!(
|
||||
closed.findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"non-empty requirement must still block the candidate: {:?}",
|
||||
closed
|
||||
.findings
|
||||
.iter()
|
||||
.all(|finding| finding.severity != ImportFindingSeverity::Error),
|
||||
"well-formed supported requirement must remain resolvable: {:?}",
|
||||
closed.findings
|
||||
);
|
||||
let ir_blocker = security_findings(&ir.findings, "GET /closed")[0];
|
||||
assert!(security_findings(&ir, "GET /closed").is_empty());
|
||||
assert!(closed.security_requirements.alternatives[0].supported);
|
||||
assert_eq!(
|
||||
ir_blocker.location.pointer, "/paths/~1closed/get/security/0",
|
||||
"blocker must point at the local requirement: {:?}",
|
||||
ir_blocker
|
||||
closed.security_requirements.alternatives[0].schemes,
|
||||
["probe"]
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /closed").len() == 1,
|
||||
"exactly one blocker for the closed operation: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /open").is_empty(),
|
||||
security_findings(&ir, "GET /open").is_empty(),
|
||||
"the public alternative must not leak into the open operation: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonempty_requirement_still_blocks_with_auth_profile_contract() {
|
||||
fn well_formed_requirement_stays_resolvable_for_auth_profile_binding() {
|
||||
let source = json!({
|
||||
"openapi": "3.0.3",
|
||||
"info": { "title": "Security scope regression", "version": "1" },
|
||||
@@ -170,25 +176,18 @@ fn nonempty_requirement_still_blocks_with_auth_profile_contract() {
|
||||
}
|
||||
});
|
||||
let (ir, preview) = run(&source);
|
||||
let blocker = security_findings(&ir.findings, "GET /secured")
|
||||
.into_iter()
|
||||
.next()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| panic!("non-empty requirement must block: {:?}", ir.findings));
|
||||
assert_eq!(blocker.location.pointer, "/security/0");
|
||||
assert_eq!(
|
||||
blocker.message,
|
||||
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
|
||||
"Auth Profile contract message must be stable"
|
||||
);
|
||||
assert!(security_findings(&ir, "GET /secured").is_empty());
|
||||
let secured = candidate(&preview, "GET /secured");
|
||||
assert!(
|
||||
secured.findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"blocked candidate must expose the blocker: {:?}",
|
||||
secured.findings
|
||||
secured
|
||||
.findings
|
||||
.iter()
|
||||
.all(|finding| finding.severity != ImportFindingSeverity::Error)
|
||||
);
|
||||
assert!(secured.security_requirements.alternatives[0].supported);
|
||||
assert_eq!(
|
||||
secured.security_requirements.source,
|
||||
crank_import::rest::SecurityRequirementsSource::Document
|
||||
);
|
||||
}
|
||||
|
||||
@@ -209,7 +208,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
|
||||
});
|
||||
let (ir, preview) = run(&source);
|
||||
for key in ["GET /one", "GET /two"] {
|
||||
let blocker = security_findings(&ir.findings, key)
|
||||
let blocker = security_findings(&ir, key)
|
||||
.into_iter()
|
||||
.next()
|
||||
.cloned()
|
||||
@@ -221,14 +220,15 @@ fn malformed_security_shapes_block_the_affected_operation() {
|
||||
);
|
||||
assert!(
|
||||
candidate(&preview, key).findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
finding.code == "invalid_security_requirements"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"{key} candidate must be blocked"
|
||||
);
|
||||
}
|
||||
|
||||
// Non-object array item: the item pointer must be preserved.
|
||||
// Non-object array item: the array is fail-closed and the item has a
|
||||
// source-located warning.
|
||||
let source = json!({
|
||||
"openapi": "3.0.3",
|
||||
"info": { "title": "Security scope regression", "version": "1" },
|
||||
@@ -240,16 +240,21 @@ fn malformed_security_shapes_block_the_affected_operation() {
|
||||
}
|
||||
});
|
||||
let (ir, _) = run(&source);
|
||||
let blocker = security_findings(&ir.findings, "GET /one")
|
||||
let blocker = security_findings(&ir, "GET /one")
|
||||
.into_iter()
|
||||
.next()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| panic!("non-object security item must block: {:?}", ir.findings));
|
||||
assert_eq!(
|
||||
blocker.location.pointer, "/security/0",
|
||||
"blocker must point at the malformed item: {:?}",
|
||||
blocker.location.pointer, "/security",
|
||||
"blocker must point at the malformed array: {:?}",
|
||||
blocker
|
||||
);
|
||||
assert!(all_findings(&ir).iter().any(|finding| {
|
||||
finding.code == "invalid_security_requirement"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
&& finding.location.pointer == "/security/0"
|
||||
}));
|
||||
|
||||
// Malformed operation-level security must not affect the neighbour.
|
||||
let source = json!({
|
||||
@@ -264,7 +269,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
|
||||
}
|
||||
});
|
||||
let (ir, preview) = run(&source);
|
||||
let blocker = security_findings(&ir.findings, "GET /bad")
|
||||
let blocker = security_findings(&ir, "GET /bad")
|
||||
.into_iter()
|
||||
.next()
|
||||
.cloned()
|
||||
@@ -275,7 +280,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
|
||||
blocker
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /fine").is_empty(),
|
||||
security_findings(&ir, "GET /fine").is_empty(),
|
||||
"the neighbour operation must not inherit a local defect: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
@@ -307,25 +312,29 @@ fn empty_alternative_needs_a_well_formed_array() {
|
||||
}
|
||||
});
|
||||
let (ir, preview) = run(&source);
|
||||
let at_item = |pointer: &str| {
|
||||
ir.findings
|
||||
.iter()
|
||||
let at_pointer = |pointer: &str, code: &str| {
|
||||
all_findings(&ir)
|
||||
.into_iter()
|
||||
.find(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
finding.code == code
|
||||
&& finding.operation_key.as_deref() == Some("GET /mixed")
|
||||
&& finding.location.pointer == pointer
|
||||
})
|
||||
.unwrap_or_else(|| panic!("blocker at {pointer} missing: {:?}", ir.findings))
|
||||
.unwrap_or_else(|| panic!("{code} finding at {pointer} missing: {:?}", ir.findings))
|
||||
};
|
||||
let secured = at_item("/security/0");
|
||||
assert_eq!(
|
||||
secured.message,
|
||||
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются."
|
||||
at_pointer("/security", "invalid_security_requirements").severity,
|
||||
ImportFindingSeverity::Error
|
||||
);
|
||||
let malformed = at_item("/security/2");
|
||||
assert_eq!(
|
||||
malformed.message,
|
||||
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
|
||||
at_pointer("/security/2", "invalid_security_requirement").severity,
|
||||
ImportFindingSeverity::Warning
|
||||
);
|
||||
assert!(
|
||||
candidate(&preview, "GET /mixed")
|
||||
.security_requirements
|
||||
.alternatives[0]
|
||||
.supported
|
||||
);
|
||||
assert!(
|
||||
candidate(&preview, "GET /mixed")
|
||||
@@ -365,10 +374,10 @@ fn object_instead_of_security_array_has_scoped_coverage() {
|
||||
} else {
|
||||
"/security"
|
||||
};
|
||||
let blocker = security_findings(&ir.findings, "GET /bad");
|
||||
let blocker = security_findings(&ir, "GET /bad");
|
||||
assert_eq!(blocker.len(), 1, "{version} {local}: {:?}", ir.findings);
|
||||
assert_eq!(blocker[0].location.pointer, expected);
|
||||
assert!(security_findings(&ir.findings, "GET /public").is_empty());
|
||||
assert!(security_findings(&ir, "GET /public").is_empty());
|
||||
assert!(
|
||||
candidate(&preview, "GET /public")
|
||||
.findings
|
||||
@@ -435,7 +444,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
for key in ["GET /one", "GET /two"] {
|
||||
let blockers = security_findings(&ir.findings, key);
|
||||
let blockers = security_findings(&ir, key);
|
||||
assert_eq!(
|
||||
blockers.len(),
|
||||
1,
|
||||
@@ -444,7 +453,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
let blocker = &blockers[0];
|
||||
assert_eq!(
|
||||
blocker.code, "unsupported_security_requirement",
|
||||
blocker.code, "invalid_security_requirements",
|
||||
"stable finding code: {blocker:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -458,7 +467,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
assert!(
|
||||
candidate(&preview, key).findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
finding.code == "invalid_security_requirements"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"{key} candidate must be blocked"
|
||||
@@ -481,10 +490,10 @@ fn openapi_31_malformed_local_security_affects_only_that_operation() {
|
||||
}),
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
let blockers = security_findings(&ir.findings, "GET /bad");
|
||||
let blockers = security_findings(&ir, "GET /bad");
|
||||
assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings);
|
||||
assert_eq!(
|
||||
blockers[0].code, "unsupported_security_requirement",
|
||||
blockers[0].code, "invalid_security_requirements",
|
||||
"stable finding code"
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -498,7 +507,7 @@ fn openapi_31_malformed_local_security_affects_only_that_operation() {
|
||||
blockers[0]
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /public").is_empty(),
|
||||
security_findings(&ir, "GET /public").is_empty(),
|
||||
"the public neighbour must not inherit a local defect: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
@@ -520,7 +529,7 @@ fn openapi_31_public_alternative_with_malformed_sibling_does_not_go_public() {
|
||||
json!({ "/mixed": get200("mixedOp31", None) }),
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
let blockers = security_findings(&ir.findings, "GET /mixed");
|
||||
let blockers = security_findings(&ir, "GET /mixed");
|
||||
assert_eq!(
|
||||
blockers.len(),
|
||||
1,
|
||||
@@ -528,14 +537,15 @@ fn openapi_31_public_alternative_with_malformed_sibling_does_not_go_public() {
|
||||
ir.findings
|
||||
);
|
||||
assert_eq!(
|
||||
blockers[0].location.pointer, "/security/1",
|
||||
"blocker must point at the non-object item: {:?}",
|
||||
blockers[0].location.pointer, "/security",
|
||||
"fail-closed blocker must point at the malformed array: {:?}",
|
||||
blockers[0]
|
||||
);
|
||||
assert_eq!(
|
||||
blockers[0].message,
|
||||
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
|
||||
);
|
||||
assert!(all_findings(&ir).iter().any(|finding| {
|
||||
finding.code == "invalid_security_requirement"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
&& finding.location.pointer == "/security/1"
|
||||
}));
|
||||
assert!(
|
||||
candidate(&preview, "GET /mixed")
|
||||
.findings
|
||||
@@ -558,7 +568,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
for key in ["GET /one", "GET /two"] {
|
||||
let blockers = security_findings(&ir.findings, key);
|
||||
let blockers = security_findings(&ir, key);
|
||||
assert_eq!(
|
||||
blockers.len(),
|
||||
1,
|
||||
@@ -567,7 +577,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
let blocker = &blockers[0];
|
||||
assert_eq!(
|
||||
blocker.code, "unsupported_security_requirement",
|
||||
blocker.code, "invalid_security_requirements",
|
||||
"stable finding code: {blocker:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -581,7 +591,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
|
||||
);
|
||||
assert!(
|
||||
candidate(&preview, key).findings.iter().any(|finding| {
|
||||
finding.code == "unsupported_security_requirement"
|
||||
finding.code == "invalid_security_requirements"
|
||||
&& finding.severity == ImportFindingSeverity::Error
|
||||
}),
|
||||
"{key} candidate must be blocked"
|
||||
@@ -604,9 +614,9 @@ fn swagger2_malformed_local_security_affects_only_that_operation() {
|
||||
}),
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
let blockers = security_findings(&ir.findings, "GET /bad");
|
||||
let blockers = security_findings(&ir, "GET /bad");
|
||||
assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings);
|
||||
assert_eq!(blockers[0].code, "unsupported_security_requirement");
|
||||
assert_eq!(blockers[0].code, "invalid_security_requirements");
|
||||
assert_eq!(blockers[0].severity, ImportFindingSeverity::Error);
|
||||
assert_eq!(
|
||||
blockers[0].location.pointer, "/paths/~1bad/get/security",
|
||||
@@ -614,7 +624,7 @@ fn swagger2_malformed_local_security_affects_only_that_operation() {
|
||||
blockers[0]
|
||||
);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /public").is_empty(),
|
||||
security_findings(&ir, "GET /public").is_empty(),
|
||||
"the public neighbour must not inherit a local defect: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
@@ -636,7 +646,7 @@ fn swagger2_public_alternative_with_malformed_sibling_does_not_go_public() {
|
||||
json!({ "/mixed": get200("mixedOpS2", None) }),
|
||||
);
|
||||
let (ir, preview) = run(&Value::Object(fixture));
|
||||
let blockers = security_findings(&ir.findings, "GET /mixed");
|
||||
let blockers = security_findings(&ir, "GET /mixed");
|
||||
assert_eq!(
|
||||
blockers.len(),
|
||||
1,
|
||||
@@ -644,14 +654,15 @@ fn swagger2_public_alternative_with_malformed_sibling_does_not_go_public() {
|
||||
ir.findings
|
||||
);
|
||||
assert_eq!(
|
||||
blockers[0].location.pointer, "/security/1",
|
||||
"blocker must point at the non-object item: {:?}",
|
||||
blockers[0].location.pointer, "/security",
|
||||
"fail-closed blocker must point at the malformed array: {:?}",
|
||||
blockers[0]
|
||||
);
|
||||
assert_eq!(
|
||||
blockers[0].message,
|
||||
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
|
||||
);
|
||||
assert!(all_findings(&ir).iter().any(|finding| {
|
||||
finding.code == "invalid_security_requirement"
|
||||
&& finding.severity == ImportFindingSeverity::Warning
|
||||
&& finding.location.pointer == "/security/1"
|
||||
}));
|
||||
assert!(
|
||||
candidate(&preview, "GET /mixed")
|
||||
.findings
|
||||
@@ -675,7 +686,7 @@ fn empty_security_array_stays_public() {
|
||||
});
|
||||
let (ir, preview) = run(&source);
|
||||
assert!(
|
||||
security_findings(&ir.findings, "GET /public").is_empty(),
|
||||
security_findings(&ir, "GET /public").is_empty(),
|
||||
"explicit empty override must stay public: {:?}",
|
||||
ir.findings
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user