feat(import): bind OpenAPI security schemes safely

This commit is contained in:
bsodfather committed 2026-09-27 23:42:11 +03:00
1 parent c562d5368d
commit 41a0e46110
38 files changed
+4651 -590

No files matched your search

+31
View File
@@ -542,17 +542,48 @@ pub struct OpenApiImportPreviewResponse {
pub job_id: String,
pub expires_at: String,
pub preview: crank_import::rest::ImportPreview,
/// Compatible Auth Profiles visible to the current workspace, grouped by
/// operation and security alternative. Profile credentials are never
/// included.
pub auth_profile_choices: Vec<OpenApiAuthProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiAuthProfileChoice {
pub operation_key: String,
pub alternative_index: usize,
pub scheme_names: Vec<String>,
pub supported: bool,
pub profiles: Vec<OpenApiAuthProfileOption>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiAuthProfileOption {
pub id: String,
pub name: String,
pub kind: AuthKind,
}
#[derive(Clone, Debug, Deserialize)]
pub struct OpenApiImportCreatePayload {
pub selected_operation_keys: Vec<String>,
#[serde(default)]
pub auth_mappings: Vec<OpenApiAuthMapping>,
#[serde(default)]
pub server_url: Option<String>,
#[serde(default = "default_openapi_conflict_mode")]
pub conflict_mode: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OpenApiAuthMapping {
pub operation_key: String,
pub alternative_index: usize,
/// `None` is an explicit unresolved mapping. It is never actionable for a
/// required authentication alternative.
pub auth_profile_id: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportCreateResponse {
pub created: Vec<OpenApiImportCreatedOperation>,
+51
View File
@@ -135,6 +135,41 @@ impl ApiError {
"OpenAPI document is invalid or unsupported"
}
}
(_, "literal_credentials_rejected") => {
if russian {
"документ OpenAPI содержит literal credential и отклонён"
} else {
"OpenAPI document contains literal credential material and was rejected"
}
}
(_, "auth_mapping_required") => {
if russian {
"для требования аутентификации выберите доступный профиль"
} else {
"an available Auth Profile mapping is required for this authentication requirement"
}
}
(_, "auth_mapping_unsupported") => {
if russian {
"это требование аутентификации не поддерживается импортом"
} else {
"this authentication requirement is not supported by import"
}
}
(_, "auth_mapping_invalid") => {
if russian {
"привязка аутентификации в запросе импорта некорректна"
} else {
"OpenAPI import authentication mapping is invalid"
}
}
(_, "auth_binding_unavailable") => {
if russian {
"выбранный профиль аутентификации недоступен для этого импорта"
} else {
"selected Auth Profile is unavailable for this import"
}
}
(_, "no_methods") => {
if russian {
"документ OpenAPI не содержит поддерживаемых методов"
@@ -195,6 +230,18 @@ impl ApiError {
}
}
pub(crate) fn openapi_auth_binding_unavailable(locale: OpenApiUploadLocale) -> Self {
let message = if locale == OpenApiUploadLocale::Ru {
"выбранный профиль аутентификации недоступен для этого импорта"
} else {
"selected Auth Profile is unavailable for this import"
};
Self::unprocessable_with_context(
message,
json!({ "error_code": "openapi_auth_binding_unavailable" }),
)
}
pub(crate) fn rate_limited_with_context(message: impl Into<String>, context: Value) -> Self {
Self::RateLimited {
message: message.into(),
@@ -675,6 +722,10 @@ impl From<RegistryError> for ApiError {
"value": value,
}),
),
RegistryError::ImportAuthBindingUnavailable => Self::unprocessable_with_context(
"selected Auth Profile is unavailable for this import",
json!({ "error_code": "openapi_auth_binding_unavailable" }),
),
RegistryError::YamlImportJobNotFound { job_id } => Self::validation_with_context(
format!("yaml import job {job_id} was not found"),
json!({ "job_id": job_id }),
+105 -9
View File
@@ -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(),
}))
@@ -4,6 +4,7 @@ use tracing::warn;
pub(super) struct MaterializedDependencies {
pub(super) snapshots: Vec<crank_import::rest::ExternalDocumentSnapshot>,
pub(super) secret_literal_rejected: bool,
envelopes: Vec<ImportJobSourceEnvelope>,
canonical_uris: Vec<String>,
guards: Vec<SourceDetachGuard>,
@@ -21,6 +22,7 @@ impl MaterializedDependencies {
fn empty() -> Self {
Self {
snapshots: Vec::new(),
secret_literal_rejected: false,
envelopes: Vec::new(),
canonical_uris: Vec::new(),
guards: Vec::new(),
@@ -207,6 +209,26 @@ impl AdminService {
);
continue;
};
match crank_import::rest::scan_secret_literals(&bytes) {
Ok(findings) if !findings.is_empty() => {
materialization_failure(
"security_scan",
"literal_credentials_rejected",
result.snapshots.len(),
);
result.secret_literal_rejected = true;
break;
}
Ok(_) => {}
Err(_) => {
materialization_failure(
"security_scan",
"invalid_document",
result.snapshots.len(),
);
continue;
}
}
let Ok(references) = crank_import::rest::external_reference_uris(
&document,
&self.external_reference_normalization,
@@ -368,6 +390,14 @@ impl AdminService {
}
let document = String::from_utf8(verified.bytes)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
let findings = crank_import::rest::scan_secret_literals(document.as_bytes())
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
if !findings.is_empty() {
return Err(ApiError::openapi_upload(
locale,
"literal_credentials_rejected",
));
}
let digest =
crank_import::rest::SourceDigest::parse(dependency.digest.digest_hex().to_owned())
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
@@ -0,0 +1,195 @@
use super::*;
use crate::service::{OpenApiAuthProfileChoice, OpenApiAuthProfileOption};
use crank_core::{AuthConfig, AuthProfile, AuthProfileId};
impl AdminService {
pub(super) async fn openapi_auth_profile_choices(
&self,
workspace_id: &WorkspaceId,
preview: &crank_import::rest::ImportPreview,
) -> Result<Vec<OpenApiAuthProfileChoice>, ApiError> {
let mut profiles = self
.registry
.list_auth_profiles(workspace_id)
.await?
.into_iter()
.filter(|profile| profile.kind == profile.config.kind())
.collect::<Vec<_>>();
let mut available_profiles = Vec::with_capacity(profiles.len());
for profile in profiles.drain(..) {
if auth_profile_is_available(&self.registry, workspace_id, &profile).await? {
available_profiles.push(profile);
}
}
let mut choices = Vec::new();
for operation in preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
{
for (alternative_index, alternative) in operation
.security_requirements
.alternatives
.iter()
.enumerate()
{
let scheme = (alternative.schemes.len() == 1)
.then(|| operation.security_schemes.get(&alternative.schemes[0]))
.flatten();
let requirement = scheme.and_then(import_auth_requirement);
let supported = alternative.supported
&& (alternative.schemes.is_empty() || requirement.is_some());
let options = requirement
.as_ref()
.map(|requirement| {
available_profiles
.iter()
.filter(|profile| auth_profile_matches(profile, requirement))
.map(|profile| OpenApiAuthProfileOption {
id: profile.id.as_str().to_owned(),
name: profile.name.clone(),
kind: profile.kind,
})
.collect()
})
.unwrap_or_default();
choices.push(OpenApiAuthProfileChoice {
operation_key: operation.key.clone(),
alternative_index,
scheme_names: alternative.schemes.clone(),
supported,
profiles: options,
});
}
}
Ok(choices)
}
pub(super) async fn resolve_openapi_auth_binding(
&self,
workspace_id: &WorkspaceId,
candidate: &ImportOperationCandidate,
mapping: Option<&OpenApiAuthMapping>,
locale: OpenApiUploadLocale,
) -> Result<(Option<AuthProfileId>, Option<ImportAuthRequirement>), ApiError> {
let alternatives = &candidate.security_requirements.alternatives;
if alternatives.is_empty() {
return if mapping.is_none() {
Ok((None, None))
} else {
Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"))
};
}
let mapping =
mapping.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?;
let alternative = alternatives
.get(mapping.alternative_index)
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_invalid"))?;
if !alternative.supported {
return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported"));
}
if alternative.schemes.is_empty() {
return if mapping.auth_profile_id.is_none() {
Ok((None, None))
} else {
Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"))
};
}
if alternative.schemes.len() != 1 {
return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported"));
}
let scheme = candidate
.security_schemes
.get(&alternative.schemes[0])
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?;
let requirement = import_auth_requirement(scheme)
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?;
let profile_id = mapping
.auth_profile_id
.as_deref()
.filter(|value| !value.is_empty())
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?;
let profile_id = AuthProfileId::new(profile_id.to_owned());
let profile = self
.registry
.get_auth_profile(workspace_id, &profile_id)
.await?
.ok_or_else(|| ApiError::openapi_auth_binding_unavailable(locale))?;
if !auth_profile_matches(&profile, &requirement)
|| !auth_profile_is_available(&self.registry, workspace_id, &profile).await?
{
return Err(ApiError::openapi_auth_binding_unavailable(locale));
}
Ok((Some(profile_id), Some(requirement)))
}
}
fn import_auth_requirement(
scheme: &crank_import::rest::NormalizedSecurityScheme,
) -> Option<ImportAuthRequirement> {
use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation};
match &scheme.kind {
NormalizedSecuritySchemeKind::Http { scheme, .. }
if scheme.eq_ignore_ascii_case("bearer") =>
{
Some(ImportAuthRequirement::Bearer)
}
NormalizedSecuritySchemeKind::Http { scheme, .. }
if scheme.eq_ignore_ascii_case("basic") =>
{
Some(ImportAuthRequirement::Basic)
}
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Header,
parameter_name,
..
} => Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: parameter_name.clone(),
}),
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Query,
parameter_name,
..
} => Some(ImportAuthRequirement::ApiKeyQuery {
parameter_name: parameter_name.clone(),
}),
_ => None,
}
}
fn auth_profile_matches(profile: &AuthProfile, requirement: &ImportAuthRequirement) -> bool {
if profile.kind != profile.config.kind() {
return false;
}
match (requirement, &profile.config) {
(ImportAuthRequirement::Bearer, AuthConfig::Bearer(_))
| (ImportAuthRequirement::Basic, AuthConfig::Basic(_)) => true,
(
ImportAuthRequirement::ApiKeyHeader { parameter_name },
AuthConfig::ApiKeyHeader(config),
) => config.header_name.eq_ignore_ascii_case(parameter_name),
(
ImportAuthRequirement::ApiKeyQuery { parameter_name },
AuthConfig::ApiKeyQuery(config),
) => config.param_name == *parameter_name,
_ => false,
}
}
async fn auth_profile_is_available(
registry: &crank_registry::PostgresRegistry,
workspace_id: &WorkspaceId,
profile: &AuthProfile,
) -> Result<bool, ApiError> {
for secret_id in profile.config.secret_ids() {
if registry
.get_current_secret_version(workspace_id, secret_id)
.await?
.is_none()
{
return Ok(false);
}
}
Ok(true)
}