Files
crank/crates/crank-import/tests/reference_memory_limits.rs
T
bsodfather e93de7015a
CI / Rust Checks (pull_request) Failing after 1s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
feat(import): complete OpenAPI onboarding and verify delivery
2026-10-09 17:50:38 +03:00

175 lines
6.0 KiB
Rust

//! Regressions for the `$ref` expansion aggregate string/key byte budget
//! (Gitea #88).
//!
//! `reference::resolve` clones the referenced target subtree per `$ref`
//! occurrence. Many individually small (but not tiny) targets therefore
//! multiply into a large materialized tree while staying under
//! `max_references`, `max_expanded_nodes`, `max_nodes`, `max_collection_items`
//! and `max_scalar_bytes`; a bounded probe reproduced a child address-space
//! abort for such an input. The aggregate copy budget must reject these with
//! `ImportParseError::LimitExceeded` before the copies allocate, for both local
//! and supplied external references, while small ordinary usage keeps working.
//!
//! The fixture is deliberately modest (16 KiB payload, 400 references → ≈6.4
//! MiB expanded, far below the risky 256 MiB reproduction and below 20 MiB).
use crank_import::rest::{
ExternalDocumentSnapshot, ImportParseError, NormalizationConfig, SourceDigest,
normalize_verified_document, validate_resolved_bundle_source_safe,
};
use serde_json::{Map, Value, json};
const DESCRIPTION_BYTES: usize = 16 * 1024;
const EXPANDING_REFS: usize = 400;
const EXTERNAL_URI: &str = "https://schemas.example.test/root.yaml";
// The fixture must stay comfortably below the unsafe reproduction size.
const _: () = assert!(DESCRIPTION_BYTES * EXPANDING_REFS < 20 * 1024 * 1024);
fn digest(byte: char) -> SourceDigest {
SourceDigest::parse(byte.to_string().repeat(64)).expect("64-char digest is valid")
}
fn big_schema() -> Value {
json!({
"type": "object",
"description": "x".repeat(DESCRIPTION_BYTES),
"properties": { "id": { "type": "string" } }
})
}
fn big_schema_with_large_property_key() -> Value {
let mut properties = Map::new();
properties.insert("k".repeat(DESCRIPTION_BYTES), json!({ "type": "string" }));
json!({ "type": "object", "properties": Value::Object(properties) })
}
fn document_with_refs(ref_uri: &str, ref_count: usize) -> String {
let all_of: Vec<Value> = (0..ref_count).map(|_| json!({ "$ref": ref_uri })).collect();
let document = json!({
"openapi": "3.1.0",
"info": { "title": "Ref budget", "version": "1" },
"servers": [{ "url": "https://api.example.test" }],
"paths": {
"/items": {
"get": {
"operationId": "listItems",
"responses": {
"200": {
"description": "ok",
"content": {
"application/json": {
"schema": { "allOf": all_of }
}
}
}
}
}
}
}
});
serde_json::to_string(&document).expect("document serializes")
}
fn local_ref_document(ref_count: usize) -> String {
local_ref_document_with(big_schema(), ref_count)
}
fn local_ref_document_with(schema: Value, ref_count: usize) -> String {
let mut document: Value =
serde_json::from_str(&document_with_refs("#/components/schemas/Big", ref_count))
.expect("valid JSON");
document["components"] = json!({ "schemas": { "Big": schema } });
serde_json::to_string(&document).expect("document serializes")
}
fn external_ref_document(ref_count: usize) -> String {
let uri = format!("{EXTERNAL_URI}#/Big");
document_with_refs(&uri, ref_count)
}
fn external_snapshot() -> ExternalDocumentSnapshot {
let document =
serde_json::to_string(&json!({ "Big": big_schema() })).expect("snapshot serializes");
ExternalDocumentSnapshot {
canonical_uri: EXTERNAL_URI.to_owned(),
digest: digest('b'),
document,
}
}
fn assert_fits_raw_limit(document: &str) {
assert!(
document.len() < NormalizationConfig::default().max_bytes,
"fixture must stay under the raw byte limit, got {} bytes",
document.len()
);
}
#[test]
fn local_ref_expansion_over_aggregate_budget_is_rejected() {
let document = local_ref_document(EXPANDING_REFS);
assert_fits_raw_limit(&document);
assert!(matches!(
normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()),
Err(ImportParseError::LimitExceeded)
));
}
#[test]
fn local_ref_expansion_counts_large_object_keys() {
let document = local_ref_document_with(big_schema_with_large_property_key(), EXPANDING_REFS);
assert_fits_raw_limit(&document);
assert!(matches!(
normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()),
Err(ImportParseError::LimitExceeded)
));
}
#[test]
fn supplied_external_ref_expansion_over_aggregate_budget_is_rejected() {
let primary = external_ref_document(EXPANDING_REFS);
assert_fits_raw_limit(&primary);
assert_eq!(
validate_resolved_bundle_source_safe(
&primary,
digest('a'),
&[external_snapshot()],
&NormalizationConfig::default()
),
Err(ImportParseError::LimitExceeded)
);
}
#[test]
fn a_single_local_ref_stays_within_budget() {
let document = local_ref_document(1);
let ir = normalize_verified_document(&document, digest('a'), &NormalizationConfig::default())
.expect("a single reference must stay within the expanded budget");
assert_eq!(ir.operations.len(), 1);
assert_eq!(ir.reference_graph.edges.len(), 1);
}
#[test]
fn several_local_refs_stay_within_budget() {
let document = local_ref_document(4);
assert!(
normalize_verified_document(&document, digest('a'), &NormalizationConfig::default())
.is_ok()
);
}
#[test]
fn a_single_supplied_external_ref_stays_within_budget() {
let primary = external_ref_document(1);
assert_eq!(
validate_resolved_bundle_source_safe(
&primary,
digest('a'),
&[external_snapshot()],
&NormalizationConfig::default()
),
Ok(())
);
}