feat(import): bind OpenAPI security schemes safely
This commit is contained in:
1 parent
c562d5368d
commit
41a0e46110
38 files changed
+4651
-590
No files matched your search
@@ -12,8 +12,8 @@ use crank_import::rest::{
|
||||
use crank_registry::{
|
||||
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
|
||||
CreateArtifactSourceRequest, CreateImportJobRequest, FinishImportJobRequest,
|
||||
ImportConflictMode, ImportJobApplyResult, ImportJobId, ImportJobKind, ImportJobSourceEnvelope,
|
||||
ImportJobStatus, ImportOperationDraft, RegistryError,
|
||||
ImportAuthRequirement, ImportConflictMode, ImportJobApplyResult, ImportJobId, ImportJobKind,
|
||||
ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, RegistryError,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -23,7 +23,7 @@ use tracing::{info, instrument};
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
service::{
|
||||
AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
|
||||
AdminService, OpenApiAuthMapping, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
|
||||
OpenApiImportCreatedOperation, OpenApiImportPreviewResponse, OpenApiImportSkippedOperation,
|
||||
OpenApiUpload, OpenApiUploadLocale, OperationPayload, new_prefixed_id,
|
||||
},
|
||||
@@ -31,6 +31,7 @@ use crate::{
|
||||
|
||||
mod external_references;
|
||||
mod job_contract;
|
||||
mod security;
|
||||
use external_references::ImportReplayContext;
|
||||
use job_contract::{
|
||||
SourceDetachGuard, import_job_dependencies, import_job_normalization_config, import_job_source,
|
||||
@@ -52,6 +53,14 @@ impl AdminService {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
|
||||
validate_openapi_upload(&upload)?;
|
||||
let literal_findings = crank_import::rest::scan_secret_literals(&upload.bytes)
|
||||
.map_err(|_| ApiError::openapi_upload(upload.locale, "invalid_document"))?;
|
||||
if !literal_findings.is_empty() {
|
||||
return Err(ApiError::openapi_upload(
|
||||
upload.locale,
|
||||
"literal_credentials_rejected",
|
||||
));
|
||||
}
|
||||
let OpenApiUpload {
|
||||
bytes,
|
||||
mime_type,
|
||||
@@ -105,6 +114,14 @@ impl AdminService {
|
||||
let mut dependencies = self
|
||||
.materialize_external_reference_snapshots(workspace_id, &verified.bytes, now)
|
||||
.await;
|
||||
if dependencies.secret_literal_rejected {
|
||||
dependencies.detach_all().await;
|
||||
detach_guard.detach_now().await;
|
||||
return Err(ApiError::openapi_upload(
|
||||
locale,
|
||||
"literal_credentials_rejected",
|
||||
));
|
||||
}
|
||||
let parsed = match parse_verified_preview(
|
||||
verified.bytes,
|
||||
artifact.artifact_ref().digest_hex().to_owned(),
|
||||
@@ -132,6 +149,17 @@ impl AdminService {
|
||||
detach_guard.detach_now().await;
|
||||
return Err(ApiError::openapi_upload(locale, "no_methods"));
|
||||
}
|
||||
let auth_profile_choices = match self
|
||||
.openapi_auth_profile_choices(workspace_id, &parsed.preview)
|
||||
.await
|
||||
{
|
||||
Ok(choices) => choices,
|
||||
Err(error) => {
|
||||
dependencies.detach_all().await;
|
||||
detach_guard.detach_now().await;
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
let source_envelope = ImportJobSourceEnvelope {
|
||||
source_id,
|
||||
digest: artifact.artifact_ref().clone(),
|
||||
@@ -184,6 +212,7 @@ impl AdminService {
|
||||
expires_at: expires_at
|
||||
.format(&Rfc3339)
|
||||
.map_err(|error| ApiError::internal(error.to_string()))?,
|
||||
auth_profile_choices,
|
||||
preview: parsed.preview,
|
||||
})
|
||||
}
|
||||
@@ -247,6 +276,15 @@ impl AdminService {
|
||||
));
|
||||
}
|
||||
let finished_at = OffsetDateTime::now_utc();
|
||||
let mut auth_mappings = BTreeMap::new();
|
||||
for mapping in &payload.auth_mappings {
|
||||
if auth_mappings
|
||||
.insert(mapping.operation_key.clone(), mapping)
|
||||
.is_some()
|
||||
{
|
||||
return Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"));
|
||||
}
|
||||
}
|
||||
let application_key = openapi_application_key(&payload)?;
|
||||
let conflict_mode = if payload.conflict_mode == "skip" {
|
||||
ImportConflictMode::Skip
|
||||
@@ -425,6 +463,20 @@ impl AdminService {
|
||||
candidates.insert(operation.key.clone(), operation);
|
||||
}
|
||||
}
|
||||
if auth_mappings.iter().any(|(operation_key, mapping)| {
|
||||
!selected.contains(operation_key)
|
||||
|| candidates.get(operation_key).is_none_or(|candidate| {
|
||||
mapping.alternative_index >= candidate.security_requirements.alternatives.len()
|
||||
})
|
||||
}) {
|
||||
return self
|
||||
.fail_openapi_import_or_replay(
|
||||
&replay_context,
|
||||
"auth_mapping_invalid",
|
||||
ApiError::openapi_upload(locale, "auth_mapping_invalid"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let mut skipped = Vec::new();
|
||||
let mut operations = Vec::new();
|
||||
@@ -438,6 +490,26 @@ impl AdminService {
|
||||
});
|
||||
continue;
|
||||
};
|
||||
let (auth_profile_ref, auth_requirement) = match self
|
||||
.resolve_openapi_auth_binding(
|
||||
workspace_id,
|
||||
candidate,
|
||||
auth_mappings.get(&operation_key).copied(),
|
||||
locale,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(binding) => binding,
|
||||
Err(error) => {
|
||||
return self
|
||||
.fail_openapi_import_or_replay(
|
||||
&replay_context,
|
||||
"auth_binding_unavailable",
|
||||
error,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
let mut draft =
|
||||
operation_draft_from_candidate(candidate, payload.server_url.as_deref());
|
||||
attach_import_findings(&mut draft, candidate);
|
||||
@@ -459,7 +531,7 @@ impl AdminService {
|
||||
idempotency: None,
|
||||
safety: None,
|
||||
approval_policy: None,
|
||||
auth_profile_ref: None,
|
||||
auth_profile_ref,
|
||||
headers: BTreeMap::new(),
|
||||
},
|
||||
tool_description: draft.tool_description,
|
||||
@@ -468,6 +540,7 @@ impl AdminService {
|
||||
operations.push(ImportOperationDraft {
|
||||
operation_key: candidate.key.clone(),
|
||||
operation,
|
||||
auth_requirement,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -482,7 +555,13 @@ impl AdminService {
|
||||
pre_skipped: &skipped,
|
||||
finished_at: &finished_at,
|
||||
})
|
||||
.await?;
|
||||
.await
|
||||
.map_err(|error| match error {
|
||||
RegistryError::ImportAuthBindingUnavailable => {
|
||||
ApiError::openapi_auth_binding_unavailable(locale)
|
||||
}
|
||||
error => error.into(),
|
||||
})?;
|
||||
|
||||
Ok(openapi_import_response(applied))
|
||||
}
|
||||
@@ -639,11 +718,13 @@ fn import_replay_contract(
|
||||
if normalizer == NORMALIZER_VERSION && projection == PROJECTION_VERSION {
|
||||
return Ok(ImportReplayContract::Current);
|
||||
}
|
||||
// Historical preview payloads are replayed as their persisted projection.
|
||||
// They must never be parsed again with the current v4 semantics.
|
||||
// Historical v2-v4 preview payloads are replayed as their persisted
|
||||
// projection. They must never be parsed again with current v5 semantics.
|
||||
if !matches!(
|
||||
(normalizer, projection),
|
||||
("normalized-ir-v2", "preview-v2") | ("normalized-ir-v3", "preview-v3")
|
||||
("normalized-ir-v2", "preview-v2")
|
||||
| ("normalized-ir-v3", "preview-v3")
|
||||
| ("normalized-ir-v4", "preview-v4")
|
||||
) {
|
||||
return Err(ApiError::openapi_upload(locale, "source_integrity"));
|
||||
}
|
||||
@@ -817,7 +898,15 @@ fn preview_has_blocker(
|
||||
.flat_map(|group| group.operations.iter())
|
||||
.filter(|operation| selected_operation_keys.contains(&operation.key))
|
||||
.flat_map(|operation| operation.findings.iter())
|
||||
.any(|finding| finding.severity == ImportFindingSeverity::Error)
|
||||
.any(|finding| {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& !matches!(
|
||||
finding.code.as_str(),
|
||||
"unsupported_security_and_requirement"
|
||||
| "unsupported_security_scheme"
|
||||
| "unresolved_security_scheme"
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result<String, ApiError> {
|
||||
@@ -826,8 +915,15 @@ fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result<Strin
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
let mut auth_mappings = payload.auth_mappings.clone();
|
||||
auth_mappings.sort_by(|left, right| {
|
||||
left.operation_key
|
||||
.cmp(&right.operation_key)
|
||||
.then(left.alternative_index.cmp(&right.alternative_index))
|
||||
});
|
||||
let canonical = serde_json::to_vec(&json!({
|
||||
"selected_operation_keys": selected_operation_keys,
|
||||
"auth_mappings": auth_mappings,
|
||||
"server_url": payload.server_url.as_deref(),
|
||||
"conflict_mode": payload.conflict_mode.as_str(),
|
||||
}))
|
||||
|
||||
Reference in new issue
Block a user