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
@@ -0,0 +1,372 @@
|
||||
//! Regression coverage for the bounded YAML/JSON materialization guard
|
||||
//! (Gitea #87).
|
||||
//!
|
||||
//! Alias expansions are charged against an aggregate expanded string/key
|
||||
//! budget *before* `serde_yaml` materializes an owned `Value`. The public
|
||||
//! entry points must return the controlled `ImportParseError::LimitExceeded`
|
||||
//! instead of materializing an oversized tree. Small, ordinary alias usage,
|
||||
//! duplicate keys, non-finite numbers and legacy behaviour are unchanged.
|
||||
//!
|
||||
//! The fixtures here are deliberately modest (a 16 KiB scalar with eight
|
||||
//! doubling levels expands to a few MiB, well under the risky 256 MiB
|
||||
//! reproduction) so the suite is safe to run in-process. Exact boundary
|
||||
//! arithmetic is covered by unit tests inside `decode_budget`.
|
||||
|
||||
use crank_import::rest::{
|
||||
ExternalDocumentSnapshot, ImportParseError, NormalizationConfig, SourceDigest,
|
||||
external_reference_uris, normalize_verified_bundle, normalize_verified_document,
|
||||
preview_document_legacy_v1, reference_uris, validate_document_bundle_source_safe,
|
||||
validate_document_source_safe, validate_resolved_bundle_source_safe,
|
||||
};
|
||||
|
||||
fn digest(byte: char) -> SourceDigest {
|
||||
SourceDigest::parse(byte.to_string().repeat(64)).expect("64-char digest is valid")
|
||||
}
|
||||
|
||||
/// Minimal valid OpenAPI 3.1 document with one GET operation. `extra` is
|
||||
/// inserted at the root, ahead of `servers`, for extension fields.
|
||||
fn valid_openapi(extra: &str) -> String {
|
||||
format!(
|
||||
"openapi: 3.1.0\n\
|
||||
info: {{ title: Alias budget, version: '1' }}\n\
|
||||
{extra}\
|
||||
servers: [{{ url: https://api.example.test }}]\n\
|
||||
paths:\n\
|
||||
\x20 /items:\n\
|
||||
\x20 get:\n\
|
||||
\x20 operationId: listItems\n\
|
||||
\x20 responses:\n\
|
||||
\x20 '200': {{ description: ok }}\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// Root extension whose value doubles the previous alias `levels` times,
|
||||
/// starting from a 16 KiB scalar. Every per-item default limit is respected;
|
||||
/// only the aggregate materialized size grows.
|
||||
fn doubling_alias_document(levels: usize) -> String {
|
||||
let scalar = "a".repeat(16 * 1024);
|
||||
let mut extra = format!("x-expansion-0: &expansion0 \"{scalar}\"\n");
|
||||
for level in 1..levels {
|
||||
let previous = level - 1;
|
||||
extra.push_str(&format!(
|
||||
"x-expansion-{level}: &expansion{level} [*expansion{previous}, *expansion{previous}]\n"
|
||||
));
|
||||
}
|
||||
let previous = levels - 1;
|
||||
extra.push_str(&format!(
|
||||
"x-expansion: &expansion{levels} [*expansion{previous}, *expansion{previous}]\n"
|
||||
));
|
||||
valid_openapi(&extra)
|
||||
}
|
||||
|
||||
/// A compact alias chain for configurable boundary tests. The top-level value
|
||||
/// repeats the scalar `2^levels` times through aliases.
|
||||
fn alias_chain(scalar_len: usize, levels: usize) -> String {
|
||||
let scalar = "b".repeat(scalar_len);
|
||||
let mut document = format!("k0: &a0 \"{scalar}\"\n");
|
||||
for level in 1..=levels {
|
||||
let previous = level - 1;
|
||||
document.push_str(&format!(
|
||||
"k{level}: &a{level} [*a{previous}, *a{previous}]\n"
|
||||
));
|
||||
}
|
||||
document
|
||||
}
|
||||
|
||||
/// Ordinary, small alias usage that must remain accepted.
|
||||
fn small_alias_document() -> String {
|
||||
valid_openapi(
|
||||
"x-anchor: &anchor { type: string }\n\
|
||||
x-aliases: [*anchor, *anchor, *anchor]\n",
|
||||
)
|
||||
}
|
||||
|
||||
const LEVELS: usize = 8;
|
||||
|
||||
#[test]
|
||||
fn primary_source_safety_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert_eq!(
|
||||
validate_document_source_safe(&document, &NormalizationConfig::default()),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_source_safety_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert_eq!(
|
||||
validate_document_bundle_source_safe(&[document.as_str()], &NormalizationConfig::default()),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_source_safety_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert_eq!(
|
||||
validate_resolved_bundle_source_safe(
|
||||
&document,
|
||||
digest('a'),
|
||||
&[],
|
||||
&NormalizationConfig::default()
|
||||
),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalization_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert!(matches!(
|
||||
normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn primary_reference_uri_scan_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert_eq!(
|
||||
reference_uris(&document, &NormalizationConfig::default()),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_reference_uri_scan_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let document = doubling_alias_document(LEVELS);
|
||||
assert_eq!(
|
||||
external_reference_uris(&document, &NormalizationConfig::default()),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supplied_external_snapshot_rejects_doubling_aliases_over_aggregate_budget() {
|
||||
let primary = valid_openapi("");
|
||||
let snapshot = ExternalDocumentSnapshot {
|
||||
canonical_uri: "https://schemas.example.test/root.yaml".to_owned(),
|
||||
digest: digest('b'),
|
||||
document: doubling_alias_document(LEVELS),
|
||||
};
|
||||
assert!(matches!(
|
||||
normalize_verified_bundle(
|
||||
&primary,
|
||||
digest('a'),
|
||||
&[snapshot],
|
||||
&NormalizationConfig::default()
|
||||
),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_source_safety_rejects_doubling_aliases_in_supplied_snapshot() {
|
||||
let primary = valid_openapi("");
|
||||
let snapshot = ExternalDocumentSnapshot {
|
||||
canonical_uri: "https://schemas.example.test/root.yaml".to_owned(),
|
||||
digest: digest('b'),
|
||||
document: doubling_alias_document(LEVELS),
|
||||
};
|
||||
assert_eq!(
|
||||
validate_resolved_bundle_source_safe(
|
||||
&primary,
|
||||
digest('a'),
|
||||
&[snapshot],
|
||||
&NormalizationConfig::default()
|
||||
),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_aliases_remain_accepted_across_public_entry_points() {
|
||||
let document = small_alias_document();
|
||||
let config = NormalizationConfig::default();
|
||||
|
||||
assert_eq!(validate_document_source_safe(&document, &config), Ok(()));
|
||||
assert_eq!(
|
||||
validate_document_bundle_source_safe(&[document.as_str()], &config),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
validate_resolved_bundle_source_safe(&document, digest('a'), &[], &config),
|
||||
Ok(())
|
||||
);
|
||||
assert!(normalize_verified_document(&document, digest('a'), &config).is_ok());
|
||||
assert_eq!(reference_uris(&document, &config), Ok(Vec::new()));
|
||||
assert_eq!(external_reference_uris(&document, &config), Ok(Vec::new()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_keys_are_still_rejected_as_invalid_document() {
|
||||
let document = valid_openapi("x-dup: 1\nx-dup: 2\n");
|
||||
assert_eq!(
|
||||
validate_document_source_safe(&document, &NormalizationConfig::default()),
|
||||
Err(ImportParseError::InvalidDocument)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_finite_yaml_scalars_are_still_rejected_as_invalid_document() {
|
||||
let document = valid_openapi("x-nonfinite: .inf\n");
|
||||
assert_eq!(
|
||||
validate_document_source_safe(&document, &NormalizationConfig::default()),
|
||||
Err(ImportParseError::InvalidDocument)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_expansion_below_boundary_is_accepted_and_above_is_rejected() {
|
||||
// allowance = 16 * max(max_bytes, max_scalar_bytes) = 16 * 4096 = 65536.
|
||||
let config = NormalizationConfig {
|
||||
max_bytes: 4096,
|
||||
max_scalar_bytes: 64,
|
||||
..NormalizationConfig::default()
|
||||
};
|
||||
let below = alias_chain(60, 9); // ~61_400 expanded string bytes
|
||||
let above = alias_chain(60, 10); // ~122_800 expanded string bytes
|
||||
assert!(
|
||||
below.len() <= config.max_bytes,
|
||||
"fixture must fit raw limit"
|
||||
);
|
||||
assert!(
|
||||
above.len() <= config.max_bytes,
|
||||
"fixture must fit raw limit"
|
||||
);
|
||||
|
||||
assert_eq!(reference_uris(&below, &config), Ok(Vec::new()));
|
||||
assert_eq!(
|
||||
reference_uris(&above, &config),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordinary_alias_output_matches_expanded_equivalent() {
|
||||
let aliased = valid_openapi("x-shared: &shared { type: string }\nx-copy: *shared\n");
|
||||
let expanded = valid_openapi("x-shared: { type: string }\nx-copy: { type: string }\n");
|
||||
let config = NormalizationConfig::default();
|
||||
let aliased = normalize_verified_document(&aliased, digest('a'), &config)
|
||||
.expect("aliased fixture parses");
|
||||
let expanded = normalize_verified_document(&expanded, digest('a'), &config)
|
||||
.expect("expanded fixture parses");
|
||||
assert_eq!(aliased, expanded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_duplicate_keys_keep_historical_invalid_document() {
|
||||
// The legacy `serde_yaml::Value` mapping rejects duplicate keys, the same
|
||||
// as the strict decoder; the preflight must not change that.
|
||||
let document = "openapi: 3.0.3\n\
|
||||
info: { title: Legacy, version: '1' }\n\
|
||||
info: { title: Second, version: '1' }\n\
|
||||
paths:\n\
|
||||
\x20 /x:\n\
|
||||
\x20 get:\n\
|
||||
\x20 responses:\n\
|
||||
\x20 '200': { description: ok }\n";
|
||||
assert_eq!(
|
||||
preview_document_legacy_v1(document),
|
||||
Err(ImportParseError::InvalidDocument)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_tagged_extension_keeps_historical_mapping_conversion() {
|
||||
let document = "openapi: 3.0.3\n\
|
||||
info: { title: Legacy tag, version: '1' }\n\
|
||||
x-tagged: !Thing value\n\
|
||||
paths:\n\
|
||||
\x20 /x:\n\
|
||||
\x20 get:\n\
|
||||
\x20 responses:\n\
|
||||
\x20 '200': { description: ok }\n";
|
||||
assert!(preview_document_legacy_v1(document).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_non_finite_numbers_keep_historical_null_conversion() {
|
||||
// The legacy path converts through `serde_yaml::Value` and then JSON, so a
|
||||
// non-finite number becomes `null` and stays accepted; the strict decoder
|
||||
// rejects it (covered above). The permissive preflight must preserve this.
|
||||
let document = "openapi: 3.0.3\n\
|
||||
info: { title: Legacy non-finite, version: '1' }\n\
|
||||
x-nonfinite: .inf\n\
|
||||
paths:\n\
|
||||
\x20 /x:\n\
|
||||
\x20 get:\n\
|
||||
\x20 responses:\n\
|
||||
\x20 '200': { description: ok }\n";
|
||||
assert!(preview_document_legacy_v1(document).is_ok());
|
||||
}
|
||||
|
||||
fn snapshot(document: &str) -> ExternalDocumentSnapshot {
|
||||
ExternalDocumentSnapshot {
|
||||
canonical_uri: "https://schemas.example.test/root.yaml".to_owned(),
|
||||
digest: digest('b'),
|
||||
document: document.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_bundle_document_count_is_capped_before_preflight() {
|
||||
// Without the early count guard the empty documents would fail later as
|
||||
// InvalidDocument; the explicit cap must win with LimitExceeded.
|
||||
let config = NormalizationConfig {
|
||||
max_reference_documents: 1,
|
||||
..NormalizationConfig::default()
|
||||
};
|
||||
let documents = ["", "", ""];
|
||||
assert_eq!(
|
||||
validate_document_bundle_source_safe(&documents, &config),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_bundle_snapshot_count_is_capped_before_preflight() {
|
||||
let config = NormalizationConfig {
|
||||
max_reference_documents: 0,
|
||||
..NormalizationConfig::default()
|
||||
};
|
||||
let primary = valid_openapi("");
|
||||
assert_eq!(
|
||||
validate_resolved_bundle_source_safe(&primary, digest('a'), &[snapshot("{}")], &config),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_snapshot_raw_bytes_and_aliases_are_guarded_before_preflight() {
|
||||
let primary = valid_openapi("");
|
||||
let bytes_config = NormalizationConfig {
|
||||
max_external_document_bytes: 4,
|
||||
..NormalizationConfig::default()
|
||||
};
|
||||
assert!(matches!(
|
||||
normalize_verified_bundle(
|
||||
&primary,
|
||||
digest('a'),
|
||||
&[snapshot("{\"a\":1}")],
|
||||
&bytes_config
|
||||
),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
));
|
||||
|
||||
let alias_config = NormalizationConfig {
|
||||
max_aliases: 8,
|
||||
..NormalizationConfig::default()
|
||||
};
|
||||
let aliased = "a: &x 1\nb: [*x, *x, *x, *x, *x, *x, *x, *x, *x]\n";
|
||||
assert_eq!(
|
||||
validate_resolved_bundle_source_safe(
|
||||
&primary,
|
||||
digest('a'),
|
||||
&[snapshot(aliased)],
|
||||
&alias_config
|
||||
),
|
||||
Err(ImportParseError::LimitExceeded)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user