story 2-10-security-schemes-secrets: implemented and reviewed via bmad-loop

This commit is contained in:
bsodfather committed 2026-10-03 00:09:08 +03:00
1 parent f5f40cb167
commit ecfb4e788f
45 files changed
+6025 -829

No files matched your search

@@ -1,11 +1,15 @@
use super::super::common::test_service_with_external_references;
use super::*;
use axum::{Router, routing::get};
use sha2::{Digest, Sha256};
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
// Serialized by the integration harness at baseline f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa; this v4 payload preserves the then-current security blocker and digest.
const BASELINE_V4_SECURITY_PREVIEW_PAYLOAD: &str = r###"{"dependencies":[],"dependency_snapshots":[],"normalization":{"config":{"external_references_enabled":false,"max_aliases":128,"max_bytes":262144,"max_collection_items":10000,"max_depth":64,"max_expanded_nodes":100000,"max_external_document_bytes":262144,"max_nodes":50000,"max_reference_depth":32,"max_reference_documents":32,"max_references":4096,"max_scalar_bytes":262144,"normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"ir_fingerprint":"ae2319de76c13d631b26843623092d966e5deea7cda9e3e40e4acc6fa36a25a8","normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"preview":{"findings":[{"code":"unsupported_security_requirement","message":"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.","operation_key":"GET /secure","severity":"error"}],"groups":[{"key":"imported_operation","operations":[{"category":"imported","description":"Выполняет GET /public","draft":{"category":"imported","display_name":"Public","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"public","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/public","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /public","examples":[{"input":{}}],"title":"Public"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У метода нет summary, отображаемое имя будет сгенерировано автоматически.","operation_key":"GET /public","severity":"warning"},{"code":"missing_description","message":"У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.","operation_key":"GET /public","severity":"warning"},{"code":"missing_response_schema","message":"У метода не найдена схема успешного ответа, результат будет описан как общий объект.","operation_key":"GET /public","severity":"warning"},{"code":"no_input_fields","message":"У инструмента нет входных параметров. Это нормально для справочных методов, но проверьте, что агенту не нужно передавать фильтры.","operation_key":"GET /public","severity":"info"},{"code":"empty_output_schema","message":"В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_description","message":"Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_name","message":"Имя инструмента `public` выглядит слишком общим. Лучше использовать имя с конкретным действием и объектом.","operation_key":"GET /public","severity":"warning"}],"input_fields":0,"key":"GET /public","method":"GET","operation_id":"public","output_fields":0,"path":"/public","server_urls":["https://api.example.test"],"suggested_display_name":"Public","suggested_name":"public"},{"category":"imported","description":"Выполняет GET /secure","draft":{"category":"imported","display_name":"Secure","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"secure","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/secure","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /secure","examples":[{"input":{}}],"title":"Secure"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У мет Line truncated
#[tokio::test]
#[serial]
async fn external_relative_chain_survives_reversed_builder_order_and_apply_never_refetches() {
@@ -35,7 +39,7 @@ async fn external_relative_chain_survives_reversed_builder_order_and_apply_never
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let origin = format!("http://{address}");
let registry = test_registry().await;
@@ -105,6 +109,7 @@ paths:
)
.await
.unwrap();
server.abort();
assert_eq!(applied.created.len(), 1);
assert_eq!(fetches.load(Ordering::SeqCst), 2);
let active_sources: i64 = sqlx::query_scalar(
@@ -117,6 +122,115 @@ paths:
assert_eq!(active_sources, 0);
}
#[tokio::test]
#[serial]
async fn external_secret_document_is_rejected_before_blob_registration() {
let external_document = "Item: { type: object, x-api-key: external-secret-canary-42, properties: { id: { type: string } } }";
let fetched = Arc::new(AtomicUsize::new(0));
let fetch_count = Arc::clone(&fetched);
let app = Router::new().route(
"/schemas.yaml",
get(move || {
let fetch_count = Arc::clone(&fetch_count);
async move {
fetch_count.fetch_add(1, Ordering::SeqCst);
external_document
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let origin = format!("http://{address}");
let registry = test_registry().await;
let storage_root = test_storage_root("openapi_import_external_secret_rejected");
let service = test_service_with_external_references(
registry.clone(),
storage_root.clone(),
vec![format!("{origin}/")],
);
let workspace_id = WorkspaceId::new("ws_default");
let error = service
.preview_openapi_import(
&workspace_id,
OpenApiUpload {
bytes: format!(
r#"
openapi: 3.1.0
info: {{ title: Safe primary with unsafe external }}
servers: [{{ url: https://api.example.test }}]
paths:
/items:
get:
operationId: listItems
responses:
'200':
description: ok
content: {{ application/json: {{ schema: {{ $ref: '{origin}/schemas.yaml#/Item' }} }} }}
"#
)
.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
},
)
.await
.expect_err("unsafe external bundle must be rejected before source persistence");
server.abort();
assert_eq!(fetched.load(Ordering::SeqCst), 1);
let rendered_error = format!("{error:?}");
assert!(rendered_error.contains("invalid_document"));
assert!(!rendered_error.contains("external-secret-canary-42"));
// The raw dependency's content-addressed path must not exist even as an
// unregistered blob, and no other stored file may contain its literal.
let external_digest = format!("{:x}", Sha256::digest(external_document.as_bytes()));
let external_blob = storage_root
.join("sha256")
.join(&external_digest[..2])
.join(&external_digest);
assert!(
!external_blob.exists(),
"secret-bearing external response was written to the artifact store"
);
assert!(!storage_contains_literal(
&storage_root,
b"external-secret-canary-42"
));
let dependency_sources: i64 = sqlx::query_scalar(
"select count(*) from artifact_sources
where workspace_id = $1 and source_id like 'src_openapi_dep_%'",
)
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(dependency_sources, 0);
let active_sources: i64 = sqlx::query_scalar(
"select count(*) from artifact_sources where workspace_id = $1 and lifecycle = 'active'",
)
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(active_sources, 0);
}
fn storage_contains_literal(root: &std::path::Path, literal: &[u8]) -> bool {
std::fs::read_dir(root).unwrap().any(|entry| {
let path = entry.unwrap().path();
if path.is_dir() {
storage_contains_literal(&path, literal)
} else {
std::fs::read(path)
.unwrap()
.windows(literal.len())
.any(|window| window == literal)
}
})
}
#[tokio::test]
#[serial]
async fn missing_external_snapshot_fails_closed_before_draft_mutation() {
@@ -316,7 +430,7 @@ async fn external_materialization_uses_one_wall_clock_timeout_and_cleans_cancell
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let timeout_server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let origin = format!("http://{address}");
let registry = test_registry().await;
@@ -368,6 +482,7 @@ paths:
)
.await
.unwrap();
timeout_server.abort();
assert!(started.elapsed() < std::time::Duration::from_millis(800));
assert_eq!(fetches.load(Ordering::SeqCst), 2);
let job = registry
@@ -403,7 +518,10 @@ paths:
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(detached_dependencies, 1);
assert_eq!(
detached_dependencies, 0,
"staged dependencies must not be persisted before the fetch chain completes"
);
}
#[tokio::test]
@@ -606,3 +724,90 @@ async fn exact_v3_contract_replays_persisted_preview_without_v4_or_dependency_re
.unwrap();
assert_eq!(completed.status, ImportJobStatus::Completed);
}
#[tokio::test]
#[serial]
async fn exact_v4_security_contract_replays_its_persisted_security_classification() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_v4_security_compatibility"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: br#"{
"openapi":"3.0.3",
"info":{"title":"Historical security","version":"1"},
"servers":[{"url":"https://api.example.test"}],
"components":{"securitySchemes":{"basic":{"type":"http","scheme":"basic"}}},
"security":[{"basic":[]}],
"paths":{
"/secure":{"get":{"operationId":"secure","responses":{"200":{"description":"ok"}}}},
"/public":{"get":{"operationId":"public","security":[],"responses":{"200":{"description":"ok"}}}}
}
}"#
.to_vec(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let preview = service
.preview_openapi_import(&workspace_id, upload)
.await
.unwrap();
let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into();
let job = registry
.get_import_job(&workspace_id, &job_id)
.await
.unwrap()
.unwrap();
// Keep the genuine baseline-v4 normalization/projection, preview and digest intact.
// Only bind its source envelope to this test's newly registered immutable source blob.
let mut payload: serde_json::Value =
serde_json::from_str(BASELINE_V4_SECURITY_PREVIEW_PAYLOAD).unwrap();
assert_eq!(
payload.pointer("/source/digest"),
job.preview_payload.pointer("/source/digest"),
"baseline fixture must refer to the exact uploaded source bytes",
);
payload["source"]["source_id"] = job.preview_payload["source"]["source_id"].clone();
assert_eq!(
payload["normalization"]["normalizer_version"],
"normalized-ir-v4"
);
assert_eq!(payload["normalization"]["projection_version"], "preview-v4");
sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2")
.bind(serde_json::to_string(&payload).unwrap())
.bind(job_id.as_str())
.execute(registry.pool())
.await
.unwrap();
let result = service
.create_openapi_import(
&workspace_id,
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /secure".to_owned()],
server_url: Some("https://api.example.test".to_owned()),
conflict_mode: "skip".to_owned(),
},
)
.await;
let error =
result.expect_err("historical v4 security blocker must refuse the selected candidate");
assert!(
format!("{error:?}").contains("invalid_document"),
"v4 contract was rejected before candidate validation: {error:?}"
);
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
}
mod security_bundle;