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
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use crate::rest::model::{
|
||||
CanonicalSourceNode, CanonicalSourceValue, ImportFindingSeverity, NormalizedFinding,
|
||||
NormalizedIr,
|
||||
NormalizedIr, NormalizedOperation, NormalizedSecuritySchemeKind, SecurityRequirementsSource,
|
||||
};
|
||||
|
||||
pub(super) fn matches_source(
|
||||
@@ -56,11 +56,158 @@ pub(super) fn matches_source(
|
||||
"secret_like_example" if finding.severity == ImportFindingSeverity::Warning => {
|
||||
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, 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),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn security_finding(
|
||||
finding: &NormalizedFinding,
|
||||
ir: &NormalizedIr,
|
||||
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 {
|
||||
return false;
|
||||
};
|
||||
let Some(node) = node else {
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
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 parameter_style(
|
||||
pointer: &str,
|
||||
node: &CanonicalSourceNode,
|
||||
|
||||
Reference in new issue
Block a user