332 lines
15 KiB
Rust
332 lines
15 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use crate::rest::model::{
|
|
CanonicalSourceNode, CanonicalSourceValue, ImportFindingSeverity, NormalizedFinding,
|
|
NormalizedIr,
|
|
};
|
|
|
|
pub(super) fn matches_source(
|
|
finding: &NormalizedFinding,
|
|
ir: &NormalizedIr,
|
|
nodes: &BTreeMap<String, &CanonicalSourceNode>,
|
|
) -> bool {
|
|
let pointer = finding.location.pointer.as_str();
|
|
let Some(node) = nodes.get(pointer).copied() else {
|
|
return false;
|
|
};
|
|
let is_error = finding.severity == ImportFindingSeverity::Error;
|
|
match finding.code.as_str() {
|
|
"unsupported_parameter_style" if is_error => parameter_style(pointer, node, nodes),
|
|
"unsupported_parameter_content" if is_error => pointer.ends_with("/content"),
|
|
"missing_parameter_schema" if is_error => {
|
|
matches!(&node.value, CanonicalSourceValue::Object(fields) if !fields.contains_key("schema") && !fields.contains_key("content"))
|
|
}
|
|
"unsupported_request_body" if is_error => pointer.ends_with("/requestBody"),
|
|
"unsupported_request_media_type" | "unsupported_xml_request_media_type" if is_error => {
|
|
media_from_pointer(pointer).is_some_and(|media| !supported_request_media(&media))
|
|
}
|
|
"unsupported_response_media_type" | "unsupported_xml_response_media_type" if is_error => {
|
|
media_from_pointer(pointer).is_some_and(|media| !supported_response_media(&media))
|
|
}
|
|
"unsupported_multipart_encoding" | "unsupported_form_encoding" if is_error => {
|
|
pointer.contains("/encoding")
|
|
}
|
|
"unsupported_form_schema" if is_error => form_schema_shape(pointer, node),
|
|
"missing_request_media_schema" | "missing_response_media_schema" if is_error => {
|
|
matches!(&node.value, CanonicalSourceValue::Object(fields) if !fields.contains_key("schema"))
|
|
}
|
|
"unsupported_consumes" if is_error => {
|
|
matches!(&node.value, CanonicalSourceValue::String(media) if !supported_request_media(media))
|
|
}
|
|
"unsupported_produces" if is_error => {
|
|
matches!(&node.value, CanonicalSourceValue::String(media) if !supported_response_media(media))
|
|
}
|
|
"path_parameter_not_required" if is_error => {
|
|
matches!(&node.value, CanonicalSourceValue::Object(fields) if !matches!(fields.get("required").map(|node| &node.value), Some(CanonicalSourceValue::Boolean(true))))
|
|
}
|
|
"path_parameter_placeholder_missing" if is_error => ir.operations.iter().any(|operation| {
|
|
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}}}"))))
|
|
}),
|
|
"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 => {
|
|
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))
|
|
}
|
|
"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 sanitized_secret_url(value: &str) -> bool {
|
|
value.contains("=[REDACTED]")
|
|
|| ((value.contains("://") || value.starts_with("//")) && !value.contains('@'))
|
|
}
|
|
|
|
fn parameter_style(
|
|
pointer: &str,
|
|
node: &CanonicalSourceNode,
|
|
nodes: &BTreeMap<String, &CanonicalSourceNode>,
|
|
) -> bool {
|
|
if pointer.ends_with("/schema") {
|
|
return compound_parameter(node);
|
|
}
|
|
if pointer.ends_with("/collectionFormat") {
|
|
return true;
|
|
}
|
|
if compound_parameter(node) {
|
|
return true;
|
|
}
|
|
let Some((parameter_pointer, field)) = pointer.rsplit_once('/') else {
|
|
return false;
|
|
};
|
|
let location = nodes
|
|
.get(&format!("{parameter_pointer}/in"))
|
|
.and_then(|node| match &node.value {
|
|
CanonicalSourceValue::String(value) => Some(value.as_str()),
|
|
_ => None,
|
|
});
|
|
match (field, &node.value, location) {
|
|
("style", CanonicalSourceValue::String(style), Some("path" | "header")) => {
|
|
style != "simple"
|
|
}
|
|
("style", CanonicalSourceValue::String(style), Some("query" | "cookie")) => style != "form",
|
|
("style", _, Some(_)) => true,
|
|
("explode", CanonicalSourceValue::Boolean(value), Some("path" | "header")) => *value,
|
|
("explode", CanonicalSourceValue::Boolean(value), Some("query" | "cookie")) => !*value,
|
|
("explode", _, Some(_)) => true,
|
|
("allowReserved", value, _) => !matches!(value, CanonicalSourceValue::Boolean(false)),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
fn compound_parameter(node: &CanonicalSourceNode) -> bool {
|
|
matches!(&node.value, CanonicalSourceValue::Object(fields) if matches!(fields.get("type").map(|node| &node.value), Some(CanonicalSourceValue::String(value)) if matches!(value.as_str(), "array" | "object")))
|
|
}
|
|
|
|
fn schema_keyword(pointer: &str, node: &CanonicalSourceNode) -> bool {
|
|
const UNSUPPORTED: &[&str] = &[
|
|
"additionalProperties",
|
|
"unevaluatedProperties",
|
|
"prefixItems",
|
|
"contains",
|
|
"dependentSchemas",
|
|
"if",
|
|
"then",
|
|
"else",
|
|
"not",
|
|
"propertyNames",
|
|
"minimum",
|
|
"maximum",
|
|
"exclusiveMinimum",
|
|
"exclusiveMaximum",
|
|
"minLength",
|
|
"maxLength",
|
|
"pattern",
|
|
"minItems",
|
|
"maxItems",
|
|
"uniqueItems",
|
|
"const",
|
|
"multipleOf",
|
|
"minProperties",
|
|
"maxProperties",
|
|
"dependentRequired",
|
|
];
|
|
let field = pointer.rsplit('/').next().unwrap_or_default();
|
|
if UNSUPPORTED.contains(&field) {
|
|
return true;
|
|
}
|
|
field == "type"
|
|
&& matches!(&node.value, CanonicalSourceValue::Array(items) if {
|
|
let concrete = items.iter().filter(|item| matches!(&item.value, CanonicalSourceValue::String(value) if value != "null")).count();
|
|
items.len() != 2 || concrete != 1 || !items.iter().any(|item| matches!(&item.value, CanonicalSourceValue::String(value) if value == "null"))
|
|
})
|
|
}
|
|
|
|
fn form_schema_shape(pointer: &str, node: &CanonicalSourceNode) -> bool {
|
|
if !pointer.contains("/requestBody/content/application~1x-www-form-urlencoded/schema")
|
|
&& !pointer.contains("/requestBody/content/multipart~1form-data/schema")
|
|
&& !pointer.contains("/parameters/")
|
|
{
|
|
return false;
|
|
}
|
|
let object = matches!(
|
|
&node.value,
|
|
CanonicalSourceValue::Object(fields)
|
|
if fields.contains_key("properties")
|
|
|| matches!(fields.get("type").map(|field| &field.value), Some(CanonicalSourceValue::String(value)) if value == "object")
|
|
);
|
|
let nested_array = pointer.ends_with("/items")
|
|
&& matches!(
|
|
&node.value,
|
|
CanonicalSourceValue::Object(fields)
|
|
if matches!(fields.get("type").map(|field| &field.value), Some(CanonicalSourceValue::String(value)) if value == "array")
|
|
);
|
|
pointer.ends_with("/schema") || object || nested_array
|
|
}
|
|
|
|
fn media_from_pointer(pointer: &str) -> Option<String> {
|
|
Some(
|
|
pointer
|
|
.rsplit('/')
|
|
.next()?
|
|
.replace("~1", "/")
|
|
.replace("~0", "~"),
|
|
)
|
|
}
|
|
|
|
fn supported_request_media(media: &str) -> bool {
|
|
matches!(
|
|
media,
|
|
"application/json" | "application/x-www-form-urlencoded" | "multipart/form-data"
|
|
) || media.ends_with("+json")
|
|
}
|
|
|
|
fn supported_response_media(media: &str) -> bool {
|
|
media == "application/json" || media.ends_with("+json")
|
|
}
|
|
|
|
fn secret_like(pointer: &str, value: &str) -> bool {
|
|
let pointer = pointer.to_ascii_lowercase();
|
|
let value = value.to_ascii_lowercase();
|
|
[
|
|
"token",
|
|
"secret",
|
|
"password",
|
|
"api_key",
|
|
"apikey",
|
|
"authorization",
|
|
]
|
|
.iter()
|
|
.any(|marker| pointer.contains(marker) || value.contains(marker))
|
|
|| value.starts_with("bearer ")
|
|
|| 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));
|
|
}
|
|
if pointer == "/basePath" {
|
|
return !matches!(&node.value, CanonicalSourceValue::String(path) if (path.is_empty() || path.starts_with('/')) && !path.chars().any(char::is_control));
|
|
}
|
|
if pointer.starts_with("/schemes/") {
|
|
return !matches!(&node.value, CanonicalSourceValue::String(scheme) if matches!(scheme.as_str(), "http" | "https"));
|
|
}
|
|
if pointer.ends_with("/url") {
|
|
return matches!(&node.value, CanonicalSourceValue::String(url) if !super::openapi3_findings::valid_http_server_url(url));
|
|
}
|
|
!matches!(&node.value, CanonicalSourceValue::Object(fields) if matches!(fields.get("url").map(|node| &node.value), Some(CanonicalSourceValue::String(_))))
|
|
}
|