feat(import): complete OpenAPI onboarding and verify delivery
This commit is contained in:
1 parent
bb3e376767
commit
e93de7015a
244 files changed
+57020
-5139
No files matched your search
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use crate::rest::model::{
|
||||
CanonicalSourceNode, CanonicalSourceValue, ImportFindingSeverity, NormalizedFinding,
|
||||
NormalizedIr, NormalizedOperation, NormalizedSecuritySchemeKind, SecurityRequirementsSource,
|
||||
NormalizedIr,
|
||||
};
|
||||
|
||||
pub(super) fn matches_source(
|
||||
@@ -48,164 +48,184 @@ pub(super) fn matches_source(
|
||||
finding.operation_key.as_deref() == Some(operation.key.as_str())
|
||||
&& matches!(&node.value, CanonicalSourceValue::Object(fields) if matches!(fields.get("name").map(|node| &node.value), Some(CanonicalSourceValue::String(name)) if !operation.path.contains(&format!("{{{name}}}"))))
|
||||
}),
|
||||
"path_parameter_not_declared" if is_error => {
|
||||
path_parameter_missing_in_source(finding, ir, node, nodes)
|
||||
}
|
||||
"unsupported_compound_default" if is_error => {
|
||||
pointer.ends_with("/default")
|
||||
&& matches!(node.value, CanonicalSourceValue::Object(_) | CanonicalSourceValue::Array(_))
|
||||
}
|
||||
"unsupported_schema_keyword" if is_error => schema_keyword(pointer, node),
|
||||
"unsupported_schema_enum" if is_error => {
|
||||
pointer.ends_with("/enum")
|
||||
&& matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::String(_))))
|
||||
}
|
||||
"secret_like_example" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value))
|
||||
matches!(&node.value, CanonicalSourceValue::String(value)
|
||||
if secret_like(pointer, value)
|
||||
|| super::metadata_findings::url_has_secret(value)
|
||||
|| (pointer.ends_with("/url") && !value.contains('@'))
|
||||
|| value == super::metadata_findings::REDACTED_LITERAL
|
||||
|| sanitized_secret_url(value))
|
||||
}
|
||||
"secret_literal_redacted" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
matches!(&node.value, CanonicalSourceValue::String(value) if value == "[REDACTED]")
|
||||
}
|
||||
"invalid_security_requirements"
|
||||
| "invalid_security_requirement"
|
||||
| "unresolved_security_scheme"
|
||||
| "unsupported_security_and_requirement"
|
||||
| "unsupported_security_scheme"
|
||||
| "unsupported_security_requirements"
|
||||
| "invalid_security_scopes"
|
||||
| "unexpected_security_scopes" => security_finding(finding, ir, nodes),
|
||||
"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)
|
||||
}
|
||||
"invalid_security_requirements" if is_error => {
|
||||
!matches!(&node.value, CanonicalSourceValue::Array(_))
|
||||
|| matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::Object(_))))
|
||||
}
|
||||
"invalid_security_requirement" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
!matches!(&node.value, CanonicalSourceValue::Object(_))
|
||||
}
|
||||
"unsupported_security_requirements" if is_error => ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str()))
|
||||
.is_some_and(|operation| {
|
||||
!operation.security_requirements.alternatives.is_empty()
|
||||
&& operation
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.iter()
|
||||
.all(|alternative| !alternative.supported)
|
||||
}),
|
||||
"unsupported_security_and_requirement"
|
||||
if finding.severity == ImportFindingSeverity::Warning =>
|
||||
{
|
||||
matches!(&node.value, CanonicalSourceValue::Object(fields) if fields.len() > 1)
|
||||
}
|
||||
"unsupported_security_scheme" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
let scheme_name = pointer
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.replace("~1", "/")
|
||||
.replace("~0", "~");
|
||||
ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
|
||||
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. })
|
||||
|| matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::ApiKey { location: crate::rest::model::SecurityParameterLocation::Cookie, .. })
|
||||
}) && matches!(&node.value, CanonicalSourceValue::Array(_))
|
||||
}
|
||||
"unsupported_security_scheme_definition"
|
||||
if finding.severity == ImportFindingSeverity::Warning =>
|
||||
{
|
||||
let scheme_name = pointer
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.replace("~1", "/")
|
||||
.replace("~0", "~");
|
||||
ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
|
||||
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. })
|
||||
}) && matches!(&node.value, CanonicalSourceValue::Object(_))
|
||||
}
|
||||
"unresolved_security_scheme" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
let scheme_name = pointer
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.replace("~1", "/")
|
||||
.replace("~0", "~");
|
||||
!ir.security_schemes.contains_key(&scheme_name)
|
||||
}
|
||||
"invalid_security_scopes" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
!matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))))
|
||||
}
|
||||
"unexpected_security_scopes" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
let scheme_name = pointer
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.replace("~1", "/")
|
||||
.replace("~0", "~");
|
||||
let supports_scopes = ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
|
||||
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { scheme_type: Some(kind) } if matches!(kind.as_str(), "oauth2" | "openIdConnect"))
|
||||
});
|
||||
!supports_scopes
|
||||
&& matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty())
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn security_finding(
|
||||
fn path_placeholders(path: &str) -> Vec<String> {
|
||||
let mut placeholders = Vec::new();
|
||||
let mut remaining = path;
|
||||
while let Some(open) = remaining.find('{') {
|
||||
let after_open = &remaining[open + 1..];
|
||||
let Some(close) = after_open.find('}') else {
|
||||
break;
|
||||
};
|
||||
if close > 0 {
|
||||
placeholders.push(after_open[..close].to_owned());
|
||||
}
|
||||
remaining = &after_open[close + 1..];
|
||||
}
|
||||
placeholders
|
||||
}
|
||||
|
||||
fn path_parameter_missing_in_source(
|
||||
finding: &NormalizedFinding,
|
||||
ir: &NormalizedIr,
|
||||
operation_node: &CanonicalSourceNode,
|
||||
nodes: &BTreeMap<String, &CanonicalSourceNode>,
|
||||
) -> bool {
|
||||
let Some(operation) = ir
|
||||
.operations
|
||||
.iter()
|
||||
.find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str()))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let Some(requirements_pointer) = requirements_pointer(operation) else {
|
||||
return false;
|
||||
};
|
||||
let pointer = finding.location.pointer.as_str();
|
||||
let node = nodes.get(pointer).copied();
|
||||
|
||||
match finding.code.as_str() {
|
||||
"invalid_security_requirements" => {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& pointer == requirements_pointer
|
||||
&& node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Array(_)))
|
||||
}
|
||||
"invalid_security_requirement" => {
|
||||
finding.severity == ImportFindingSeverity::Warning
|
||||
&& alternative_at(operation, pointer).is_some_and(|_| {
|
||||
node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Object(_)))
|
||||
})
|
||||
}
|
||||
"unresolved_security_scheme" => {
|
||||
finding.severity == ImportFindingSeverity::Warning
|
||||
&& scheme_at(operation, pointer)
|
||||
.is_some_and(|name| node.is_some() && !ir.security_schemes.contains_key(name))
|
||||
}
|
||||
"unsupported_security_and_requirement" => {
|
||||
finding.severity == ImportFindingSeverity::Warning
|
||||
&& alternative_at(operation, pointer)
|
||||
.is_some_and(|alternative| alternative.schemes.len() > 1)
|
||||
&& node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Object(_)))
|
||||
}
|
||||
"unsupported_security_scheme" => {
|
||||
finding.severity == ImportFindingSeverity::Warning
|
||||
&& scheme_at(operation, pointer).is_some_and(|name| {
|
||||
let Some(scheme) = ir.security_schemes.get(name) else {
|
||||
return false;
|
||||
};
|
||||
node.is_some()
|
||||
&& matches!(
|
||||
&scheme.kind,
|
||||
NormalizedSecuritySchemeKind::Unsupported { .. }
|
||||
| NormalizedSecuritySchemeKind::ApiKey {
|
||||
location: crate::rest::model::SecurityParameterLocation::Cookie,
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
"unsupported_security_requirements" => {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& pointer == requirements_pointer
|
||||
&& !operation.security_requirements.alternatives.is_empty()
|
||||
&& operation
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.iter()
|
||||
.all(|alternative| !alternative.supported)
|
||||
&& node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Array(_)))
|
||||
}
|
||||
"invalid_security_scopes" | "unexpected_security_scopes" => {
|
||||
if finding.severity != ImportFindingSeverity::Warning {
|
||||
return false;
|
||||
}
|
||||
let Some(name) = scheme_at(operation, pointer) else {
|
||||
let segments = pointer.split('/').collect::<Vec<_>>();
|
||||
if segments.len() != 4 || segments[1] != "paths" {
|
||||
return false;
|
||||
}
|
||||
let path = segments[2].replace("~1", "/").replace("~0", "~");
|
||||
let method = segments[3];
|
||||
let expected_key = format!("{} {path}", method.to_uppercase());
|
||||
let Some(operation) = ir.operations.iter().find(|operation| {
|
||||
operation.key == expected_key
|
||||
&& operation.path == path
|
||||
&& operation.location.pointer == pointer
|
||||
}) else {
|
||||
return false;
|
||||
};
|
||||
let Some(path_item) = nodes.get(&format!("/paths/{}", segments[2])).copied() else {
|
||||
return false;
|
||||
};
|
||||
let has_declared_parameter = |owner: &CanonicalSourceNode, name: &str| {
|
||||
let CanonicalSourceValue::Object(fields) = &owner.value else {
|
||||
return false;
|
||||
};
|
||||
let Some(CanonicalSourceNode {
|
||||
value: CanonicalSourceValue::Array(parameters),
|
||||
..
|
||||
}) = fields.get("parameters")
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
parameters.iter().any(|parameter| {
|
||||
let CanonicalSourceValue::Object(fields) = ¶meter.value else {
|
||||
return false;
|
||||
};
|
||||
let Some(node) = node else {
|
||||
if fields.contains_key("$ref") {
|
||||
return false;
|
||||
};
|
||||
let valid_scopes = matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))));
|
||||
if finding.code == "invalid_security_scopes" {
|
||||
!valid_scopes
|
||||
} else {
|
||||
let nonempty = matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty() && items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))));
|
||||
let oauth_scoped = ir.security_schemes.get(name).is_some_and(|scheme| {
|
||||
matches!(
|
||||
&scheme.kind,
|
||||
NormalizedSecuritySchemeKind::Unsupported {
|
||||
scheme_type: Some(scheme_type)
|
||||
} if matches!(scheme_type.as_str(), "oauth2" | "openIdConnect")
|
||||
)
|
||||
});
|
||||
nonempty && !oauth_scoped
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn requirements_pointer(operation: &NormalizedOperation) -> Option<String> {
|
||||
match operation.security_requirements.source {
|
||||
SecurityRequirementsSource::Document => Some("/security".to_owned()),
|
||||
SecurityRequirementsSource::Operation => {
|
||||
Some(format!("{}/security", operation.location.pointer))
|
||||
}
|
||||
SecurityRequirementsSource::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn alternative_at<'a>(
|
||||
operation: &'a NormalizedOperation,
|
||||
pointer: &str,
|
||||
) -> Option<&'a crate::rest::model::SecurityRequirementAlternative> {
|
||||
operation
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.iter()
|
||||
.find(|alternative| {
|
||||
alternative
|
||||
.source_location
|
||||
.as_ref()
|
||||
.is_some_and(|location| location.pointer == pointer)
|
||||
matches!(fields.get("name").map(|node| &node.value), Some(CanonicalSourceValue::String(value)) if value == name)
|
||||
&& matches!(fields.get("in").map(|node| &node.value), Some(CanonicalSourceValue::String(value)) if value == "path")
|
||||
})
|
||||
};
|
||||
path_placeholders(&operation.path).into_iter().any(|name| {
|
||||
!has_declared_parameter(path_item, &name)
|
||||
&& !has_declared_parameter(operation_node, &name)
|
||||
&& !operation.parameters.iter().any(|parameter| {
|
||||
parameter.name == name
|
||||
&& parameter.location == crate::rest::model::RestParameterLocation::Path
|
||||
})
|
||||
}) && finding.operation_key.as_deref() == Some(expected_key.as_str())
|
||||
}
|
||||
|
||||
fn scheme_at<'a>(operation: &'a NormalizedOperation, pointer: &str) -> Option<&'a str> {
|
||||
let (alternative_pointer, scheme) = pointer.rsplit_once('/')?;
|
||||
let alternative = alternative_at(operation, alternative_pointer)?;
|
||||
let name = scheme.replace("~1", "/").replace("~0", "~");
|
||||
alternative
|
||||
.schemes
|
||||
.iter()
|
||||
.find(|candidate| **candidate == name)
|
||||
.map(String::as_str)
|
||||
fn sanitized_secret_url(value: &str) -> bool {
|
||||
value.contains("=[REDACTED]")
|
||||
|| ((value.contains("://") || value.starts_with("//")) && !value.contains('@'))
|
||||
}
|
||||
|
||||
fn parameter_style(
|
||||
@@ -348,6 +368,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));
|
||||
|
||||
Reference in new issue
Block a user