story 2-14-openapi-support-matrix-regression-corpus: implemented and reviewed via bmad-loop
This commit is contained in:
1 parent
16dc777911
commit
194cac169f
84 files changed
+23255
-1
No files matched your search
@@ -12,6 +12,7 @@ mod normalize_finding_validation;
|
||||
mod normalize_limits;
|
||||
mod normalize_projection;
|
||||
mod normalize_schema;
|
||||
mod normalize_security;
|
||||
mod openapi3;
|
||||
mod openapi3_findings;
|
||||
mod payload;
|
||||
|
||||
@@ -146,6 +146,7 @@ pub fn normalize_verified_bundle(
|
||||
}
|
||||
None => return Err(ImportParseError::UnsupportedDocument),
|
||||
};
|
||||
super::normalize_security::append_security_requirement_findings(&mut parsed, &root);
|
||||
super::metadata_findings::collect_secret_like_examples(
|
||||
&root,
|
||||
"",
|
||||
@@ -238,6 +239,8 @@ fn preview_from_operations<'a>(
|
||||
.push(candidate);
|
||||
}
|
||||
|
||||
super::normalize_security::attach_operation_findings(&mut groups, &findings);
|
||||
|
||||
sort_findings(&mut findings);
|
||||
ImportPreview {
|
||||
source: source.clone(),
|
||||
|
||||
@@ -57,6 +57,10 @@ pub(super) fn matches_source(
|
||||
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value))
|
||||
}
|
||||
"invalid_server" if is_error => invalid_server(pointer, node),
|
||||
"unsupported_security_requirement" if is_error => {
|
||||
matches!(&node.value, CanonicalSourceValue::Object(fields) if !fields.is_empty())
|
||||
|| invalid_security_requirement(pointer, node)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -201,6 +205,31 @@ fn secret_like(pointer: &str, value: &str) -> bool {
|
||||
|| value.starts_with("sk-")
|
||||
}
|
||||
|
||||
/// Accepts malformed `security` shapes at their scoped source location only:
|
||||
/// the document `security` member, an operation `security` member, or a
|
||||
/// non-object item of the corresponding security array. An object is valid
|
||||
/// only as an array item, including the empty `{}` public alternative.
|
||||
fn invalid_security_requirement(pointer: &str, node: &CanonicalSourceNode) -> bool {
|
||||
const METHODS: &[&str] = &[
|
||||
"get", "post", "put", "patch", "delete", "head", "options", "trace", "connect",
|
||||
];
|
||||
let is_index = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit());
|
||||
let segments: Vec<&str> = pointer.split('/').collect();
|
||||
match segments.as_slice() {
|
||||
["", "security"] => true,
|
||||
["", "security", index] => {
|
||||
is_index(index) && !matches!(&node.value, CanonicalSourceValue::Object(_))
|
||||
}
|
||||
["", "paths", _, method, "security"] => METHODS.contains(method),
|
||||
["", "paths", _, method, "security", index] => {
|
||||
METHODS.contains(method)
|
||||
&& is_index(index)
|
||||
&& !matches!(&node.value, CanonicalSourceValue::Object(_))
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_server(pointer: &str, node: &CanonicalSourceNode) -> bool {
|
||||
if pointer == "/host" {
|
||||
return !matches!(&node.value, CanonicalSourceValue::String(host) if !host.is_empty() && !host.contains('@') && !host.chars().any(char::is_whitespace));
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
//! Effective per-operation security scopes for the OpenAPI importer.
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{
|
||||
model::{
|
||||
ImportFinding, ImportFindingSeverity, ImportGroupPreview, RestImportDocument,
|
||||
RestImportOperation, SourceLocation,
|
||||
},
|
||||
normalize_coverage,
|
||||
};
|
||||
|
||||
pub(super) fn attach_operation_findings(
|
||||
groups: &mut std::collections::BTreeMap<String, ImportGroupPreview>,
|
||||
findings: &[ImportFinding],
|
||||
) {
|
||||
for group in groups.values_mut() {
|
||||
for candidate in &mut group.operations {
|
||||
let additions = findings
|
||||
.iter()
|
||||
.filter(|finding| {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& finding.operation_key.as_deref() == Some(candidate.key.as_str())
|
||||
&& !candidate.findings.iter().any(|existing| {
|
||||
existing.code == finding.code
|
||||
&& existing.severity == finding.severity
|
||||
&& existing.message == finding.message
|
||||
})
|
||||
})
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
candidate.findings.extend(additions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the effective security requirements for every candidate operation
|
||||
/// and attaches source-located blockers to precisely the affected candidates.
|
||||
///
|
||||
/// Effective requirements follow the OpenAPI semantics: an operation that
|
||||
/// declares its own `security` member wins over the document root; an empty
|
||||
/// array (`security: []`) explicitly disables inheritance. Within a
|
||||
/// requirement array the items are alternatives (OR): an empty requirement
|
||||
/// object `{}` is the public alternative and makes the whole operation
|
||||
/// public, so no blocker is produced for that operation.
|
||||
///
|
||||
/// A malformed security shape — a non-array `security` value or a non-object
|
||||
/// array item — must never be discarded silently: the affected operation
|
||||
/// receives one blocker with the exact source location. A supported non-empty
|
||||
/// requirement remains a blocker until an administrator binds a compatible
|
||||
/// Auth Profile.
|
||||
pub(super) fn append_security_requirement_findings(parsed: &mut RestImportDocument, root: &Value) {
|
||||
let operations = &parsed.operations;
|
||||
let findings = &mut parsed.findings;
|
||||
let locations = &mut parsed.internal_finding_locations;
|
||||
let root_security = root.get("security");
|
||||
for operation in operations {
|
||||
let Some(path_item) = root
|
||||
.pointer(&format!(
|
||||
"/paths/{}",
|
||||
normalize_coverage::escape_pointer(&operation.path)
|
||||
))
|
||||
.and_then(|item| item.get(method_as_lower(operation)))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let Some(security) = path_item
|
||||
.get("security")
|
||||
.or_else(|| root_security.filter(|_| path_item.get("security").is_none()))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let base_pointer = if path_item.get("security").is_some() {
|
||||
format!(
|
||||
"/paths/{}/{}/security",
|
||||
normalize_coverage::escape_pointer(&operation.path),
|
||||
method_as_lower(operation)
|
||||
)
|
||||
} else {
|
||||
"/security".to_owned()
|
||||
};
|
||||
let alternatives = match security.as_array() {
|
||||
Some(alternatives) => alternatives,
|
||||
None => {
|
||||
push_security_blocker(
|
||||
findings,
|
||||
locations,
|
||||
operation,
|
||||
&base_pointer,
|
||||
"security имеет некорректный формат: ожидается массив требований; операции с незапакованным блоком безопасности не публикуются.",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
// Validate the item shapes first: only a fully well-formed array may
|
||||
// use the empty requirement object `{}` as its public OR alternative.
|
||||
if !alternatives
|
||||
.iter()
|
||||
.all(|requirement| requirement.as_object().is_some())
|
||||
{
|
||||
for (index, requirement) in alternatives.iter().enumerate() {
|
||||
let pointer = format!("{base_pointer}/{index}");
|
||||
match requirement.as_object() {
|
||||
Some(schemes) if schemes.is_empty() => {
|
||||
// In a malformed array the empty alternative does
|
||||
// not make the operation public, but it also cannot
|
||||
// block on its own.
|
||||
continue;
|
||||
}
|
||||
Some(_) => push_security_blocker(
|
||||
findings,
|
||||
locations,
|
||||
operation,
|
||||
&pointer,
|
||||
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
|
||||
),
|
||||
None => push_security_blocker(
|
||||
findings,
|
||||
locations,
|
||||
operation,
|
||||
&pointer,
|
||||
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется.",
|
||||
),
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if alternatives.iter().any(|requirement| {
|
||||
requirement
|
||||
.as_object()
|
||||
.is_some_and(serde_json::Map::is_empty)
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
for (index, _) in alternatives.iter().enumerate() {
|
||||
push_security_blocker(
|
||||
findings,
|
||||
locations,
|
||||
operation,
|
||||
&format!("{base_pointer}/{index}"),
|
||||
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn push_security_blocker(
|
||||
findings: &mut Vec<ImportFinding>,
|
||||
locations: &mut Vec<Option<SourceLocation>>,
|
||||
operation: &RestImportOperation,
|
||||
pointer: &str,
|
||||
message: &str,
|
||||
) {
|
||||
findings.push(ImportFinding {
|
||||
code: "unsupported_security_requirement".to_owned(),
|
||||
severity: ImportFindingSeverity::Error,
|
||||
message: message.to_owned(),
|
||||
operation_key: Some(operation.key.clone()),
|
||||
});
|
||||
locations.push(Some(SourceLocation {
|
||||
pointer: pointer.to_owned(),
|
||||
}));
|
||||
}
|
||||
|
||||
fn method_as_lower(operation: &RestImportOperation) -> String {
|
||||
format!("{:?}", operation.method).to_ascii_lowercase()
|
||||
}
|
||||
Reference in new issue
Block a user