feat(import): preserve supported HTTP contracts
This commit is contained in:
1 parent
083974c27f
commit
f8ee7c9bfe
87 files changed
+7496
-534
No files matched your search
@@ -0,0 +1,218 @@
|
||||
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 => {
|
||||
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value))
|
||||
}
|
||||
"invalid_server" if is_error => invalid_server(pointer, node),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
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-")
|
||||
}
|
||||
|
||||
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(_))))
|
||||
}
|
||||
Reference in new issue
Block a user