594 lines
19 KiB
Rust
594 lines
19 KiB
Rust
use crank_core::HttpMethod;
|
|
use crank_import::rest::model::{NormalizedFinding, NormalizedLiteral, NormalizedScalarKind};
|
|
use crank_import::rest::{
|
|
ImportFindingSeverity, NormalizationConfig, NormalizedSchemaKind, SourceDigest,
|
|
normalize_verified_document, validate_normalized_ir,
|
|
};
|
|
|
|
fn normalize(document: &str) -> crank_import::rest::NormalizedIr {
|
|
normalize_verified_document(
|
|
document,
|
|
SourceDigest::parse("5".repeat(64)).unwrap(),
|
|
&NormalizationConfig::default(),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn undeclared_path_placeholders_are_exact_blockers_in_all_supported_formats() {
|
|
let openapi31 = r#"
|
|
openapi: 3.1.0
|
|
info: {title: Missing path declaration, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/items/{item_id}:
|
|
get:
|
|
operationId: getItem
|
|
responses: {'200': {description: ok}}
|
|
"#;
|
|
let openapi30 = openapi31.replace("3.1.0", "3.0.3");
|
|
let swagger2 = r#"
|
|
swagger: '2.0'
|
|
info: {title: Missing path declaration, version: '1'}
|
|
host: example.test
|
|
schemes: [https]
|
|
paths:
|
|
/items/{item_id}:
|
|
get:
|
|
operationId: getItem
|
|
responses: {'200': {description: ok}}
|
|
"#;
|
|
let documents = [
|
|
("OpenAPI 3.1", openapi31),
|
|
("OpenAPI 3.0", openapi30.as_str()),
|
|
("Swagger 2.0", swagger2),
|
|
];
|
|
|
|
for (version, document) in documents {
|
|
let ir = normalize(document);
|
|
validate_normalized_ir(&ir).expect("the source-attested path blocker must validate");
|
|
let operation = &ir.operations[0];
|
|
assert!(
|
|
operation.findings.iter().any(|finding| {
|
|
finding.code == "path_parameter_not_declared"
|
|
&& finding.severity == ImportFindingSeverity::Error
|
|
&& finding.operation_key.as_deref() == Some("GET /items/{item_id}")
|
|
&& finding.location.pointer == "/paths/~1items~1{item_id}/get"
|
|
}),
|
|
"{version} must report an undeclared path placeholder at the existing operation node: {:?}",
|
|
operation.findings
|
|
);
|
|
}
|
|
|
|
let declared = r#"
|
|
openapi: 3.1.0
|
|
info: {title: Declared path parameter, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/items/{item_id}:
|
|
get:
|
|
operationId: getItem
|
|
parameters:
|
|
- {name: item_id, in: path, required: true, schema: {type: string}}
|
|
responses: {'200': {description: ok}}
|
|
"#;
|
|
let mut ir = normalize(declared);
|
|
let operation = &mut ir.operations[0];
|
|
operation.findings.push(NormalizedFinding {
|
|
code: "path_parameter_not_declared".to_owned(),
|
|
severity: ImportFindingSeverity::Error,
|
|
message: "fabricated blocker".to_owned(),
|
|
construct_id: operation.stable_id.clone(),
|
|
location: operation.location.clone(),
|
|
operation_key: Some(operation.key.clone()),
|
|
});
|
|
assert!(
|
|
validate_normalized_ir(&ir).is_err(),
|
|
"a path blocker must not validate when the canonical source declares the placeholder"
|
|
);
|
|
|
|
let referenced = r#"
|
|
openapi: 3.1.0
|
|
info: {title: Referenced path parameter, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/items/{item_id}:
|
|
get:
|
|
operationId: getItem
|
|
parameters: [{$ref: '#/components/parameters/ItemId'}]
|
|
responses: {'200': {description: ok}}
|
|
components:
|
|
parameters:
|
|
ItemId: {name: item_id, in: path, required: true, schema: {type: string}}
|
|
"#;
|
|
let mut ir = normalize(referenced);
|
|
validate_normalized_ir(&ir).expect("a resolved referenced path parameter must validate");
|
|
let operation = &mut ir.operations[0];
|
|
assert!(operation.parameters.iter().any(|parameter| {
|
|
parameter.name == "item_id"
|
|
&& parameter.location == crank_import::rest::model::RestParameterLocation::Path
|
|
}));
|
|
operation.findings.push(NormalizedFinding {
|
|
code: "path_parameter_not_declared".to_owned(),
|
|
severity: ImportFindingSeverity::Error,
|
|
message: "fabricated blocker".to_owned(),
|
|
construct_id: operation.stable_id.clone(),
|
|
location: operation.location.clone(),
|
|
operation_key: Some(operation.key.clone()),
|
|
});
|
|
assert!(
|
|
validate_normalized_ir(&ir).is_err(),
|
|
"a blocker must not validate when a referenced parameter is effective"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn compound_schema_defaults_are_blocked_without_changing_scalar_defaults() {
|
|
let openapi31 = r#"
|
|
openapi: 3.1.0
|
|
info: {title: Compound defaults, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/defaults:
|
|
get:
|
|
operationId: getDefaults
|
|
responses:
|
|
'200':
|
|
description: ok
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
compound: {type: object, default: {nested: value}}
|
|
list: {type: array, items: {type: string}, default: [a, b]}
|
|
scalar: {type: string, default: original}
|
|
"#;
|
|
let openapi30 = openapi31.replace("3.1.0", "3.0.3");
|
|
let swagger2 = r#"
|
|
swagger: '2.0'
|
|
info: {title: Compound defaults, version: '1'}
|
|
host: example.test
|
|
schemes: [https]
|
|
paths:
|
|
/defaults:
|
|
get:
|
|
operationId: getDefaults
|
|
responses:
|
|
'200':
|
|
description: ok
|
|
schema:
|
|
type: object
|
|
properties:
|
|
compound: {type: object, default: {nested: value}}
|
|
list: {type: array, items: {type: string}, default: [a, b]}
|
|
scalar: {type: string, default: original}
|
|
"#;
|
|
let documents = [
|
|
("OpenAPI 3.1", openapi31),
|
|
("OpenAPI 3.0", openapi30.as_str()),
|
|
("Swagger 2.0", swagger2),
|
|
];
|
|
|
|
for (version, document) in documents {
|
|
let ir = normalize(document);
|
|
validate_normalized_ir(&ir).expect("source-attested compound blockers must validate");
|
|
let operation = &ir.operations[0];
|
|
let mut blockers = operation
|
|
.findings
|
|
.iter()
|
|
.filter(|finding| finding.code == "unsupported_compound_default")
|
|
.collect::<Vec<_>>();
|
|
blockers.sort_by(|left, right| left.location.pointer.cmp(&right.location.pointer));
|
|
assert_eq!(
|
|
blockers.len(),
|
|
2,
|
|
"{version} should block only object/array defaults: {:?}",
|
|
operation.findings
|
|
);
|
|
assert!(blockers.iter().all(|finding| {
|
|
finding.severity == ImportFindingSeverity::Error
|
|
&& finding.location.pointer.ends_with("/default")
|
|
&& finding.operation_key.as_deref() == Some("GET /defaults")
|
|
}));
|
|
let Some(NormalizedSchemaKind::Object { properties, .. }) = operation
|
|
.response_variants
|
|
.first()
|
|
.and_then(|variant| variant.schema.as_ref())
|
|
.map(|schema| &schema.kind)
|
|
else {
|
|
panic!("{version} response schema should remain an object");
|
|
};
|
|
assert!(matches!(
|
|
&properties["scalar"].kind,
|
|
NormalizedSchemaKind::Scalar { default_value: Some(value), .. }
|
|
if value == &NormalizedLiteral::String("original".to_owned())
|
|
));
|
|
|
|
let mut tampered = ir.clone();
|
|
let operation = &mut tampered.operations[0];
|
|
let scalar_default = operation
|
|
.findings
|
|
.iter_mut()
|
|
.find(|finding| finding.code == "unsupported_compound_default")
|
|
.unwrap();
|
|
let scalar_pointer = if version == "Swagger 2.0" {
|
|
"/paths/~1defaults/get/responses/200/schema/properties/scalar/default"
|
|
} else {
|
|
"/paths/~1defaults/get/responses/200/content/application~1json/schema/properties/scalar/default"
|
|
};
|
|
scalar_default.location.pointer = scalar_pointer.to_owned();
|
|
assert!(
|
|
validate_normalized_ir(&tampered).is_err(),
|
|
"{version} must reject a compound-default finding moved onto a scalar source value"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn variants_keep_exact_schema_coverage_and_reject_tampered_provenance() {
|
|
let document = r#"
|
|
openapi: 3.1.0
|
|
info: {title: Exact variants, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/items:
|
|
post:
|
|
operationId: createItem
|
|
requestBody:
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
name: {type: string}
|
|
responses:
|
|
'201':
|
|
description: ok
|
|
content:
|
|
application/json:
|
|
schema: {type: object, properties: {id: {type: integer}}}
|
|
"#;
|
|
let mut ir = normalize(document);
|
|
let request = ir.operations[0].request_body_variants[0]
|
|
.schema
|
|
.as_ref()
|
|
.unwrap();
|
|
assert_eq!(
|
|
request.location.pointer,
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema"
|
|
);
|
|
assert!(ir.coverage.iter().any(|entry| {
|
|
entry.location.pointer
|
|
== "/paths/~1items/post/requestBody/content/application~1json/schema/properties/name"
|
|
}));
|
|
|
|
ir.operations[0].request_body_variants[0]
|
|
.schema
|
|
.as_mut()
|
|
.unwrap()
|
|
.location
|
|
.pointer = "/paths/~1items/post/requestBody/content/application~1json".into();
|
|
assert!(validate_normalized_ir(&ir).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn exact_blockers_cover_unknown_method_style_media_and_missing_schema() {
|
|
let ir = normalize(
|
|
r#"
|
|
openapi: 3.0.3
|
|
info: {title: Findings, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/items/{id}:
|
|
propfind: {responses: {'200': {description: nope}}}
|
|
get:
|
|
operationId: getItem
|
|
parameters:
|
|
- {name: id, in: path, required: false, style: matrix, schema: {type: string}}
|
|
requestBody:
|
|
content:
|
|
text/plain: {schema: {type: string}}
|
|
responses:
|
|
'200':
|
|
description: ok
|
|
content:
|
|
application/json: {}
|
|
"#,
|
|
);
|
|
let findings = ir
|
|
.findings
|
|
.iter()
|
|
.chain(
|
|
ir.operations
|
|
.iter()
|
|
.flat_map(|operation| &operation.findings),
|
|
)
|
|
.collect::<Vec<_>>();
|
|
for (code, pointer) in [
|
|
("unsupported_http_method", "/paths/~1items~1{id}/propfind"),
|
|
(
|
|
"unsupported_parameter_style",
|
|
"/paths/~1items~1{id}/get/parameters/0/style",
|
|
),
|
|
(
|
|
"path_parameter_not_required",
|
|
"/paths/~1items~1{id}/get/parameters/0",
|
|
),
|
|
(
|
|
"unsupported_request_media_type",
|
|
"/paths/~1items~1{id}/get/requestBody/content/text~1plain",
|
|
),
|
|
(
|
|
"missing_response_media_schema",
|
|
"/paths/~1items~1{id}/get/responses/200/content/application~1json",
|
|
),
|
|
] {
|
|
assert!(
|
|
findings.iter().any(|finding| {
|
|
finding.code == code
|
|
&& finding.location.pointer == pointer
|
|
&& finding.severity == ImportFindingSeverity::Error
|
|
}),
|
|
"missing {code} at {pointer}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn form_encoding_and_unserializable_shapes_are_exact_blockers() {
|
|
let ir = normalize(
|
|
r#"
|
|
openapi: 3.1.0
|
|
info: {title: Form blockers, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/forms:
|
|
post:
|
|
operationId: sendForm
|
|
requestBody:
|
|
content:
|
|
application/x-www-form-urlencoded:
|
|
schema: {type: string}
|
|
encoding:
|
|
tag: {style: form, explode: false}
|
|
multipart/form-data:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
metadata: {type: object, properties: {name: {type: string}}}
|
|
nested_tags: {type: array, items: {type: array, items: {type: string}}}
|
|
composed_metadata:
|
|
oneOf:
|
|
- {type: object, properties: {name: {type: string}}}
|
|
responses: {'204': {description: Empty}}
|
|
"#,
|
|
);
|
|
let findings = &ir.operations[0].findings;
|
|
for (code, pointer) in [
|
|
(
|
|
"unsupported_form_schema",
|
|
"/paths/~1forms/post/requestBody/content/application~1x-www-form-urlencoded/schema",
|
|
),
|
|
(
|
|
"unsupported_form_encoding",
|
|
"/paths/~1forms/post/requestBody/content/application~1x-www-form-urlencoded/encoding/tag/explode",
|
|
),
|
|
(
|
|
"unsupported_form_schema",
|
|
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/metadata",
|
|
),
|
|
(
|
|
"unsupported_form_schema",
|
|
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/nested_tags/items",
|
|
),
|
|
(
|
|
"unsupported_form_schema",
|
|
"/paths/~1forms/post/requestBody/content/multipart~1form-data/schema/properties/composed_metadata/oneOf/0",
|
|
),
|
|
] {
|
|
assert!(
|
|
findings.iter().any(|finding| {
|
|
finding.code == code
|
|
&& finding.location.pointer == pointer
|
|
&& finding.severity == ImportFindingSeverity::Error
|
|
}),
|
|
"missing {code} at {pointer}"
|
|
);
|
|
}
|
|
validate_normalized_ir(&ir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn swagger_form_data_arrays_require_multi_collection_format() {
|
|
let ir = normalize(
|
|
r#"
|
|
swagger: '2.0'
|
|
info: {title: Swagger form arrays, version: '1'}
|
|
host: example.test
|
|
schemes: [https]
|
|
consumes: [application/x-www-form-urlencoded]
|
|
paths:
|
|
/forms:
|
|
post:
|
|
operationId: sendForm
|
|
parameters:
|
|
- {name: default_csv, in: formData, type: array, items: {type: string}}
|
|
- {name: explicit_csv, in: formData, type: array, collectionFormat: csv, items: {type: string}}
|
|
- {name: repeat_keys, in: formData, type: array, collectionFormat: multi, items: {type: string}}
|
|
responses: {'204': {description: Empty}}
|
|
"#,
|
|
);
|
|
let findings = &ir.operations[0].findings;
|
|
for pointer in [
|
|
"/paths/~1forms/post/parameters/0",
|
|
"/paths/~1forms/post/parameters/1/collectionFormat",
|
|
] {
|
|
assert!(
|
|
findings.iter().any(|finding| {
|
|
finding.code == "unsupported_parameter_style"
|
|
&& finding.location.pointer == pointer
|
|
&& finding.severity == ImportFindingSeverity::Error
|
|
}),
|
|
"missing CSV blocker at {pointer}"
|
|
);
|
|
}
|
|
assert!(!findings.iter().any(|finding| {
|
|
finding.code == "unsupported_parameter_style"
|
|
&& finding.location.pointer == "/paths/~1forms/post/parameters/2/collectionFormat"
|
|
}));
|
|
validate_normalized_ir(&ir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn unsupported_schema_keywords_and_invalid_server_port_are_exact_blockers() {
|
|
let ir = normalize(
|
|
r#"
|
|
openapi: 3.1.0
|
|
info: {title: Schema and server blockers, version: '1'}
|
|
servers:
|
|
- {url: 'https://example.test:not-a-port'}
|
|
- {url: "https://control.test/\n"}
|
|
paths:
|
|
/items:
|
|
post:
|
|
operationId: createItem
|
|
requestBody:
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
const: {kind: item}
|
|
minProperties: 1
|
|
maxProperties: 2
|
|
dependentRequired: {kind: [name]}
|
|
properties:
|
|
quantity: {type: number, multipleOf: 0.5}
|
|
responses: {'204': {description: Empty}}
|
|
"#,
|
|
);
|
|
let findings = ir
|
|
.findings
|
|
.iter()
|
|
.chain(ir.operations[0].findings.iter())
|
|
.collect::<Vec<_>>();
|
|
for (code, pointer) in [
|
|
("invalid_server", "/servers/0/url"),
|
|
("invalid_server", "/servers/1/url"),
|
|
(
|
|
"unsupported_schema_keyword",
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema/const",
|
|
),
|
|
(
|
|
"unsupported_schema_keyword",
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema/minProperties",
|
|
),
|
|
(
|
|
"unsupported_schema_keyword",
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema/maxProperties",
|
|
),
|
|
(
|
|
"unsupported_schema_keyword",
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema/dependentRequired",
|
|
),
|
|
(
|
|
"unsupported_schema_keyword",
|
|
"/paths/~1items/post/requestBody/content/application~1json/schema/properties/quantity/multipleOf",
|
|
),
|
|
] {
|
|
assert!(
|
|
findings.iter().any(|finding| {
|
|
finding.code == code
|
|
&& finding.location.pointer == pointer
|
|
&& finding.severity == ImportFindingSeverity::Error
|
|
}),
|
|
"missing {code} at {pointer}"
|
|
);
|
|
}
|
|
validate_normalized_ir(&ir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn swagger_merges_path_form_data_and_preserves_file_type() {
|
|
let ir = normalize(
|
|
r#"
|
|
swagger: '2.0'
|
|
info: {title: Upload, version: '1'}
|
|
host: example.test
|
|
schemes: [https]
|
|
consumes: [multipart/form-data]
|
|
paths:
|
|
/upload:
|
|
parameters:
|
|
- {name: file, in: formData, type: file, required: true}
|
|
- {name: title, in: formData, type: string}
|
|
post:
|
|
operationId: upload
|
|
parameters:
|
|
- {name: title, in: formData, type: integer, required: true}
|
|
responses:
|
|
'204': {description: done}
|
|
"#,
|
|
);
|
|
let operation = &ir.operations[0];
|
|
let variant = &operation.request_body_variants[0];
|
|
let NormalizedSchemaKind::Object {
|
|
properties,
|
|
required,
|
|
..
|
|
} = &variant.schema.as_ref().unwrap().kind
|
|
else {
|
|
panic!("formData must normalize to an object");
|
|
};
|
|
assert_eq!(required, &["file".to_owned(), "title".to_owned()]);
|
|
assert!(matches!(
|
|
properties["file"].kind,
|
|
NormalizedSchemaKind::Scalar {
|
|
scalar_type: NormalizedScalarKind::String,
|
|
ref format,
|
|
..
|
|
} if format.as_deref() == Some("binary")
|
|
));
|
|
assert!(matches!(
|
|
properties["title"].kind,
|
|
NormalizedSchemaKind::Scalar {
|
|
scalar_type: NormalizedScalarKind::Integer,
|
|
..
|
|
}
|
|
));
|
|
assert_eq!(
|
|
properties["file"].location.pointer,
|
|
"/paths/~1upload/parameters/0"
|
|
);
|
|
assert_eq!(
|
|
properties["title"].location.pointer,
|
|
"/paths/~1upload/post/parameters/0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn head_declared_body_is_normalized_as_no_content() {
|
|
let ir = normalize(
|
|
r#"
|
|
openapi: 3.0.3
|
|
info: {title: Head, version: '1'}
|
|
servers: [{url: https://example.test}]
|
|
paths:
|
|
/health:
|
|
head:
|
|
operationId: health
|
|
responses:
|
|
'200':
|
|
description: ok
|
|
content:
|
|
application/json: {schema: {type: object}}
|
|
"#,
|
|
);
|
|
let operation = &ir.operations[0];
|
|
assert_eq!(operation.method, HttpMethod::Head);
|
|
assert!(operation.response_schema.is_none());
|
|
assert_eq!(operation.response_variants.len(), 1);
|
|
assert!(operation.response_variants[0].schema.is_none());
|
|
assert!(operation.response_variants[0].content_type.is_none());
|
|
}
|