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
@@ -15,6 +15,7 @@ use crate::rest::{
|
||||
openapi3, swagger2,
|
||||
};
|
||||
|
||||
use super::decode_budget;
|
||||
use super::normalize_coverage;
|
||||
use super::normalize_limits;
|
||||
|
||||
@@ -61,9 +62,9 @@ pub fn external_reference_uris(
|
||||
pub fn preview_document_legacy_v1(document: &str) -> Result<ImportPreview, ImportParseError> {
|
||||
let root = decode_legacy_v1(document)?;
|
||||
let mut parsed = match root.get("openapi") {
|
||||
Some(_) => super::openapi3_legacy::parse_document_legacy_v1(&root)?,
|
||||
Some(_) => openapi3::parse_document_legacy_v1(&root)?,
|
||||
None if root.get("swagger").and_then(Value::as_str) == Some("2.0") => {
|
||||
super::swagger2_legacy::parse_document_legacy_v1(&root)?
|
||||
swagger2::parse_document_legacy_v1(&root)?
|
||||
}
|
||||
None => return Err(ImportParseError::UnsupportedDocument),
|
||||
};
|
||||
@@ -80,7 +81,7 @@ pub fn preview_document_legacy_v1(document: &str) -> Result<ImportPreview, Impor
|
||||
operation.response_schema = Some(resolve_local_ref(&root, &schema, 0));
|
||||
}
|
||||
}
|
||||
Ok(super::normalize_preview::preview_from_document(parsed))
|
||||
Ok(preview_from_document(parsed))
|
||||
}
|
||||
|
||||
fn decode_legacy_v1(document: &str) -> Result<Value, ImportParseError> {
|
||||
@@ -90,6 +91,7 @@ fn decode_legacy_v1(document: &str) -> Result<Value, ImportParseError> {
|
||||
{
|
||||
return Err(ImportParseError::LimitExceeded);
|
||||
}
|
||||
decode_budget::preflight_primary(document, &config)?;
|
||||
let root = decode(document)?;
|
||||
validate_value_limits(&root, &config)?;
|
||||
Ok(root)
|
||||
@@ -103,6 +105,39 @@ pub fn normalize_verified_document(
|
||||
normalize_verified_bundle(document, digest, &[], config)
|
||||
}
|
||||
|
||||
/// Rejects source documents containing credential-like literals before a
|
||||
/// caller persists their original bytes. Accepted source bytes remain verbatim.
|
||||
pub fn validate_document_source_safe(
|
||||
document: &str,
|
||||
config: &NormalizationConfig,
|
||||
) -> Result<(), ImportParseError> {
|
||||
config
|
||||
.validate_versions()
|
||||
.map_err(|_| ImportParseError::UnsupportedDocument)?;
|
||||
if document.len() > config.max_bytes
|
||||
|| normalize_limits::alias_count(document) > config.max_aliases
|
||||
{
|
||||
return Err(ImportParseError::LimitExceeded);
|
||||
}
|
||||
decode_budget::preflight_primary(document, config)?;
|
||||
let mut root =
|
||||
super::strict_decode::decode(document).map_err(|_| ImportParseError::InvalidDocument)?;
|
||||
validate_value_limits(&root, config)?;
|
||||
let mut findings = Vec::new();
|
||||
let mut locations = Vec::new();
|
||||
super::metadata_findings::redact_secret_like_literals(
|
||||
&mut root,
|
||||
"",
|
||||
&mut findings,
|
||||
&mut locations,
|
||||
);
|
||||
if findings.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ImportParseError::InvalidDocument)
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalizes a verified primary document together with immutable external
|
||||
/// snapshots. This function is pure: callers must perform all URL policy,
|
||||
/// fetching, artifact persistence and digest verification beforehand.
|
||||
@@ -120,12 +155,24 @@ pub fn normalize_verified_bundle(
|
||||
{
|
||||
return Err(ImportParseError::LimitExceeded);
|
||||
}
|
||||
if snapshots.len() > config.max_reference_documents {
|
||||
return Err(ImportParseError::LimitExceeded);
|
||||
}
|
||||
for snapshot in snapshots {
|
||||
if snapshot.document.len() > config.max_external_document_bytes
|
||||
|| normalize_limits::alias_count(&snapshot.document) > config.max_aliases
|
||||
{
|
||||
return Err(ImportParseError::LimitExceeded);
|
||||
}
|
||||
}
|
||||
decode_budget::preflight_bundle(document, snapshots, config)?;
|
||||
let source_syntax = if serde_json::from_str::<Value>(document).is_ok() {
|
||||
SourceSyntax::Json
|
||||
} else {
|
||||
SourceSyntax::Yaml
|
||||
};
|
||||
let root = decode(document)?;
|
||||
let root =
|
||||
super::strict_decode::decode(document).map_err(|_| ImportParseError::InvalidDocument)?;
|
||||
validate_value_limits(&root, config)?;
|
||||
for snapshot in snapshots {
|
||||
if snapshot.document.len() > config.max_external_document_bytes {
|
||||
@@ -136,7 +183,14 @@ pub fn normalize_verified_bundle(
|
||||
let mut root = resolution.root;
|
||||
validate_value_limits(&root, config)?;
|
||||
|
||||
let redacted_literals = super::security_literals::inspect_and_redact(&mut root);
|
||||
let mut literal_findings = Vec::new();
|
||||
let mut literal_locations = Vec::new();
|
||||
super::metadata_findings::redact_secret_like_literals(
|
||||
&mut root,
|
||||
"",
|
||||
&mut literal_findings,
|
||||
&mut literal_locations,
|
||||
);
|
||||
|
||||
let mut parsed = match root.get("openapi").and_then(Value::as_str) {
|
||||
Some(version) if supported_oas_version(version) => openapi3::parse_document(&root)?,
|
||||
@@ -146,15 +200,8 @@ pub fn normalize_verified_bundle(
|
||||
}
|
||||
None => return Err(ImportParseError::UnsupportedDocument),
|
||||
};
|
||||
for literal in redacted_literals {
|
||||
parsed.findings.push(super::recommendations::document_finding(
|
||||
"secret_literal_redacted",
|
||||
"Документ содержит literal credential; значение удалено из normalized representation.",
|
||||
));
|
||||
parsed
|
||||
.internal_finding_locations
|
||||
.push(Some(literal.location));
|
||||
}
|
||||
parsed.findings.extend(literal_findings);
|
||||
parsed.internal_finding_locations.extend(literal_locations);
|
||||
|
||||
if parsed.operations.is_empty()
|
||||
&& !parsed
|
||||
@@ -176,7 +223,11 @@ pub fn normalize_verified_bundle(
|
||||
}
|
||||
|
||||
pub fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview {
|
||||
super::normalize_preview::preview_from_ir(ir)
|
||||
super::normalize_projection::preview_from_ir(ir)
|
||||
}
|
||||
|
||||
fn preview_from_document(document: RestImportDocument) -> ImportPreview {
|
||||
super::normalize_projection::preview_from_document(document)
|
||||
}
|
||||
|
||||
pub(super) fn decode(document: &str) -> Result<Value, ImportParseError> {
|
||||
@@ -205,7 +256,6 @@ fn canonicalize(
|
||||
reference_graph: crate::rest::model::ResolvedReferenceGraph,
|
||||
resolution_findings: Vec<NormalizedFinding>,
|
||||
) -> Result<NormalizedIr, ImportParseError> {
|
||||
let security_schemes = document.security_schemes.clone();
|
||||
let source = ImportSourcePreview {
|
||||
format: document.format,
|
||||
version: document.version,
|
||||
@@ -505,8 +555,6 @@ fn canonicalize(
|
||||
key: operation.key,
|
||||
method: operation.method,
|
||||
path: operation.path,
|
||||
security_requirements: operation.security_requirements,
|
||||
security_schemes: security_schemes.clone(),
|
||||
operation_id: operation.operation_id,
|
||||
summary: operation.summary,
|
||||
description: operation.description,
|
||||
@@ -517,6 +565,8 @@ fn canonicalize(
|
||||
request_body_variants,
|
||||
response_variants,
|
||||
servers: operation.servers,
|
||||
security_requirements: operation.security_requirements,
|
||||
security_schemes: document.security_schemes.clone(),
|
||||
findings: operation_findings,
|
||||
})
|
||||
})
|
||||
@@ -708,11 +758,11 @@ fn canonicalize(
|
||||
}
|
||||
|
||||
pub fn validate_normalized_ir(ir: &NormalizedIr) -> Result<(), ImportParseError> {
|
||||
let current =
|
||||
ir.normalizer_version == NORMALIZER_VERSION && ir.projection_version == PROJECTION_VERSION;
|
||||
let historical_v4 = ir.normalizer_version == super::model::HISTORICAL_NORMALIZER_VERSION
|
||||
&& ir.projection_version == super::model::HISTORICAL_PROJECTION_VERSION;
|
||||
if !current && !historical_v4 {
|
||||
if !((ir.normalizer_version == NORMALIZER_VERSION
|
||||
&& ir.projection_version == PROJECTION_VERSION)
|
||||
|| (ir.normalizer_version == crate::rest::model::HISTORICAL_NORMALIZER_VERSION
|
||||
&& ir.projection_version == crate::rest::model::HISTORICAL_PROJECTION_VERSION))
|
||||
{
|
||||
return Err(ImportParseError::InvalidDocument);
|
||||
}
|
||||
validate_reference_graph(ir)?;
|
||||
|
||||
Reference in new issue
Block a user