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

@@ -2,8 +2,9 @@
title: '2.10 — Привязать security schemes через безопасный import API'
type: feature
created: '2026-09-13'
status: ready-for-dev
review_loop_iteration: 0
status: done
baseline_revision: 9c208682a42539b287cac929e715b8f9ecf1e6e9
review_loop_iteration: 1
followup_review_recommended: false
context:
- '_bmad-output/project-context.md'
@@ -11,7 +12,21 @@ context:
- '_bmad-output/implementation-artifacts/1-10-lifecycle-auth-profiles-machine-keys.md'
- '_bmad-output/implementation-artifacts/spec-2-9-http-contract.md'
warnings: [oversized]
deferred: []
deferred:
- summary: >-
Проверить credential URL query, если поддерживаемый downstream consumer использует semicolon separator.
evidence: |-
BH-F7, maybe-false: scanner использует form_urlencoded с &; конкретный поддерживаемый consumer, который интерпретирует ; как границу credential parameter, не показан. Нужны consumer/config и wire regression для установления фактического поведения. Это новая unverified гипотеза, не статус существующей записи ledger.
location: >-
crates/crank-import/src/rest/metadata_findings.rs::query_has_secret_parameter
severity: high (unverified)
- summary: >-
Проверить credential URL query для поддерживаемого proxy/server с повторным percent decoding имени.
evidence: |-
BH-F8, maybe-false: текущая проверка совпадает с однократным form decoding; reachable proxy/server со вторым decoding не продемонстрирован. Нужны конкретный consumer/config и wire regression. Это новая unverified гипотеза, не статус существующей записи ledger.
location: >-
crates/crank-import/src/rest/metadata_findings.rs::query_has_secret_parameter
severity: high (unverified)
---
<intent-contract>
@@ -73,13 +88,13 @@ deferred: []
**Execution (intent gap разрешён пользователем 2026-09-13; реализация выполняется при повторном запуске оркестратора):**
- [ ] `crates/crank-import/src/rest/{model.rs,openapi3.rs,swagger2.rs,normalize.rs,normalize_projection.rs,payload.rs}` — добавить typed schemes и effective requirements, deterministic OR/AND projection, source coverage и blockers без credentials.
- [ ] `crates/crank-import/src/rest/{metadata_findings.rs,normalize_finding_validation.rs}`, `apps/admin-api/src/service/imports.rs`, `apps/admin-api/src/service/imports/external_references.rs` — проверять secret-bearing inputs до persistence и исключить raw values из IR/diagnostics.
- [ ] `apps/admin-api/src/{app.rs,dto.rs,service/imports.rs}` — предоставить выбор/revalidation alternative и совместимых workspace profiles, включить mappings в application digest, сохранять только profile reference.
- [ ] `crates/crank-registry/src/postgres/import_job.rs` — проверять существование и workspace профиля, совместимость binding и доступность всех необходимых связанных Secrets по существующим правилам active/current version в transaction перед mutation; независимый lifecycle профиля не добавлять.
- [ ] `apps/admin-api/src/service/imports/job_contract.rs` — сохранить исторический v4 replay при изменении normalizer/projection contract.
- [ ] `crates/crank-import/tests/`, `apps/admin-api/tests/integration/openapi_import/`, `crates/crank-runtime/tests/` — покрыть все строки матрицы и AC fixtures/negative tests, в том числе race, foreign refs, redaction canaries и digest mismatch.
- [ ] `docs/openapi-import.md` — описать security API, поддерживаемые mappings, blockers и revalidation; не обещать непроверенные semantics.
- [x] `crates/crank-import/src/rest/{model.rs,openapi3.rs,swagger2.rs,normalize.rs,normalize_projection.rs,payload.rs}` — добавить typed schemes и effective requirements, deterministic OR/AND projection, source coverage и blockers без credentials.
- [x] `crates/crank-import/src/rest/{metadata_findings.rs,normalize_finding_validation.rs}`, `apps/admin-api/src/service/imports.rs`, `apps/admin-api/src/service/imports/external_references.rs` — проверять secret-bearing inputs до persistence и исключить raw values из IR/diagnostics.
- [x] `apps/admin-api/src/{app.rs,dto.rs,service/imports.rs}` — предоставить выбор/revalidation alternative и совместимых workspace profiles, включить mappings в application digest, сохранять только profile reference.
- [x] `crates/crank-registry/src/postgres/import_job.rs` — проверять существование и workspace профиля, совместимость binding и доступность всех необходимых связанных Secrets по существующим правилам active/current version в transaction перед mutation; независимый lifecycle профиля не добавлять.
- [x] `apps/admin-api/src/service/imports/job_contract.rs` — сохранить исторический v4 replay при изменении normalizer/projection contract.
- [x] `crates/crank-import/tests/`, `apps/admin-api/tests/integration/openapi_import/`, `crates/crank-runtime/tests/` — покрыть все строки матрицы и AC fixtures/negative tests, в том числе race, foreign refs, redaction canaries и digest mismatch.
- [x] `docs/openapi-import.md` — описать security API, поддерживаемые mappings, blockers и revalidation; не обещать непроверенные semantics.
**Acceptance Criteria (из Story 2.10; семантика доступности binding уточнена решением пользователя 2026-09-13):**
@@ -145,19 +160,106 @@ deferred: []
## Spec Change Log
- 2026-10-02 — BH-F4 вызвал bad_spec loop: прежний план независимой проверки raw документов не учитывал credential names, доступные после $ref resolution. Добавлены staged bundle preflight и local/external split canaries до persistence. Known-bad state: raw artifact содержит opaque credential, хотя normalized preview уже redacted. KEEP перечислены в Design Notes; frozen intent не менялся. Возврат к step-03 затрагивает изменения текущего follow-up, исходные commits реализации сохраняются.
- 2026-09-13 — Пользователь выбрал вариант 1: сохранить существующий lifecycle Secrets; независимый отзыв Auth Profile остаётся отложенным. Уточнены intent, AC, матрица и transaction predicate. Запись Auto Run Result ниже сохранена как историческое свидетельство остановки; требуемое в ней решение принято. Статус переустанавливает оркестратор.
## Review Triage Log
### 2026-10-02 — Review pass
- verdicts: 17 findings из четырёх review layers — high 3, medium 4, low 2, false 8, maybe-false 0
- findings:
- `[false]` `[reject]` IA-1: UI mapping отсутствует — Frozen intent прямо исключает mapping UI Story 2.11; DTO/plan decisions реализуют разрешённую import API поверхность.
- `[low]` `[patch]` IA-2: execution evidence в checklist неоднороден — Старые секции являются историческим ATDD/preflight evidence; добавлена явно актуальная итоговая секция checklist, история сохранена.
- `[medium]` `[patch]` VG-1: второй OR choice не проверен — Принято pre-verified evidence verification-gap: тесты утверждали только index 0; добавлены distinct profiles, assertions для обеих choices и успешный plan с index 1.
- `[high]` `[patch]` EC-1: encoded query name обходит redaction — query_has_secret_parameter проверял сырое имя api%5Fkey; detection и sanitizer используют одинаковый form decoding, regression проверяет rejection и sanitized output.
- `[medium]` `[patch]` EC-2: inherited profile отклоняется без security declaration — new_version подставляет существующий ref, затем resolve_security_binding отвергает пустые alternatives; binding при source None сохранён с явным internal unconstrained requirement и transaction validation, integration Apply прошёл.
- `[medium]` `[patch]` BH-1: unused unsupported scheme не получает finding — schemes() создаёт Unsupported metadata, но findings формируются только в requirements; добавлен located definition warning для OAS/Swagger без global blocker, обе regression fixtures прошли.
- `[false]` `[reject]` BH-2: malformed sibling должен не блокировать valid OR — Invalid requirement по матрице не становится actionable; existing fail-closed malformed tests сохраняют это правило. Well-formed unsupported AND/OAuth siblings остаются scoped и не блокируют поддерживаемую alternative.
- `[high]` `[patch]` BH-3: default пропускает literal heuristic — Existing scalar/JWT heuristic запускается только в examples, поэтому recognizable credential default в ordinary schema property сохраняется; эта же проверка распространена на defaults и подтверждена canary regression. Произвольная benign строка сама по себе не считается доказанным credential.
- `[high]` `[patch]` BH-4: percent-encoded API key query name — Тот же подтверждённый scanner defect, что EC-1; один patch и regression detection/sanitization.
- `[false]` `[reject]` BH-5: unsafe primary upload обязан возвращать preview finding — AC требует generation redacted finding при security analysis и отсутствия raw output, но не успешного preview небезопасного source. Scanner создаёт findings, pure normalization их сохраняет; source ingress намеренно безопасно отвергает upload до persistence, как разрешено Design Notes.
- `[false]` `[reject]` BH-6: source safety отбрасывает findings и нарушает AC-8 — Safety check использует сгенерированные findings для typed rejection; pure importer выдаёт redacted findings и source locations. External rejection возвращает bounded diagnostics без literal и без записи bytes; возврат findings в rejected upload API не заявлен AC.
- `[false]` `[reject]` BH-7: missing-version error-code test падает — 111-test Admin suite исполнил security_binding_refuses_foreign_and_unavailable_auth_material_before_draft с PASS. Missing current version отклоняется operation workspace validator до import_auth_snapshot, поэтому operation_auth_profile_invalid соответствует реальному пути.
- `[false]` `[reject]` BH-8: external observability не проверена — External scanner передаёт в materialization_failure только static stage/error_code и count, не body/URL. Bounded telemetry tests и parser canary выполнялись в full suite; external regression отдельно проверяет persisted preview/job diagnostics и artifact bytes. Ни одного канала передачи canary в logger этот путь не имеет.
- `[medium]` `[patch]` BH-9: v4 replay fixture retagged из v5 — Shape-derived fixture не является настоящим historical output; serialized fixture заменена output canonical baseline с provenance, baseline generation и current replay исполнились с PASS.
- `[false]` `[reject]` BH-10: текущий PASS противоречит старым failures — Перед прежним blocked result есть явный заголовок «Историческая остановка из-за прежних ограничений среды»; актуальный run после восстановления доступа отделён. Изменение spec как исправление review finding дополнительно исключено правилами workflow.
- `[low]` `[patch]` BH-11: checklist scenario snapshot остаётся NOT RUN — Baseline ATDD snapshot должен остаться историческим, но добавлена явно актуальная final execution секция с матрицей и command outcomes; общий docs patch с IA-2.
- `[false]` `[reject]` BH-12: in-review конфликтует с historical blocked — Frontmatter и первый актуальный Auto Run Result совпадают; blocked явно помещён в историческую секцию. Статус orchestrator не используется для доказательства acceptance.
### 2026-10-02 — Дополнительная проверка ведущего в том же pass
- verdicts: 7 findings — high 4, medium 2, low 0, false 1, maybe-false 0
- findings:
- `[high]` `[patch]` L-1: sensitive parameter schema.default сохраняет opaque credential — Сканер учитывал имя header только для immediate examples; у schema.default имя родителя schema и context терялся. Sensitive header/query/cookie context передан к default/example без изменения declarations/types, canary regression прошла.
- `[high]` `[patch]` L-2: credential-shaped format hint остаётся в canonical IR — Fixture UnsafeHint.bearerFormat=sk-private-value убирал typed hint, но scanner не проверял literal в hint field. Existing scalar heuristic применена к bearerFormat/x-format-hint, benign JWT hint сохранён и проверен.
- `[high]` `[patch]` L-3: protocol-relative URL userinfo не распознаётся — Scanner требовал :// и пропускал valid network-path Server URL //user:pass@host; добавлены userinfo rejection/redaction без потери безопасной URL части, regression прошла.
- `[high]` `[patch]` L-4: declared API-key names без secret marker пропускают credentials — Scheme type apiKey явно задаёт credential header/query/cookie name X-Client/key/session, но name heuristic их не распознаёт. Declared names собраны перед visit и применены к parameter/schema/header contexts и URL query detection/redaction; оба containers и все locations покрыты canaries.
- `[false]` `[reject]` L-5: old import-plan-v1 без auth_requirement требует registry adaptation — Upstream Admin plan_apply.rs требует current normalizer/projection и возвращает importer_version_changed для historical plans до registry; это существующая version-change policy. Historical job replay сохраняется отдельным legacy path, а для новых v5 планов requirement остаётся обязательным.
- `[medium]` `[patch]` L-6: explicit no-auth new_version наследует binding — При operation-level security: [] профиль target наследовался, а затем отклонялся как incompatible. Наследование исключено при явно пустом requirement; integration Apply создаёт version 3 без auth_profile_ref, явный несовместимый профиль по-прежнему отклоняется. Общий корень inheritance policy с EC-2.
- `[medium]` `[patch]` L-7: sanitized URL finding не проходит validator вне /url — Redaction удаляла userinfo из schema example либо меняла declared query value в description, но warning predicate не признавал безопасный результат по этим pointers. Predicate допускает sanitized URL / [REDACTED] representation; regression проверяет успешную normalization без credential canaries.
### 2026-10-02 — Follow-up review, iteration 1
- verdicts: 14 findings — high 3, medium 2, low 1, false 6, maybe-false 2
- findings:
- `[false]` `[reject]` BH-F1: legacy create сохраняет защищённую Operation без binding — verified_preview.rs::preview_has_blocker (395–398) отвергает selected required-auth candidate до цикла создания; legacy_apply_rejects_required_auth_without_binding_before_creating_draft прошёл в Admin suite текущего запуска.
- `[false]` `[reject]` BH-F2: malformed OR sibling должен не блокировать всю Operation — carried BH-2: frozen matrix требует fail-closed для invalid requirement; documented per-alternative support применяется к well-formed unsupported siblings, существующие malformed tests подтверждают отказ.
- `[low]` `[patch]` BH-F3: apiKey name пустой ошибочно supported — schemes() принимает пустую строку и выдаёт selectable alternative без возможного profile. Regression empty_api_key_parameter_name_is_unsupported воспроизвёл FAIL; direct correction — классифицировать как Unsupported. KEEP для re-derivation.
- `[high]` `[bad_spec]` BH-F4: credential name из $ref неизвестно scanner до persistence — concrete local #/x-scheme/#/x-key fixture проходит raw source check, reference resolver позже раскрывает X-Custom и opaque default; external scheme/parameter snapshots проверяются отдельно с тем же пробелом. Design Notes и задача bundle preflight уточнены: все raw documents проверять в общем разрешённом credential context до первого put_registered. KEEP существующие безопасные refs, immutable snapshots, bounded timeout/limits и diagnostics; нельзя молча отключать external refs.
- `[false]` `[reject]` BH-F5: credential в scheme identifier копируется — raw source names являются обязательными identifiers по AC; reviewer не демонстрирует credential value, а только гипотетически назначает смысл имени. AccessToken identifiers намеренно сохраняются, secret-bearing metadata сканируется отдельно.
- `[false]` `[reject]` BH-F6: любой opaque format hint является credential — carried BH-3/L-2: marker/JWT credential hints отклоняются и redacted; произвольная alphanumeric строка без доказанной credential semantics может быть legitimate format hint по AC. Сам синтаксис не доказывает leak.
- `[maybe-false]` `[defer]` BH-F7: semicolon query separator интерпретируется downstream как новый secret parameter — scanner использует form_urlencoded и &; ни caller, ни server, который повторно разбирает ;, не продемонстрирован. Нужен конкретный supported consumer с ; parsing; if-true high (unverified). Ветка defer до окончания bad_spec loop не обрабатывается.
- `[maybe-false]` `[defer]` BH-F8: двойное decoding query name раскрывает secret — текущая проверка совпадает с однократным form decoding; reachable proxy/server с вторым decoding не показан. Нужен consumer/config и wire regression; if-true high (unverified). Ветка defer до окончания bad_spec loop не обрабатывается.
- `[high]` `[patch]` BH-F9: suffix url исключает credentialUrl default и x-credential-url literal — blanket exemption в is_sensitive_name действительно пропускает opaque value в явно credential-named context. Direct correction — сохранить exemption только известных URL metadata полей, добавить ingress/IR/preview canaries; KEEP для re-derivation.
- `[false]` `[reject]` BH-F10: нет legacy required-auth regression — test integration::openapi_import::legacy_apply_rejects_required_auth_without_binding_before_creating_draft зарегистрирован, выполнялся и прошёл в полном Admin suite.
- `[high]` `[patch]` EC-F1: OAuth token в URL fragment сохраняется — query_has_secret_parameter обрезает #, sanitizer копирует fragment; #access_token=opaque value допускается перед persistence. Direct correction — detection и sanitization sensitive fragment parameters, включая encoded names, с сохранением benign parts; KEEP для re-derivation.
- `[medium]` `[patch]` VG-F1: Basic/query profile compatibility не проверяется через preview/plan — pre-verified: existing binding integration покрывает header и Bearer, parser-only Basic/query tests не покрывают API matching branches. Добавить profiles, choice assertions и actionable plan+Apply; KEEP для re-derivation.
- `[medium]` `[patch]` VG-F2: registry Basic/query source requirement не проверяется — pre-verified: matching auth integration и source requirement unit test покрывают header. Добавить успешные Basic/query Apply и query parameter mismatch при matching snapshot с typed rejection/zero mutation; KEEP для re-derivation.
- `[false]` `[reject]` IA-F1: redacted finding должен возвращаться rejected upload API — carried BH-5/BH-6: intent допускает ingress rejection до persistence; внутренний analysis создаёт located redacted findings, успешный unsafe preview не требуется. Audit подтверждает остальную согласованность API surface и исключённых UI/profile lifecycle.
### 2026-10-02 — Follow-up review после re-derivation
- verdicts: 14 findings — high 0, medium 2, low 3, false 9, maybe-false 0
- findings:
- `[false]` `[reject]` IA-R1: ingress rejection расходится с redacted finding — carried IA-F1/BH-5/BH-6: frozen constraint требует no-persistence, internal importer выдаёт located findings; bounded rejection на upload допустим. Более широкий story scope проверен сохранёнными и реально исполненными matrix tests, не только новым diff.
- `[false]` `[reject]` EC-R1: slash fragment с query marker сохраняет access_token — query_has_secret_parameter раньше fragment guard разбирает часть после ? и отклоняет источник. Временный public-helper probe для #/route?access_token=opaque и encoded варианта прошёл 1/1; файл удалён. Proposed guard не является текущим кодом.
- `[false]` `[reject]` EC-R2: claim no raw credentials опровергнут slash-fragment case — тот же concrete trigger EC-R1 отвергается до put_registered; намеренно разрешённый JSON-pointer fragment сам по себе не credential parameter.
- `[medium]` `[patch]` VG-R1: encoded fragment credential name не имеет regression — pre-verified consumer gap: новый decoding branch не защищён тестом без scalar fallback. Исправлено: encoded_access_token_fragment_is_rejected_before_persistence проверяет fully encoded имя в URL metadata, bounded invalid_document и zero blob/source/job; 5/5 targeted и оба final Admin runs PASS; группа с BH-R7.
- `[false]` `[reject]` BH-R1: corpus evidence в target засоряет diff — входной untracked файл сохранён; проект уже version-controls evidence в этом же каталоге, corpus test проверяет content/manifest/matrix digests и deterministic identity. Локальных paths/credentials в JSON нет, concrete harm не показан.
- `[false]` `[reject]` BH-R2: source_safety sibling запрещён scope — разрешён internal helper/module extraction при file-size limit, ведущий поручил минимальное извлечение после реального code-health failure; no unrelated implementation. Предложение переписать build spec отклоняется по workflow.
- `[false]` `[reject]` BH-R3: pure helper exports нарушают public API — spec явно допускает pure-input экспорт через importer/Admin crate boundary; HTTP/product surface не расширен. Две функции не имеют network/registry доступа и нужны caller до persistence; public signatures существующих функций сохранены.
- `[low]` `[patch]` BH-R4: benign split default control отсутствует — safe scheme test действительно не содержит ordinary default; safe control дополнен locale=en вне credential context и assertion сохранения, final PASS. Default у X-Custom является credential literal, поэтому не допускается.
- `[false]` `[reject]` BH-R5: docs обещают распознавание любого opaque credential — формулировка условная «если проверка находит literal credential», а не гарантия классификации всех произвольных строк; имена/контекст и bounded findings соответствуют intent.
- `[false]` `[reject]` BH-R6: telemetry не проверяется — carried BH-8: materialization_failure принимает только static stage/code/count. parser_canary_never_reaches_diagnostics_logs_traces_or_metrics реально исполнился; содержит failing external URL/body canary и assertions logs/spans/metrics. Новый dependency_redaction использует тот же bounded logger.
- `[medium]` `[patch]` BH-R7: checklist заявляет encoded fragment без теста — та же missing regression, что VG-R1; fully encoded ingress canary устраняет пробел. Одно grouped patch entry, отдельная строка finding сохранена.
- `[low]` `[patch]` BH-R8: текущий matrix audit не связывает rows с exact leaves/logs — в checklist добавлена текущая воспроизводимая table, затем повторно подтверждена на final logs; historical table не подменяет новый log. Production semantics и frozen matrix не меняются.
- `[low]` `[reject]` BH-R9: нет отдельного benign multilevel security-context fixture — такой точной комбинации действительно нет; cross-document typed context и multi-level chain/replay покрыты отдельными executed tests. Это дополнительная редкая комбинация сверх обязательной matrix, без показанного сбоя; новый server/chain fixture сложнее direct correction, поэтому low не исправляется. Ordinary default gap отдельно устранён BH-R4.
- `[false]` `[reject]` BH-R10: external URL canary наблюдаемости отсутствует — existing telemetry.rs строит /{CANARY}.yaml с HTTP500 body canary и external ref; проверяет response, logs, spans и metric labels. Тест зарегистрирован и прошёл в обоих полных Admin прогонах.
## Design Notes
Pure importer хранит schemes/requirements, но не читает Auth Profiles или Secrets. Workspace-safe lookup принадлежит admin service, authoritative проверка непосредственно перед созданием Draft — registry transaction. Текущий единичный profile reference позволяет выдавать blocker для unsupported AND: это прямо предусмотрено AC и не требует расширения runtime до составного auth.
Проверка literal credentials до `put_registered` позволяет отвергать небезопасный source без изменения bytes принятого immutable snapshot; AC не требует успешного preview для документа с literal secret. Новые формы import API и внутреннее представление alternatives сами по себе являются инженерными решениями, не основанием для остановки.
### Уточнение bundle ingress после follow-up review
Raw primary source и все fetched dependency documents должны оставаться в памяти до общей security preflight. Для определения credential names нужно использовать resolved security schemes и общие names всех документов bundle; это относится и к неиспользуемым raw nodes: raw artifact сохраняется целиком. Нельзя сначала сохранять один документ, а затем обнаруживать его credential при resolution другого. Проверить каждый raw документ с общим credential context и отдельно resolved tree (в том числе default в referenced schema, raw target которой сам не содержит parameter name), с rejected-source canaries до первого put_registered; unsafe bundle отвергается bounded typed error без bytes/URL/literal в telemetry. Существующие external reference allowlist, timeout, budgets, deduplication, immutable source verification, historical replay и безопасные reference imports сохраняются. Не отключать refs и не отвергать benign defaults только потому, что они находятся в external документе. Public API расширять не требуется; internal staging/preflight принадлежат importer/Admin service boundaries.
### Follow-up implementation — разрешённые изменения
- [x] `crates/crank-import/src/rest/{metadata_findings.rs,normalize.rs,mod.rs}` и `apps/admin-api/src/service/imports{.rs,/external_references.rs}` — staged fetch и общий credential-context preflight primary + dependencies до persistence; сначала RED local/external ref ingress tests, затем минимальная реализация. Если private internal helper требует экспорт через crate boundary, сохранить его строгую pure-input функцию, без network/registry доступа.
- [x] `crates/crank-import/src/rest/security.rs`, `crates/crank-import/tests/security_schemes.rs` — сохранить исправления empty apiKey name, URL fragment, credentialUrl suffix и regression canaries/benign OAuth endpoints.
- [x] `apps/admin-api/tests/integration/openapi_import{.rs,/external_references.rs,/external_references/security_bundle.rs,/import_plan_create/security_binding.rs}`, `crates/crank-registry/tests/integration/import_plan/apply/auth.rs` — local/external referenced scheme, split primary/dependency credentials, benign ref control, Basic/query preview-plan-Apply и query mismatch/no mutation. Существующие helpers и fixture conventions переиспользовать.
- [x] `_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md`, `docs/openapi-import.md` — обновить только execution evidence/описание bundle preflight; frozen AC snapshot без изменений. Не записывать sprint-status.yaml и existing deferred-work ledger entries.
Материал KEEP: `/tmp/crank210-keep-followup.patch` содержит принятые review ведущего Basic/query тесты (Admin 3/3, registry 8/8) и direct correction empty API-key name (RED→GREEN). После отката follow-up code к входному HEAD этот patch можно применить для re-derivation; он не заменяет spec и итоговые проверки.
KEEP: вся утверждённая реализация security metadata/OR/AND/inheritance/no-auth, workspace isolation, transactional profile/Secret validation, настоящий baseline-v4 replay и существующий matrix coverage остаются. Новый scope — устранение подтверждённых scanner/ref ingress gaps и Basic/query verification gaps; UI/qualification/independent profile lifecycle по-прежнему исключены. Один writer на файл; implementation worker читает этот spec как единственный источник задания. Исполнитель не один в репозитории: не откатывать правки других участников. При реализации запускает targeted suites для изменённых модулей и fmt/code-health; ведущий выполняет полный раздел Verification после handoff. Все команды выполняются из абсолютного worktree, не из /home/bsodfather/code/crank. Изменения разрешены только по paths нового Follow-up implementation (включая вложенные модули при лимите 1000 lines); никакого unrelated cleanup.
## Verification
**Commands (не запускались; production code не изменён):**
**Commands (обязательные проверки; фактические результаты текущего запуска приведены в Auto Run Result):**
- `cargo test -p crank-import --all-targets` — schemes, inheritance, OR/AND, deterministic projection и redaction.
- `cargo test -p admin-api --test integration -- --test-threads=1` — реальные upload/binding/revalidation/Apply, no-mutation и workspace isolation.
@@ -172,20 +274,46 @@ Pure importer хранит schemes/requirements, но не читает Auth Pro
## Auto Run Result
Status: blocked
Blocking condition: intent gap
Status: done
Планирование остановлено до ATDD и реализации. Код, migration, API и tests не изменены. Обновлён локальный epic context и подготовлен этот spec; прохождение AC не заявляется. `sprint-status.yaml` не изменялся и не откатывался.
### Follow-up после независимого review — 2026-10-02
### Требуемое решение
Исправлены подтверждённые ingress gaps: raw primary/dependencies собираются в памяти и проверяются совместно, включая resolved security context, до первого artifact put. Local/external split-ref opaque canaries отвергаются без blob/source/job и без literal в API error. Безопасные refs и ordinary defaults сохраняются; historical v4 replay, deterministic IR, OR/AND/no-auth, workspace isolation и authoritative transactional profile/Secret validation подтверждены текущими suites. Исправлены credentialUrl suffix bypass, OAuth fragment redaction и empty apiKey name; добавлено Basic/query сквозное evidence.
Определить, что означает «Auth Profile был отозван» в AC Story 2.10:
Первый follow-up review: 14 findings (high 3, medium 2, low 1, false 6, maybe-false 2). BH-F4 вызвал один bad_spec loop; Design Notes уточнены вне frozen intent, code re-derived с KEEP. Исправлены BH-F3/BH-F9/EC-F1/VG-F1/VG-F2; отклонены BH-F1 (upstream required-auth guard), BH-F2 (malformed fail-closed contract), BH-F5 (identifier не credential value), BH-F6 (нет доказанной credential semantics hint), BH-F10 (existing executed legacy regression), IA-F1 (bounded ingress rejection допустим). Каждая refutation записана отдельно в triage.
- Проверять только существующий lifecycle связанных Secrets (active/current version), считая профиль недоступным при отключении credential, а отдельный lifecycle профиля оставить отложенным.
- Ввести в этой истории независимый отзыв Auth Profile (status/tombstone и соответствующее управление), который запрещает только этот profile ref, сохраняя работоспособность другого профиля с тем же Secret.
Ревью re-derived diff: 14 findings (high 0, medium 2, low 3, false 9). Применены **3 grouped patch entries: high 0, medium 1, low 2** — fully encoded fragment ingress regression (VG-R1/BH-R7), benign default control (BH-R4), exact matrix/log mapping (BH-R8). Отклонены IA-R1 (carried permitted ingress rejection), EC-R1/EC-R2 (slash-query probe отвергается upstream), BH-R1 (deterministic corpus evidence convention), BH-R2 (разрешённая minimal module extraction), BH-R3 (pure crate-boundary helpers предусмотрены), BH-R5 (docs не обещают classifier всех opaque strings), BH-R6/BH-R10 (existing executed external URL/body telemetry canary), BH-R9 (редкая дополнительная комбинация потребовала бы сложный fixture; обязательные surfaces покрыты отдельно).
Наблюдаемое различие: профили A и B с одним активным Secret. Независимый отзыв A должен отклонить binding A и сохранить binding B; отключение общего Secret отклоняет оба. `AuthProfile` сейчас не имеет status, а отдельный lifecycle явно отложен в Story 1.10 и deferred-work. Поэтому нельзя считать negative test с disabled Secret доказательством независимого отзыва Auth Profile, либо расширить lifecycle/domain/storage scope молча.
Deferred: **2 новые unverified hypotheses** BH-F7/BH-F8 про semicolon query parsing и повторный decoding downstream. Полные evidence/способ проверки — в единственном frontmatter deferred list; существующий deferred-work ledger не читался для изменения статусов и не изменялся. Подтверждённых незакрытых high/medium defects нет. Отдельного benign multilevel security-context fixture нет; обязательные cross-document security и multilevel immutable-chain controls исполнены отдельно.
Основание остановки: правило `step-02-plan.md`, INSTRUCTIONS 5: “Multiple defensible readings of the intent that lead to observably different outcomes, with nothing in the intent to select between them, are an intent gap — do not resolve one by picking a reading.”
`followup_review_recommended: false`: текущий follow-up pass не применял high patch; medium/low test evidence repairs прошли итоговую Verification. Frozen intent не менялся. HUMAN-only решения или operator actions не требовались. UI 2.11, qualification 2.14 и отдельный lifecycle Auth Profile не реализовывались.
Это пробел нормативного scope, а не внешнее действие оператора. AC не требуют покупки домена, выдачи реального API key или действий в vendor console; `awaiting-operator` к данной остановке не применяется.
### Verification
- `cargo test -p crank-import --all-targets` — exit 0, 117 passed.
- `cargo test -p admin-api --test integration -- --test-threads=1` — exit 0, 117 passed.
- `cargo test -p crank-runtime --all-targets` — exit 0, 83 passed.
- `just verify` — exit 0; contracts, dependencies, fmt, clippy и workspace suites; 945 passed, 0 failed, 1 existing ignored million-row query-plan benchmark. Mandatory security acceptance не skipped/ignored.
- Все 8 frozen matrix rows имеют actual final PASS, exact mapping в [checklist](../test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md). Финальные logs: `/tmp/crank210-final-{import,admin,runtime,gate}.log`.
- Первый gate обнаружил normalize.rs 1048 >1000; новые helpers извлечены в source_safety.rs, после чего code-health и полный gate повторены. Historical regular-file stdout/RLIMIT_FSIZE harness effect не маскировался кодом: финальные команды используют pipe с сохранением exit status.
- Resource cleanup: собственные test/check/Pi jobs завершены, `docker volume prune -f` без `--all` успешно удалил unused anonymous volumes (7.68GB по Docker). Active container/volume сохранены. Повторно проверены docker system df, df и Btrfs: 98.10GiB free, Metadata DUP 1.19/7.01GiB (16.95%), global reserve used 0; дефицита нет. Подробные per-device Btrfs сведения требуют root, основные свободное место/metadata доступны.
### Изменённые файлы
- [apps/admin-api/src/service/imports.rs](../../apps/admin-api/src/service/imports.rs) — общая source safety до сохранения primary, корректное освобождение dependencies.
- [apps/admin-api/src/service/imports/external_references.rs](../../apps/admin-api/src/service/imports/external_references.rs) — bounded staged fetch, bundle preflight до первой записи, прежние snapshot/dedup guards.
- [crates/crank-import/src/rest/metadata_findings.rs](../../crates/crank-import/src/rest/metadata_findings.rs) — общие credential names, URL fragment sanitization и узкие URL metadata exemptions.
- [crates/crank-import/src/rest/source_safety.rs](../../crates/crank-import/src/rest/source_safety.rs) — pure raw/resolved bundle helpers; hard errors не допускают persistence.
- [crates/crank-import/src/rest/normalize.rs](../../crates/crank-import/src/rest/normalize.rs) — видимость decode для соседнего private helper без изменения parser semantics.
- [crates/crank-import/src/rest/mod.rs](../../crates/crank-import/src/rest/mod.rs) — module wiring и reexports preflight через crate boundary.
- [crates/crank-import/src/rest/security.rs](../../crates/crank-import/src/rest/security.rs) — empty apiKey name классифицируется Unsupported.
- [apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs](../../apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs) — split-ref RED→GREEN, encoded fragment и safe default controls.
- [apps/admin-api/tests/integration/openapi_import/external_references.rs](../../apps/admin-api/tests/integration/openapi_import/external_references.rs) — unsafe-bundle rejection и staging timeout evidence.
- [apps/admin-api/tests/integration/openapi_import/import_plan_create/security_binding.rs](../../apps/admin-api/tests/integration/openapi_import/import_plan_create/security_binding.rs) — Basic/query choices, plan и typed-ref Apply.
- [crates/crank-registry/tests/integration/import_plan/apply/auth.rs](../../crates/crank-registry/tests/integration/import_plan/apply/auth.rs) — matching Basic/query и source mismatch/no mutation.
- [crates/crank-import/tests/security_schemes.rs](../../crates/crank-import/tests/security_schemes.rs) — credentialUrl/x-credential-url и fragment IR canaries.
- [docs/openapi-import.md](../../docs/openapi-import.md) — описание bundle ingress rejection и immutable dependencies.
- [spec](spec-2-10-security-schemes-secrets.md) и [checklist](../test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md) — triage, deferred hypotheses и фактический acceptance evidence.
- [corpus evidence](../../crates/crank-import/target/openapi-corpus/evidence-089a330a8055dc50b96a4ac39ae8c364d08610171b952f1a2f2e942cfbb94c49.json) — сохранён входной deterministic generated evidence; content digest проверен.
Reviewed diff финализирован локальным commit без push; sprint-status.yaml не записывался и не откатывался.
@@ -0,0 +1,247 @@
---
stepsCompleted: ['specification-mode']
lastStep: 'specification-mode'
lastSaved: '2026-10-01'
workflowType: 'testarch-atdd'
mode: 'specification'
storyId: '2.10'
storyKey: '2-10-security-schemes-secrets'
storyFile: '_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md'
atddChecklistPath: '_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md'
generatedTestFiles:
- 'apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs'
inputDocuments:
- '_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md'
- '_bmad-output/project-context.md'
- '_bmad-output/implementation-artifacts/epic-2-context.md'
- '_bmad-output/implementation-artifacts/1-10-lifecycle-auth-profiles-machine-keys.md'
- '_bmad-output/implementation-artifacts/spec-2-9-http-contract.md'
- '_bmad-output/test-artifacts/test-design-epic-2.md'
- '_bmad-output/test-artifacts/test-design-qa.md'
- '_bmad/tea/config.yaml'
---
# ATDD Checklist — Story 2.10: Security schemes and secrets
**Date:** 2026-10-01
**Author:** Bsodfather
**Story key:** `2-10-security-schemes-secrets`
**Baseline:** `f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa`
## Режим и preflight
Checklist создан в specification mode: matching checklist в текущем worktree отсутствовал. По инструкции родительского workflow этот этап фиксирует approved AC, связь со сценариями, существующие тесты и фактические результаты; новые файлы тестов и искусственные RED-сценарии не создавались. Исторический checklist из `41a0e46` не считается текущим execution evidence.
Story одобрена; критерии взяты в исходном порядке из замороженной спецификации. Канонический ID — `2.10`, ключ — `2-10-security-schemes-secrets`. Стек проекта — fullstack (Rust backend и существующий Playwright), но acceptance boundary этой story — importer/Admin API/registry; mapping UI относится к 2.11, полная qualification — к 2.14. В конфигурации TEA: stack `auto`, Playwright Utils включены, Pact Utils включены, Pact MCP `none`; Pact и browser recording для этого API/importer этапа не применяются.
Прочитаны спецификация, все её context-файлы, epic-2 context, `test-design-epic-2.md` и `test-design-qa.md`. Для Epic 2 дизайн задаёт 2.10-API-A/B, FR-10, риск E2-R2: сохранить overrides, OR/AND и explicit no-auth; отклонять недопустимые auth refs; исключать imported secret canaries из API/IR/plan/export. QA design отмечает существующие importer corpus/security, plan API и registry integration suites как частичное покрытие, не как полное доказательство AC.
## Approved acceptance-criteria snapshot
Поскольку в spec нет `AC-n` меток, IDs назначены детерминированно в порядке Given-групп исходного документа.
| ID | Approved acceptance criterion |
|---|---|
| AC-1 | **Given** OpenAPI 3.x `components.securitySchemes` или Swagger 2.0 `securityDefinitions`; **When** importer анализирует schemes; **Then** bearer HTTP, basic HTTP и API key в header/query/cookie преобразуются в typed security metadata; **And** source names, locations и применимые format hints сохраняются без credential values. |
| AC-2 | **Given** global security requirement; **When** Operation не задаёт собственный override; **Then** candidate наследует global requirement согласно specification semantics; **And** operation-level requirement или explicit no-auth корректно заменяет global rule. |
| AC-3 | **Given** security requirement с несколькими alternatives; **When** source определяет OR-семантику; **Then** preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант; **And** importer не объединяет alternatives в ложное обязательное AND. |
| AC-4 | **Given** security requirement, требующий несколько schemes одновременно; **When** source определяет AND-семантику; **Then** candidate сохраняет составное требование, если runtime contract его поддерживает; **And** иначе affected Operation получает blocker finding с перечислением несохраняемой semantics. |
| AC-5 | **Given** bearer, basic или API-key requirement; **When** формируется preview; **Then** администратору предлагается выбрать совместимый Auth Profile из текущего workspace либо оставить явный unresolved binding; **And** unresolved required authentication блокирует actionable Draft/Apply для affected Operation. |
| AC-6 | **Given** выбранный Auth Profile; **When** формируется candidate или ImportPlan; **Then** сохраняется только typed `AuthProfileRef` и scheme metadata; **And** raw secret, password, token, API key, hash или encrypted ciphertext не копируется в source snapshot, IR, findings, plan, Operation export. |
| AC-7 | **Given** OAuth2, OpenID Connect, mutual TLS или неизвестная scheme; **When** release scope не поддерживает автоматическое mapping; **Then** importer создаёт explicit manual-configuration/unsupported finding; **And** не извлекает client secret, token URL credential, example token или vendor-extension secret. |
| AC-8 | **Given** OpenAPI document содержит literal credential в example, default, extension, URL userinfo или header example; **When** security analysis выполняется; **Then** значение не используется как credential и создаёт redacted secret finding; **And** raw value не появляется в preview UI, logs, traces, metrics или diagnostics. |
| AC-9 | **Given** Auth Profile отсутствует/удалён, принадлежит другому workspace либо любой необходимый связанный Secret недоступен по существующим правилам active/current version; **When** preview либо Apply проверяет binding; **Then** reference отклоняется до создания Draft; **And** ошибка не раскрывает metadata foreign profile. |
| AC-10 | **Given** профили A и B используют один активный Secret с доступной текущей версией; **When** проверяются binding этих профилей; **Then** оба binding допустимы при выполнении остальных проверок; **And** после отключения общего Secret оба binding отклоняются до создания Draft; test с disabled Secret не является доказательством независимого отзыва Auth Profile, который вне scope. |
| AC-11 | **Given** исходная security scheme изменилась после preview; **When** старый ImportPlan применяют к новому source; **Then** plan digest mismatch блокирует Apply; **And** требуется новый preview и явное подтверждение Auth Profile mappings. |
| AC-12 | **Given** security scheme test suite; **When** выполняются fixtures и negative tests; **Then** покрыты global/operation overrides, no-auth, bearer/basic/API-key locations, OR/AND requirements, OAuth/OpenID/unknown schemes, secret-like examples, missing/deleted и cross-workspace profile refs, недоступные Secrets по active/current version и оба профиля с общим отключённым Secret; **And** redaction tests и `just verify` проходят. |
## Сценарии и сценарий-to-AC mapping
Статус отражает только соответствующие существующие проверки и выполненные ниже команды. PASS отдельного теста не означает, что его AC полностью принят. `not-run` — работа для green-phase/test completion; это не RED и не провал теста.
| Scenario | AC | Существующий источник / точное доказательство | Текущий статус |
|---|---|---|---|
| 2.10-API-01: OpenAPI/Swagger effective security, global inheritance, operation override, no-auth и корректное закрытие malformed requirements | AC-2, AC-4 | `crates/crank-import/tests/security_scope.rs`: `empty_or_alternative_makes_the_operation_public`, `or_alternative_keeps_only_that_operation_public`, `nonempty_requirement_still_blocks_with_auth_profile_contract`, `empty_security_array_stays_public`, OpenAPI 3.1 и Swagger 2.0 malformed/root cases | **GREEN partial** — 13 тестов suite прошли; full scheme metadata и all overrides не исчерпаны |
| 2.10-API-02: Typed schemes для OpenAPI 3.0/3.1 и Swagger 2.0, names/location/format без credential | AC-1 | Текущий baseline содержит `crates/crank-import/src/rest/normalize_security.rs` и blocker analysis; `security_scope.rs` покрывает requirements, но не доказывает всю typed-scheme projection | **NOT RUN / gap** — нет отдельного выполненного теста на полный typed mapping |
| 2.10-API-03: OR alternatives сохраняются все, доступен выбор поддерживаемой alternative без collapse в AND | AC-3 | `security_scope.rs`: `or_alternative_keeps_only_that_operation_public` — только обработка public alternative; API plan binding path: `apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs::auth_profile_references_are_validated_stored_and_inherited` | **GREEN partial** — оба источника существуют; полный набор сохранённых selectable alternatives не доказан |
| 2.10-API-04: Unsupported AND блокирует affected operation с объяснимым finding; сохранение составного требования только при поддержке runtime | AC-4 | `crates/crank-import/tests/security_scope.rs::nonempty_requirement_still_blocks_with_auth_profile_contract`; malformed/local-scope cases | **GREEN partial** — blocker path проверен; все варианты multi-scheme и finding semantics остаются не полностью квалифицированы |
| 2.10-API-05: Совместимый workspace Auth Profile либо явный unresolved binding; required auth не создаёт Draft при unresolved | AC-5 | `apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs::auth_profile_references_are_validated_stored_and_inherited` | **GREEN partial** — выполненный тест подтверждает сохранение/валидацию refs и inheritance; полный profile compatibility matrix not-run |
| 2.10-API-06: Profile ref и scheme metadata без raw secret/ciphertext на snapshot/IR/findings/plan/export surfaces | AC-6 | `apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs::disabled_auth_secret_rejects_apply_without_leaking_credentials`; canary coverage также в `.../import_plan_apply/corpus_hostile.rs` и `crates/crank-import/tests/corpus.rs` | **GREEN partial** — выполненный canary test проходит для своего маршрута; все поверхности/типы секрета не проверены одной этой командой |
| 2.10-API-07: OAuth2/OpenID/mTLS/unknown scheme становятся explicit manual/unsupported findings без извлечения секретов | AC-7 | `crates/crank-import/tests/security_scope.rs` не доказывает все types; hostile fixtures/matrix: `crates/crank-import/tests/corpus/manifest.json`, `support-matrix.yaml` | **NOT RUN** — соответствующие corpus/matrix команды здесь не выполнялись |
| 2.10-API-08: Literal credentials redacted до observable/persisted surfaces | AC-8 | `crates/crank-import/tests/corpus.rs`, fixture `crates/crank-import/tests/corpus/hostile-secret.json`; Admin canary tests выше | **GREEN partial** — точечный Admin canary прошёл; corpus-wide redaction and logs/traces/metrics/diagnostics not-run |
| 2.10-API-09: Missing/foreign profile refs и недоступный linked Secret безопасно отклоняются при preview/apply до Draft | AC-9 | `apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs::auth_profile_references_are_validated_stored_and_inherited`; `.../import_plan_apply.rs::disabled_auth_secret_rejects_apply_without_leaking_credentials`; `crates/crank-registry/tests/integration/import_plan/apply/auth.rs::apply_rejects_secret_rotation_or_revocation` | **GREEN partial** — три относящихся теста выполнены ниже; foreign/missing no-enumeration and all preview variants not-run |
| 2.10-API-10: Профили A/B с общим Secret оба допустимы active и оба rejected после disable | AC-10 | Disabled Secret apply behavior covered by Admin test above; frozen intent in spec says independent profile tombstone out of scope | **NOT RUN** — отдельно проверить обе профили в обеих состояниях; независимое отключение только A не заявлять |
| 2.10-API-11: Изменившийся security source приводит к digest mismatch до mutation и требует нового preview/mapping confirmation | AC-11 | Общие ImportPlan changed-digest tests: `apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs::same_key_and_digest_replay_report_without_extra_draft_and_changed_digest_conflicts`; security-specific mutation path not isolated | **NOT RUN for security-specific source** — general digest case exists; source scheme mutation scenario not executed |
| 2.10-API-12: Release coverage inventory and `just verify` | AC-12 | Epic test-design 2.10-API-A/B, QA CP-05/06; this checklist records focused evidence only | **NOT RUN** — whole suite / `just verify` is later story verification, not qualification in this ATDD step |
## Фактические команды на текущем baseline
Команды запускались на `f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa`; каждый suite запускался последовательно. Все приведённые ниже команды завершились exit code 0.
| Command | Результат |
|---|---|
| `cargo test -p crank-import --test security_scope -- --test-threads=1` | **PASS:** 13 passed, 0 failed, 0 ignored, 0 filtered; 0.03 s. |
| `cargo test -p admin-api --test integration -- --test-threads=1 auth_profile_references_are_validated_stored_and_inherited` | **PASS:** 1 passed, 0 failed, 103 filtered; 27.92 s. |
| `cargo test -p admin-api --test integration -- --test-threads=1 disabled_auth_secret_rejects_apply_without_leaking_credentials` | **PASS:** 1 passed, 0 failed, 103 filtered; 28.10 s. |
| `cargo test -p crank-registry --test integration -- --test-threads=1 apply_rejects_secret_rotation_or_revocation` | **PASS:** 1 passed, 0 failed, 127 filtered; 33.53 s. |
`just verify`, corpus-wide suites, full integration suite, browser E2E и whole-epic qualification не запускались. Во время подготовки была прервана отдельная попытка `crank-import` на ожидании build lock (exit 130); она не является результатом тестирования. Неудачная/прерванная попытка Pi print-mode также не засчитывается; указанные выше результаты получены повторным запуском точечных команд и захвачены напрямую.
## Артефакты и handoff
- **Generated test files:** нет. Существующие тесты переиспользуются; skipped scaffolds не создавались и не учитываются как выполненный RED.
- **Checklist:** `_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md`.
- **Первичные существующие тесты:** `crates/crank-import/tests/security_scope.rs`; `apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs`; `apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs`; `crates/crank-registry/tests/integration/import_plan/apply/auth.rs`.
- **Приоритет реализации/покрытия:** полный typed-scheme projection для OAS 3.0/3.1 и Swagger 2; complete OR preservation/selection and AND blocker matrix; foreign/missing references; оба profile bindings при shared Secret enable/disable; security-source digest change; end-to-end secret redaction across all required outputs.
- **Scope:** mapping UI 2.11, support-matrix qualification 2.14 и независимый Auth Profile revoke/status/tombstone остаются вне этой story.
- **Следующий этап:** implementation workflow может активировать/добавить только сценарии для подтверждённых AC gaps и повторно использовать перечисленные тесты; этот checklist не объявляет AC полностью GREEN.
## Implementation continuation — 2026-10-02
Ниже зафиксированы только команды и результаты, подтверждённые в implementation-подэтапе. После смены runtime sandbox PostgreSQL-backed тесты и `just verify` не запускались: доступ к Docker socket запрещён (`docker system df` завершился `permission denied unix:///var/run/docker.sock`). Эти проверки остаются обязательными; story не закрыта.
| Command | Результат и связь с AC |
|---|---|
| `cargo test --offline -p crank-import --all-targets` | **PASS:** все importer targets прошли, включая corpus (3), typed schemes/redaction (9) и security scope (13). Покрывает существенную часть AC-1–4, AC-7–8, но сам по себе не закрывает API/registry критерии. |
| `cargo check --offline -p admin-api -p crank-registry --all-targets` | **PASS:** проверены production и test targets после добавления profile choices, transaction revalidation и registry race regression. Это compile/typecheck, не execution evidence для PostgreSQL. |
| `cargo fmt --all -- --check` | **PASS** после форматирования новых файлов. |
| `cargo test -p admin-api --test integration -- --test-threads=1 auth_profile_references_are_validated_stored_and_inherited` | **PASS до sandbox switch:** 1 passed. Это доказывает compatible profile binding и persisted mapping на исходном runtime; новая preview-choice assertion добавлена позднее и execution не подтверждён. |
| `cargo test -p admin-api --test integration -- --test-threads=1 legacy_apply_rejects_required_auth_without_binding_before_creating_draft` | **PASS до sandbox switch:** 1 passed. Required-auth legacy Apply отказал до создания Draft. |
| `cargo test -p admin-api --test integration -- --test-threads=1 exact_v4_security_contract_replays_its_persisted_security_classification` | **PASS до sandbox switch:** 1 passed. Проверен persisted-v4 replay shape с историческим blocker; fixture преобразуется из preview текущего кода, а не является byte-for-byte golden output старого бинарника. |
| `cargo test -p admin-api --test integration -- --test-threads=1 profiles_sharing_active_secret_are_selectable_and_both_block_after_disable` | **NOT VERIFIED:** первый запуск выявил тестовую ошибку — disabled планы создавались уже после отключения Secret и потому отклонялись на plan creation. Тест скорректирован так, чтобы оба плана создаваться до отключения; rerun запрещён Docker permission blocker. |
| `cargo test -p crank-registry --test integration -- --test-threads=1 apply_rechecks_live_profile_against_source_requirement_even_when_snapshot_matches` | **NOT RUN:** новая обязательная registry Apply regression моделирует captured/live `X-B` при source requirement `X-Apply-Key`; execution требует PostgreSQL/Docker runtime. |
| `just verify` | **NOT RUN:** обязательный whole-project gate требует недоступного Docker API/runtime. |
Оставшиеся acceptance-проверки: повторить preview-choice integration assertion; corrected shared-secret A/B scenario; registry transaction race regression; matching auth/disabled-secret and complete registry auth suite; настоящий baseline-v4 serialized replay fixture (current replay test is shape-derived); `just verify` и предусмотренная проектом resource cleanup. Пока они не завершатся, AC-5/6/9/10/11/12 и story остаются частично проверенными.
## Историческая проверка ведущего при ограниченной среде — 2026-10-02
Эта запись уточняет предшествующие промежуточные результаты; ранние PASS не означают acceptance итогового diff.
- Итоговый `cargo test -p crank-import --all-targets`: **PASS, 109 passed**, включая 10 security scheme/redaction tests и regression сохранения чувствительных identifiers/schema metadata.
- Итоговый `cargo clippy --workspace --all-targets --all-features -- -D warnings`: **PASS**. Новые mandatory integration cases скомпилированы; compile не заменяет исполнение.
- fmt, Rust code-health и diff whitespace checks: **PASS**.
- Ранний полный Admin integration: 105 passed / 3 failed stale-fixture expectations. Preview-choice, shared-secret A/B, legacy required-auth refusal и persisted-v4 replay прошли. Три fixtures исправлены под approved contract.
- Финальный Admin integration: **не пройден**, 7 passed / 104 failed на PostgreSQL Testcontainer (`Operation not permitted`). Новые/исправленные PostgreSQL cases не засчитываются.
- Финальный runtime suite: **не пройден**, отказ TCP bind в http_contract_wire; более ранний полный PASS остаётся историческим.
- Whole-workspace suite: **не пройден**, admin-api lib PostgreSQL startup permission errors; дальнейшие targets не исполнялись.
- `just verify`: **не пройден** на dependency audit (read-only cache lock, затем DNS github.com / registry.npmjs.org при разрешённом temp-cache retry).
Новые execution obligations: `security_source.rs::changed_security_scheme_invalidates_old_plan_before_draft_creation` (AC-11), `security_availability.rs::security_binding_refuses_foreign_and_unavailable_auth_material_before_draft` (AC-9), `external_references.rs::external_secret_document_is_rejected_before_blob_registration` (AC-6/8), registry `apply_rechecks_live_profile_against_source_requirement_even_when_snapshot_matches` (AC-5/9), исправленные disabled-auth/corpus regressions. Все обязательны и зарегистрированы, но текущая среда не позволила исполнить их с успехом. Frozen AC snapshot не изменён. Matrix audit и обязательный review остаются незавершёнными; story status **blocked / implementation verification failed**.
## Сводка сохранённых runtime логов — 2026-10-02
Этот раздел добавлен после «Финальной проверки ведущего» и фиксирует отдельные фактические прогоны из файлов в `/tmp`. Он не переписывает историю выше: логи относятся к состоянию на момент запуска, а не автоматически к последующим изменениям. Новые целевые проверки, выполненные после этих логов, указаны отдельно.
| Источник / команда | Зафиксированный исход |
|---|---|
| `/tmp/bmad-2-10-access-check-1.log` — `cargo test -p crank-import --all-targets` | **PASS:** все importer targets в логе успешны; включая corpus 3, security_schemes 10, security_scope 13 и остальные parser/coverage/unit targets; 0 failed, 0 ignored. Это исторический прогон до последних дополнительных scanner regressions. |
| `/tmp/bmad-2-10-access-check-2.log` — `cargo test -p admin-api --test integration -- --test-threads=1` | **PASS:** 111 passed, 0 failed, 0 ignored. Включает сценарии на момент этого запуска, в том числе тогдашний persisted-v4 replay test, чья fixture позднее выявлена как current-derived. Этот результат не доказывает baseline-v4 replay. |
| `/tmp/bmad-2-10-access-check-3.log` — `cargo test -p crank-runtime --all-targets` | **PASS:** runtime lib 42, HTTP contract 5, HTTP wire 1, integration 11, product metrics 1, stages 10 и unit 13; во всех завершённых targets 0 failed/ignored. |
| `/tmp/bmad-2-10-verify-pipe.log` — `just verify` в pipe-backed запуске | **PASS:** ведущий непосредственно получил exit code 0 от всего `just verify`; stdout записан родительским процессом через pipe. Admin integration: 111 passed; MCP integration: 40 passed; registry integration: 128 passed / 1 существующий ignored. Успех относится к дереву до review patches; обязательный повтор после них указан в итоговой секции ниже. |
| `cargo test -p crank-import --test security_schemes -- --test-threads=1` (после последних scanner изменений) | **PASS:** 16 passed, 0 failed, 0 ignored. Проверены encoded query names, defaults, protocol-relative userinfo, bearer/x-format hints, объявленные apiKey имена header/query/cookie в обоих security maps и сохранение schema type после redaction; обычный schema URL example и credential URL в description корректно валидируются после sanitization. |
| `cargo test -p admin-api --test integration integration::openapi_import::import_plan_create::security_binding::auth_profile_references_are_validated_stored_and_inherited -- --exact --test-threads=1` | **PASS после извлечения теста в отдельный модуль:** 1 passed, 110 filtered; включает OR alternative/profile choice, решение с alternative index 1, `new_version` без declaration с наследованием профиля и explicit `security: []` с успешным Apply без унаследованного профиля. Явно заданный profile для empty security по-прежнему отклоняется как incompatible. |
| `cargo test -p admin-api --test integration integration::openapi_import::external_references::exact_v4_security_contract_replays_its_persisted_security_classification -- --exact --test-threads=1` | **PASS:** 1 passed, 110 filtered. Использует сериализованный payload, полученный от baseline `f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa`; сохраняет historical v4 preview/fingerprint/digest и подтверждает persisted replay на текущем коде. |
Остаются обязательными фактические повторные проверки для baseline-derived v4 payload после его замены, затронутых API/registry integration regressions на итоговом дереве и полный `just verify`. Приведённые ранее failures и ограничения остаются историей; успех отдельных логов не закрывает незапущенные после последних изменений acceptance cases.
## Итоговая acceptance-проверка после review patches — 2026-10-02
Это актуальный результат на итоговом коде; preceding baseline/sandbox snapshots сохранены как история. Ведущий проверил original unified diff и incremental patches, command exit codes и исполнение всех восьми строк frozen matrix. Docker/PostgreSQL и TCP bind доступны.
| Обязательная команда | Итог |
|---|---|
| `cargo test -p crank-import --all-targets` | PASS, exit 0; 115 passed, 0 failed, 0 ignored; security_schemes 16 и security_scope 13. |
| `cargo test -p admin-api --test integration -- --test-threads=1` | PASS, exit 0; 111 passed, 0 failed, 0 ignored. |
| `cargo test -p crank-runtime --all-targets` | PASS, exit 0; 83 passed, 0 failed, 0 ignored. |
| `just verify` | PASS, exit 0; tooling, inventory, config, migrations, metrics, boundaries, code health, dependency audit, fmt, clippy и workspace suites. |
Workspace harness totals: 932 passed, 0 failed, 1 existing ignored (counts include nested harness output). Единственный ignored — существующий million-row production-size invocation-history тест, не обязательный security case. Registry integration: 128 passed / 1 ignored; source-requirement race `apply_rechecks_live_profile_against_source_requirement_even_when_snapshot_matches` исполнилась с PASS.
| Frozen matrix row | Результат | Исполненные tests |
|---|---|---|
| Schemes | PASS | `openapi_30_and_31_project_typed_schemes_and_safe_hints`, `swagger2_projects_basic_and_api_key_header_and_query` |
| Effective security | PASS | `effective_requirements_inherit_override_and_allow_explicit_no_auth`, `non_array_security_is_rejected_as_an_import_error_surface` |
| Alternatives | PASS | `or_alternatives_are_preserved_and_unsupported_and_is_scoped` |
| Binding | PASS | `auth_profile_references_are_validated_stored_and_inherited`, `legacy_apply_rejects_required_auth_without_binding_before_creating_draft` |
| Credential availability | PASS | `security_binding_refuses_foreign_and_unavailable_auth_material_before_draft`, `disabled_auth_secret_rejects_apply_without_leaking_credentials` |
| Shared Secret | PASS | `profiles_sharing_active_secret_are_selectable_and_both_block_after_disable` |
| Secret source | PASS | `secret_like_literals_are_redacted_from_ir_preview_and_source_snapshot`, `upload_rejects_credential_literals_before_persisting_source_artifacts`, `external_secret_document_is_rejected_before_blob_registration`, `parser_canary_never_reaches_diagnostics_logs_traces_or_metrics` |
| Stale source | PASS | `changed_security_scheme_invalidates_old_plan_before_draft_creation` |
Дополнительные review regressions: encoded query names, declared apiKey names во всех locations, sensitive schema defaults, format hints, protocol-relative userinfo, sanitized URL warning validation, unused unsupported scheme warnings, второй OR choice, наследование existing binding без declaration и сброс binding при explicit no-auth. Настоящий serialized v4 payload получен от canonical baseline; на текущем коде historical replay проверен без переименования versions/digests.
Первый post-review gate остановился на clippy scanner lint; исправления private traversal context и эквивалентного boolean выражения приняты ведущим. После них все четыре обязательные команды повторены; ниже указаны только итоговые logs.
Логи этого итогового прогона: `/tmp/bmad-2-10-final-1.log` … `/tmp/bmad-2-10-final-4.log`; wrapper непосредственно вернул exit 0 для всех четырёх команд. Stdout дочерних процессов передан через pipe: direct regular-file redirection ломает существующий RLIMIT_FSIZE=1 subprocess regression независимо от production diff.
Все обязательные security сценарии исполнены, skipped/ignored acceptance cases нет. UI 2.11, qualification 2.14 и отдельный lifecycle Auth Profile не заявляются как выполненные в этой story.
## Follow-up ATDD перед re-derivation — 2026-10-02
Approved AC-1…AC-12 и frozen intent остаются без изменений. Completed specification-mode checklist переиспользован, historical PASS выше не засчитываются как verification нового diff. Дополнительно выполнены active regressions AC-6/8 (security ingress):
| Сценарий | Test path / leaf | Actual result до production fix |
|---|---|---|
| Local referenced scheme и schema default | `external_references/security_bundle.rs::referenced_security_credentials_local_scheme_ref_with_parameter_default_canary_rejected` | RED: `raw credential literal from referenced local scheme ref persisted to storage` |
| External scheme + primary parameter default | `external_references/security_bundle.rs::referenced_security_credentials_external_scheme_ref_local_parameter_default_canary_rejected` | RED: `raw credential literal from split external scheme ref persisted to storage` |
| Primary scheme + external schema default | `external_references/security_bundle.rs::referenced_security_credentials_local_scheme_external_parameter_default_canary_rejected` | RED: `raw credential literal from external referenced parameter persisted to storage` |
Команда из правильного worktree: `cargo test -p admin-api --test integration referenced_security_credentials -- --test-threads=1` — exit 101, 0 passed / 3 failed / 0 ignored / 111 filtered, 31.89 s. Все три fixtures компилируются; первое assertion проверяет отсутствие canary в API response, затем artifact-store assertion воспроизводит реальный raw-source leak. Production на этапе ATDD не менялся. Первоначальная compile error delimiter исправлена; compile failure не засчитана как RED. Попытки Pi запустить команду из основного checkout прерваны и не считаются execution evidence.
Basic/query verification gaps (AC-5/9): accepted KEEP patch `/tmp/crank210-keep-followup.patch` сохраняет новые profile-choice/plan/Apply и registry mismatch tests; они были GREEN на входном implementation (Admin security_binding 3/3; registry auth 8/8), не объявляются искусственным RED. При re-derivation сохраняются и повторно исполняются.
Implementation handoff: устранить leak общим staged bundle preflight до любого put_registered, сохранив безопасный local/external reference import. Существующие benign external chain и replay tests остаются обязательными контролями; добавить benign credential-context split control. URL fragment, credentialUrl suffix и empty API-key name fixes сохранить из review triage. Ведущий выполняет полный Verification и frozen matrix audit после implementation handoff. UI/qualification/independent Auth Profile lifecycle исключены.
## Follow-up implementation execution — 2026-10-02
Изменения follow-up проверены на итоговом рабочем diff. Raw primary и staged dependencies проходят общий security preflight до первого `put_registered`; после этого pure resolver проверяет разрешённое дерево. Небезопасный bundle получает typed `invalid_document` без blob, source, import job или canary в ошибке. Безопасные local/external refs остаются рабочими. В таймаутном сценарии неполная chain остаётся только в памяти и dependency source не создаётся.
| Обязательный сценарий / команда | Результат |
|---|---|
| `cargo test -p crank-import --test security_schemes -- --test-threads=1` | **PASS:** 17 passed, 0 failed, 0 ignored; включены encoded URL fragment/query, opaque `credentialUrl` и `x-credential-url`, safe JSON-pointer fragment и benign OAuth URL regressions. |
| `cargo test -p crank-import --lib empty_api_key_parameter_name_is_unsupported -- --test-threads=1` | **PASS:** 1 passed, 0 failed, 0 ignored. |
| `cargo test -p admin-api --test integration -- --test-threads=1 security_bundle` | **PASS:** 4 passed, 0 failed, 0 ignored: три local/external split canaries и benign external apiKey scheme с typed `X-Custom` header и одним immutable dependency snapshot. |
| `cargo test -p admin-api --test integration -- --test-threads=1 external_` | **PASS:** 12 passed, 0 failed, 0 ignored; включает весь external reference набор, replay, безопасный chain, timeout, external literal rejection и четыре `security_bundle` случая. |
| `cargo test -p admin-api --test integration -- --test-threads=1 basic_and_query_profiles_bind_to_matching_security_alternatives_and_apply` | **PASS:** 1 passed, 0 failed, 0 ignored. |
| `cargo test -p crank-registry --test integration -- --test-threads=1 apply_accepts_basic_auth_with_matching_snapshot_and_source_requirement` | **PASS:** 1 passed, 0 failed, 0 ignored. |
| `cargo test -p crank-registry --test integration -- --test-threads=1 apply_accepts_query_key_with_matching_snapshot_and_source_requirement` | **PASS:** 1 passed, 0 failed, 0 ignored. |
| `cargo test -p crank-registry --test integration -- --test-threads=1 apply_rejects_query_key_source_mismatch_with_matching_live_snapshot` | **PASS:** 1 passed, 0 failed, 0 ignored; regression подтверждает отказ при mismatch. |
| `cargo test -p admin-api --test integration -- --test-threads=1 external_relative_chain_survives_reversed_builder_order_and_apply_never_refetches` | **PASS:** 1 passed, 0 failed, 0 ignored; benign dependency import и immutable replay. |
| `cargo test -p admin-api --test integration -- --test-threads=1 multipart_boundary_rejections_are_localized_and_leave_no_entities` | **PASS:** 1 passed, 0 failed, 0 ignored; включает `no_methods` без активного source. |
| `cargo test -p admin-api --test integration -- --test-threads=1 local_operation_reference_resolves_and_creates_draft` | **PASS:** 1 passed, 0 failed, 0 ignored. |
| `cargo fmt --all` | **PASS.** |
| `cargo clippy -p crank-import -p admin-api --all-targets -- -D warnings` | **PASS** на итоговых исходниках и acceptance targets. |
Полные verification команды из Spec Verification — `cargo test -p crank-import --all-targets`, полный Admin integration, `cargo test -p crank-runtime --all-targets` и `just verify` — оставлены ведущему для финального gate; этот handoff не закрывает story. `git diff --check` обнаружил только pre-existing trailing blank line в изменённой ведущим спецификации на строке 241; implementation worker спецификацию не редактировал.
## Проверка ведущего после follow-up — 2026-10-02
Обязательные команды: importer all-targets **117 passed**, полный Admin integration **116 passed**, runtime all-targets **83 passed**; все exit 0. `just verify` после извлечения preflight в `source_safety.rs` — **exit 0**, workspace harness totals **944 passed, 0 failed, 1 existing ignored**. Единственный ignored — million-row invocation-history benchmark, не security acceptance. Targeted importer после извлечения также 117/117. Первый gate остановился на code-health (normalize.rs 1048 >1000); helpers вынесены, code-health и полный gate повторены с PASS. Output передан через pipe, чтобы сохранить существующий RLIMIT_FSIZE subprocess harness.
Frozen matrix audit: все восемь строк покрыты проверяющими ожидаемое поведение tests и найдены с `... ok` в текущих execution logs. Schemes — OpenAPI/Swagger typed projection; Effective security — inherit/override/no-auth и malformed refusal; Alternatives — OR/unsupported AND; Binding — header/Bearer и новый Basic/query plan–Apply, legacy unresolved refusal; Credential availability — foreign/unavailable material и disabled Secret refusal; Shared Secret — два профиля active и оба blocked после disable; Secret source — pure redaction, primary/external canaries, три split-ref rejection и observability; Stale source — changed security scheme mismatch без Draft. Новые safe external scheme control и registry Basic/query/mismatch cases также PASS в полном gate. Mandatory security tests не skipped/ignored.
Логи ведущего: `/tmp/crank210-verified-import.log`, `/tmp/crank210-verified-admin.log`, `/tmp/crank210-verified-runtime.log`, `/tmp/crank210-verified-gate2.log`. Исторические результаты выше сохранены отдельно и не подменяют этот прогон.
### Exact matrix execution mapping текущего прогона
Все leaves ниже найдены с `... ok`; соответствующие logs `/tmp/crank210-verified-import.log` и `/tmp/crank210-verified-admin.log`, повторный whole-workspace `/tmp/crank210-verified-gate2.log`.
| Frozen row | Suite log | Exact test leaves (PASS) |
|---|---|---|
| Schemes | import | openapi_30_and_31_project_typed_schemes_and_safe_hints; swagger2_projects_basic_and_api_key_header_and_query |
| Effective security | import | effective_requirements_inherit_override_and_allow_explicit_no_auth; non_array_security_is_rejected_as_an_import_error_surface |
| Alternatives | import | or_alternatives_are_preserved_and_unsupported_and_is_scoped |
| Binding | admin | auth_profile_references_are_validated_stored_and_inherited; basic_and_query_profiles_bind_to_matching_security_alternatives_and_apply; legacy_apply_rejects_required_auth_without_binding_before_creating_draft |
| Credential availability | admin | security_binding_refuses_foreign_and_unavailable_auth_material_before_draft; disabled_auth_secret_rejects_apply_without_leaking_credentials |
| Shared Secret | admin | profiles_sharing_active_secret_are_selectable_and_both_block_after_disable |
| Secret source | import/admin | secret_like_literals_are_redacted_from_ir_preview_and_source_snapshot; external_secret_document_is_rejected_before_blob_registration; referenced_security_credentials_local_scheme_external_parameter_default_canary_rejected; parser_canary_never_reaches_diagnostics_logs_traces_or_metrics |
| Stale source | admin | changed_security_scheme_invalidates_old_plan_before_draft_creation |
## Окончательный post-review gate — 2026-10-02
После всех review patches четыре обязательные команды повторены ведущим: importer **117 passed**, Admin integration **117 passed**, runtime **83 passed**, `just verify` **exit 0**, workspace harness totals **945 passed, 0 failed, 1 existing ignored**. Mandatory security cases не skipped/ignored. Exact matrix mapping выше повторно подтверждён на финальных logs `/tmp/crank210-final-import.log`, `/tmp/crank210-final-admin.log`, `/tmp/crank210-final-runtime.log`, `/tmp/crank210-final-gate.log`.
Дополнительно PASS: `encoded_access_token_fragment_is_rejected_before_persistence` проверяет полностью percent-encoded credential name вне default/example/query fallback, bounded error и zero entities/blobs; `safe_external_security_scheme_and_parameter_are_preserved` сохраняет ordinary `locale=en` default, typed внешнюю scheme и immutable dependency snapshot. Targeted `security_bundle` после patches: **5 passed**. Production-код в этом review repair не менялся. Review verdicts и новые unverified deferred hypotheses находятся в spec; ledger и sprint-status не изменялись.
+20
View File
@@ -542,6 +542,23 @@ pub struct OpenApiImportPreviewResponse {
pub job_id: String,
pub expires_at: String,
pub preview: crank_import::rest::ImportPreview,
#[serde(default)]
pub security_profile_choices: Vec<OpenApiImportSecurityProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportSecurityProfileChoice {
pub operation_key: String,
pub alternative_index: usize,
pub schemes: Vec<String>,
pub profiles: Vec<OpenApiImportAuthProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportAuthProfileChoice {
pub id: String,
pub name: String,
pub kind: crank_core::AuthKind,
}
#[derive(Clone, Debug, Deserialize)]
@@ -610,6 +627,8 @@ pub struct OpenApiImportPlanDecision {
pub confirmed_name: Option<String>,
#[serde(default)]
pub auth_profile_id: Option<String>,
#[serde(default)]
pub security_alternative_index: Option<usize>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
@@ -629,6 +648,7 @@ pub struct OpenApiImportPlanCandidateView {
pub target_operation_id: Option<String>,
pub confirmed_name: Option<String>,
pub auth_profile_id: Option<String>,
pub security_alternative_index: Option<usize>,
pub reason: Option<String>,
}
+130 -17
View File
@@ -3,11 +3,11 @@ use std::collections::{BTreeMap, BTreeSet};
use crank_artifacts::{ArtifactError, MAX_ARTIFACT_BYTES};
use crank_core::{
AuthProfileId, ExecutionConfig, OperationId, OperationSecurityLevel, OperationStatus, Protocol,
ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
};
use crank_import::rest::{
ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION,
PROJECTION_VERSION, operation_draft_from_candidate,
PROJECTION_VERSION, operation_draft_from_candidate, validate_document_source_safe,
};
use crank_registry::{
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
@@ -24,12 +24,12 @@ use tracing::{info, instrument};
use crate::{
error::ApiError,
service::{
AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload,
OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload,
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, OpenApiImportPreviewResponse,
OpenApiImportSkippedOperation, OpenApiUpload, OpenApiUploadLocale, OperationPayload,
new_prefixed_id,
OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload,
OpenApiUploadLocale, OperationPayload, new_prefixed_id,
},
};
@@ -71,20 +71,52 @@ impl AdminService {
validate_openapi_upload(&upload)?;
let OpenApiUpload {
bytes,
bytes: raw_bytes,
mime_type,
locale,
} = upload;
let store = self.artifact_store.clone();
let artifact = tokio::task::spawn_blocking(move || store.put_registered(&bytes))
.await
.map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))?
.map_err(|error| artifact_error(locale, error))?;
let raw_document = std::str::from_utf8(&raw_bytes)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?;
validate_document_source_safe(raw_document, &self.external_reference_normalization)
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
let now = OffsetDateTime::now_utc();
let mut dependencies = self
.materialize_external_reference_snapshots(workspace_id, &raw_bytes, now)
.await;
if dependencies.unsafe_source {
return Err(ApiError::openapi_upload(locale, "invalid_document"));
}
let source_digest =
crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(&raw_bytes)))
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
if crank_import::rest::validate_resolved_bundle_source_safe(
raw_document,
source_digest,
&dependencies.snapshots,
&self.external_reference_normalization,
)
.is_err()
{
dependencies.detach_all().await;
return Err(ApiError::openapi_upload(locale, "invalid_document"));
}
let bytes = raw_bytes;
let store = self.artifact_store.clone();
let artifact_result = tokio::task::spawn_blocking(move || store.put_registered(&bytes))
.await
.map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))
.and_then(|result| result.map_err(|error| artifact_error(locale, error)));
let artifact = match artifact_result {
Ok(artifact) => artifact,
Err(error) => {
dependencies.detach_all().await;
return Err(error);
}
};
let expires_at = now + Duration::hours(IMPORT_JOB_TTL_HOURS);
let job_id = ImportJobId::new(new_prefixed_id("imp"));
let source_id = ArtifactSourceId::new(new_prefixed_id("src_openapi"));
let source = self
let source_result = self
.registry
.create_artifact_source(CreateArtifactSourceRequest {
workspace_id,
@@ -94,7 +126,14 @@ impl AdminService {
sensitivity: ArtifactSourceSensitivity::Internal,
created_at: now,
})
.await?;
.await;
let source = match source_result {
Ok(source) => source,
Err(error) => {
dependencies.detach_all().await;
return Err(ApiError::from(error));
}
};
let mut detach_guard = SourceDetachGuard::new(
self.registry.clone(),
workspace_id.clone(),
@@ -120,9 +159,6 @@ impl AdminService {
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
let mut dependencies = self
.materialize_external_reference_snapshots(workspace_id, &verified.bytes, now)
.await;
let parsed = match parse_verified_preview(
verified.bytes,
artifact.artifact_ref().digest_hex().to_owned(),
@@ -197,15 +233,92 @@ impl AdminService {
dependencies.disarm();
detach_guard.disarm();
let security_profile_choices = self
.security_profile_choices(workspace_id, &parsed.preview)
.await?;
Ok(OpenApiImportPreviewResponse {
job_id: job_id.as_str().to_owned(),
expires_at: expires_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
preview: parsed.preview,
security_profile_choices,
})
}
async fn security_profile_choices(
&self,
workspace_id: &WorkspaceId,
preview: &crank_import::rest::ImportPreview,
) -> Result<Vec<OpenApiImportSecurityProfileChoice>, ApiError> {
let profiles = self.registry.list_auth_profiles(workspace_id).await?;
let mut available = Vec::new();
for profile in profiles {
if profile.kind != profile.config.kind() {
continue;
}
let mut usable = true;
for secret_id in profile.config.secret_ids() {
let Some(secret) = self.registry.get_secret(workspace_id, secret_id).await? else {
usable = false;
break;
};
if secret.secret.status != SecretStatus::Active
|| self
.registry
.get_current_secret_version(workspace_id, secret_id)
.await?
.is_none()
{
usable = false;
break;
}
}
if usable {
available.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()
{
if !alternative.supported || alternative.schemes.len() != 1 {
continue;
}
let Some(scheme) = operation.security_schemes.get(&alternative.schemes[0]) else {
continue;
};
let profiles = available
.iter()
.filter(|profile| {
import_plan::auth_config_matches_scheme(&profile.config, scheme)
})
.map(|profile| OpenApiImportAuthProfileChoice {
id: profile.id.as_str().to_owned(),
name: profile.name.clone(),
kind: profile.kind,
})
.collect::<Vec<_>>();
choices.push(OpenApiImportSecurityProfileChoice {
operation_key: operation.key.clone(),
alternative_index,
schemes: alternative.schemes.clone(),
profiles,
});
}
}
Ok(choices)
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
pub async fn create_openapi_import(
&self,
@@ -7,6 +7,13 @@ pub(super) struct MaterializedDependencies {
envelopes: Vec<ImportJobSourceEnvelope>,
canonical_uris: Vec<String>,
guards: Vec<SourceDetachGuard>,
pub(super) unsafe_source: bool,
}
struct StagedDependency {
canonical_uri: String,
bytes: Vec<u8>,
document: String,
}
pub(super) struct ImportReplayContext<'a> {
@@ -24,6 +31,7 @@ impl MaterializedDependencies {
envelopes: Vec::new(),
canonical_uris: Vec::new(),
guards: Vec::new(),
unsafe_source: false,
}
}
@@ -176,8 +184,8 @@ impl AdminService {
}
}
let mut seen = BTreeSet::new();
let mut result = MaterializedDependencies::empty();
let mut dependency_by_digest = BTreeMap::<String, usize>::new();
let mut staged = Vec::<StagedDependency>::new();
let mut staged_documents = vec![primary.to_owned()];
while let Some((canonical_uri, depth)) = queue.pop_front() {
if !seen.insert(canonical_uri.clone())
|| depth > self.external_reference_normalization.max_reference_depth
@@ -194,32 +202,77 @@ impl AdminService {
materialization_failure(
"fetch",
external_fetch_error_code(&error),
result.snapshots.len(),
staged.len(),
);
continue;
}
};
let Ok(document) = std::str::from_utf8(&bytes).map(str::to_owned) else {
materialization_failure(
"dependency_decode",
"invalid_utf8",
result.snapshots.len(),
);
materialization_failure("dependency_decode", "invalid_utf8", staged.len());
continue;
};
let Ok(references) = crank_import::rest::external_reference_uris(
&document,
&self.external_reference_normalization,
) else {
materialization_failure(
"dependency_scan",
"invalid_document",
result.snapshots.len(),
);
materialization_failure("dependency_scan", "invalid_document", staged.len());
continue;
};
staged_documents.push(document.clone());
staged.push(StagedDependency {
canonical_uri: canonical_uri.clone(),
bytes,
document,
});
for reference in references {
if let Some(uri) = canonical_external_document_uri(Some(&canonical_uri), &reference)
{
queue.push_back((uri, depth.saturating_add(1)));
}
}
}
let staged_snapshots = staged
.iter()
.filter_map(|dependency| {
let digest = format!("{:x}", Sha256::digest(&dependency.bytes));
let digest = crank_import::rest::SourceDigest::parse(digest).ok()?;
Some(crank_import::rest::ExternalDocumentSnapshot {
canonical_uri: dependency.canonical_uri.clone(),
digest,
document: dependency.document.clone(),
})
})
.collect::<Vec<_>>();
let primary_digest =
crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(primary_bytes)))
.expect("SHA-256 digest is valid");
let raw_bundle_is_unsafe = crank_import::rest::validate_document_bundle_source_safe(
&staged_documents
.iter()
.map(String::as_str)
.collect::<Vec<_>>(),
&self.external_reference_normalization,
)
.is_err();
let resolved_bundle_is_unsafe = crank_import::rest::validate_resolved_bundle_source_safe(
primary,
primary_digest,
&staged_snapshots,
&self.external_reference_normalization,
)
.is_err();
if raw_bundle_is_unsafe || resolved_bundle_is_unsafe {
materialization_failure("dependency_redaction", "invalid_document", staged.len());
let mut rejected = MaterializedDependencies::empty();
rejected.unsafe_source = true;
return rejected;
}
let mut result = MaterializedDependencies::empty();
let mut dependency_by_digest = BTreeMap::<String, usize>::new();
for dependency in staged {
let store = self.artifact_store.clone();
let artifact_bytes = bytes.clone();
let artifact_bytes = dependency.bytes.clone();
let Ok(Ok(artifact)) =
tokio::task::spawn_blocking(move || store.put_registered(&artifact_bytes)).await
else {
@@ -295,31 +348,22 @@ impl AdminService {
dependency_by_digest.insert(digest_key, index);
index
};
let dependency = &result.envelopes[envelope_index];
let snapshot_digest = match crank_import::rest::SourceDigest::parse(
dependency.digest.digest_hex().to_owned(),
) {
Ok(digest) => digest,
Err(_) => {
materialization_failure("snapshot", "source_integrity", result.snapshots.len());
continue;
}
let dependency_envelope = &result.envelopes[envelope_index];
let Ok(snapshot_digest) = crank_import::rest::SourceDigest::parse(
dependency_envelope.digest.digest_hex().to_owned(),
) else {
materialization_failure("snapshot", "source_integrity", result.snapshots.len());
continue;
};
result
.snapshots
.push(crank_import::rest::ExternalDocumentSnapshot {
canonical_uri: canonical_uri.clone(),
canonical_uri: dependency.canonical_uri.clone(),
digest: snapshot_digest,
document,
document: dependency.document,
});
materialized_count.store(result.snapshots.len(), std::sync::atomic::Ordering::Relaxed);
result.canonical_uris.push(canonical_uri.clone());
for reference in references {
if let Some(uri) = canonical_external_document_uri(Some(&canonical_uri), &reference)
{
queue.push_back((uri, depth.saturating_add(1)));
}
}
result.canonical_uris.push(dependency.canonical_uri);
}
// Registry ownership is one envelope per immutable digest, while the
// resolver may map several canonical URIs to that same snapshot. Keep
@@ -1,4 +1,6 @@
use super::*;
use crank_core::AuthConfig;
use crank_import::rest::SecurityRequirementsSource as SecuritySource;
impl AdminService {
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
@@ -204,6 +206,7 @@ impl AdminService {
target_operation_id: candidate.target_operation_id.clone(),
confirmed_name: candidate.confirmed_name.clone(),
auth_profile_id: candidate.auth_profile_id.clone(),
security_alternative_index: candidate.security_alternative_index,
reason: candidate.reason.clone(),
})
.collect();
@@ -220,6 +223,7 @@ impl AdminService {
target_operation_id: None,
confirmed_name: None,
auth_profile_id: None,
security_alternative_index: None,
reason: Some("not_selected".to_owned()),
}
}));
@@ -257,9 +261,12 @@ impl AdminService {
decision.and_then(|decision| decision.target_operation_id.clone());
let confirmed_name = decision.and_then(|decision| decision.confirmed_name.clone());
let auth_profile_id = decision.and_then(|decision| decision.auth_profile_id.clone());
let requested_security_alternative =
decision.and_then(|decision| decision.security_alternative_index);
let mut blocker_reason = blocker_reason;
let candidate_name = candidate.draft.name.clone();
let blocked = blocker_reason.is_some();
let mut blocked = blocker_reason.is_some();
// Explicit skip/new_version targets are always resolved in the
// workspace: an unknown target, a name mismatch, or an incompatible
@@ -353,6 +360,8 @@ impl AdminService {
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index: requested_security_alternative,
auth_requirement: None,
auth_snapshot: None,
target: None,
operation: None,
@@ -368,17 +377,17 @@ impl AdminService {
));
}
}
// The final server-authored auth profile reference: the explicit
// decision, or the target's reference inherited for an actionable
// new_version. Only the reference (never a secret value) is stored.
// Use an explicit decision or inherit the target reference for new_version.
let mut auth_profile_ref = auth_profile_id
.as_deref()
.map(|id| AuthProfileId::new(id.to_owned()));
let mut inherited_auth_profile = false;
if let Some(summary) = target_summary.as_ref()
&& !blocked
&& action == "new_version"
&& auth_profile_ref.is_none()
&& (candidate.security_requirements.source == SecuritySource::None
|| !candidate.security_requirements.alternatives.is_empty())
{
let record = self
.registry
@@ -386,8 +395,65 @@ impl AdminService {
.await?
.ok_or_else(|| ApiError::internal("current operation version is unavailable"))?;
auth_profile_ref = record.snapshot.execution_config.auth_profile_ref;
inherited_auth_profile = auth_profile_ref.is_some();
}
let auth_profile_id = auth_profile_ref.as_ref().map(|id| id.as_str().to_owned());
let inherited_unconstrained_auth = inherited_auth_profile
&& action == "new_version"
&& candidate.security_requirements.source
== crank_import::rest::SecurityRequirementsSource::None
&& candidate.security_requirements.alternatives.is_empty()
&& requested_security_alternative.is_none();
let security_alternative_index = if blocked {
requested_security_alternative
} else if inherited_unconstrained_auth {
None
} else {
resolve_security_binding(
self,
workspace_id,
candidate,
auth_profile_ref.as_ref(),
requested_security_alternative,
&mut blocker_reason,
)
.await?
};
blocked = blocker_reason.is_some();
let auth_requirement = if !blocked && inherited_unconstrained_auth {
Some(json!({ "kind": "unconstrained" }))
} else if !blocked && auth_profile_ref.is_some() {
security_alternative_index
.and_then(|index| candidate.security_requirements.alternatives.get(index))
.and_then(|alternative| {
(alternative.schemes.len() == 1).then(|| &alternative.schemes[0])
})
.and_then(|scheme_name| candidate.security_schemes.get(scheme_name))
.and_then(|scheme| match &scheme.kind {
crank_import::rest::NormalizedSecuritySchemeKind::Http { scheme, .. } => {
match scheme.to_ascii_lowercase().as_str() {
"bearer" => Some(json!({ "kind": "bearer" })),
"basic" => Some(json!({ "kind": "basic" })),
_ => None,
}
}
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
location: crank_import::rest::SecurityParameterLocation::Header,
parameter_name,
..
} => {
Some(json!({ "kind": "api_key_header", "parameter_name": parameter_name }))
}
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
location: crank_import::rest::SecurityParameterLocation::Query,
parameter_name,
..
} => Some(json!({ "kind": "api_key_query", "parameter_name": parameter_name })),
_ => None,
})
} else {
None
};
if blocked {
return Ok(PlannedCandidate {
@@ -399,6 +465,8 @@ impl AdminService {
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement,
auth_snapshot: None,
target,
operation: None,
@@ -415,6 +483,8 @@ impl AdminService {
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement: None,
auth_snapshot: None,
target,
operation: None,
@@ -479,6 +549,8 @@ impl AdminService {
target_operation_id,
confirmed_name,
auth_profile_id,
security_alternative_index,
auth_requirement,
auth_snapshot,
target,
operation: Some(operation),
@@ -492,7 +564,12 @@ impl AdminService {
workspace_id: &WorkspaceId,
profile_id: &AuthProfileId,
) -> Result<Value, ApiError> {
let unavailable = || import_plan_repreview_error("import_plan_auth_unavailable");
let unavailable = || {
ApiError::unprocessable_with_context(
"The selected authentication profile is unavailable for this import.",
json!({ "error_code": "import_plan_auth_unavailable" }),
)
};
let profile = self
.registry
.get_auth_profile(workspace_id, profile_id)
@@ -543,6 +620,8 @@ struct PlannedCandidate {
target_operation_id: Option<String>,
confirmed_name: Option<String>,
auth_profile_id: Option<String>,
security_alternative_index: Option<usize>,
auth_requirement: Option<Value>,
auth_snapshot: Option<Value>,
target: Option<Value>,
operation: Option<RegistryOperation>,
@@ -705,6 +784,12 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
{
return invalid("decision auth_profile_id is empty or too long");
}
if decision
.security_alternative_index
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
{
return invalid("decision security_alternative_index is too large");
}
match decision.action.as_str() {
"create" => {
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
@@ -714,8 +799,8 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
}
}
"skip" => {
if decision.auth_profile_id.is_some() {
return invalid("auth_profile_id is not allowed for action skip");
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
return invalid("security binding is not allowed for action skip");
}
if decision.target_operation_id.is_none() {
return invalid("action skip requires the exact target_operation_id");
@@ -745,6 +830,138 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
Ok(())
}
async fn resolve_security_binding(
service: &AdminService,
workspace_id: &WorkspaceId,
candidate: &ImportOperationCandidate,
profile_ref: Option<&AuthProfileId>,
requested_index: Option<usize>,
blocker_reason: &mut Option<String>,
) -> Result<Option<usize>, ApiError> {
let alternatives = &candidate.security_requirements.alternatives;
if alternatives.is_empty() {
if profile_ref.is_some() || requested_index.is_some() {
return Err(ApiError::validation_with_context(
"security binding was supplied for an operation with no security requirement",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
return Ok(None);
}
let public_index = alternatives
.iter()
.position(|alternative| alternative.supported && alternative.schemes.is_empty());
let compatible = if let Some(profile_ref) = profile_ref {
let profile = service
.registry
.get_auth_profile(workspace_id, profile_ref)
.await?
.ok_or_else(|| {
ApiError::unprocessable_with_context(
"selected Auth Profile is unavailable in this workspace",
json!({ "error_code": "import_plan_auth_unavailable" }),
)
})?;
let compatible = alternatives
.iter()
.enumerate()
.filter(|(_, alternative)| {
alternative.supported
&& alternative.schemes.len() == 1
&& candidate
.security_schemes
.get(&alternative.schemes[0])
.is_some_and(|scheme| auth_config_matches_scheme(&profile.config, scheme))
})
.map(|(index, _)| index)
.collect::<Vec<_>>();
Some((profile, compatible))
} else {
None
};
if let Some((_, compatible_indices)) = compatible.as_ref() {
let selected = match requested_index {
Some(index) if compatible_indices.contains(&index) => index,
Some(_) => {
return Err(ApiError::validation_with_context(
"selected Auth Profile is incompatible with the selected security alternative",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
None if compatible_indices.len() == 1 => compatible_indices[0],
None if compatible_indices.is_empty() => {
return Err(ApiError::validation_with_context(
"selected Auth Profile is incompatible with the source security schemes",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
None => {
return Err(ApiError::validation_with_context(
"security_alternative_index is required when a profile matches multiple alternatives",
json!({ "operation_key": candidate.key, "error_code": "import_plan_security_alternative_required" }),
));
}
};
return Ok(Some(selected));
}
if let Some(index) = requested_index {
if alternatives
.get(index)
.is_none_or(|alternative| !alternative.supported)
{
return Err(ApiError::validation_with_context(
"selected security alternative is unsupported",
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
));
}
if alternatives[index].schemes.is_empty() {
return Ok(Some(index));
}
}
if let Some(public_index) = public_index
&& requested_index.is_none()
{
return Ok(Some(public_index));
}
*blocker_reason = Some("auth_profile_required".to_owned());
Ok(requested_index)
}
pub(super) fn auth_config_matches_scheme(
config: &AuthConfig,
scheme: &crank_import::rest::NormalizedSecurityScheme,
) -> bool {
use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation};
match (&scheme.kind, config) {
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Bearer(_)) => {
scheme.eq_ignore_ascii_case("bearer")
}
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Basic(_)) => {
scheme.eq_ignore_ascii_case("basic")
}
(
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Header,
parameter_name,
..
},
AuthConfig::ApiKeyHeader(config),
) => parameter_name.eq_ignore_ascii_case(&config.header_name),
(
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Query,
parameter_name,
..
},
AuthConfig::ApiKeyQuery(config),
) => parameter_name == &config.param_name,
_ => false,
}
}
/// Serializes one planned candidate into the versioned plan payload: the
/// redacted decision, the bounded rejection reason (if any), the exact
/// authorized target with expected state and, for mutating actions, the full
@@ -759,6 +976,7 @@ fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result<Value, ApiErro
"target_operation_id": candidate.target_operation_id,
"confirmed_name": candidate.confirmed_name,
"auth_profile_id": candidate.auth_profile_id,
"security_alternative_index": candidate.security_alternative_index,
},
});
if let Some(reason) = &candidate.reason {
@@ -770,6 +988,9 @@ fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result<Value, ApiErro
if let Some(auth_snapshot) = &candidate.auth_snapshot {
object["auth_snapshot"] = auth_snapshot.clone();
}
if let Some(auth_requirement) = &candidate.auth_requirement {
object["auth_requirement"] = auth_requirement.clone();
}
if let Some(operation) = &candidate.operation {
let value = serde_json::to_value(operation)
.map_err(|error| ApiError::internal(error.to_string()))?;
@@ -203,10 +203,12 @@ fn import_replay_contract(
return Ok(ImportReplayContract::Current);
}
// Historical preview payloads are replayed as their persisted projection.
// They must never be parsed again with the current v4 semantics.
// They must never be parsed again with current normalization 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"));
}
@@ -385,6 +387,14 @@ pub(super) fn preview_has_blocker(
.iter()
.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(|operation| {
operation
.findings
.iter()
.any(|finding| finding.severity == ImportFindingSeverity::Error)
|| (!operation.security_requirements.alternatives.is_empty()
&& !operation.security_requirements.alternatives.iter().any(
|alternative| alternative.supported && alternative.schemes.is_empty(),
))
})
}
@@ -14,6 +14,115 @@ mod http_contract;
mod import_plan_apply;
mod import_plan_create;
#[tokio::test]
#[serial]
async fn upload_rejects_credential_literals_before_persisting_source_artifacts() {
let registry = test_registry().await;
let storage_root = test_storage_root("openapi_import_storage_redaction");
let service = test_service(
registry.clone(),
storage_root,
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: br#"{
"openapi":"3.0.3",
"info":{"title":"Redaction persistence","version":"1"},
"servers":[{"url":"https://bare-canary@api.example.test/v1?api_key=query-canary"}],
"components":{"securitySchemes":{"key":{"type":"apiKey","in":"header","name":"X-Api-Key"}}},
"paths":{"/private":{"get":{
"operationId":"privateOperation",
"parameters":[{"name":"X-Api-Key","in":"header","example":"opaque-canary-42"}],
"responses":{"200":{"description":"ok"}},
"x-client-secret":true
}}}
}"#
.to_vec(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let error = service
.preview_openapi_import(&workspace_id, upload)
.await
.expect_err("credential-bearing uploads must be rejected before persistence");
let rendered_error = format!("{error:?}");
assert!(
rendered_error.contains("invalid_document"),
"unexpected typed error: {rendered_error}"
);
for canary in ["bare-canary", "query-canary", "opaque-canary-42"] {
assert!(
!rendered_error.contains(canary),
"API error leaked {canary}"
);
}
let source_count: i64 =
sqlx::query_scalar("select count(*) from artifact_sources where workspace_id = $1")
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(source_count, 0, "unsafe upload created an artifact source");
}
#[tokio::test]
#[serial]
async fn legacy_apply_rejects_required_auth_without_binding_before_creating_draft() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_unresolved_auth"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: br#"{
"openapi":"3.0.3",
"info":{"title":"Required auth","version":"1"},
"servers":[{"url":"https://api.example.test"}],
"components":{"securitySchemes":{"bearer":{"type":"http","scheme":"bearer"}}},
"security":[{"bearer":[]}],
"paths":{"/secure":{"get":{"operationId":"secureOperation","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 result = service
.create_openapi_import(
&workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /secure".to_owned()],
server_url: None,
conflict_mode: "rename".to_owned(),
},
)
.await;
let error = result.expect_err("legacy apply cannot bypass unresolved required auth");
assert!(format!("{error:?}").contains("invalid_document"));
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
let job = registry
.get_import_job(&workspace_id, &preview.job_id.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(job.status, ImportJobStatus::Failed);
}
const OPENAPI3: &str = r#"
openapi: 3.0.3
info:
@@ -80,11 +189,11 @@ async fn previews_openapi_and_creates_draft_operations() {
assert_eq!(preview_job.status, ImportJobStatus::Pending);
assert_eq!(
preview_job.preview_payload["normalization"]["normalizer_version"],
"normalized-ir-v4"
crank_import::rest::NORMALIZER_VERSION
);
assert_eq!(
preview_job.preview_payload["normalization"]["projection_version"],
"preview-v4"
crank_import::rest::PROJECTION_VERSION
);
assert!(preview_job.preview_payload["normalization"]["ir_fingerprint"].is_string());
@@ -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;
@@ -0,0 +1,413 @@
use super::*;
// --- Referenced security credentials RED regressions ---
// These tests prove that credential literals hidden behind local/external $ref
// security scheme + parameter splits are rejected before artifact persistence.
#[tokio::test]
#[serial]
async fn referenced_security_credentials_local_scheme_ref_with_parameter_default_canary_rejected() {
let canary = b"local-ref-canary-001-Zx9qK";
let registry = test_registry().await;
let storage_root = test_storage_root("openapi_import_local_scheme_ref_canary");
let service = test_service(
registry.clone(),
storage_root.clone(),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: r##"{
"openapi":"3.1.0",
"info":{"title":"Local ref scheme","version":"1"},
"servers":[{"url":"https://api.example.test"}],
"x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"},
"x-key":{"type":"string","default":"local-ref-canary-001-Zx9qK"},
"components":{
"securitySchemes":{"custom":{"$ref":"#/x-scheme"}}
},
"security":[{"custom":[]}],
"paths":{
"/test":{"get":{
"operationId":"test", "security":[{"custom":[]}],
"parameters":[{"name":"X-Custom","in":"header","schema":{"$ref":"#/x-key"}}],
"responses":{"200":{"description":"ok"}}
}}
}
}"##
.as_bytes()
.to_vec(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let result = service.preview_openapi_import(&workspace_id, upload).await;
// Canary must NOT appear in the API response (error or preview).
let response_repr = match &result {
Ok(preview) => serde_json::to_string(preview).unwrap(),
Err(err) => format!("{err:?}"),
};
assert!(
response_repr.contains("invalid_document"),
"unsafe local ref bundle was not rejected: {response_repr}"
);
assert!(
!response_repr.contains(std::str::from_utf8(canary).unwrap()),
"credential canary leaked into API response"
);
// Canary must NOT be persisted to artifact storage.
assert!(
!storage_contains_literal(&storage_root, canary),
"raw credential literal from referenced local scheme ref persisted to storage"
);
assert_rejected_bundle_unpersisted(&registry, &workspace_id, &storage_root).await;
}
#[tokio::test]
#[serial]
async fn encoded_access_token_fragment_is_rejected_before_persistence() {
let canary = b"fragment-ingress-canary-004-Qm7vN";
let registry = test_registry().await;
let storage_root = test_storage_root("openapi_import_encoded_fragment_canary");
let service = test_service(
registry.clone(),
storage_root.clone(),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: br##"{
"openapi":"3.1.0",
"info":{"title":"Encoded fragment credential","version":"1"},
"servers":[{"url":"https://api.example.test/v1#%61%63%63%65%73%73%5F%74%6F%6B%65%6E=fragment-ingress-canary-004-Qm7vN"}],
"paths":{"/health":{"get":{"responses":{"200":{"description":"ok"}}}}}
}"##
.to_vec(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let error = service
.preview_openapi_import(&workspace_id, upload)
.await
.expect_err("encoded access_token fragment must be rejected at ingress");
let rendered_error = format!("{error:?}");
assert!(rendered_error.contains("invalid_document"));
assert!(rendered_error.len() <= 512, "error must remain bounded");
assert!(!rendered_error.contains("fragment-ingress-canary-004-Qm7vN"));
assert!(!rendered_error.contains("%61%63%63%65%73%73%5F%74%6F%6B%65%6E"));
assert_rejected_bundle_unpersisted(&registry, &workspace_id, &storage_root).await;
assert!(!storage_contains_literal(&storage_root, canary));
}
#[tokio::test]
#[serial]
async fn referenced_security_credentials_external_scheme_ref_local_parameter_default_canary_rejected()
{
let canary = b"split-ext-canary-002-Yw8pJ";
// External document defines the apiKey security scheme with header X-Custom.
let external_doc = r#"{"x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"}}"#;
let fetched = Arc::new(AtomicUsize::new(0));
let fetch_count = Arc::clone(&fetched);
let app = Router::new().route(
"/scheme.yaml",
get(move || {
let fc = Arc::clone(&fetch_count);
async move {
fc.fetch_add(1, Ordering::SeqCst);
external_doc.to_owned()
}
}),
);
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_ext_scheme_ref_canary");
let service = test_service_with_external_references(
registry.clone(),
storage_root.clone(),
vec![format!("{origin}/")],
);
let workspace_id = WorkspaceId::new("ws_default");
// Primary references the external scheme; contains a local parameter
// with name X-Custom and schema.default = canary.
let upload = OpenApiUpload {
bytes: format!(
r#"{{
"openapi":"3.1.0",
"info":{{"title":"Ext scheme ref","version":"1"}},
"servers":[{{"url":"https://api.example.test"}}],
"components":{{
"securitySchemes":{{"custom":{{"$ref":"{origin}/scheme.yaml#/x-scheme"}}}}
}},
"security":[{{"custom":[]}}],
"paths":{{
"/test":{{"get":{{
"operationId":"test", "security":[{{"custom":[]}}],
"parameters":[{{"name":"X-Custom","in":"header","schema":{{"type":"string","default":"{canary}"}}}}],
"responses":{{"200":{{"description":"ok"}}}}
}}}}
}}
}}"#,
origin = origin,
canary = std::str::from_utf8(canary).unwrap(),
)
.into_bytes(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let result = service.preview_openapi_import(&workspace_id, upload).await;
server.abort();
assert_eq!(
fetched.load(Ordering::SeqCst),
1,
"external scheme must be fetched before the split is checked"
);
let response_repr = match &result {
Ok(preview) => serde_json::to_string(preview).unwrap(),
Err(err) => format!("{err:?}"),
};
assert!(
response_repr.contains("invalid_document"),
"unsafe external scheme bundle was not rejected: {response_repr}"
);
assert!(
!response_repr.contains(std::str::from_utf8(canary).unwrap()),
"credential canary leaked into API response"
);
assert!(
!storage_contains_literal(&storage_root, canary),
"raw credential literal from split external scheme ref persisted to storage"
);
assert_rejected_bundle_unpersisted(&registry, &workspace_id, &storage_root).await;
}
#[tokio::test]
#[serial]
async fn referenced_security_credentials_local_scheme_external_parameter_default_canary_rejected() {
let canary = b"split-local-canary-003-Xv7oI";
// External document defines a parameter schema whose default is the canary.
let external_doc = format!(
r#"{{"x-key":{{"type":"string","default":"{canary}"}}}}"#,
canary = std::str::from_utf8(canary).unwrap(),
);
let fetched = Arc::new(AtomicUsize::new(0));
let fetch_count = Arc::clone(&fetched);
let app = Router::new().route(
"/key.yaml",
get(move || {
let fc = Arc::clone(&fetch_count);
async move {
fc.fetch_add(1, Ordering::SeqCst);
external_doc.clone()
}
}),
);
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_local_scheme_ext_param_canary");
let service = test_service_with_external_references(
registry.clone(),
storage_root.clone(),
vec![format!("{origin}/")],
);
let workspace_id = WorkspaceId::new("ws_default");
// Primary declares the scheme directly (no $ref); parameter schema is
// an external $ref whose resolved default would be the canary.
let upload = OpenApiUpload {
bytes: format!(
r#"{{
"openapi":"3.1.0",
"info":{{"title":"Local scheme ext param","version":"1"}},
"servers":[{{"url":"https://api.example.test"}}],
"components":{{
"securitySchemes":{{"custom":{{"type":"apiKey","in":"header","name":"X-Custom"}}}}
}},
"security":[{{"custom":[]}}],
"paths":{{
"/test":{{"get":{{
"operationId":"test", "security":[{{"custom":[]}}],
"parameters":[{{"name":"X-Custom","in":"header","schema":{{"$ref":"{origin}/key.yaml#/x-key"}}}}],
"responses":{{"200":{{"description":"ok"}}}}
}}}}
}}
}}"#,
origin = origin,
)
.into_bytes(),
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
};
let result = service.preview_openapi_import(&workspace_id, upload).await;
server.abort();
assert_eq!(
fetched.load(Ordering::SeqCst),
1,
"external parameter must be fetched before the split is checked"
);
let response_repr = match &result {
Ok(preview) => serde_json::to_string(preview).unwrap(),
Err(err) => format!("{err:?}"),
};
assert!(
response_repr.contains("invalid_document"),
"unsafe external parameter bundle was not rejected: {response_repr}"
);
assert!(
!response_repr.contains(std::str::from_utf8(canary).unwrap()),
"credential canary leaked into API response"
);
assert!(
!storage_contains_literal(&storage_root, canary),
"raw credential literal from external referenced parameter persisted to storage"
);
assert_rejected_bundle_unpersisted(&registry, &workspace_id, &storage_root).await;
}
#[tokio::test]
#[serial]
async fn safe_external_security_scheme_and_parameter_are_preserved() {
let external_doc = r#"{"x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"}}"#;
let fetched = Arc::new(AtomicUsize::new(0));
let fetch_count = Arc::clone(&fetched);
let app = Router::new().route(
"/scheme.yaml",
get(move || {
let count = Arc::clone(&fetch_count);
async move {
count.fetch_add(1, Ordering::SeqCst);
external_doc
}
}),
);
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_safe_external_security_ref");
let service = test_service_with_external_references(
registry.clone(),
storage_root,
vec![format!("{origin}/")],
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(
&workspace_id,
OpenApiUpload {
bytes: format!(
r#"openapi: 3.1.0
info: {{ title: Safe external scheme, version: '1' }}
servers: [{{ url: https://api.example.test }}]
components:
securitySchemes:
custom: {{ $ref: '{origin}/scheme.yaml#/x-scheme' }}
security: [{{ custom: [] }}]
paths:
/test:
get:
operationId: safeExternalSecurity
security: [{{ custom: [] }}]
parameters:
- name: X-Custom
in: header
schema: {{ type: string }}
- name: locale
in: query
schema: {{ type: string, default: en }}
responses:
'200': {{ description: ok }}
"#
)
.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
},
)
.await
.expect("safe referenced schemes and parameters remain importable");
server.abort();
assert_eq!(fetched.load(Ordering::SeqCst), 1);
let operation = preview
.preview
.groups
.iter()
.flat_map(|group| &group.operations)
.find(|operation| operation.key == "GET /test")
.expect("operation preview is preserved");
assert!(matches!(
&operation.security_schemes["custom"].kind,
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
parameter_name,
location: crank_import::rest::SecurityParameterLocation::Header,
..
} if parameter_name == "X-Custom"
));
let input_schema = serde_json::to_value(&operation.draft.input_schema).unwrap();
assert_eq!(
input_schema.pointer("/fields/locale/default_value"),
Some(&serde_json::json!("en")),
"benign locale default must remain visible in the preview"
);
let job = registry
.get_import_job(&workspace_id, &preview.job_id.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
job.preview_payload["dependency_snapshots"]
.as_array()
.unwrap()
.len(),
1
);
}
async fn assert_rejected_bundle_unpersisted(
registry: &crank_registry::PostgresRegistry,
workspace_id: &WorkspaceId,
storage_root: &std::path::Path,
) {
let sources: i64 =
sqlx::query_scalar("select count(*) from artifact_sources where workspace_id = $1")
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
let jobs: i64 = sqlx::query_scalar("select count(*) from import_jobs where workspace_id = $1")
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(sources, 0, "rejected bundle registered a source");
assert_eq!(jobs, 0, "rejected bundle created an import job");
assert!(
!storage_has_files(storage_root),
"rejected bundle stored artifact bytes"
);
}
fn storage_has_files(path: &std::path::Path) -> bool {
std::fs::read_dir(path).unwrap().any(|entry| {
let path = entry.unwrap().path();
if path.is_dir() {
storage_has_files(&path)
} else {
true
}
})
}
@@ -58,6 +58,26 @@ paths:
responses: { '200': { description: ok } }
"#;
const OPENAPI_TWO_APIKEY_OPERATIONS: &str = r#"
openapi: 3.0.3
info: { title: Authenticated Apply test, version: '1' }
servers: [{ url: https://api.example.test }]
components: { securitySchemes: { apiKey: { type: apiKey, in: header, name: X-Api-Key } } }
security: [{ apiKey: [] }]
paths:
/alpha: { get: { operationId: alpha, responses: { '200': { description: ok } } } }
/beta: { get: { operationId: beta, responses: { '200': { description: ok } } } }
"#;
const OPENAPI_SINGLE_APIKEY_OPERATION: &str = r#"
openapi: 3.0.3
info: { title: Authenticated Apply test, version: '1' }
servers: [{ url: https://api.example.test }]
components: { securitySchemes: { apiKey: { type: apiKey, in: header, name: X-Api-Key } } }
paths:
/alpha: { get: { operationId: alpha, security: [{ apiKey: [] }], responses: { '200': { description: ok } } } }
"#;
const OPENAPI_WITH_BLOCKER: &str = r#"
openapi: 3.0.3
info: { title: Blocked full Apply test }
@@ -593,7 +613,7 @@ async fn disabled_auth_secret_rejects_apply_without_leaking_credentials() {
let redacted_plan = create_plan(
&client,
&base_url,
OPENAPI_TWO_OPERATIONS,
OPENAPI_TWO_APIKEY_OPERATIONS,
json!({
"selection_mode": "partial",
"selected_operation_keys": ["GET /beta"],
@@ -619,7 +639,7 @@ async fn disabled_auth_secret_rejects_apply_without_leaking_credentials() {
let plan = create_plan(
&client,
&base_url,
OPENAPI_SINGLE,
OPENAPI_SINGLE_APIKEY_OPERATION,
json!({
"selection_mode": "full",
"selected_operation_keys": [],
@@ -657,7 +677,7 @@ async fn disabled_auth_secret_rejects_apply_without_leaking_credentials() {
let plan = create_plan(
&client,
&base_url,
OPENAPI_SINGLE,
&OPENAPI_SINGLE_APIKEY_OPERATION.replace("X-Api-Key", "X-Changed-Key"),
json!({
"selection_mode": "full",
"selected_operation_keys": [],
@@ -695,6 +715,128 @@ async fn disabled_auth_secret_rejects_apply_without_leaking_credentials() {
assert_eq!(after["items"][0]["name"], "beta");
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn profiles_sharing_active_secret_are_selectable_and_both_block_after_disable() {
let registry = test_registry().await;
let base_url = spawn_admin_api(build_test_app(
registry.clone(),
test_storage_root("import_plan_shared_auth_secret"),
))
.await;
let client = authorized_client(&base_url).await;
let secret_response = client
.post(format!("{base_url}/secrets"))
.json(&json!({
"name": "shared-import-auth-token",
"kind": "token",
"value": { "token": "shared-import-canary" }
}))
.send()
.await
.unwrap();
assert_eq!(secret_response.status(), reqwest::StatusCode::OK);
let secret: Value = secret_response.json().await.unwrap();
let secret_id = secret["id"].as_str().unwrap();
let make_profile = |name: &str| {
client
.post(format!("{base_url}/auth-profiles"))
.json(&json!({
"name": name,
"kind": "api_key_header",
"config": { "api_key_header": {
"header_name": "X-Api-Key",
"secret_id": secret_id
}}
}))
};
let first: Value = make_profile("shared-auth-a")
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let second: Value = make_profile("shared-auth-b")
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let document = r#"{
"openapi":"3.0.3",
"info":{"title":"Shared profile test","version":"1"},
"servers":[{"url":"https://api.example.test"}],
"components":{"securitySchemes":{"key":{"type":"apiKey","in":"header","name":"X-Api-Key"}}},
"paths":{
"/a":{"get":{"operationId":"sharedA","security":[{"key":[]}],"responses":{"200":{"description":"ok"}}}},
"/b":{"get":{"operationId":"sharedB","security":[{"key":[]}],"responses":{"200":{"description":"ok"}}}}
}
}"#;
let plan = create_plan(
&client,
&base_url,
document,
json!({
"selection_mode":"full",
"selected_operation_keys":[],
"decisions":[
{"operation_key":"GET /a","action":"create","auth_profile_id":first["id"]},
{"operation_key":"GET /b","action":"create","auth_profile_id":second["id"]}
]
}),
)
.await;
let (status, report) = apply_plan(&client, &base_url, &plan, "shared-secret-active").await;
assert_eq!(status, reqwest::StatusCode::OK, "{report}");
assert_eq!(operation_list(&client, &base_url).await["total"], 2);
let first_id = first["id"].as_str().unwrap();
let second_id = second["id"].as_str().unwrap();
let disabled_document = document
.replace("sharedA", "sharedAAfterDisable")
.replace("sharedB", "sharedBAfterDisable");
let mut disabled_plans = Vec::new();
for (key, profile_id) in [("GET /a", first_id), ("GET /b", second_id)] {
disabled_plans.push((
key,
create_plan(
&client,
&base_url,
&disabled_document,
json!({
"selection_mode":"partial",
"selected_operation_keys":[key],
"decisions":[{"operation_key":key,"action":"create","auth_profile_id":profile_id}]
}),
)
.await,
));
}
sqlx::query("update secrets set status = 'disabled', updated_at = now() where workspace_id = $1 and id = $2")
.bind("ws_default")
.bind(secret_id)
.execute(registry.pool())
.await
.unwrap();
for (key, plan) in disabled_plans {
let (status, error) = apply_plan(
&client,
&base_url,
&plan,
&format!("shared-secret-disabled-{key}"),
)
.await;
assert_eq!(status, reqwest::StatusCode::CONFLICT, "{error}");
assert_eq!(
error["error"]["context"]["error_code"],
"import_plan_auth_unavailable"
);
assert_eq!(operation_list(&client, &base_url).await["total"], 2);
}
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn ac_4_corpus_operation_completes_the_real_http_to_mcp_workflow() {
@@ -844,3 +986,4 @@ async fn ac_4_corpus_operation_completes_the_real_http_to_mcp_workflow() {
}
mod corpus_hostile;
mod security_source;
@@ -45,16 +45,13 @@ async fn ac_5_hostile_corpus_cases_are_bounded_atomic_and_redacted() {
);
assert_eq!(operation_list(&client, &base_url).await, before);
let secret = std::fs::read_to_string(root.join("hostile-secret.json")).unwrap();
let secret_plan = create_plan(&client, &base_url, &secret, full_plan()).await;
let exposed = format!("{}{}", secret_plan.preview, secret_plan.body);
assert!(
!exposed.contains("sk_test_corpus_secret_canary"),
"secret canary leaked into plan evidence"
);
assert_eq!(operation_list(&client, &base_url).await, before);
for file in ["hostile-malformed.json", "hostile-oversized.json"] {
// Credential-bearing sources are rejected before their original bytes
// can enter artifact storage, rather than producing a persisted plan.
for file in [
"hostile-secret.json",
"hostile-malformed.json",
"hostile-oversized.json",
] {
let source = std::fs::read(root.join(file)).unwrap();
let started = std::time::Instant::now();
let upload = reqwest::multipart::Part::bytes(source)
@@ -90,7 +87,7 @@ async fn ac_5_hostile_corpus_cases_are_bounded_atomic_and_redacted() {
// Real HTTP preview+plan: a root security requirement combined with an
// explicit empty operation override must leave the public operation
// actionable and block only the inherited one. No Apply mutation follows.
// actionable and leave the inherited one unresolved without a profile.
let mixed_security: Value = serde_json::json!({
"openapi": "3.0.3",
"info": { "title": "Mixed security plan probe", "version": "1" },
@@ -149,8 +146,8 @@ async fn ac_5_hostile_corpus_cases_are_bounded_atomic_and_redacted() {
);
assert_eq!(
secured["reason"].as_str(),
Some("unsupported_security_requirement"),
"secured candidate must expose the exact security blocker: {}",
Some("auth_profile_required"),
"secured candidate must expose its unresolved binding: {}",
security_plan.body
);
assert_eq!(
@@ -264,7 +261,7 @@ async fn p0_api_malformed_local_security_is_atomic_across_openapi_versions() {
.find(|item| item["key"] == "GET /public")
.unwrap();
assert_eq!(bad["state"], "blocked", "{version}: {}", plan.body);
assert_eq!(bad["reason"], "unsupported_security_requirement");
assert_eq!(bad["reason"], "invalid_security_requirements");
assert_eq!(public["state"], "actionable", "{version}: {}", plan.body);
assert!(public["reason"].is_null());
@@ -0,0 +1,184 @@
use super::*;
const SECURITY_SOURCE_A: &str = r#"
openapi: 3.0.3
info: { title: Security source A, version: '1' }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
apiKey:
type: apiKey
in: header
name: X-Api-Key
paths:
/secure:
get:
operationId: secureWithApiKey
security: [{ apiKey: [] }]
responses: { '200': { description: ok } }
"#;
const SECURITY_SOURCE_B: &str = r#"
openapi: 3.0.3
info: { title: Security source A, version: '1' }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
apiKey:
type: apiKey
in: header
name: X-Rotated-Key
paths:
/secure:
get:
operationId: secureWithApiKey
security: [{ apiKey: [] }]
responses: { '200': { description: ok } }
"#;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn changed_security_scheme_invalidates_old_plan_before_draft_creation() {
let registry = test_registry().await;
let storage_root = test_storage_root("import_plan_security_source_change");
let base_url = spawn_admin_api(build_test_app(registry.clone(), storage_root)).await;
let client = authorized_client(&base_url).await;
let secret_response = client
.post(format!("{base_url}/secrets"))
.json(&json!({
"name": "security-source-test-secret",
"kind": "token",
"value": { "token": "source-change-canary" }
}))
.send()
.await
.unwrap();
assert_eq!(secret_response.status(), reqwest::StatusCode::OK);
let secret: Value = secret_response.json().await.unwrap();
let profile_response = client
.post(format!("{base_url}/auth-profiles"))
.json(&json!({
"name": "security-source-test-profile",
"kind": "api_key_header",
"config": { "api_key_header": {
"header_name": "X-Api-Key",
"secret_id": secret["id"]
}}
}))
.send()
.await
.unwrap();
assert_eq!(profile_response.status(), reqwest::StatusCode::OK);
let profile: Value = profile_response.json().await.unwrap();
let plan = create_plan(
&client,
&base_url,
SECURITY_SOURCE_A,
json!({
"selection_mode": "full",
"selected_operation_keys": [],
"decisions": [{
"operation_key": "GET /secure",
"action": "create",
"auth_profile_id": profile["id"]
}]
}),
)
.await;
assert_eq!(plan.body["candidates"][0]["auth_profile_id"], profile["id"]);
assert_eq!(plan.body["candidates"][0]["security_alternative_index"], 0);
let stored_plan = registry
.get_import_plan(
&crank_core::WorkspaceId::new("ws_default"),
&plan.reference.clone().into(),
)
.await
.unwrap()
.unwrap();
assert_eq!(
stored_plan.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"],
profile["id"],
"the old plan must persist its explicit profile binding"
);
assert_eq!(
stored_plan.payload["candidates"][0]["auth_requirement"]["parameter_name"],
"X-Api-Key"
);
// Register a second immutable source with only the security parameter
// changed. Its own upload/preview proves these are valid changed bytes,
// rather than corruption of the first content-addressed artifact.
let source_b_upload = reqwest::multipart::Part::bytes(SECURITY_SOURCE_B.as_bytes().to_vec())
.file_name("security-b.yaml")
.mime_str("application/yaml")
.unwrap();
let source_b_response = client
.post(format!("{base_url}/imports/openapi/preview"))
.multipart(reqwest::multipart::Form::new().part("file", source_b_upload))
.send()
.await
.unwrap();
assert_eq!(source_b_response.status(), reqwest::StatusCode::OK);
let source_b_preview: Value = source_b_response.json().await.unwrap();
assert_eq!(
source_b_preview["preview"]["groups"][0]["operations"][0]["security_schemes"]["apiKey"]["kind"]
["parameter_name"],
"X-Rotated-Key"
);
let workspace_id = crank_core::WorkspaceId::new("ws_default");
let original_job_id: crank_registry::ImportJobId =
plan.preview["job_id"].as_str().unwrap().into();
let changed_job_id: crank_registry::ImportJobId =
source_b_preview["job_id"].as_str().unwrap().into();
let original_job = registry
.get_import_job(&workspace_id, &original_job_id)
.await
.unwrap()
.unwrap();
let changed_job = registry
.get_import_job(&workspace_id, &changed_job_id)
.await
.unwrap()
.unwrap();
assert_ne!(
original_job.preview_payload["source"]["digest"],
changed_job.preview_payload["source"]["digest"],
"the changed scheme must come from a different verified source artifact"
);
assert_eq!(
stored_plan.payload["source"]["digest"], original_job.preview_payload["source"]["digest"],
"the plan must remain anchored to source A"
);
// Simulate the existing new-source/re-preview condition on the anchored
// pending job while retaining its original plan. The replacement is a
// separately registered and digest-verified B snapshot; Apply must
// compare the newly derived fingerprint with the old plan fingerprint.
sqlx::query("update import_jobs set preview_payload = $3 where id = $1 and workspace_id = $2")
.bind(original_job_id.as_str())
.bind(workspace_id.as_str())
.bind(&changed_job.preview_payload)
.execute(registry.pool())
.await
.unwrap();
let replaced_job = registry
.get_import_job(&workspace_id, &original_job_id)
.await
.unwrap()
.unwrap();
assert_eq!(
replaced_job.preview_payload["source"]["digest"],
changed_job.preview_payload["source"]["digest"]
);
let (status, error) = apply_plan(&client, &base_url, &plan, "security-source-changed").await;
assert_eq!(status, reqwest::StatusCode::CONFLICT, "{error}");
assert_eq!(
error["error"]["context"]["error_code"], "import_plan_fingerprint_stale",
"the old plan must require re-preview after source security changes"
);
assert_eq!(error["error"]["context"]["repreview_required"], true);
assert_eq!(operation_list(&client, &base_url).await["total"], 0);
}
@@ -1,5 +1,21 @@
use super::*;
pub(crate) const OPENAPI_SINGLE_SECURITY: &str = r#"
openapi: 3.0.3
info: { title: Import security, version: '1' }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
Key: { type: apiKey, in: header, name: X-Api-Key }
Bearer: { type: http, scheme: bearer }
paths:
/v2/latest:
get:
operationId: latestRates
security: [{ Key: [] }, { Bearer: [] }]
responses: { '200': { description: ok } }
"#;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation() {
@@ -636,167 +652,6 @@ async fn exact_skip_rename_and_new_version_decisions_are_saved_verbatim() {
assert!(candidate.get("operation").is_none());
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn auth_profile_references_are_validated_stored_and_inherited() {
let registry = test_registry().await;
let upstream_base_url = spawn_upstream_server().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_plan_auth"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let secret_token = "plan-secret-canary-2-13";
let secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "plan-auth-token".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": secret_token }),
},
None,
)
.await
.unwrap();
let auth_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "plan-auth-profile".to_owned(),
kind: AuthKind::ApiKeyHeader,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new(secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"create",
None,
None,
Some(auth_profile.id.as_str()),
)],
),
)
.await
.unwrap();
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
stored.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"],
auth_profile.id.as_str()
);
// Only the reference survives: no secret material anywhere in the plan.
assert!(!stored.payload.to_string().contains(secret_token));
assert_eq!(
plan.candidates.first().unwrap().auth_profile_id.as_deref(),
Some(auth_profile.id.as_str())
);
// An unknown profile reference fails closed in the workspace scope check.
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let error = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"create",
None,
None,
Some("auth_missing_profile"),
)],
),
)
.await
.expect_err("an unknown auth profile reference must be rejected");
assert_error_code(&error, "operation_auth_profile_invalid");
assert_http_status(error, axum::http::StatusCode::UNPROCESSABLE_ENTITY);
// `new_version` without an explicit choice inherits the target's ref.
let existing = service
.create_operation(&workspace_id, {
let mut payload = test_operation_payload(&upstream_base_url, "latest_rates");
payload.execution_config.auth_profile_ref =
Some(AuthProfileId::new(auth_profile.id.as_str()));
payload
})
.await
.expect("an existing operation with the candidate name");
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"new_version",
Some(existing.operation_id.as_str()),
None,
None,
)],
),
)
.await
.unwrap();
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
let candidate = &stored.payload["candidates"][0];
assert_eq!(
candidate["decision"]["auth_profile_id"],
auth_profile.id.as_str(),
"the inherited target reference is reflected in the candidate summary"
);
assert_eq!(
candidate["operation"]["execution_config"]["auth_profile_ref"],
auth_profile.id.as_str(),
"the inherited target reference must be retained in the snapshot"
);
assert!(!stored.payload.to_string().contains(secret_token));
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn stale_or_terminal_jobs_preview_again_without_mutating_the_job() {
@@ -109,6 +109,7 @@ fn decision(
target_operation_id: target.map(ToOwned::to_owned),
confirmed_name: name.map(ToOwned::to_owned),
auth_profile_id: auth.map(ToOwned::to_owned),
security_alternative_index: None,
}
}
@@ -160,4 +161,7 @@ fn assert_http_status(error: ApiError, expected: axum::http::StatusCode) {
}
mod creation;
pub(super) use creation::OPENAPI_SINGLE_SECURITY;
mod security_availability;
mod security_binding;
mod validation;
@@ -0,0 +1,292 @@
use super::*;
const OPENAPI_SECURITY_BINDING: &str = r#"
openapi: 3.0.3
info: { title: Security binding availability, version: '1' }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
Key: { type: apiKey, in: header, name: X-Api-Key }
Bearer: { type: http, scheme: bearer }
paths:
/v2/latest:
get:
operationId: latestRates
security: [{ Key: [] }, { Bearer: [] }]
responses: { '200': { description: ok } }
"#;
fn security_upload() -> OpenApiUpload {
OpenApiUpload {
bytes: OPENAPI_SECURITY_BINDING.as_bytes().to_vec(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
}
}
fn binding_decision(auth_profile_id: &str) -> OpenApiImportPlanDecision {
let mut binding = decision(
"GET /v2/latest",
"create",
None,
None,
Some(auth_profile_id),
);
binding.security_alternative_index = Some(0);
binding
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn security_binding_refuses_foreign_and_unavailable_auth_material_before_draft() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_plan_security_availability"),
test_auth_settings(),
test_secret_crypto(),
);
let primary = WorkspaceId::new("ws_default");
let foreign = WorkspaceId::new("ws_security_foreign");
let created_at = OffsetDateTime::now_utc();
registry
.create_workspace(CreateWorkspaceRequest {
workspace: &Workspace {
id: foreign.clone(),
slug: "ws-security-foreign".to_owned(),
display_name: "Foreign security workspace".to_owned(),
status: WorkspaceStatus::Active,
settings: json!({}),
created_at,
updated_at: created_at,
},
})
.await
.unwrap();
let foreign_canary = "foreign-security-profile-canary";
let foreign_secret = service
.create_secret(
&foreign,
None,
SecretPayload {
name: "foreign-security-secret".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": foreign_canary }),
},
None,
)
.await
.unwrap();
let foreign_profile = service
.create_auth_profile(
&foreign,
AuthProfilePayload {
name: "foreign-security-profile-name".to_owned(),
kind: AuthKind::ApiKeyHeader,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new(foreign_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
// A foreign profile is absent from the current workspace's advertised
// choices and submission fails with a generic workspace-safe error.
let preview = service
.preview_openapi_import(&primary, security_upload())
.await
.unwrap();
let preview_text = serde_json::to_string(&preview).unwrap();
for private_value in [
foreign_profile.id.as_str(),
foreign_profile.name.as_str(),
foreign_secret.id.as_str(),
foreign_canary,
] {
assert!(
!preview_text.contains(private_value),
"foreign profile metadata appeared in preview choices"
);
}
assert!(preview.security_profile_choices.iter().all(|choice| {
choice
.profiles
.iter()
.all(|profile| profile.id != foreign_profile.id.as_str())
}));
let job_id: ImportJobId = preview.job_id.as_str().into();
let foreign_error = service
.create_openapi_import_plan(
&primary,
&job_id,
plan_payload(
"full",
vec![],
vec![binding_decision(foreign_profile.id.as_str())],
),
)
.await
.expect_err("a profile from another workspace must not bind");
assert_error_code(&foreign_error, "import_plan_auth_unavailable");
let foreign_error_text = format!("{foreign_error:?}");
assert_http_status(foreign_error, axum::http::StatusCode::UNPROCESSABLE_ENTITY);
for private_value in [
foreign_profile.id.as_str(),
foreign_profile.name.as_str(),
foreign_secret.id.as_str(),
foreign_canary,
] {
assert!(
!foreign_error_text.contains(private_value),
"foreign profile or secret metadata leaked: {foreign_error_text}"
);
}
// A previously valid local profile whose Secret is disabled is removed
// from choices and cannot be used in a newly confirmed ImportPlan.
let disabled_canary = "disabled-security-secret-canary";
let disabled_secret = service
.create_secret(
&primary,
None,
SecretPayload {
name: "disabled-security-secret".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": disabled_canary }),
},
None,
)
.await
.unwrap();
let disabled_profile = service
.create_auth_profile(
&primary,
AuthProfilePayload {
name: "disabled-security-profile".to_owned(),
kind: AuthKind::ApiKeyHeader,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new(disabled_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
sqlx::query(
"update secrets set status = 'disabled', updated_at = now() where workspace_id = $1 and id = $2",
)
.bind(primary.as_str())
.bind(disabled_secret.id.as_str())
.execute(registry.pool())
.await
.unwrap();
let preview = service
.preview_openapi_import(&primary, security_upload())
.await
.unwrap();
assert!(preview.security_profile_choices.iter().all(|choice| {
choice
.profiles
.iter()
.all(|profile| profile.id != disabled_profile.id.as_str())
}));
let job_id: ImportJobId = preview.job_id.as_str().into();
let disabled_error = service
.create_openapi_import_plan(
&primary,
&job_id,
plan_payload(
"full",
vec![],
vec![binding_decision(disabled_profile.id.as_str())],
),
)
.await
.expect_err("a disabled linked Secret must reject the profile binding");
assert_error_code(&disabled_error, "secret_not_active");
let disabled_error_text = format!("{disabled_error:?}");
assert!(!disabled_error_text.contains(disabled_canary));
// A profile whose active Secret has no stored current version is equally
// unavailable. Direct SQL models the broken persisted reference while
// retaining the real Admin preview and plan validation paths.
let missing_version_canary = "missing-version-security-canary";
let missing_version_secret = service
.create_secret(
&primary,
None,
SecretPayload {
name: "missing-version-security-secret".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": missing_version_canary }),
},
None,
)
.await
.unwrap();
let missing_version_profile = service
.create_auth_profile(
&primary,
AuthProfilePayload {
name: "missing-version-security-profile".to_owned(),
kind: AuthKind::ApiKeyHeader,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new(missing_version_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
sqlx::query("delete from secret_versions where secret_id = $1 and version = 1")
.bind(missing_version_secret.id.as_str())
.execute(registry.pool())
.await
.unwrap();
let preview = service
.preview_openapi_import(&primary, security_upload())
.await
.unwrap();
assert!(preview.security_profile_choices.iter().all(|choice| {
choice
.profiles
.iter()
.all(|profile| profile.id != missing_version_profile.id.as_str())
}));
let job_id: ImportJobId = preview.job_id.as_str().into();
let missing_version_error = service
.create_openapi_import_plan(
&primary,
&job_id,
plan_payload(
"full",
vec![],
vec![binding_decision(missing_version_profile.id.as_str())],
),
)
.await
.expect_err("a Secret without a current version must reject the profile binding");
assert_error_code(&missing_version_error, "operation_auth_profile_invalid");
let missing_version_error_text = format!("{missing_version_error:?}");
for private_value in [
missing_version_profile.id.as_str(),
missing_version_profile.name.as_str(),
missing_version_secret.id.as_str(),
missing_version_canary,
] {
assert!(
!missing_version_error_text.contains(private_value),
"missing-version Secret metadata leaked: {missing_version_error_text}"
);
}
assert!(
service.list_operations(&primary).await.unwrap().is_empty(),
"no unavailable profile binding may create a Draft"
);
}
@@ -0,0 +1,732 @@
use super::*;
const OPENAPI_SINGLE_PUBLIC: &str = r#"
openapi: 3.0.3
info: { title: Public import, version: '1' }
servers: [{ url: https://api.example.test }]
paths:
/v2/latest:
get:
operationId: latestRates
responses: { '200': { description: ok } }
"#;
const OPENAPI_SINGLE_EXPLICIT_NO_AUTH: &str = r#"
openapi: 3.0.3
info: { title: Explicit no auth import, version: '1' }
servers: [{ url: https://api.example.test }]
paths:
/v2/latest:
get:
operationId: latestRates
security: []
responses: { '200': { description: ok } }
"#;
const OPENAPI_BASIC_AND_QUERY_SECURITY: &str = r#"
openapi: 3.0.3
info: { title: Basic and query import, version: '1' }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
Basic: { type: http, scheme: basic }
QueryKey: { type: apiKey, in: query, name: access_token }
paths:
/v2/basic:
get:
operationId: basicRates
security: [{ Basic: [] }, { QueryKey: [] }]
responses: { '200': { description: ok } }
/v2/query:
get:
operationId: queryRates
security: [{ Basic: [] }, { QueryKey: [] }]
responses: { '200': { description: ok } }
"#;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn auth_profile_references_are_validated_stored_and_inherited() {
let registry = test_registry().await;
let upstream_base_url = spawn_upstream_server().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_plan_auth"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let secret_token = "plan-secret-canary-2-13";
let secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "plan-auth-token".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": secret_token }),
},
None,
)
.await
.unwrap();
let auth_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "plan-auth-profile".to_owned(),
kind: AuthKind::ApiKeyHeader,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new(secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let bearer_secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "plan-bearer-token".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": "bearer-choice-canary" }),
},
None,
)
.await
.unwrap();
let bearer_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "plan-bearer-profile".to_owned(),
kind: AuthKind::Bearer,
config: AuthConfig::Bearer(crank_core::BearerAuthConfig {
header_name: "Authorization".to_owned(),
secret_id: SecretId::new(bearer_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY))
.await
.unwrap();
let selectable = preview
.security_profile_choices
.iter()
.find(|choice| choice.operation_key == "GET /v2/latest" && choice.alternative_index == 0)
.expect("preview must expose choices for supported security alternatives");
assert!(selectable.profiles.iter().any(|choice| {
choice.id == auth_profile.id.as_str() && choice.kind == AuthKind::ApiKeyHeader
}));
assert!(
selectable
.profiles
.iter()
.all(|choice| { choice.id != bearer_profile.id.as_str() })
);
let bearer_choice = preview
.security_profile_choices
.iter()
.find(|choice| choice.operation_key == "GET /v2/latest" && choice.alternative_index == 1)
.expect("preview must expose profile choices for the second OR alternative");
assert!(bearer_choice.profiles.iter().any(|choice| {
choice.id == bearer_profile.id.as_str() && choice.kind == AuthKind::Bearer
}));
assert!(
bearer_choice
.profiles
.iter()
.all(|choice| { choice.id != auth_profile.id.as_str() })
);
let job_id: ImportJobId = preview.job_id.as_str().into();
let mut auth_decision = decision(
"GET /v2/latest",
"create",
None,
None,
Some(auth_profile.id.as_str()),
);
auth_decision.security_alternative_index = Some(0);
let plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload("full", vec![], vec![auth_decision]),
)
.await
.unwrap();
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
stored.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"],
auth_profile.id.as_str()
);
assert_eq!(
stored.payload["candidates"][0]["decision"]["security_alternative_index"],
0
);
// Only the reference survives: no secret material anywhere in the plan.
assert!(!stored.payload.to_string().contains(secret_token));
assert_eq!(
plan.candidates.first().unwrap().auth_profile_id.as_deref(),
Some(auth_profile.id.as_str())
);
let mut bearer_decision = decision(
"GET /v2/latest",
"create",
None,
None,
Some(bearer_profile.id.as_str()),
);
bearer_decision.security_alternative_index = Some(1);
let bearer_plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload("full", vec![], vec![bearer_decision]),
)
.await
.unwrap();
let stored_bearer = registry
.get_import_plan(&workspace_id, &bearer_plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
stored_bearer.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"],
bearer_profile.id.as_str()
);
assert_eq!(
stored_bearer.payload["candidates"][0]["decision"]["security_alternative_index"],
1
);
assert!(
!stored_bearer
.payload
.to_string()
.contains("bearer-choice-canary")
);
// An unknown profile reference fails closed in the workspace scope check.
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let error = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"create",
None,
None,
Some("auth_missing_profile"),
)],
),
)
.await
.expect_err("an unknown auth profile reference must be rejected");
assert_error_code(&error, "import_plan_auth_unavailable");
assert_http_status(error, axum::http::StatusCode::UNPROCESSABLE_ENTITY);
// `new_version` without an explicit choice inherits the target's ref.
let existing = service
.create_operation(&workspace_id, {
let mut payload = test_operation_payload(&upstream_base_url, "latest_rates");
payload.execution_config.auth_profile_ref =
Some(AuthProfileId::new(auth_profile.id.as_str()));
payload
})
.await
.expect("an existing operation with the candidate name");
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"new_version",
Some(existing.operation_id.as_str()),
None,
None,
)],
),
)
.await
.unwrap();
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
let candidate = &stored.payload["candidates"][0];
assert_eq!(
candidate["decision"]["auth_profile_id"],
auth_profile.id.as_str(),
"the inherited target reference is reflected in the candidate summary"
);
assert_eq!(
candidate["operation"]["execution_config"]["auth_profile_ref"],
auth_profile.id.as_str(),
"the inherited target reference must be retained in the snapshot"
);
assert!(!stored.payload.to_string().contains(secret_token));
let public_preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_PUBLIC))
.await
.unwrap();
let public_job_id: ImportJobId = public_preview.job_id.as_str().into();
let public_plan = service
.create_openapi_import_plan(
&workspace_id,
&public_job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"new_version",
Some(existing.operation_id.as_str()),
None,
None,
)],
),
)
.await
.expect("a no-security source may inherit the target's existing profile");
let public_stored = registry
.get_import_plan(&workspace_id, &public_plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
public_stored.payload["candidates"][0]["auth_requirement"]["kind"], "unconstrained",
"no source security declaration is represented explicitly for transactional Apply"
);
let report = service
.apply_openapi_import_plan(
&workspace_id,
&crank_registry::ImportPlanId::new(public_plan.plan_reference.clone()),
admin_api::service::OpenApiImportPlanApplyPayload {
plan_digest: public_plan.plan_digest.clone(),
idempotency_key: "new-version-inherited-public".to_owned(),
},
)
.await
.expect("Apply must retain profile and Secret transactional validation");
assert_eq!(
report.status,
crank_registry::ImportPlanApplyStatus::Applied
);
assert_eq!(
report.outcomes[0].outcome,
crank_registry::ImportPlanCandidateOutcome::Versioned
);
assert_eq!(report.outcomes[0].version, Some(2));
let operation = service
.list_operations(&workspace_id)
.await
.unwrap()
.into_iter()
.find(|operation| operation.id == existing.operation_id.as_str())
.unwrap();
assert_eq!(operation.current_draft_version, 2);
let explicit_no_auth_preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_EXPLICIT_NO_AUTH))
.await
.unwrap();
let explicit_no_auth_job_id: ImportJobId = explicit_no_auth_preview.job_id.as_str().into();
let incompatible_profile_decision = decision(
"GET /v2/latest",
"new_version",
Some(existing.operation_id.as_str()),
None,
Some(auth_profile.id.as_str()),
);
let error = service
.create_openapi_import_plan(
&workspace_id,
&explicit_no_auth_job_id,
plan_payload("full", vec![], vec![incompatible_profile_decision]),
)
.await
.expect_err("explicit Auth Profile stays incompatible with security: []");
assert_error_code(&error, "import_plan_auth_incompatible");
let explicit_no_auth_plan = service
.create_openapi_import_plan(
&workspace_id,
&explicit_no_auth_job_id,
plan_payload(
"full",
vec![],
vec![decision(
"GET /v2/latest",
"new_version",
Some(existing.operation_id.as_str()),
None,
None,
)],
),
)
.await
.expect("an explicit empty security requirement clears inherited auth");
let no_auth_stored = registry
.get_import_plan(
&workspace_id,
&explicit_no_auth_plan.plan_reference.as_str().into(),
)
.await
.unwrap()
.unwrap();
assert!(no_auth_stored.payload["candidates"][0]["operation"]["execution_config"]
["auth_profile_ref"]
.is_null());
let no_auth_report = service
.apply_openapi_import_plan(
&workspace_id,
&crank_registry::ImportPlanId::new(explicit_no_auth_plan.plan_reference.clone()),
admin_api::service::OpenApiImportPlanApplyPayload {
plan_digest: explicit_no_auth_plan.plan_digest.clone(),
idempotency_key: "new-version-explicit-no-auth".to_owned(),
},
)
.await
.expect("explicit no-auth new_version applies without inherited credentials");
assert_eq!(
no_auth_report.outcomes[0].outcome,
crank_registry::ImportPlanCandidateOutcome::Versioned
);
assert_eq!(no_auth_report.outcomes[0].version, Some(3));
let operation = service
.list_operations(&workspace_id)
.await
.unwrap()
.into_iter()
.find(|operation| operation.id == existing.operation_id.as_str())
.unwrap();
assert_eq!(operation.current_draft_version, 3);
let operation_id = OperationId::new(existing.operation_id.clone());
let version = registry
.get_operation_version(&workspace_id, &operation_id, 3)
.await
.unwrap()
.unwrap();
assert!(version.snapshot.execution_config.auth_profile_ref.is_none());
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn basic_and_query_profiles_bind_to_matching_security_alternatives_and_apply() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_plan_basic_query_auth"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let username_secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "basic-username".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": "plan-user" }),
},
None,
)
.await
.unwrap();
let password_secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "basic-password".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": "plan-password-canary" }),
},
None,
)
.await
.unwrap();
let basic_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "basic-profile".to_owned(),
kind: AuthKind::Basic,
config: AuthConfig::Basic(crank_core::BasicAuthConfig {
username_secret_id: SecretId::new(username_secret.id.as_str()),
password_secret_id: SecretId::new(password_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let query_secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "query-key".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": "query-key-canary" }),
},
None,
)
.await
.unwrap();
let query_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "query-profile".to_owned(),
kind: AuthKind::ApiKeyQuery,
config: AuthConfig::ApiKeyQuery(crank_core::ApiKeyQueryAuthConfig {
param_name: "access_token".to_owned(),
secret_id: SecretId::new(query_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let incompatible_secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "incompatible-token".to_owned(),
kind: SecretKind::Token,
value: json!({ "token": "incompatible-canary" }),
},
None,
)
.await
.unwrap();
let incompatible_profile = service
.create_auth_profile(
&workspace_id,
AuthProfilePayload {
name: "incompatible-bearer-profile".to_owned(),
kind: AuthKind::Bearer,
config: AuthConfig::Bearer(crank_core::BearerAuthConfig {
header_name: "Authorization".to_owned(),
secret_id: SecretId::new(incompatible_secret.id.as_str()),
}),
},
None,
)
.await
.unwrap();
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_BASIC_AND_QUERY_SECURITY))
.await
.unwrap();
for operation_key in ["GET /v2/basic", "GET /v2/query"] {
let basic_alternative = preview
.security_profile_choices
.iter()
.find(|choice| choice.operation_key == operation_key && choice.alternative_index == 0)
.expect("Basic alternative must be represented in preview");
assert!(basic_alternative.profiles.iter().any(|choice| {
choice.id == basic_profile.id.as_str() && choice.kind == AuthKind::Basic
}));
assert!(
basic_alternative
.profiles
.iter()
.all(|choice| choice.id != query_profile.id.as_str())
);
assert!(
basic_alternative
.profiles
.iter()
.all(|choice| choice.id != incompatible_profile.id.as_str())
);
let query_alternative = preview
.security_profile_choices
.iter()
.find(|choice| choice.operation_key == operation_key && choice.alternative_index == 1)
.expect("query alternative must be represented in preview");
assert!(query_alternative.profiles.iter().any(|choice| {
choice.id == query_profile.id.as_str() && choice.kind == AuthKind::ApiKeyQuery
}));
assert!(
query_alternative
.profiles
.iter()
.all(|choice| choice.id != basic_profile.id.as_str())
);
assert!(
query_alternative
.profiles
.iter()
.all(|choice| choice.id != incompatible_profile.id.as_str())
);
}
let job_id: ImportJobId = preview.job_id.as_str().into();
let mut basic_decision = decision(
"GET /v2/basic",
"create",
None,
None,
Some(basic_profile.id.as_str()),
);
basic_decision.security_alternative_index = Some(0);
let basic_plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"partial",
vec!["GET /v2/basic".to_owned()],
vec![basic_decision],
),
)
.await
.unwrap();
assert_eq!(basic_plan.counts.actionable, 1);
let basic_candidate = basic_plan
.candidates
.iter()
.find(|candidate| candidate.key == "GET /v2/basic")
.unwrap();
assert_eq!(
basic_candidate.auth_profile_id.as_deref(),
Some(basic_profile.id.as_str())
);
let mut query_decision = decision(
"GET /v2/query",
"create",
None,
None,
Some(query_profile.id.as_str()),
);
query_decision.security_alternative_index = Some(1);
let query_plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload(
"partial",
vec!["GET /v2/query".to_owned()],
vec![query_decision],
),
)
.await
.unwrap();
assert_eq!(query_plan.counts.actionable, 1);
let query_candidate = query_plan
.candidates
.iter()
.find(|candidate| candidate.key == "GET /v2/query")
.unwrap();
assert_eq!(
query_candidate.auth_profile_id.as_deref(),
Some(query_profile.id.as_str())
);
for (plan, expected_profile, idempotency_key) in [
(
&basic_plan,
basic_profile.id.as_str(),
"apply-basic-profile",
),
(
&query_plan,
query_profile.id.as_str(),
"apply-query-profile",
),
] {
let report = service
.apply_openapi_import_plan(
&workspace_id,
&crank_registry::ImportPlanId::new(plan.plan_reference.clone()),
admin_api::service::OpenApiImportPlanApplyPayload {
plan_digest: plan.plan_digest.clone(),
idempotency_key: idempotency_key.to_owned(),
},
)
.await
.unwrap();
assert_eq!(
report.status,
crank_registry::ImportPlanApplyStatus::Applied
);
let operation_id = OperationId::new(report.outcomes[0].operation_id.clone().unwrap());
let version = registry
.get_operation_version(&workspace_id, &operation_id, 1)
.await
.unwrap()
.unwrap();
assert_eq!(
version
.snapshot
.execution_config
.auth_profile_ref
.as_ref()
.map(|profile_id| profile_id.as_str()),
Some(expected_profile)
);
}
let basic_stored = registry
.get_import_plan(&workspace_id, &basic_plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert!(
!basic_stored
.payload
.to_string()
.contains("plan-password-canary")
);
let query_stored = registry
.get_import_plan(&workspace_id, &query_plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert!(
!query_stored
.payload
.to_string()
.contains("query-key-canary")
);
}
@@ -127,6 +127,8 @@ pub fn candidate_from_operation(
key: operation.key.clone(),
method: operation.method,
path: operation.path.clone(),
security_requirements: Default::default(),
security_schemes: BTreeMap::new(),
operation_id: operation.operation_id.clone(),
suggested_name: name,
suggested_display_name: display_name,
+395 -21
View File
@@ -1,70 +1,444 @@
use serde_json::Value;
use std::collections::BTreeSet;
use crate::rest::{
model::{ImportFinding, SourceLocation},
recommendations::document_finding,
};
pub(super) fn collect_secret_like_examples(
value: &Value,
pub(super) const REDACTED_LITERAL: &str = "[REDACTED]";
/// Redacts credential-like literals before they enter the normalized tree,
/// while retaining stable source locations for review findings.
pub(super) fn redact_secret_like_literals(
value: &mut Value,
pointer: &str,
inside_example: bool,
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
) {
let declared_names = credential_names(std::iter::once(&*value));
redact_secret_like_literals_with_names(value, pointer, &declared_names, findings, locations);
}
pub(super) fn redact_secret_like_literals_with_names(
value: &mut Value,
pointer: &str,
declared_names: &CredentialNames,
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
) {
visit(
value,
pointer,
VisitContext {
in_secret_example: false,
sensitive_parameter_schema: false,
force_redact: false,
declared_names,
},
findings,
locations,
);
}
#[derive(Default)]
pub(super) struct CredentialNames {
header: BTreeSet<String>,
query: BTreeSet<String>,
cookie: BTreeSet<String>,
}
#[derive(Clone, Copy)]
struct VisitContext<'a> {
in_secret_example: bool,
sensitive_parameter_schema: bool,
force_redact: bool,
declared_names: &'a CredentialNames,
}
impl CredentialNames {
fn contains(&self, location: &str, name: &str) -> bool {
let names = match location.to_ascii_lowercase().as_str() {
"header" => &self.header,
"query" => &self.query,
"cookie" => &self.cookie,
_ => return false,
};
names.contains(&name.to_ascii_lowercase())
}
}
pub(super) fn credential_names<'a>(roots: impl IntoIterator<Item = &'a Value>) -> CredentialNames {
let mut names = CredentialNames::default();
for root in roots {
collect_credential_names(root, &mut names);
}
names
}
fn collect_credential_names(value: &Value, names: &mut CredentialNames) {
match value {
Value::Object(fields) => {
if fields.get("type").and_then(Value::as_str) == Some("apiKey")
&& let (Some(location), Some(name)) = (
fields.get("in").and_then(Value::as_str),
fields.get("name").and_then(Value::as_str),
)
{
let destination = match location.to_ascii_lowercase().as_str() {
"header" => Some(&mut names.header),
"query" => Some(&mut names.query),
"cookie" => Some(&mut names.cookie),
_ => None,
};
if let Some(destination) = destination {
destination.insert(name.to_ascii_lowercase());
}
}
for child in fields.values() {
collect_credential_names(child, names);
}
}
Value::Array(items) => {
for child in items {
collect_credential_names(child, names);
}
}
_ => {}
}
}
fn visit(
value: &mut Value,
pointer: &str,
context: VisitContext<'_>,
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
) {
if context.force_redact
|| value
.as_str()
.is_some_and(|text| url_has_secret_named(text, context.declared_names))
|| (context.in_secret_example && value.as_str() == Some(REDACTED_LITERAL))
|| (context.in_secret_example && scalar_looks_secret(value))
{
redact(value, context.force_redact, context.declared_names);
findings.push(document_finding(
"secret_like_example",
"В исходном документе обнаружено значение, похожее на секрет; оно удалено из импортируемых данных.",
));
locations.push(Some(SourceLocation {
pointer: pointer.to_owned(),
}));
return;
}
match value {
Value::Object(fields) => {
let sensitive_parameter =
fields
.get("in")
.and_then(Value::as_str)
.is_some_and(|location| {
["header", "query", "cookie"]
.iter()
.any(|allowed| location.eq_ignore_ascii_case(allowed))
})
&& fields
.get("name")
.and_then(Value::as_str)
.is_some_and(|name| {
is_sensitive_name(name)
|| fields.get("in").and_then(Value::as_str).is_some_and(
|location| context.declared_names.contains(location, name),
)
});
for (key, child) in fields {
let child_pointer = format!(
"{pointer}/{}",
super::normalize_coverage::escape_pointer(key)
);
collect_secret_like_examples(
let key_lower = key.to_ascii_lowercase();
let in_example = context.in_secret_example
|| matches!(
key_lower.as_str(),
"default"
| "example"
| "examples"
| "x-example"
| "x-examples"
| "bearerformat"
| "x-format-hint"
);
let sensitive_header_definition = pointer.ends_with("/headers")
&& (is_sensitive_name(key) || context.declared_names.contains("header", key));
let parameter_value = (sensitive_parameter || sensitive_header_definition)
&& matches!(
key_lower.as_str(),
"example" | "examples" | "x-example" | "x-examples"
);
let parameter_schema = context.sensitive_parameter_schema
|| sensitive_header_definition
|| (sensitive_parameter && key_lower == "schema");
let parent_is_secret_property =
pointer.rsplit('/').next().is_some_and(is_sensitive_name);
// Names in schema/scheme maps and security requirements are
// identifiers, not credential values. Traverse their metadata
// and redact the actual defaults/examples instead.
let identifier_map = (child.is_object()
&& (pointer.ends_with("/properties")
|| pointer.ends_with("/headers")
|| pointer == "/components/schemas"
|| pointer == "/definitions"
|| pointer == "/components/securitySchemes"
|| pointer == "/securityDefinitions"))
|| (child.is_array()
&& pointer.rsplit_once('/').is_some_and(|(parent, index)| {
parent.ends_with("/security") && index.parse::<usize>().is_ok()
}));
let force_child = (is_sensitive_name(key) && !identifier_map)
|| (matches!(key_lower.as_str(), "default" | "example" | "examples")
&& parent_is_secret_property)
|| parameter_value
|| (sensitive_parameter
&& matches!(key_lower.as_str(), "default" | "example" | "examples"))
|| (parameter_schema
&& matches!(key_lower.as_str(), "default" | "example" | "examples"));
visit(
child,
&child_pointer,
inside_example || matches!(key.as_str(), "example" | "examples"),
VisitContext {
in_secret_example: in_example,
sensitive_parameter_schema: parameter_schema,
force_redact: force_child,
..context
},
findings,
locations,
);
}
}
Value::Array(items) => {
for (index, child) in items.iter().enumerate() {
collect_secret_like_examples(
for (index, child) in items.iter_mut().enumerate() {
visit(
child,
&format!("{pointer}/{index}"),
inside_example,
VisitContext {
force_redact: false,
..context
},
findings,
locations,
);
}
}
Value::String(value) if inside_example && looks_secret_like(pointer, value) => {
findings.push(document_finding(
"secret_like_example",
"Example содержит значение, похожее на секрет, и не включается в executable contract.",
));
locations.push(Some(SourceLocation {
pointer: pointer.to_owned(),
}));
}
_ => {}
}
}
fn looks_secret_like(pointer: &str, value: &str) -> bool {
let pointer = pointer.to_ascii_lowercase();
fn is_sensitive_name(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
if lower.starts_with('/')
|| matches!(
lower.as_str(),
"url"
| "tokenurl"
| "authorizationurl"
| "refreshurl"
| "openidconnecturl"
| "clientcredentials"
| "authorizationcode"
)
{
return false;
}
let compact = lower.replace(['-', '_'], "");
[
"token",
"secret",
"password",
"credential",
"authorization",
"apikey",
"accesskey",
"ciphertext",
"privatekey",
"passwd",
]
.iter()
.any(|marker| compact.contains(marker))
}
fn scalar_looks_secret(value: &Value) -> bool {
let Value::String(value) = value else {
return false;
};
let value = value.to_ascii_lowercase();
[
"token",
"secret",
"password",
"credential",
"authorization",
"api_key",
"apikey",
"authorization",
]
.iter()
.any(|marker| pointer.contains(marker) || value.contains(marker))
.any(|marker| value.contains(marker))
|| value.starts_with("bearer ")
|| value.starts_with("sk-")
|| looks_like_jwt(&value)
}
fn looks_like_jwt(value: &str) -> bool {
let mut segments = value.split('.');
let (Some(header), Some(payload), Some(signature), None) = (
segments.next(),
segments.next(),
segments.next(),
segments.next(),
) else {
return false;
};
header.starts_with("eyj")
&& !payload.is_empty()
&& signature.len() >= 8
&& [header, payload, signature].iter().all(|segment| {
segment
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
})
}
pub(super) fn url_has_secret(value: &str) -> bool {
url_has_secret_named(value, &CredentialNames::default())
}
fn url_has_secret_named(value: &str, declared_names: &CredentialNames) -> bool {
url_userinfo(value).is_some()
|| query_has_secret_parameter(value, &declared_names.query)
|| fragment_has_secret_parameter(value)
}
fn fragment_has_secret_parameter(value: &str) -> bool {
let Some((_, fragment)) = value.split_once('#') else {
return false;
};
if fragment.starts_with('/') {
return false;
}
url::form_urlencoded::parse(fragment.as_bytes()).any(|(name, _)| is_sensitive_name(&name))
}
fn redact_url_fragment(value: &str) -> String {
let Some((base, fragment)) = value.split_once('#') else {
return value.to_owned();
};
if fragment.starts_with('/') || !fragment.contains('=') {
return value.to_owned();
}
let redacted = fragment
.split('&')
.map(|pair| {
let (name, parameter_value) = pair.split_once('=').unwrap_or((pair, ""));
let decoded_name = url::form_urlencoded::parse(format!("{name}=").as_bytes())
.next()
.map(|(name, _)| name.into_owned())
.unwrap_or_default();
if is_sensitive_name(&decoded_name) {
format!("{name}={REDACTED_LITERAL}")
} else if pair.contains('=') {
format!("{name}={parameter_value}")
} else {
name.to_owned()
}
})
.collect::<Vec<_>>()
.join("&");
format!("{base}#{redacted}")
}
fn url_userinfo(value: &str) -> Option<(&str, &str, &str)> {
let (scheme, rest) = value
.split_once("://")
.or_else(|| value.strip_prefix("//").map(|rest| ("", rest)))?;
let (authority, suffix) = rest
.find(['/', '?', '#'])
.map_or((rest, ""), |index| (&rest[..index], &rest[index..]));
let (userinfo, host) = authority.split_once('@')?;
if userinfo.is_empty() || host.is_empty() {
return None;
}
Some((scheme, host, suffix))
}
fn query_has_secret_parameter(value: &str, declared_query_names: &BTreeSet<String>) -> bool {
let Some((_, query)) = value.split_once('?') else {
return false;
};
let query = query.split('#').next().unwrap_or(query);
url::form_urlencoded::parse(query.as_bytes()).any(|(name, _)| {
is_sensitive_name(&name) || declared_query_names.contains(&name.to_ascii_lowercase())
})
}
fn redact(value: &mut Value, force: bool, declared_names: &CredentialNames) {
if force
|| !value
.as_str()
.is_some_and(|text| url_has_secret_named(text, declared_names))
{
*value = Value::String(REDACTED_LITERAL.to_owned());
return;
}
if let Value::String(text) = value {
if force {
*text = REDACTED_LITERAL.to_owned();
return;
}
let mut sanitized = text.clone();
if let Some((scheme, host, suffix)) = url_userinfo(&sanitized) {
sanitized = if scheme.is_empty() {
format!("//{host}{suffix}")
} else {
format!("{scheme}://{host}{suffix}")
};
}
if query_has_secret_parameter(&sanitized, &declared_names.query)
&& let Some((base, query)) = sanitized.split_once('?')
{
let (query, fragment) = query
.split_once('#')
.map_or((query, None), |(query, fragment)| (query, Some(fragment)));
let redacted = query
.split('&')
.map(|pair| {
let (name, value) = pair.split_once('=').unwrap_or((pair, ""));
let decoded_name = url::form_urlencoded::parse(format!("{name}=").as_bytes())
.next()
.map(|(name, _)| name.into_owned())
.unwrap_or_default();
if is_sensitive_name(&decoded_name)
|| declared_names
.query
.contains(&decoded_name.to_ascii_lowercase())
{
format!("{name}={REDACTED_LITERAL}")
} else if pair.contains('=') {
format!("{name}={value}")
} else {
name.to_owned()
}
})
.collect::<Vec<_>>()
.join("&");
sanitized = match fragment {
Some(fragment) => format!("{base}?{redacted}#{fragment}"),
None => format!("{base}?{redacted}"),
};
}
*text = redact_url_fragment(&sanitized);
} else {
*value = Value::String(REDACTED_LITERAL.to_owned());
}
}
+19 -9
View File
@@ -15,26 +15,36 @@ mod normalize_schema;
mod normalize_security;
mod openapi3;
mod openapi3_findings;
mod parser_findings;
mod payload;
mod recommendations;
mod reference;
mod schema;
mod security;
mod source_safety;
mod swagger2;
mod swagger2_findings;
pub use model::{
ExternalDocumentSnapshot, ImportFinding, ImportFindingSeverity, ImportGroupPreview,
ImportOperationCandidate, ImportPreview, ImportSourcePreview, NORMALIZER_VERSION,
NormalizationConfig, NormalizedFinding, NormalizedIr, NormalizedOperation, NormalizedParameter,
NormalizedReference, NormalizedRequestBodyVariant, NormalizedResponseVariant, NormalizedSchema,
NormalizedSchemaConstraints, NormalizedSchemaKind, PROJECTION_VERSION, ResolvedReferenceEdge,
ResolvedReferenceGraph, ResolvedReferenceNode, RestImportCandidate, RestImportDocument,
RestImportOperation, RestImportParameter, RestParameterLocation, RestRequestBodyVariant,
RestResponseVariant, SourceDigest, SourceIdentity, SourceLocation, UnresolvedReference,
ExternalDocumentSnapshot, HISTORICAL_NORMALIZER_VERSION, HISTORICAL_PROJECTION_VERSION,
ImportFinding, ImportFindingSeverity, ImportGroupPreview, ImportOperationCandidate,
ImportPreview, ImportSourcePreview, NORMALIZER_VERSION, NormalizationConfig, NormalizedFinding,
NormalizedIr, NormalizedOperation, NormalizedParameter, NormalizedReference,
NormalizedRequestBodyVariant, NormalizedResponseVariant, NormalizedSchema,
NormalizedSchemaConstraints, NormalizedSchemaKind, NormalizedSecurityRequirements,
NormalizedSecurityScheme, NormalizedSecuritySchemeKind, PROJECTION_VERSION,
ResolvedReferenceEdge, ResolvedReferenceGraph, ResolvedReferenceNode, RestImportCandidate,
RestImportDocument, RestImportOperation, RestImportParameter, RestParameterLocation,
RestRequestBodyVariant, RestResponseVariant, SecurityParameterLocation,
SecurityRequirementAlternative, SecurityRequirementsSource, SourceDigest, SourceIdentity,
SourceLocation, UnresolvedReference,
};
pub use normalize::{
ImportParseError, external_reference_uris, normalize_verified_bundle,
normalize_verified_document, preview_document, preview_document_legacy_v1, preview_from_ir,
reference_uris, validate_normalized_ir,
reference_uris, validate_document_source_safe, validate_normalized_ir,
};
pub use payload::operation_draft_from_candidate;
pub use source_safety::{
validate_document_bundle_source_safe, validate_resolved_bundle_source_safe,
};
+102 -2
View File
@@ -9,8 +9,10 @@ use serde_json::Value;
/// The immutable contract used to normalize an OpenAPI source. These names
/// deliberately travel with an import job: changing either contract must not
/// silently reinterpret a pending preview.
pub const NORMALIZER_VERSION: &str = "normalized-ir-v4";
pub const PROJECTION_VERSION: &str = "preview-v4";
pub const NORMALIZER_VERSION: &str = "normalized-ir-v5";
pub const PROJECTION_VERSION: &str = "preview-v5";
pub const HISTORICAL_NORMALIZER_VERSION: &str = "normalized-ir-v4";
pub const HISTORICAL_PROJECTION_VERSION: &str = "preview-v4";
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct SourceDigest(String);
@@ -232,6 +234,13 @@ pub struct NormalizedOperation {
pub key: String,
pub method: HttpMethod,
pub path: String,
#[serde(
default,
skip_serializing_if = "NormalizedSecurityRequirements::is_empty"
)]
pub security_requirements: NormalizedSecurityRequirements,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub security_schemes: BTreeMap<String, NormalizedSecurityScheme>,
pub operation_id: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
@@ -251,6 +260,81 @@ pub struct NormalizedOperation {
pub findings: Vec<NormalizedFinding>,
}
/// A supported OpenAPI/Swagger authentication scheme, without credential values.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NormalizedSecurityScheme {
pub name: String,
pub kind: NormalizedSecuritySchemeKind,
pub source_location: SourceLocation,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum NormalizedSecuritySchemeKind {
Http {
scheme: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
format_hint: Option<String>,
},
ApiKey {
location: SecurityParameterLocation,
parameter_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
format_hint: Option<String>,
},
Unsupported {
#[serde(default, skip_serializing_if = "Option::is_none")]
scheme_type: Option<String>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SecurityParameterLocation {
Header,
Query,
Cookie,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct NormalizedSecurityRequirements {
pub source: SecurityRequirementsSource,
/// Each item is one OR alternative; names within an alternative are ANDed.
#[serde(default)]
pub alternatives: Vec<SecurityRequirementAlternative>,
}
impl NormalizedSecurityRequirements {
fn is_empty(&self) -> bool {
self.source == SecurityRequirementsSource::None && self.alternatives.is_empty()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum SecurityRequirementsSource {
#[default]
None,
Document,
Operation,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct SecurityRequirementAlternative {
#[serde(default)]
pub schemes: Vec<String>,
#[serde(default = "default_supported_security_alternative")]
pub supported: bool,
#[serde(default)]
pub findings: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_location: Option<SourceLocation>,
}
fn default_supported_security_alternative() -> bool {
true
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NormalizedRequestBodyVariant {
pub content_type: String,
@@ -387,6 +471,8 @@ pub struct NormalizedIr {
#[serde(default)]
pub reference_graph: ResolvedReferenceGraph,
pub source: ImportSourcePreview,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub security_schemes: BTreeMap<String, NormalizedSecurityScheme>,
#[serde(default)]
pub operations: Vec<NormalizedOperation>,
#[serde(default)]
@@ -442,6 +528,13 @@ pub struct ImportOperationCandidate {
pub key: String,
pub method: HttpMethod,
pub path: String,
#[serde(
default,
skip_serializing_if = "NormalizedSecurityRequirements::is_empty"
)]
pub security_requirements: NormalizedSecurityRequirements,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub security_schemes: BTreeMap<String, NormalizedSecurityScheme>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub operation_id: Option<String>,
pub suggested_name: String,
@@ -477,6 +570,8 @@ pub struct RestImportDocument {
pub version: Option<String>,
pub title: String,
pub servers: Vec<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub security_schemes: BTreeMap<String, NormalizedSecurityScheme>,
pub operations: Vec<RestImportOperation>,
pub findings: Vec<ImportFinding>,
#[serde(skip)]
@@ -488,6 +583,11 @@ pub struct RestImportOperation {
pub key: String,
pub method: HttpMethod,
pub path: String,
#[serde(
default,
skip_serializing_if = "NormalizedSecurityRequirements::is_empty"
)]
pub security_requirements: NormalizedSecurityRequirements,
pub operation_id: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
+59 -87
View File
@@ -6,15 +6,13 @@ use thiserror::Error;
use crate::rest::{
model::{
CoverageDisposition, CoverageEntry, ExternalDocumentSnapshot, ImportFinding,
ImportFindingSeverity, ImportGroupPreview, ImportPreview, ImportSourcePreview,
NORMALIZER_VERSION, NormalizationConfig, NormalizedApiMetadata, NormalizedFinding,
NormalizedIr, NormalizedOperation, NormalizedParameter, NormalizedRequestBodyVariant,
ImportFindingSeverity, ImportPreview, ImportSourcePreview, NORMALIZER_VERSION,
NormalizationConfig, NormalizedApiMetadata, NormalizedFinding, NormalizedIr,
NormalizedOperation, NormalizedParameter, NormalizedRequestBodyVariant,
NormalizedResponseVariant, NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION,
RestImportDocument, SourceDigest, SourceIdentity, SourceLocation, SourceSyntax,
},
openapi3,
payload::candidate_from_operation,
swagger2,
openapi3, swagger2,
};
use super::normalize_coverage;
@@ -105,6 +103,37 @@ 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);
}
let mut root = decode(document)?;
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.
@@ -135,9 +164,18 @@ pub fn normalize_verified_bundle(
}
}
let resolution = super::reference::resolve(root, digest.clone(), snapshots, config)?;
let root = resolution.root;
let mut root = resolution.root;
validate_value_limits(&root, config)?;
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)?,
Some(_) => return Err(ImportParseError::UnsupportedDocument),
@@ -146,14 +184,8 @@ pub fn normalize_verified_bundle(
}
None => return Err(ImportParseError::UnsupportedDocument),
};
super::normalize_security::append_security_requirement_findings(&mut parsed, &root);
super::metadata_findings::collect_secret_like_examples(
&root,
"",
false,
&mut parsed.findings,
&mut parsed.internal_finding_locations,
);
parsed.findings.extend(literal_findings);
parsed.internal_finding_locations.extend(literal_locations);
if parsed.operations.is_empty()
&& !parsed
@@ -175,81 +207,14 @@ pub fn normalize_verified_bundle(
}
pub fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview {
let operations = ir
.operations
.iter()
.map(super::normalize_projection::operation_from_normalized)
.collect::<Vec<_>>();
let findings = ir
.findings
.iter()
.map(|finding| ImportFinding {
code: finding.code.clone(),
severity: finding.severity.clone(),
message: finding.message.clone(),
operation_key: finding.operation_key.clone(),
})
.collect();
preview_from_operations(&ir.source, operations.iter(), findings, false)
super::normalize_projection::preview_from_ir(ir)
}
fn preview_from_document(document: RestImportDocument) -> ImportPreview {
let source = ImportSourcePreview {
format: document.format,
version: document.version,
title: document.title,
servers: document.servers,
};
preview_from_operations(&source, document.operations.iter(), document.findings, true)
super::normalize_projection::preview_from_document(document)
}
fn preview_from_operations<'a>(
source: &ImportSourcePreview,
operations: impl Iterator<Item = &'a crate::rest::model::RestImportOperation>,
mut findings: Vec<ImportFinding>,
legacy: bool,
) -> ImportPreview {
let mut groups: BTreeMap<String, ImportGroupPreview> = BTreeMap::new();
let mut used_names = BTreeSet::new();
for operation in operations {
let candidate = if legacy {
super::legacy_payload::candidate_from_operation(
operation,
&source.servers,
&mut used_names,
)
} else {
candidate_from_operation(operation, &source.servers, &mut used_names)
};
let group_title = operation
.tags
.first()
.cloned()
.unwrap_or_else(|| "Без группы".to_owned());
let group_key = crate::rest::naming::snake_name(&group_title);
groups
.entry(group_key.clone())
.or_insert_with(|| ImportGroupPreview {
key: group_key,
title: group_title,
operations: Vec::new(),
})
.operations
.push(candidate);
}
super::normalize_security::attach_operation_findings(&mut groups, &findings);
sort_findings(&mut findings);
ImportPreview {
source: source.clone(),
groups: groups.into_values().collect(),
findings,
}
}
fn decode(document: &str) -> Result<Value, ImportParseError> {
pub(super) fn decode(document: &str) -> Result<Value, ImportParseError> {
if let Ok(json) = serde_json::from_str(document) {
return Ok(json);
}
@@ -584,6 +549,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,
})
})
@@ -765,6 +732,7 @@ fn canonicalize(
unresolved_references,
reference_graph,
source,
security_schemes: document.security_schemes,
operations,
findings,
coverage,
@@ -774,7 +742,11 @@ fn canonicalize(
}
pub fn validate_normalized_ir(ir: &NormalizedIr) -> Result<(), ImportParseError> {
if ir.normalizer_version != NORMALIZER_VERSION || ir.projection_version != PROJECTION_VERSION {
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)?;
@@ -927,7 +899,7 @@ fn severity_rank(severity: &ImportFindingSeverity) -> u8 {
}
}
fn sort_findings(findings: &mut [ImportFinding]) {
pub(super) fn sort_findings(findings: &mut [ImportFinding]) {
findings.sort_by(|left, right| {
(
severity_rank(&left.severity),
@@ -4,9 +4,9 @@ use serde_json::Value;
use crate::rest::model::{
CanonicalSourceNode, CanonicalSourceValue, CoverageDisposition, CoverageEntry,
ImportSourcePreview, NORMALIZER_VERSION, NormalizedIr, NormalizedOperation, NormalizedPath,
NormalizedReference, NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION,
SourceLocation,
HISTORICAL_NORMALIZER_VERSION, HISTORICAL_PROJECTION_VERSION, ImportSourcePreview,
NORMALIZER_VERSION, NormalizedIr, NormalizedOperation, NormalizedPath, NormalizedReference,
NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION, SourceLocation,
};
use super::ImportParseError;
@@ -16,7 +16,11 @@ pub(super) use super::normalize_schema::{
};
pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseError> {
if ir.normalizer_version != NORMALIZER_VERSION || ir.projection_version != PROJECTION_VERSION {
if !((ir.normalizer_version == NORMALIZER_VERSION
&& ir.projection_version == PROJECTION_VERSION)
|| (ir.normalizer_version == HISTORICAL_NORMALIZER_VERSION
&& ir.projection_version == HISTORICAL_PROJECTION_VERSION))
{
return Err(ImportParseError::InvalidDocument);
}
let mut source_nodes = BTreeMap::new();
@@ -54,17 +54,101 @@ pub(super) fn matches_source(
&& matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::String(_))))
}
"secret_like_example" if finding.severity == ImportFindingSeverity::Warning => {
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value))
matches!(&node.value, CanonicalSourceValue::String(value)
if secret_like(pointer, value)
|| super::metadata_findings::url_has_secret(value)
|| (pointer.ends_with("/url") && !value.contains('@'))
|| value == super::metadata_findings::REDACTED_LITERAL
|| sanitized_secret_url(value))
}
"invalid_server" if is_error => invalid_server(pointer, node),
"unsupported_security_requirement" if is_error => {
matches!(&node.value, CanonicalSourceValue::Object(fields) if !fields.is_empty())
|| invalid_security_requirement(pointer, node)
}
"invalid_security_requirements" if is_error => {
!matches!(&node.value, CanonicalSourceValue::Array(_))
|| matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::Object(_))))
}
"invalid_security_requirement" if finding.severity == ImportFindingSeverity::Warning => {
!matches!(&node.value, CanonicalSourceValue::Object(_))
}
"unsupported_security_requirements" if is_error => ir
.operations
.iter()
.find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str()))
.is_some_and(|operation| {
!operation.security_requirements.alternatives.is_empty()
&& operation
.security_requirements
.alternatives
.iter()
.all(|alternative| !alternative.supported)
}),
"unsupported_security_and_requirement"
if finding.severity == ImportFindingSeverity::Warning =>
{
matches!(&node.value, CanonicalSourceValue::Object(fields) if fields.len() > 1)
}
"unsupported_security_scheme" if finding.severity == ImportFindingSeverity::Warning => {
let scheme_name = pointer
.rsplit('/')
.next()
.unwrap_or_default()
.replace("~1", "/")
.replace("~0", "~");
ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. })
|| matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::ApiKey { location: crate::rest::model::SecurityParameterLocation::Cookie, .. })
}) && matches!(&node.value, CanonicalSourceValue::Array(_))
}
"unsupported_security_scheme_definition"
if finding.severity == ImportFindingSeverity::Warning =>
{
let scheme_name = pointer
.rsplit('/')
.next()
.unwrap_or_default()
.replace("~1", "/")
.replace("~0", "~");
ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. })
}) && matches!(&node.value, CanonicalSourceValue::Object(_))
}
"unresolved_security_scheme" if finding.severity == ImportFindingSeverity::Warning => {
let scheme_name = pointer
.rsplit('/')
.next()
.unwrap_or_default()
.replace("~1", "/")
.replace("~0", "~");
!ir.security_schemes.contains_key(&scheme_name)
}
"invalid_security_scopes" if finding.severity == ImportFindingSeverity::Warning => {
!matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))))
}
"unexpected_security_scopes" if finding.severity == ImportFindingSeverity::Warning => {
let scheme_name = pointer
.rsplit('/')
.next()
.unwrap_or_default()
.replace("~1", "/")
.replace("~0", "~");
let supports_scopes = ir.security_schemes.get(&scheme_name).is_some_and(|scheme| {
matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { scheme_type: Some(kind) } if matches!(kind.as_str(), "oauth2" | "openIdConnect"))
});
!supports_scopes
&& matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty())
}
_ => false,
}
}
fn sanitized_secret_url(value: &str) -> bool {
value.contains("=[REDACTED]")
|| ((value.contains("://") || value.starts_with("//")) && !value.contains('@'))
}
fn parameter_style(
pointer: &str,
node: &CanonicalSourceNode,
@@ -1,18 +1,113 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use serde_json::Value;
use crate::rest::model::{
ImportFinding, NormalizedLiteral, NormalizedOperation, NormalizedScalarKind, NormalizedSchema,
NormalizedSchemaKind, RestImportOperation, RestImportParameter, RestRequestBodyVariant,
RestResponseVariant,
ImportFinding, ImportGroupPreview, ImportOperationCandidate, ImportPreview,
ImportSourcePreview, NormalizedLiteral, NormalizedOperation, NormalizedScalarKind,
NormalizedSchema, NormalizedSchemaKind, NormalizedSecurityScheme, RestImportDocument,
RestImportOperation, RestImportParameter, RestRequestBodyVariant, RestResponseVariant,
};
pub(super) fn preview_from_ir(ir: &crate::rest::model::NormalizedIr) -> ImportPreview {
let operations = ir
.operations
.iter()
.map(operation_from_normalized)
.collect::<Vec<_>>();
let findings = ir
.findings
.iter()
.map(|finding| ImportFinding {
code: finding.code.clone(),
severity: finding.severity.clone(),
message: finding.message.clone(),
operation_key: finding.operation_key.clone(),
})
.collect();
preview_from_operations(
&ir.source,
operations.iter(),
findings,
&ir.security_schemes,
false,
)
}
pub(super) fn preview_from_document(document: RestImportDocument) -> ImportPreview {
let security_schemes = document.security_schemes.clone();
let source = ImportSourcePreview {
format: document.format,
version: document.version,
title: document.title,
servers: document.servers,
};
preview_from_operations(
&source,
document.operations.iter(),
document.findings,
&security_schemes,
true,
)
}
fn preview_from_operations<'a>(
source: &ImportSourcePreview,
operations: impl Iterator<Item = &'a RestImportOperation>,
mut findings: Vec<ImportFinding>,
security_schemes: &BTreeMap<String, NormalizedSecurityScheme>,
legacy: bool,
) -> ImportPreview {
let mut groups: BTreeMap<String, ImportGroupPreview> = BTreeMap::new();
let mut used_names = BTreeSet::new();
for operation in operations {
let candidate = if legacy {
super::legacy_payload::candidate_from_operation(
operation,
&source.servers,
&mut used_names,
)
} else {
crate::rest::payload::candidate_from_operation(
operation,
security_schemes,
&source.servers,
&mut used_names,
)
};
let group_title = operation
.tags
.first()
.cloned()
.unwrap_or_else(|| "Без группы".to_owned());
let group_key = crate::rest::naming::snake_name(&group_title);
groups
.entry(group_key.clone())
.or_insert_with(|| ImportGroupPreview {
key: group_key,
title: group_title,
operations: Vec::<ImportOperationCandidate>::new(),
})
.operations
.push(candidate);
}
super::normalize_security::attach_operation_findings(&mut groups, &findings);
super::normalize::sort_findings(&mut findings);
ImportPreview {
source: source.clone(),
groups: groups.into_values().collect(),
findings,
}
}
pub(super) fn operation_from_normalized(operation: &NormalizedOperation) -> RestImportOperation {
RestImportOperation {
key: operation.key.clone(),
method: operation.method,
path: operation.path.clone(),
security_requirements: operation.security_requirements.clone(),
operation_id: operation.operation_id.clone(),
summary: operation.summary.clone(),
description: operation.description.clone(),
@@ -1,13 +1,6 @@
//! Effective per-operation security scopes for the OpenAPI importer.
use serde_json::Value;
//! Attaches document-level findings to their affected operation candidates.
use super::{
model::{
ImportFinding, ImportFindingSeverity, ImportGroupPreview, RestImportDocument,
RestImportOperation, SourceLocation,
},
normalize_coverage,
};
use super::model::{ImportFinding, ImportFindingSeverity, ImportGroupPreview};
pub(super) fn attach_operation_findings(
groups: &mut std::collections::BTreeMap<String, ImportGroupPreview>,
@@ -32,135 +25,3 @@ pub(super) fn attach_operation_findings(
}
}
}
/// Computes the effective security requirements for every candidate operation
/// and attaches source-located blockers to precisely the affected candidates.
///
/// Effective requirements follow the OpenAPI semantics: an operation that
/// declares its own `security` member wins over the document root; an empty
/// array (`security: []`) explicitly disables inheritance. Within a
/// requirement array the items are alternatives (OR): an empty requirement
/// object `{}` is the public alternative and makes the whole operation
/// public, so no blocker is produced for that operation.
///
/// A malformed security shape — a non-array `security` value or a non-object
/// array item — must never be discarded silently: the affected operation
/// receives one blocker with the exact source location. A supported non-empty
/// requirement remains a blocker until an administrator binds a compatible
/// Auth Profile.
pub(super) fn append_security_requirement_findings(parsed: &mut RestImportDocument, root: &Value) {
let operations = &parsed.operations;
let findings = &mut parsed.findings;
let locations = &mut parsed.internal_finding_locations;
let root_security = root.get("security");
for operation in operations {
let Some(path_item) = root
.pointer(&format!(
"/paths/{}",
normalize_coverage::escape_pointer(&operation.path)
))
.and_then(|item| item.get(method_as_lower(operation)))
else {
continue;
};
let Some(security) = path_item
.get("security")
.or_else(|| root_security.filter(|_| path_item.get("security").is_none()))
else {
continue;
};
let base_pointer = if path_item.get("security").is_some() {
format!(
"/paths/{}/{}/security",
normalize_coverage::escape_pointer(&operation.path),
method_as_lower(operation)
)
} else {
"/security".to_owned()
};
let alternatives = match security.as_array() {
Some(alternatives) => alternatives,
None => {
push_security_blocker(
findings,
locations,
operation,
&base_pointer,
"security имеет некорректный формат: ожидается массив требований; операции с незапакованным блоком безопасности не публикуются.",
);
continue;
}
};
// Validate the item shapes first: only a fully well-formed array may
// use the empty requirement object `{}` as its public OR alternative.
if !alternatives
.iter()
.all(|requirement| requirement.as_object().is_some())
{
for (index, requirement) in alternatives.iter().enumerate() {
let pointer = format!("{base_pointer}/{index}");
match requirement.as_object() {
Some(schemes) if schemes.is_empty() => {
// In a malformed array the empty alternative does
// not make the operation public, but it also cannot
// block on its own.
continue;
}
Some(_) => push_security_blocker(
findings,
locations,
operation,
&pointer,
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
),
None => push_security_blocker(
findings,
locations,
operation,
&pointer,
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется.",
),
}
}
continue;
}
if alternatives.iter().any(|requirement| {
requirement
.as_object()
.is_some_and(serde_json::Map::is_empty)
}) {
continue;
}
for (index, _) in alternatives.iter().enumerate() {
push_security_blocker(
findings,
locations,
operation,
&format!("{base_pointer}/{index}"),
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
);
}
}
}
fn push_security_blocker(
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
operation: &RestImportOperation,
pointer: &str,
message: &str,
) {
findings.push(ImportFinding {
code: "unsupported_security_requirement".to_owned(),
severity: ImportFindingSeverity::Error,
message: message.to_owned(),
operation_key: Some(operation.key.clone()),
});
locations.push(Some(SourceLocation {
pointer: pointer.to_owned(),
}));
}
fn method_as_lower(operation: &RestImportOperation) -> String {
format!("{:?}", operation.method).to_ascii_lowercase()
}
+34 -40
View File
@@ -1,6 +1,7 @@
use crank_core::HttpMethod;
use serde_json::Value;
use super::parser_findings::append_unused_security_scheme_findings as append_unused;
use crate::rest::{
model::{
ImportFinding, RestImportDocument, RestImportOperation, RestImportParameter,
@@ -15,6 +16,7 @@ pub fn parse_document(root: &Value) -> Result<RestImportDocument, ImportParseErr
}
fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseError> {
let security_schemes = super::security::schemes(root, false);
let version = root
.get("openapi")
.and_then(Value::as_str)
@@ -25,20 +27,16 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
.unwrap_or("Imported API")
.to_owned();
let mut findings = Vec::new();
let mut internal_finding_locations = Vec::new();
let mut locations = Vec::new();
let servers = parse_servers(root.get("servers"), "/servers");
append_findings_with_locations(
&mut findings,
&mut internal_finding_locations,
servers.findings,
);
super::parser_findings::append_located(&mut findings, &mut locations, servers.findings);
let servers = servers.servers;
if servers.is_empty() {
findings.push(document_finding(
"missing_servers",
"В документе не указаны servers, base URL нужно будет выбрать вручную.",
));
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
locations.push(Some(crate::rest::model::SourceLocation {
pointer: String::new(),
}));
} else if servers.len() > 1 {
@@ -46,7 +44,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
"multiple_servers",
"В документе несколько servers, при импорте нужно выбрать нужный base URL.",
));
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
locations.push(Some(crate::rest::model::SourceLocation {
pointer: "/servers".to_owned(),
}));
}
@@ -60,7 +58,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
for (path, path_item) in paths {
if !path_item.is_object() {
findings.push(super::http_methods::invalid_path_item());
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
locations.push(Some(crate::rest::model::SourceLocation {
pointer: format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")),
}));
continue;
@@ -71,7 +69,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
"unsupported_http_method",
format!("Метод {method_name} для пути {path} пока не поддерживается."),
));
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
locations.push(Some(crate::rest::model::SourceLocation {
pointer: format!(
"/paths/{}/{}",
path.replace('~', "~0").replace('/', "~1"),
@@ -85,17 +83,17 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
append_findings_with_locations(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
&mut locations,
path_parameters.findings,
);
let path_parameters = path_parameters.parameters;
let path_servers =
parse_servers(path_item.get("servers"), &format!("{path_pointer}/servers"));
append_findings_with_locations(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
&mut locations,
path_servers.findings,
);
let path_servers = path_servers.servers;
@@ -108,7 +106,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
"invalid_operation",
"Операция имеет неверную структуру и не была интерпретирована.",
));
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
locations.push(Some(crate::rest::model::SourceLocation {
pointer: format!(
"/paths/{}/{}",
path.replace('~', "~0").replace('/', "~1"),
@@ -127,9 +125,9 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
operation_value.get("parameters"),
&format!("{path_pointer}/{method_name}/parameters"),
);
append_findings_with_locations(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
&mut locations,
parsed_parameters.findings,
);
operation_parameters.extend(parsed_parameters.parameters);
@@ -138,9 +136,9 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
operation_value.get("servers"),
&format!("{operation_pointer}/servers"),
);
append_findings_with_locations(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
&mut locations,
operation_servers.findings,
);
let operation_servers = operation_servers.servers;
@@ -148,20 +146,23 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
operation_value.get("tags"),
&format!("{operation_pointer}/tags"),
);
append_findings_with_locations(
&mut findings,
&mut internal_finding_locations,
tags.findings,
);
super::parser_findings::append_located(&mut findings, &mut locations, tags.findings);
let mut located_operation_findings =
super::openapi3_findings::operation(operation_value, &operation_pointer);
let (security_requirements, security_findings) =
super::security::collect_operation_requirements(
root,
operation_value,
&operation_pointer,
&security_schemes,
);
located_operation_findings.extend(security_findings);
located_operation_findings.extend(super::openapi3_findings::path_parameters(
path,
&operation_parameters,
));
let (operation_findings, internal_finding_locations) =
located_operation_findings.into_iter().unzip();
let (operation_findings, locations) = located_operation_findings.into_iter().unzip();
let response_schema = (method != HttpMethod::Head)
.then(|| response_schema(operation_value, &operation_pointer))
.flatten();
@@ -169,6 +170,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
security_requirements,
operation_id: operation_value
.get("operationId")
.and_then(Value::as_str)
@@ -200,19 +202,20 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
operation_servers
},
findings: operation_findings,
internal_finding_locations,
internal_finding_locations: locations,
});
}
}
append_unused(root, &security_schemes, &mut findings, &mut locations);
Ok(RestImportDocument {
format: "openapi".to_owned(),
version,
title,
servers,
security_schemes,
operations,
findings,
internal_finding_locations,
internal_finding_locations: locations,
})
}
@@ -277,6 +280,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
security_requirements: Default::default(),
operation_id: operation
.get("operationId")
.and_then(Value::as_str)
@@ -308,6 +312,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
version,
title,
servers,
security_schemes: Default::default(),
operations,
findings,
internal_finding_locations: Vec::new(),
@@ -700,17 +705,6 @@ fn parameter_blocker(
)
}
fn append_findings_with_locations(
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<crate::rest::model::SourceLocation>>,
parameter_findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>,
) {
for (finding, location) in parameter_findings {
findings.push(finding);
locations.push(Some(location));
}
}
fn request_body_schema(
operation: &Value,
operation_pointer: &str,
@@ -0,0 +1,28 @@
use crate::rest::model::NormalizedSecurityScheme;
use crate::rest::model::{ImportFinding, SourceLocation};
use serde_json::Value;
use std::collections::BTreeMap;
pub(super) fn append_located(
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
entries: Vec<(ImportFinding, SourceLocation)>,
) {
for (finding, location) in entries {
findings.push(finding);
locations.push(Some(location));
}
}
pub(super) fn append_unused_security_scheme_findings(
root: &Value,
schemes: &BTreeMap<String, NormalizedSecurityScheme>,
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<SourceLocation>>,
) {
append_located(
findings,
locations,
super::security::unused_unsupported_scheme_findings(root, schemes),
);
}
+3
View File
@@ -21,6 +21,7 @@ use crate::rest::{
pub fn candidate_from_operation(
operation: &RestImportOperation,
security_schemes: &BTreeMap<String, crate::rest::model::NormalizedSecurityScheme>,
document_servers: &[String],
used_names: &mut BTreeSet<String>,
) -> ImportOperationCandidate {
@@ -154,6 +155,8 @@ pub fn candidate_from_operation(
key: operation.key.clone(),
method: operation.method,
path: operation.path.clone(),
security_requirements: operation.security_requirements.clone(),
security_schemes: security_schemes.clone(),
operation_id: operation.operation_id.clone(),
suggested_name: name,
suggested_display_name: display_name,
+462
View File
@@ -0,0 +1,462 @@
//! Typed security scheme extraction and effective requirement
//! classification for OpenAPI 3.x and Swagger 2.0 documents.
//!
//! The module is pure: it never reads Auth Profiles or Secrets and never
//! captures credential values. Requirements are classified alternative by
//! alternative (OR semantics preserved): a supported single-scheme
//! alternative stays resolvable via a compatible Auth Profile; malformed
//! requirements, unresolvable schemes and AND combinations mark the
//! affected alternative as unsupported; an operation is blocked only when
//! none of its alternatives is selectable.
use std::collections::BTreeMap;
use serde_json::Value;
use super::model::{
ImportFinding, NormalizedSecurityRequirements, NormalizedSecurityScheme,
NormalizedSecuritySchemeKind, SecurityParameterLocation, SecurityRequirementAlternative,
SecurityRequirementsSource, SourceLocation,
};
use super::recommendations::{document_blocker, document_finding};
/// Extracts typed schema metadata from `components.securitySchemes`
/// (OpenAPI) or `securityDefinitions` (Swagger 2.0). Only scheme names,
/// kinds, locations and safe format hints are preserved; credential values
/// are never captured.
pub(super) fn schemes(root: &Value, swagger: bool) -> BTreeMap<String, NormalizedSecurityScheme> {
let (container, prefix) = if swagger {
(root.get("securityDefinitions"), "/securityDefinitions")
} else {
(
root.pointer("/components/securitySchemes"),
"/components/securitySchemes",
)
};
let Some(definitions) = container.and_then(Value::as_object) else {
return BTreeMap::new();
};
definitions
.iter()
.map(|(name, definition)| {
let location = SourceLocation {
pointer: format!(
"{prefix}/{}",
super::normalize_coverage::escape_pointer(name)
),
};
let kind = if swagger {
match definition.get("type").and_then(Value::as_str) {
Some("basic") => NormalizedSecuritySchemeKind::Http {
scheme: "basic".to_owned(),
format_hint: None,
},
Some("apiKey") => api_key(definition, false),
other => NormalizedSecuritySchemeKind::Unsupported {
scheme_type: other.map(ToOwned::to_owned),
},
}
} else {
match definition.get("type").and_then(Value::as_str) {
Some("http") => {
let scheme = definition
.get("scheme")
.and_then(Value::as_str)
.unwrap_or("")
.to_owned();
if matches!(scheme.to_ascii_lowercase().as_str(), "basic" | "bearer") {
NormalizedSecuritySchemeKind::Http {
scheme,
format_hint: definition
.get("bearerFormat")
.and_then(Value::as_str)
.filter(|value| {
safe_format_hint(value) && !looks_secret_hint(value)
})
.map(ToOwned::to_owned),
}
} else {
NormalizedSecuritySchemeKind::Unsupported {
scheme_type: Some(format!("http:{}", scheme)),
}
}
}
Some("apiKey") => api_key(definition, true),
other => NormalizedSecuritySchemeKind::Unsupported {
scheme_type: other.map(ToOwned::to_owned),
},
}
};
(
name.clone(),
NormalizedSecurityScheme {
name: name.clone(),
kind,
source_location: location,
},
)
})
.collect()
}
/// Reports unsupported scheme definitions that are never used by a security
/// requirement. They still need manual configuration, but cannot affect an
/// operation that does not select them.
pub(super) fn unused_unsupported_scheme_findings(
root: &Value,
schemes: &BTreeMap<String, NormalizedSecurityScheme>,
) -> Vec<(ImportFinding, SourceLocation)> {
let mut used = std::collections::BTreeSet::new();
collect_requirement_names(root.get("security"), &mut used);
if let Some(paths) = root.get("paths").and_then(Value::as_object) {
for path_item in paths.values().filter_map(Value::as_object) {
for operation in path_item.values().filter_map(Value::as_object) {
collect_requirement_names(operation.get("security"), &mut used);
}
}
}
schemes
.iter()
.filter_map(|(name, scheme)| {
if used.contains(name)
|| !matches!(
&scheme.kind,
NormalizedSecuritySchemeKind::Unsupported { .. }
)
{
return None;
}
Some((
document_finding(
"unsupported_security_scheme_definition",
format!(
"Объявленная схема {name} пока не поддерживается и не используется операциями; настройте authentication вручную, если она потребуется."
),
),
scheme.source_location.clone(),
))
})
.collect()
}
fn collect_requirement_names(value: Option<&Value>, used: &mut std::collections::BTreeSet<String>) {
let Some(alternatives) = value.and_then(Value::as_array) else {
return;
};
for alternative in alternatives.iter().filter_map(Value::as_object) {
used.extend(alternative.keys().cloned());
}
}
fn api_key(definition: &Value, allow_cookie: bool) -> NormalizedSecuritySchemeKind {
let location = match definition.get("in").and_then(Value::as_str) {
Some("header") => Some(SecurityParameterLocation::Header),
Some("query") => Some(SecurityParameterLocation::Query),
Some("cookie") if allow_cookie => Some(SecurityParameterLocation::Cookie),
_ => None,
};
match (location, definition.get("name").and_then(Value::as_str)) {
(Some(location), Some(parameter_name)) if !parameter_name.is_empty() => {
NormalizedSecuritySchemeKind::ApiKey {
location,
parameter_name: parameter_name.to_owned(),
format_hint: definition
.get("x-format-hint")
.and_then(Value::as_str)
.filter(|value| safe_format_hint(value) && !looks_secret_hint(value))
.map(ToOwned::to_owned),
}
}
_ => NormalizedSecuritySchemeKind::Unsupported {
scheme_type: Some("apiKey".to_owned()),
},
}
}
fn safe_format_hint(value: &str) -> bool {
!value.is_empty()
&& value.len() <= 64
&& value
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'+'))
}
fn looks_secret_hint(value: &str) -> bool {
let value = value.to_ascii_lowercase();
["token", "secret", "password", "credential", "authorization"]
.iter()
.any(|marker| value.contains(marker))
|| value.starts_with("sk-")
|| value.starts_with("bearer ")
}
/// Computes the effective requirement for one operation: the operation
/// `security` member wins over the document root; a missing member means no
/// requirement. Every array item is one OR alternative and is classified
/// independently, so a single unsupported alternative never collapses
/// supported siblings into a false AND.
pub(super) fn effective_requirements(
root: &Value,
operation: &Value,
operation_pointer: &str,
schemes: &BTreeMap<String, NormalizedSecurityScheme>,
) -> (
NormalizedSecurityRequirements,
Vec<(super::model::ImportFinding, SourceLocation)>,
) {
let (value, source, pointer) = if let Some(value) = operation.get("security") {
(
Some(value),
SecurityRequirementsSource::Operation,
format!("{operation_pointer}/security"),
)
} else if let Some(value) = root.get("security") {
(
Some(value),
SecurityRequirementsSource::Document,
"/security".to_owned(),
)
} else {
return (
NormalizedSecurityRequirements {
source: SecurityRequirementsSource::None,
alternatives: Vec::new(),
},
Vec::new(),
);
};
let mut findings = Vec::new();
let Some(alternatives) = value.and_then(Value::as_array) else {
findings.push((
document_blocker(
"invalid_security_requirements",
"Security requirements имеют неверную структуру и блокируют импорт операции.",
),
SourceLocation { pointer },
));
return (
NormalizedSecurityRequirements {
source,
alternatives: Vec::new(),
},
findings,
);
};
let mut projected = Vec::new();
let mut supported_alternatives = 0usize;
let mut has_malformed_alternative = false;
for (alternative_index, alternative) in alternatives.iter().enumerate() {
let alternative_pointer = format!("{pointer}/{alternative_index}");
let Some(requirement) = alternative.as_object() else {
has_malformed_alternative = true;
findings.push((
document_finding(
"invalid_security_requirement",
"Security alternative имеет неверную структуру и недоступна для выбора.",
),
SourceLocation {
pointer: alternative_pointer.clone(),
},
));
projected.push(SecurityRequirementAlternative {
schemes: Vec::new(),
supported: false,
findings: vec!["invalid_security_requirement".to_owned()],
source_location: Some(SourceLocation {
pointer: alternative_pointer,
}),
});
continue;
};
let mut names = requirement.keys().cloned().collect::<Vec<_>>();
names.sort();
let mut alternative_findings = Vec::new();
for name in &names {
let scopes = requirement.get(name).and_then(Value::as_array);
let valid_scope_array =
scopes.is_some_and(|scopes| scopes.iter().all(Value::is_string));
if !valid_scope_array {
alternative_findings.push("invalid_security_scopes".to_owned());
findings.push((
document_finding(
"invalid_security_scopes",
"Security scopes должны быть массивом строк; эта альтернатива недоступна для выбора.",
),
SourceLocation {
pointer: format!(
"{alternative_pointer}/{}",
super::normalize_coverage::escape_pointer(name)
),
},
));
} else if scopes.is_some_and(|scopes| !scopes.is_empty())
&& !schemes.get(name).is_some_and(supports_named_scopes)
{
alternative_findings.push("unexpected_security_scopes".to_owned());
findings.push((
document_finding(
"unexpected_security_scopes",
format!("Схема {name} не поддерживает OAuth scopes; непустой список блокирует эту alternative."),
),
SourceLocation {
pointer: format!(
"{alternative_pointer}/{}",
super::normalize_coverage::escape_pointer(name)
),
},
));
}
if !schemes.contains_key(name) {
alternative_findings.push("unresolved_security_scheme".to_owned());
findings.push((
document_finding(
"unresolved_security_scheme",
format!(
"Альтернатива требует неизвестную схему {name} и недоступна для выбора."
),
),
SourceLocation {
pointer: format!(
"{alternative_pointer}/{}",
super::normalize_coverage::escape_pointer(name)
),
},
));
}
}
if names.len() > 1 {
alternative_findings.push("unsupported_security_and_requirement".to_owned());
findings.push((
document_finding(
"unsupported_security_and_requirement",
format!("Альтернатива требует AND-сочетание схем {}; текущий импорт поддерживает только одну Auth Profile и не сохраняет это сочетание.", names.join(", ")),
),
SourceLocation {
pointer: alternative_pointer.clone(),
},
));
}
if let Some((unsupported, message)) = names.iter().find_map(|name| {
schemes.get(name).and_then(|scheme| match &scheme.kind {
NormalizedSecuritySchemeKind::Unsupported { .. } => Some((
name,
format!("Альтернатива использует неподдерживаемую схему {name} и недоступна для выбора."),
)),
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Cookie,
..
} => Some((
name,
format!("Схема {name} передаёт ключ через cookie, чего текущий Auth Profile runtime не поддерживает; настройте authentication вручную."),
)),
_ => None,
})
}) {
alternative_findings.push("unsupported_security_scheme".to_owned());
findings.push((
document_finding("unsupported_security_scheme", message),
SourceLocation {
pointer: format!(
"{alternative_pointer}/{}",
super::normalize_coverage::escape_pointer(unsupported)
),
},
));
}
alternative_findings.sort();
alternative_findings.dedup();
let supported = alternative_findings.is_empty();
supported_alternatives += usize::from(supported);
projected.push(SecurityRequirementAlternative {
schemes: names,
supported,
findings: alternative_findings,
source_location: Some(SourceLocation {
pointer: alternative_pointer,
}),
});
}
if has_malformed_alternative || (!projected.is_empty() && supported_alternatives == 0) {
findings.push((
document_blocker(
if has_malformed_alternative {
"invalid_security_requirements"
} else {
"unsupported_security_requirements"
},
if has_malformed_alternative {
"Security requirements содержат malformed alternative и блокируют импорт операции."
} else {
"Ни одну authentication alternative нельзя безопасно выбрать; импорт операции заблокирован."
},
),
SourceLocation { pointer },
));
}
(
NormalizedSecurityRequirements {
source,
alternatives: projected,
},
findings,
)
}
/// Convenience wrapper keeping the parser call site short. The operation
/// value must be the method object (`paths.{path}.{method}`); pointers use
/// the same JSON-pointer escaping as the rest of the importer.
pub(super) fn collect_operation_requirements(
root: &Value,
operation: &Value,
operation_pointer: &str,
schemes: &BTreeMap<String, NormalizedSecurityScheme>,
) -> (
NormalizedSecurityRequirements,
Vec<(super::model::ImportFinding, SourceLocation)>,
) {
effective_requirements(root, operation, operation_pointer, schemes)
}
fn supports_named_scopes(scheme: &NormalizedSecurityScheme) -> bool {
matches!(
&scheme.kind,
NormalizedSecuritySchemeKind::Unsupported {
scheme_type: Some(scheme_type)
} if matches!(scheme_type.as_str(), "oauth2" | "openIdConnect")
)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn empty_api_key_parameter_name_is_unsupported() {
for swagger in [false, true] {
let definition = json!({ "type": "apiKey", "in": "header", "name": "" });
let root = if swagger {
json!({ "securityDefinitions": { "Key": definition } })
} else {
json!({ "components": { "securitySchemes": { "Key": definition } } })
};
let schemes = schemes(&root, swagger);
assert!(matches!(
schemes["Key"].kind,
NormalizedSecuritySchemeKind::Unsupported { .. }
));
let (requirements, findings) = effective_requirements(
&root,
&json!({ "security": [{ "Key": [] }] }),
"/paths/~1secure/get",
&schemes,
);
assert!(!requirements.alternatives[0].supported);
assert!(
findings
.iter()
.any(|(finding, _)| { finding.code == "unsupported_security_requirements" })
);
}
}
}
@@ -0,0 +1,82 @@
use crate::rest::model::{ExternalDocumentSnapshot, SourceDigest};
use super::model::NormalizationConfig;
use super::{
metadata_findings,
normalize::{ImportParseError, decode, validate_value_limits},
normalize_limits, reference,
};
/// Rejects a source bundle containing credential-like literals before any
/// primary or dependency bytes are persisted. Credential names are collected
/// across all raw documents so split `$ref` targets receive the same checks.
pub fn validate_document_bundle_source_safe(
documents: &[&str],
config: &NormalizationConfig,
) -> Result<(), ImportParseError> {
config
.validate_versions()
.map_err(|_| ImportParseError::UnsupportedDocument)?;
let mut roots = Vec::with_capacity(documents.len());
for document in documents {
if document.len() > config.max_bytes
|| normalize_limits::alias_count(document) > config.max_aliases
{
return Err(ImportParseError::LimitExceeded);
}
let root = decode(document)?;
validate_value_limits(&root, config)?;
roots.push(root);
}
let declared_names = metadata_findings::credential_names(roots.iter());
for mut root in roots {
let mut findings = Vec::new();
let mut locations = Vec::new();
metadata_findings::redact_secret_like_literals_with_names(
&mut root,
"",
&declared_names,
&mut findings,
&mut locations,
);
if !findings.is_empty() {
return Err(ImportParseError::InvalidDocument);
}
}
Ok(())
}
/// Validates the resolved source tree with the same security-aware traversal
/// used by normalization, before a caller persists any bytes in the bundle.
pub fn validate_resolved_bundle_source_safe(
document: &str,
digest: SourceDigest,
snapshots: &[ExternalDocumentSnapshot],
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);
}
let root = decode(document)?;
validate_value_limits(&root, config)?;
for snapshot in snapshots {
if snapshot.document.len() > config.max_external_document_bytes {
return Err(ImportParseError::LimitExceeded);
}
}
let resolution = reference::resolve(root, digest, snapshots, config)?;
let mut root = resolution.root;
validate_value_limits(&root, config)?;
let mut findings = Vec::new();
let mut locations = Vec::new();
metadata_findings::redact_secret_like_literals(&mut root, "", &mut findings, &mut locations);
if !findings.is_empty() {
return Err(ImportParseError::InvalidDocument);
}
Ok(())
}
+22 -14
View File
@@ -15,6 +15,7 @@ pub fn parse_document(root: &Value) -> Result<RestImportDocument, ImportParseErr
}
fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseError> {
let security_schemes = super::security::schemes(root, true);
let title = root
.pointer("/info/title")
.and_then(Value::as_str)
@@ -23,7 +24,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
let servers = swagger_servers(root);
let mut findings = Vec::new();
let mut internal_finding_locations = Vec::new();
append_parameter_findings(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
super::swagger2_findings::servers(root),
@@ -73,7 +74,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
append_parameter_findings(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
path_parameters.findings,
@@ -113,7 +114,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
operation_value.get("tags"),
&format!("{path_pointer}/{method_name}/tags"),
);
append_parameter_findings(
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
tags.findings,
@@ -146,6 +147,14 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
let operation_pointer = format!("{path_pointer}/{method_name}");
let mut located_operation_findings =
super::swagger2_findings::operation(root, operation_value, &operation_pointer);
let (security_requirements, security_findings) =
super::security::collect_operation_requirements(
root,
operation_value,
&operation_pointer,
&security_schemes,
);
located_operation_findings.extend(security_findings);
located_operation_findings.extend(parsed_parameter_findings);
located_operation_findings.extend(super::swagger2_findings::path_parameters(
path,
@@ -160,6 +169,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
security_requirements,
operation_id: operation_value
.get("operationId")
.and_then(Value::as_str)
@@ -194,11 +204,18 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
}
}
super::parser_findings::append_located(
&mut findings,
&mut internal_finding_locations,
super::security::unused_unsupported_scheme_findings(root, &security_schemes),
);
Ok(RestImportDocument {
format: "swagger".to_owned(),
version: Some("2.0".to_owned()),
title,
servers,
security_schemes,
operations,
findings,
internal_finding_locations,
@@ -264,6 +281,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
security_requirements: Default::default(),
operation_id: operation
.get("operationId")
.and_then(Value::as_str)
@@ -295,6 +313,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
version: Some("2.0".to_owned()),
title,
servers,
security_schemes: Default::default(),
operations,
findings,
internal_finding_locations: Vec::new(),
@@ -651,17 +670,6 @@ fn parameter_blocker(
)
}
fn append_parameter_findings(
findings: &mut Vec<ImportFinding>,
locations: &mut Vec<Option<crate::rest::model::SourceLocation>>,
parameter_findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>,
) {
for (finding, location) in parameter_findings {
findings.push(finding);
locations.push(Some(location));
}
}
fn swagger_parameter_schema(_root: &Value, parameter: &Value) -> Option<Value> {
if let Some(schema) = parameter.get("schema") {
return Some(schema.clone());
@@ -0,0 +1,29 @@
{
"actionable_count": 50,
"content_digest": "a5c4d3c1da8ce9bcc820a97b3260bcef0fa2bd321493c35f03a4949541ec5462",
"content_digest_algorithm": "sha256",
"content_digest_definition": "SHA-256 of compact key-sorted JSON before the content_digest field is added.",
"corpus_version": "1.0.3",
"denominator": 50,
"evidence_identity": "089a330a8055dc50b96a4ac39ae8c364d08610171b952f1a2f2e942cfbb94c49",
"evidence_version": "1.0.0",
"exclusions": [],
"manifest_digest": "99ca69fb5ac10d637181a507d9e29d81888a9d4696a708ce6c3ac2b63eb61d57",
"matrix_contract": {
"construct_rows": 384,
"semantic_probes": "AC-1 runs a generated normalizer probe for every matrix row, including every 2xx status in all three formats."
},
"matrix_digest": "bf6419574fe27e2244ba6ace51bbc8f8be5936be9e0884fd25b253ad3c8cd9b8",
"matrix_version": "1.0.2",
"method": "real normalize_verified_document + preview_from_ir; every NormalizedIr passed validate_normalized_ir",
"raw_counts": {
"covered_source_constructs": 1837,
"documents": 63,
"findings": 0,
"representative_operations": 50,
"semantic_coverage_entries": 514,
"source_constructs": 1837
},
"silent_loss_count": 0,
"uncovered_source_constructs": []
}
+12 -43
View File
@@ -576,35 +576,13 @@ fn ac_2_manifest_covers_fifty_provenanced_specs_with_exact_expectations() {
"security-scope fixture must keep both candidates explicit: {:?}",
ir.operations.iter().map(|op| &op.key).collect::<Vec<_>>()
);
let requirements = ir
.findings
.iter()
.filter(|finding| finding.code == "unsupported_security_requirement")
.collect::<Vec<_>>();
assert_eq!(
requirements.len(),
1,
"exactly the inherited requirement must produce a blocker: {:?}",
ir.findings
);
let requirement = &requirements[0];
assert_eq!(
requirement.severity,
crank_import::rest::ImportFindingSeverity::Error,
"security requirement must block its candidate: {:?}",
requirement
);
assert_eq!(
requirement.operation_key.as_deref(),
Some("GET /secure"),
"blocker must belong only to the inherited secured candidate: {:?}",
requirement
);
assert_eq!(
requirement.location.pointer, "/security/0",
"inherited requirement must point at the document root requirement: {:?}",
requirement
);
let requirements = ir.findings.iter().filter(|finding| {
finding.code == "unsupported_security_requirements"
}).collect::<Vec<_>>();
assert!(requirements.is_empty(), "supported inherited basic requirement must remain selectable: {:?}", ir.findings);
let secured_operation = ir.operations.iter().find(|operation| operation.key == "GET /secure").unwrap();
assert!(secured_operation.security_requirements.alternatives[0].supported);
assert_eq!(secured_operation.security_requirements.alternatives[0].source_location.as_ref().unwrap().pointer, "/security/0");
let mut by_key = std::collections::BTreeMap::new();
for group in &preview.groups {
for candidate in &group.operations {
@@ -618,18 +596,9 @@ fn ac_2_manifest_covers_fifty_provenanced_specs_with_exact_expectations() {
"public operation with security: [] must stay actionable: {:?}",
public.findings
);
assert!(
by_key
.get("GET /secure")
.expect("secured candidate present")
.findings
.iter()
.any(|finding| finding.code == "unsupported_security_requirement"
&& finding.severity
== crank_import::rest::ImportFindingSeverity::Error),
"inherited requirement must block the secured candidate: {:?}",
by_key.get("GET /secure").unwrap().findings
);
assert!(by_key.get("GET /secure").expect("secured candidate present").findings.iter().all(|finding| {
finding.severity != crank_import::rest::ImportFindingSeverity::Error
}), "supported security must remain selectable before API binding: {:?}", by_key.get("GET /secure").unwrap().findings);
assert!(
preview
.findings
@@ -642,8 +611,8 @@ fn ac_2_manifest_covers_fifty_provenanced_specs_with_exact_expectations() {
ir.coverage
.iter()
.any(|entry| entry.location.pointer == "/security/0"
&& entry.disposition == CoverageDisposition::Finding),
"inherited requirement must stay in the source ledger bound to the finding: {:?}",
&& entry.disposition == CoverageDisposition::Mapped),
"inherited requirement must stay mapped in the source ledger: {:?}",
ir.coverage
);
let mut actual_operation_keys = preview
@@ -14405,16 +14405,9 @@
"GET /secure"
],
"warning_findings": [],
"blocker_findings": [
{
"code": "unsupported_security_requirement",
"severity": "Error",
"operation_key": "GET /secure",
"location": "/security/0"
}
]
"blocker_findings": []
}
}
],
"compatibility_impact": "Corpus 1.0.3 adds hostile-security-scope.json: an effective-security-scope fixture showing that a document-level requirement blocks only the inherited candidate (GET /secure, location /security/0) while the public candidate with an explicit security: [] stays actionable and no document-global blocker is produced; all 50 representative previews and the previous hostile oracles are unchanged."
"compatibility_impact": "Corpus 1.0.3 adds hostile-security-scope.json: an effective-security-scope fixture showing that a document-level basic requirement remains selectable on the inherited candidate (GET /secure, location /security/0) while the public candidate with an explicit security: [] stays public; malformed or unsupported requirements retain scoped blockers."
}
+66 -58
View File
@@ -172,53 +172,63 @@ fn ac_1_support_matrix_rows_have_exact_construct_contracts() {
"paths":{"/probe":{"get":{"security":[{"probe":[]}],"operationId":"securityProbe","responses":{"200":{"description":"ok"}}}}}
})
};
assert_eq!(
row["status"], "unsupported",
"security is not yet persisted as Auth Profile semantics"
);
let source = serde_json::to_string(&probe).unwrap();
let ir = normalize(&source).unwrap();
validate_normalized_ir(&ir).unwrap();
let expected_finding = row["expected_finding"].as_str().unwrap();
let requirement = ir
.findings
.iter()
.find(|finding| {
finding.code == expected_finding
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
})
.unwrap_or_else(|| {
panic!(
"{version} {construct} must be explicitly blocked: {:?}",
ir.findings
)
});
assert_eq!(
requirement.operation_key.as_deref(),
Some("GET /probe"),
"{version} {construct} blocker must belong to the affected candidate"
);
assert_eq!(
requirement.location.pointer, "/paths/~1probe/get/security/0",
"{version} {construct} blocker must point at the local requirement"
);
let preview = preview_from_ir(&ir);
let candidate = &preview.groups[0].operations[0];
assert!(
candidate
let requirement = &ir.operations[0].security_requirements.alternatives[0];
if expected_finding == "none" {
assert!(
requirement.supported,
"{version} {construct} is not selectable"
);
assert!(
candidate.findings.iter().all(|finding| {
finding.severity != crank_import::rest::ImportFindingSeverity::Error
}),
"{version} {construct} unexpectedly blocked: {:?}",
candidate.findings
);
assert!(ir.security_schemes.contains_key("probe"));
assert!(
ir.coverage.iter().any(|entry| {
entry.location.pointer == "/paths/~1probe/get/security/0"
&& entry.disposition == CoverageDisposition::Mapped
}),
"{version} {construct} requirement has no mapped source coverage"
);
} else {
let finding = ir
.findings
.iter()
.any(|finding| finding.code == expected_finding
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error),
"{version} {construct} candidate must be blocked: {:?}",
candidate.findings
);
assert!(
ir.coverage.iter().any(|entry| entry.location.pointer
== "/paths/~1probe/get/security/0"
&& entry.disposition == CoverageDisposition::Finding),
"{version} {construct} has no finding coverage"
);
.chain(
ir.operations
.iter()
.flat_map(|operation| operation.findings.iter()),
)
.find(|finding| {
finding.code == expected_finding
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
})
.unwrap_or_else(|| {
panic!("{version} {construct} missing blocker: {:?}", ir.findings)
});
assert_eq!(finding.operation_key.as_deref(), Some("GET /probe"));
assert_eq!(finding.location.pointer, "/paths/~1probe/get/security");
assert!(
candidate
.findings
.iter()
.any(|finding| finding.code == expected_finding
&& finding.severity
== crank_import::rest::ImportFindingSeverity::Error)
);
assert!(ir.coverage.iter().any(|entry| entry.location.pointer
== "/paths/~1probe/get/security"
&& entry.disposition == CoverageDisposition::Finding));
}
probed(&construct);
}
{
@@ -301,23 +311,21 @@ fn ac_1_support_matrix_rows_have_exact_construct_contracts() {
.unwrap()["expected_finding"]
.as_str()
.unwrap();
let requirements = ir
.findings
assert_eq!(expected_finding, "none");
let secured_operation = ir
.operations
.iter()
.filter(|finding| finding.code == expected_finding)
.collect::<Vec<_>>();
.find(|operation| operation.key == "GET /secured")
.unwrap();
assert!(secured_operation.security_requirements.alternatives[0].supported);
assert_eq!(
requirements.len(),
1,
"exactly the inherited operation must be blocked: {:?}",
requirements
secured_operation.security_requirements.alternatives[0]
.source_location
.as_ref()
.unwrap()
.pointer,
"/security/0"
);
assert_eq!(
requirements[0].operation_key.as_deref(),
Some("GET /secured"),
"blocker must belong only to the affected candidate"
);
assert_eq!(requirements[0].location.pointer, "/security/0");
let preview = preview_from_ir(&ir);
let mut by_key = std::collections::BTreeMap::new();
for group in &preview.groups {
@@ -342,17 +350,17 @@ fn ac_1_support_matrix_rows_have_exact_construct_contracts() {
secured
.findings
.iter()
.any(|finding| finding.code == expected_finding
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error),
"secured operation must be blocked: {:?}",
.all(|finding| finding.severity
!= crank_import::rest::ImportFindingSeverity::Error),
"supported secured operation must remain selectable before API binding: {:?}",
secured.findings
);
assert!(
ir.coverage
.iter()
.any(|entry| entry.location.pointer == "/security/0"
&& entry.disposition == CoverageDisposition::Finding),
"mixed probe has no finding coverage"
&& entry.disposition == CoverageDisposition::Mapped),
"mixed probe has no mapped security coverage"
);
probed(construct);
}
@@ -0,0 +1,799 @@
use crank_import::rest::{
ImportFindingSeverity, ImportOperationCandidate, NormalizationConfig,
NormalizedSecuritySchemeKind, SecurityParameterLocation, SecurityRequirementsSource,
SourceDigest, normalize_verified_document, preview_from_ir, validate_normalized_ir,
};
use serde_json::{Value, json};
fn normalize(document: &Value) -> crank_import::rest::NormalizedIr {
let text = serde_json::to_string(document).expect("fixture serializes");
normalize_verified_document(
&text,
SourceDigest::parse("b".repeat(64)).expect("digest is valid"),
&NormalizationConfig::default(),
)
.expect("security fixture normalizes")
}
fn candidate<'a>(
candidates: &'a [ImportOperationCandidate],
key: &str,
) -> &'a ImportOperationCandidate {
candidates
.iter()
.find(|candidate| candidate.key == key)
.unwrap_or_else(|| panic!("candidate {key} missing: {candidates:?}"))
}
fn candidates(ir: &crank_import::rest::NormalizedIr) -> Vec<ImportOperationCandidate> {
preview_from_ir(ir)
.groups
.into_iter()
.flat_map(|group| group.operations)
.collect()
}
fn operation<'a>(
ir: &'a crank_import::rest::NormalizedIr,
path: &str,
) -> &'a crank_import::rest::NormalizedOperation {
ir.operations
.iter()
.find(|operation| operation.path == path)
.unwrap_or_else(|| panic!("operation {path} missing: {ir:?}"))
}
#[test]
fn openapi_30_and_31_project_typed_schemes_and_safe_hints() {
for version in ["3.0.3", "3.1.0"] {
let document = json!({
"openapi": version,
"info": { "title": "Security metadata", "version": "1" },
"components": { "securitySchemes": {
"Bearer": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" },
"Basic": { "type": "http", "scheme": "basic" },
"HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" },
"QueryKey": { "type": "apiKey", "in": "query", "name": "access_token" },
"CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" },
"UnsafeHint": { "type": "http", "scheme": "bearer", "bearerFormat": "sk-private-value" }
}},
"paths": { "/private": { "get": {
"security": [{ "Bearer": [] }],
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
assert!(matches!(ir.security_schemes["Bearer"].kind,
NormalizedSecuritySchemeKind::Http { ref scheme, ref format_hint }
if scheme == "bearer" && format_hint.as_deref() == Some("JWT")));
assert!(matches!(ir.security_schemes["Basic"].kind,
NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic"));
assert!(matches!(ir.security_schemes["HeaderKey"].kind,
NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Header, ref parameter_name, .. }
if parameter_name == "X-Api-Key"));
assert!(matches!(ir.security_schemes["QueryKey"].kind,
NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. }
if parameter_name == "access_token"));
assert!(matches!(ir.security_schemes["CookieKey"].kind,
NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Cookie, ref parameter_name, .. }
if parameter_name == "session"));
assert!(matches!(
ir.security_schemes["UnsafeHint"].kind,
NormalizedSecuritySchemeKind::Http {
format_hint: None,
..
}
));
let requirement = &operation(&ir, "/private")
.security_requirements
.alternatives[0];
assert_eq!(requirement.schemes, ["Bearer"]);
assert!(requirement.supported);
}
}
#[test]
fn swagger2_projects_basic_and_api_key_header_and_query() {
let document = json!({
"swagger": "2.0",
"info": { "title": "Swagger security", "version": "1" },
"host": "security.example.test",
"schemes": ["https"],
"securityDefinitions": {
"BasicAuth": { "type": "basic" },
"HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" },
"QueryKey": { "type": "apiKey", "in": "query", "name": "api_key" },
"CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" }
},
"security": [{ "BasicAuth": [] }, { "HeaderKey": [] }, { "QueryKey": [] }],
"paths": { "/private": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
assert!(matches!(ir.security_schemes["BasicAuth"].kind,
NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic"));
assert!(matches!(
ir.security_schemes["HeaderKey"].kind,
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Header,
..
}
));
assert!(matches!(
ir.security_schemes["QueryKey"].kind,
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Query,
..
}
));
assert!(matches!(
ir.security_schemes["CookieKey"].kind,
NormalizedSecuritySchemeKind::Unsupported { .. }
));
assert_eq!(
operation(&ir, "/private")
.security_requirements
.alternatives
.len(),
3
);
}
#[test]
fn effective_requirements_inherit_override_and_allow_explicit_no_auth() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Requirements", "version": "1" },
"components": { "securitySchemes": {
"Key": { "type": "apiKey", "in": "header", "name": "X-Key" },
"Bearer": { "type": "http", "scheme": "bearer" }
}},
"security": [{ "Key": [] }],
"paths": {
"/inherited": { "get": { "responses": { "200": { "description": "ok" } } } },
"/override": { "get": { "security": [{ "Bearer": [] }], "responses": { "200": { "description": "ok" } } } },
"/public": { "get": { "security": [], "responses": { "200": { "description": "ok" } } } },
"/unsecured": { "get": { "responses": { "200": { "description": "ok" } } } }
}
});
let ir = normalize(&document);
let inherited = &operation(&ir, "/inherited").security_requirements;
assert_eq!(inherited.source, SecurityRequirementsSource::Document);
assert_eq!(inherited.alternatives[0].schemes, ["Key"]);
let overridden = &operation(&ir, "/override").security_requirements;
assert_eq!(overridden.source, SecurityRequirementsSource::Operation);
assert_eq!(overridden.alternatives[0].schemes, ["Bearer"]);
let public = &operation(&ir, "/public").security_requirements;
assert_eq!(public.source, SecurityRequirementsSource::Operation);
assert!(public.alternatives.is_empty());
assert_eq!(
operation(&ir, "/unsecured").security_requirements.source,
SecurityRequirementsSource::Document
);
}
#[test]
fn or_alternatives_are_preserved_and_unsupported_and_is_scoped() {
let document = json!({
"openapi": "3.1.0",
"info": { "title": "Alternatives", "version": "1" },
"components": { "securitySchemes": {
"Bearer": { "type": "http", "scheme": "bearer" },
"One": { "type": "apiKey", "in": "header", "name": "X-One" },
"Two": { "type": "apiKey", "in": "query", "name": "two" }
}},
"paths": {
"/mixed": { "get": { "security": [
{ "One": [], "Two": [] }, { "Bearer": [] }
], "responses": { "200": { "description": "ok" } } } },
"/and-only": { "get": { "security": [
{ "One": [], "Two": [] }
], "responses": { "200": { "description": "ok" } } } },
"/or": { "get": { "security": [
{ "Bearer": [] }, { "One": [] }, { "Two": [] }
], "responses": { "200": { "description": "ok" } } } }
}
});
let ir = normalize(&document);
let preview = preview_from_ir(&ir);
let candidates = preview
.groups
.iter()
.flat_map(|group| &group.operations)
.cloned()
.collect::<Vec<_>>();
let mixed = candidate(&candidates, "GET /mixed");
assert_eq!(mixed.security_requirements.alternatives.len(), 2);
assert!(!mixed.security_requirements.alternatives[0].supported);
assert!(
mixed.security_requirements.alternatives[0]
.findings
.contains(&"unsupported_security_and_requirement".to_owned())
);
assert_eq!(
mixed.security_requirements.alternatives[0].schemes,
["One", "Two"]
);
assert!(mixed.security_requirements.alternatives[1].supported);
assert_eq!(
mixed.security_requirements.alternatives[1].schemes,
["Bearer"]
);
assert!(
mixed
.findings
.iter()
.all(|finding| finding.severity != ImportFindingSeverity::Error)
);
let and_only = candidate(&candidates, "GET /and-only");
assert!(and_only.findings.iter().any(|finding| {
finding.code == "unsupported_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
}));
assert!(and_only.findings.iter().any(|finding| {
finding.code == "unsupported_security_and_requirement"
&& finding.severity == ImportFindingSeverity::Warning
&& finding.message.contains("One, Two")
}));
let or = candidate(&candidates, "GET /or");
assert_eq!(or.security_requirements.alternatives.len(), 3);
assert!(
or.security_requirements
.alternatives
.iter()
.all(|alternative| alternative.supported)
);
}
#[test]
fn unsupported_schemes_produce_manual_configuration_findings_without_typed_credentials() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Unsupported", "version": "1" },
"components": { "securitySchemes": {
"OAuth": { "type": "oauth2", "flows": { "clientCredentials": {
"tokenUrl": "https://issuer.example/token?token=embedded",
"scopes": { "read": "read" },
"x-client-secret": "never-copy-this"
}}, "x-example-token": "also-never-copy" },
"Oidc": { "type": "openIdConnect", "openIdConnectUrl": "https://issuer.example" },
"Mutual": { "type": "mutualTLS" },
"Unknown": { "type": "vendorAuth", "x-api-key": "vendor-secret" }
}},
"paths": { "/private": { "get": {
"security": [{ "OAuth": ["read"] }, { "Oidc": [] }, { "Mutual": [] }, { "Unknown": [] }],
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
let findings = &operation(&ir, "/private").findings;
assert!(
findings
.iter()
.any(|finding| finding.code == "unsupported_security_scheme")
);
assert!(
findings
.iter()
.any(|finding| finding.code == "unsupported_security_requirements")
);
assert!(
operation(&ir, "/private")
.security_requirements
.alternatives
.iter()
.all(|alternative| !alternative.supported)
);
let typed = serde_json::to_string(&ir.security_schemes).unwrap();
for secret_field in [
"flows",
"tokenUrl",
"x-client-secret",
"x-example-token",
"x-api-key",
] {
assert!(
!typed.contains(secret_field),
"typed metadata retained {secret_field}"
);
}
assert!(!typed.contains("never-copy-this"));
assert!(!typed.contains("vendor-secret"));
}
#[test]
fn normalized_ir_accepts_historical_v4_contract_without_security_fields() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Historical", "version": "1" },
"paths": { "/health": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let mut ir = normalize(&document);
ir.normalizer_version = crank_import::rest::HISTORICAL_NORMALIZER_VERSION.to_owned();
ir.projection_version = crank_import::rest::HISTORICAL_PROJECTION_VERSION.to_owned();
ir.security_schemes.clear();
ir.operations[0].security_schemes.clear();
ir.operations[0].security_requirements = Default::default();
validate_normalized_ir(&ir).expect("historical v4 shape remains valid");
let serialized = serde_json::to_value(&ir).unwrap();
let operation = &serialized["operations"][0];
assert!(operation.get("security_requirements").is_none());
assert!(operation.get("security_schemes").is_none());
}
#[test]
fn non_array_security_is_rejected_as_an_import_error_surface() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Malformed", "version": "1" },
"security": "Bearer",
"paths": { "/private": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
assert!(operation(&ir, "/private").findings.iter().any(|finding| {
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
&& finding.location.pointer == "/security"
}));
let candidates = candidates(&ir);
assert!(
candidate(&candidates, "GET /private")
.findings
.iter()
.any(|finding| {
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
})
);
}
#[test]
fn secret_like_literals_are_redacted_from_ir_preview_and_source_snapshot() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Redaction", "version": "1" },
"servers": [{ "url": "https://user:secret-canary@api.example.test" }],
"paths": { "/private": { "get": {
"parameters": [{
"name": "Authorization",
"in": "header",
"example": "Bearer header-canary"
}, {
"name": "X-Api-Key",
"in": "header",
"example": "opaque-canary-42",
"schema": { "type": "string" }
}, {
"name": "X-Trace",
"in": "header",
"example": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjYW5hcnktNDIifQ.c2lnbmF0dXJlMTIzNDU2",
"schema": { "type": "string" }
}],
"requestBody": { "content": { "application/json": { "schema": {
"type": "object",
"properties": {
"password": { "type": "string", "default": "default-canary" },
"token": { "type": "integer", "default": 4242 },
"passwd": { "type": "boolean", "default": true },
"ordinaryDefault": { "type": "string", "default": "Bearer default-literal-canary" },
"ordinary": { "type": "string", "example": "token-example-canary" }
}
}}}},
"parameters": [{
"name": "X-Api-Key", "in": "header",
"schema": { "type": "string", "default": "opaque-header-default-canary" }
}, {
"name": "api_key", "in": "query",
"schema": { "type": "string", "default": "opaque-query-default-canary" }
}, {
"name": "session-token", "in": "cookie",
"schema": { "type": "string", "default": "opaque-cookie-default-canary" }
}],
"responses": { "200": { "description": "ok" } },
"x-api-key": "extension-canary",
"x-ciphertext": "opaque-ciphertext-canary",
"x-private-key": "private-key-canary"
}}}
});
let ir = normalize(&document);
let serialized_ir = serde_json::to_string(&ir).unwrap();
let serialized_preview = serde_json::to_string(&preview_from_ir(&ir)).unwrap();
for output in [&serialized_ir, &serialized_preview] {
for secret in [
"secret-canary",
"header-canary",
"opaque-canary-42",
"default-canary",
"default-literal-canary",
"opaque-header-default-canary",
"opaque-query-default-canary",
"opaque-cookie-default-canary",
"token-example-canary",
"extension-canary",
"opaque-ciphertext-canary",
"private-key-canary",
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjYW5hcnktNDIifQ.c2lnbmF0dXJlMTIzNDU2",
"4242",
] {
assert!(
!output.contains(secret),
"output retained {secret}: {output}"
);
}
}
assert!(!serialized_ir.contains("\"default\":true"));
assert!(serialized_ir.contains("[REDACTED]"));
assert!(
ir.findings
.iter()
.any(|finding| finding.code == "secret_like_example")
);
}
#[test]
fn encoded_sensitive_url_query_names_are_detected_and_sanitized() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Encoded URL credentials", "version": "1" },
"servers": [{ "url": "https://api.example.test/?%61pi%5Fkey=encoded-query-canary&region=west" }],
"paths": { "/health": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&document);
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains("encoded-query-canary"));
assert!(serialized.contains("%61pi%5Fkey=[REDACTED]"));
assert!(serialized.contains("region=west"));
}
#[test]
fn declared_api_key_names_seed_parameter_and_url_redaction_context() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Declared API keys", "version": "1" },
"servers": [{ "url": "https://api.example.test/?key=query-canary&region=west" }],
"components": { "securitySchemes": {
"ClientKey": { "type": "apiKey", "in": "header", "name": "X-Client" },
"QueryKey": { "type": "apiKey", "in": "query", "name": "key" },
"CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" }
}},
"paths": { "/public": {
"get": {
"parameters": [
{ "name": "X-Client", "in": "header", "schema": { "type": "string", "default": "header-canary" }, "example": "header-example-canary" },
{ "name": "session", "in": "cookie", "schema": { "type": "string", "default": "cookie-canary" } }
],
"responses": { "200": { "description": "ok" } }
}
}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&document);
let serialized = serde_json::to_string(&ir).unwrap();
for canary in [
"query-canary",
"header-canary",
"header-example-canary",
"cookie-canary",
] {
assert!(!serialized.contains(canary), "{canary} leaked into IR");
}
assert!(serialized.contains("key=[REDACTED]"));
assert!(serialized.contains("region=west"));
let mixed_containers = json!({
"openapi": "3.0.3",
"info": { "title": "Legacy security map", "version": "1" },
"components": { "securitySchemes": {} },
"securityDefinitions": {
"QueryKey": { "type": "apiKey", "in": "query", "name": "key" }
},
"servers": [{ "url": "https://api.example.test/?key=legacy-container-canary" }],
"paths": { "/public": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&mixed_containers).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&mixed_containers);
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains("legacy-container-canary"));
assert!(serialized.contains("key=[REDACTED]"));
}
#[test]
fn credential_like_security_format_hints_are_redacted_but_jwt_is_safe() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Format hints", "version": "1" },
"components": { "securitySchemes": {
"Bearer": { "type": "http", "scheme": "bearer", "bearerFormat": "sk-private-value" },
"HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key", "x-format-hint": "token-format-canary" },
"SafeBearer": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }
}, "headers": {
"X-Api-Key": { "schema": { "type": "string", "default": "opaque-component-header-canary" } }
}},
"paths": { "/health": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&document);
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains("sk-private-value"));
assert!(!serialized.contains("token-format-canary"));
assert!(!serialized.contains("opaque-component-header-canary"));
assert!(serialized.contains("X-Api-Key"));
let source_tree = serde_json::to_value(&ir.source_tree).unwrap();
let schema = &source_tree["value"]["value"]["components"]["value"]["value"]["headers"]["value"]
["value"]["X-Api-Key"]["value"]["value"]["schema"]["value"]["value"];
assert_eq!(schema["type"]["value"]["value"], "string");
assert_eq!(schema["default"]["value"]["value"], "[REDACTED]");
assert!(matches!(
ir.security_schemes["Bearer"].kind,
NormalizedSecuritySchemeKind::Http {
format_hint: None,
..
}
));
assert!(matches!(
ir.security_schemes["HeaderKey"].kind,
NormalizedSecuritySchemeKind::ApiKey {
format_hint: None,
..
}
));
assert!(matches!(ir.security_schemes["SafeBearer"].kind,
NormalizedSecuritySchemeKind::Http { ref format_hint, .. }
if format_hint.as_deref() == Some("JWT")));
}
#[test]
fn unused_unsupported_openapi_definitions_get_located_manual_warning_without_blocking_public() {
for version in ["3.0.3", "3.1.0"] {
let document = json!({
"openapi": version,
"info": { "title": "Unused OAuth definition", "version": "1" },
"components": { "securitySchemes": {
"OAuth": { "type": "oauth2", "flows": {} }
}},
"paths": { "/public": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
let warning = ir
.findings
.iter()
.find(|finding| finding.code == "unsupported_security_scheme_definition")
.expect("unused unsupported definition needs a manual configuration warning");
assert_eq!(warning.severity, ImportFindingSeverity::Warning);
assert_eq!(
warning.location.pointer,
"/components/securitySchemes/OAuth"
);
assert!(warning.operation_key.is_none());
assert!(
candidate(&candidates(&ir), "GET /public")
.findings
.iter()
.all(|finding| finding.severity != ImportFindingSeverity::Error)
);
}
}
#[test]
fn unused_unsupported_swagger_definition_gets_located_manual_warning_without_blocking_public() {
let document = json!({
"swagger": "2.0",
"info": { "title": "Unused OAuth definition", "version": "1" },
"host": "security.example.test",
"schemes": ["https"],
"securityDefinitions": {
"OAuth": { "type": "oauth2", "flow": "application", "tokenUrl": "https://issuer.example/token", "scopes": {} }
},
"paths": { "/public": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let ir = normalize(&document);
let warning = ir
.findings
.iter()
.find(|finding| finding.code == "unsupported_security_scheme_definition")
.expect("unused unsupported definition needs a manual configuration warning");
assert_eq!(warning.severity, ImportFindingSeverity::Warning);
assert_eq!(warning.location.pointer, "/securityDefinitions/OAuth");
assert!(warning.operation_key.is_none());
assert!(
candidate(&candidates(&ir), "GET /public")
.findings
.iter()
.all(|finding| finding.severity != ImportFindingSeverity::Error)
);
}
#[test]
fn url_credentials_are_redacted_without_discarding_safe_oauth_endpoints() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "URL redaction", "version": "1" },
"components": { "securitySchemes": {
"OAuth": { "type": "oauth2", "flows": { "clientCredentials": {
"tokenUrl": "https://issuer.example/token",
"authorizationUrl": "https://issuer.example/authorize?client_id=public-client"
}} }
}},
"servers": [
{ "url": "https://bare-canary@api.example.test/v1?api_key=query-canary&region=west" },
{ "url": "//relative-userinfo-canary@api.example.test/v1" }
],
"paths": { "/health": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&document);
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains("bare-canary"));
assert!(!serialized.contains("relative-userinfo-canary"));
assert!(!serialized.contains("query-canary"));
assert!(serialized.contains("api_key=[REDACTED]"));
assert!(serialized.contains("https://issuer.example/token"));
assert!(serialized.contains("https://issuer.example/authorize?client_id=public-client"));
}
#[test]
fn sanitized_userinfo_examples_and_declared_query_urls_validate_after_redaction() {
let document = json!({
"openapi": "3.0.3",
"info": {
"title": "Sanitized URL contexts",
"version": "1",
"description": "Docs: https://api.example.test/?key=description-query-canary"
},
"components": { "securitySchemes": {
"QueryKey": { "type": "apiKey", "in": "query", "name": "key" }
}},
"paths": { "/health": { "get": {
"requestBody": { "content": { "application/json": { "schema": {
"type": "string",
"example": "https://example-user:example-password@api.example.test/path"
}}}},
"responses": { "200": { "description": "ok" }
}}}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err()
);
let ir = normalize(&document);
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains("description-query-canary"));
assert!(!serialized.contains("example-user"));
assert!(!serialized.contains("example-password"));
assert!(serialized.contains("key=[REDACTED]"));
assert!(serialized.contains("https://api.example.test/path"));
}
#[test]
fn credential_named_url_defaults_and_url_fragments_are_redacted() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Credential URLs", "version": "1" },
"components": { "schemas": {
"Config": { "type": "object", "properties": {
"credentialUrl": { "type": "string", "default": "opaque-credential-url-canary" },
"x-credential-url": "opaque-extension-url-canary",
"callback": { "type": "string", "default": "https://issuer.example/callback#access_token=fragment-canary&state=safe" },
"reference": { "type": "string", "default": "https://issuer.example/#/components/securitySchemes/AccessToken" }
}}
}},
"paths": { "/health": { "get": {
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&document).unwrap();
assert!(
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.is_err(),
"credentialUrl and URL fragment credentials must fail source preflight"
);
let serialized = serde_json::to_string(&normalize(&document)).unwrap();
assert!(!serialized.contains("opaque-credential-url-canary"));
assert!(!serialized.contains("opaque-extension-url-canary"));
assert!(!serialized.contains("fragment-canary"));
assert!(serialized.contains("[REDACTED]"));
assert!(serialized.contains("#access_token=[REDACTED]&state=safe"));
let safe_pointer = json!({
"openapi": "3.0.3",
"info": { "title": "JSON pointer", "version": "1" },
"paths": { "/health": { "get": {
"description": "#/components/securitySchemes/AccessToken",
"responses": { "200": { "description": "ok" } }
}}}
});
crank_import::rest::validate_document_source_safe(
&serde_json::to_string(&safe_pointer).unwrap(),
&NormalizationConfig::default(),
)
.expect("JSON-pointer fragments are references, not credential parameters");
}
#[test]
fn sensitive_identifiers_preserve_schemes_requirements_and_schema_metadata() {
let document = json!({
"openapi": "3.0.3",
"info": { "title": "Safe identifiers", "version": "1" },
"components": {
"securitySchemes": {
"AccessToken": { "type": "http", "scheme": "bearer" }
},
"schemas": { "Password": { "type": "string" } }
},
"security": [{ "AccessToken": [] }],
"paths": { "/login": { "post": {
"requestBody": { "content": { "application/json": { "schema": {
"type": "object", "properties": {
"password": { "type": "string", "description": "Password input" }
}
}}}},
"responses": { "200": { "description": "ok" } }
}}}
});
let source = serde_json::to_string(&document).unwrap();
crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default())
.expect("names and schema declarations contain no literal credentials");
let ir = normalize(&document);
assert!(ir.security_schemes.contains_key("AccessToken"));
assert!(operation(&ir, "/login").security_requirements.alternatives[0].supported);
let schema = operation(&ir, "/login")
.request_body_schema
.as_ref()
.unwrap();
let crank_import::rest::NormalizedSchemaKind::Object { properties, .. } = &schema.kind else {
panic!("request object schema must survive redaction");
};
assert_eq!(
properties["password"].description.as_deref(),
Some("Password input")
);
assert!(matches!(
properties["password"].kind,
crank_import::rest::NormalizedSchemaKind::Scalar { .. }
));
}
+98 -87
View File
@@ -4,7 +4,7 @@
//! - an OR array with an empty requirement object `{}` exposes the public
//! alternative and must keep the affected operation actionable, without
//! touching other operations;
//! - a single non-empty requirement must remain a blocker (regression anchor);
//! - supported non-empty requirements remain resolvable for profile binding;
//! - malformed security shapes (non-array value, non-object array item) must
//! produce a source-located operation blocker instead of silently
//! disappearing.
@@ -50,19 +50,30 @@ fn candidate<'a>(
}
fn security_findings<'a>(
findings: &'a [NormalizedFinding],
ir: &'a crank_import::rest::NormalizedIr,
key: &str,
) -> Vec<&'a NormalizedFinding> {
findings
.iter()
all_findings(ir)
.into_iter()
.filter(|finding| {
finding.code == "unsupported_security_requirement"
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
&& finding.operation_key.as_deref() == Some(key)
})
.collect()
}
fn all_findings(ir: &crank_import::rest::NormalizedIr) -> Vec<&NormalizedFinding> {
ir.findings
.iter()
.chain(
ir.operations
.iter()
.flat_map(|operation| &operation.findings),
)
.collect()
}
#[test]
fn empty_or_alternative_makes_the_operation_public() {
// Document-level OR array: [{"probe": []}, {}] offers an explicit public
@@ -91,7 +102,7 @@ fn empty_or_alternative_makes_the_operation_public() {
public.findings
);
assert!(
security_findings(&ir.findings, "GET /public").is_empty(),
security_findings(&ir, "GET /public").is_empty(),
"no security blocker may belong to the public operation: {:?}",
ir.findings
);
@@ -129,33 +140,28 @@ fn or_alternative_keeps_only_that_operation_public() {
);
let closed = candidate(&preview, "GET /closed");
assert!(
closed.findings.iter().any(|finding| {
finding.code == "unsupported_security_requirement"
&& finding.severity == ImportFindingSeverity::Error
}),
"non-empty requirement must still block the candidate: {:?}",
closed
.findings
.iter()
.all(|finding| finding.severity != ImportFindingSeverity::Error),
"well-formed supported requirement must remain resolvable: {:?}",
closed.findings
);
let ir_blocker = security_findings(&ir.findings, "GET /closed")[0];
assert!(security_findings(&ir, "GET /closed").is_empty());
assert!(closed.security_requirements.alternatives[0].supported);
assert_eq!(
ir_blocker.location.pointer, "/paths/~1closed/get/security/0",
"blocker must point at the local requirement: {:?}",
ir_blocker
closed.security_requirements.alternatives[0].schemes,
["probe"]
);
assert!(
security_findings(&ir.findings, "GET /closed").len() == 1,
"exactly one blocker for the closed operation: {:?}",
ir.findings
);
assert!(
security_findings(&ir.findings, "GET /open").is_empty(),
security_findings(&ir, "GET /open").is_empty(),
"the public alternative must not leak into the open operation: {:?}",
ir.findings
);
}
#[test]
fn nonempty_requirement_still_blocks_with_auth_profile_contract() {
fn well_formed_requirement_stays_resolvable_for_auth_profile_binding() {
let source = json!({
"openapi": "3.0.3",
"info": { "title": "Security scope regression", "version": "1" },
@@ -170,25 +176,18 @@ fn nonempty_requirement_still_blocks_with_auth_profile_contract() {
}
});
let (ir, preview) = run(&source);
let blocker = security_findings(&ir.findings, "GET /secured")
.into_iter()
.next()
.cloned()
.unwrap_or_else(|| panic!("non-empty requirement must block: {:?}", ir.findings));
assert_eq!(blocker.location.pointer, "/security/0");
assert_eq!(
blocker.message,
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.",
"Auth Profile contract message must be stable"
);
assert!(security_findings(&ir, "GET /secured").is_empty());
let secured = candidate(&preview, "GET /secured");
assert!(
secured.findings.iter().any(|finding| {
finding.code == "unsupported_security_requirement"
&& finding.severity == ImportFindingSeverity::Error
}),
"blocked candidate must expose the blocker: {:?}",
secured.findings
secured
.findings
.iter()
.all(|finding| finding.severity != ImportFindingSeverity::Error)
);
assert!(secured.security_requirements.alternatives[0].supported);
assert_eq!(
secured.security_requirements.source,
crank_import::rest::SecurityRequirementsSource::Document
);
}
@@ -209,7 +208,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
});
let (ir, preview) = run(&source);
for key in ["GET /one", "GET /two"] {
let blocker = security_findings(&ir.findings, key)
let blocker = security_findings(&ir, key)
.into_iter()
.next()
.cloned()
@@ -221,14 +220,15 @@ fn malformed_security_shapes_block_the_affected_operation() {
);
assert!(
candidate(&preview, key).findings.iter().any(|finding| {
finding.code == "unsupported_security_requirement"
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
}),
"{key} candidate must be blocked"
);
}
// Non-object array item: the item pointer must be preserved.
// Non-object array item: the array is fail-closed and the item has a
// source-located warning.
let source = json!({
"openapi": "3.0.3",
"info": { "title": "Security scope regression", "version": "1" },
@@ -240,16 +240,21 @@ fn malformed_security_shapes_block_the_affected_operation() {
}
});
let (ir, _) = run(&source);
let blocker = security_findings(&ir.findings, "GET /one")
let blocker = security_findings(&ir, "GET /one")
.into_iter()
.next()
.cloned()
.unwrap_or_else(|| panic!("non-object security item must block: {:?}", ir.findings));
assert_eq!(
blocker.location.pointer, "/security/0",
"blocker must point at the malformed item: {:?}",
blocker.location.pointer, "/security",
"blocker must point at the malformed array: {:?}",
blocker
);
assert!(all_findings(&ir).iter().any(|finding| {
finding.code == "invalid_security_requirement"
&& finding.severity == ImportFindingSeverity::Warning
&& finding.location.pointer == "/security/0"
}));
// Malformed operation-level security must not affect the neighbour.
let source = json!({
@@ -264,7 +269,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
}
});
let (ir, preview) = run(&source);
let blocker = security_findings(&ir.findings, "GET /bad")
let blocker = security_findings(&ir, "GET /bad")
.into_iter()
.next()
.cloned()
@@ -275,7 +280,7 @@ fn malformed_security_shapes_block_the_affected_operation() {
blocker
);
assert!(
security_findings(&ir.findings, "GET /fine").is_empty(),
security_findings(&ir, "GET /fine").is_empty(),
"the neighbour operation must not inherit a local defect: {:?}",
ir.findings
);
@@ -307,25 +312,29 @@ fn empty_alternative_needs_a_well_formed_array() {
}
});
let (ir, preview) = run(&source);
let at_item = |pointer: &str| {
ir.findings
.iter()
let at_pointer = |pointer: &str, code: &str| {
all_findings(&ir)
.into_iter()
.find(|finding| {
finding.code == "unsupported_security_requirement"
finding.code == code
&& finding.operation_key.as_deref() == Some("GET /mixed")
&& finding.location.pointer == pointer
})
.unwrap_or_else(|| panic!("blocker at {pointer} missing: {:?}", ir.findings))
.unwrap_or_else(|| panic!("{code} finding at {pointer} missing: {:?}", ir.findings))
};
let secured = at_item("/security/0");
assert_eq!(
secured.message,
"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются."
at_pointer("/security", "invalid_security_requirements").severity,
ImportFindingSeverity::Error
);
let malformed = at_item("/security/2");
assert_eq!(
malformed.message,
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
at_pointer("/security/2", "invalid_security_requirement").severity,
ImportFindingSeverity::Warning
);
assert!(
candidate(&preview, "GET /mixed")
.security_requirements
.alternatives[0]
.supported
);
assert!(
candidate(&preview, "GET /mixed")
@@ -365,10 +374,10 @@ fn object_instead_of_security_array_has_scoped_coverage() {
} else {
"/security"
};
let blocker = security_findings(&ir.findings, "GET /bad");
let blocker = security_findings(&ir, "GET /bad");
assert_eq!(blocker.len(), 1, "{version} {local}: {:?}", ir.findings);
assert_eq!(blocker[0].location.pointer, expected);
assert!(security_findings(&ir.findings, "GET /public").is_empty());
assert!(security_findings(&ir, "GET /public").is_empty());
assert!(
candidate(&preview, "GET /public")
.findings
@@ -435,7 +444,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
);
let (ir, preview) = run(&Value::Object(fixture));
for key in ["GET /one", "GET /two"] {
let blockers = security_findings(&ir.findings, key);
let blockers = security_findings(&ir, key);
assert_eq!(
blockers.len(),
1,
@@ -444,7 +453,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
);
let blocker = &blockers[0];
assert_eq!(
blocker.code, "unsupported_security_requirement",
blocker.code, "invalid_security_requirements",
"stable finding code: {blocker:?}"
);
assert_eq!(
@@ -458,7 +467,7 @@ fn openapi_31_non_array_root_security_blocks_inherited_operations() {
);
assert!(
candidate(&preview, key).findings.iter().any(|finding| {
finding.code == "unsupported_security_requirement"
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
}),
"{key} candidate must be blocked"
@@ -481,10 +490,10 @@ fn openapi_31_malformed_local_security_affects_only_that_operation() {
}),
);
let (ir, preview) = run(&Value::Object(fixture));
let blockers = security_findings(&ir.findings, "GET /bad");
let blockers = security_findings(&ir, "GET /bad");
assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings);
assert_eq!(
blockers[0].code, "unsupported_security_requirement",
blockers[0].code, "invalid_security_requirements",
"stable finding code"
);
assert_eq!(
@@ -498,7 +507,7 @@ fn openapi_31_malformed_local_security_affects_only_that_operation() {
blockers[0]
);
assert!(
security_findings(&ir.findings, "GET /public").is_empty(),
security_findings(&ir, "GET /public").is_empty(),
"the public neighbour must not inherit a local defect: {:?}",
ir.findings
);
@@ -520,7 +529,7 @@ fn openapi_31_public_alternative_with_malformed_sibling_does_not_go_public() {
json!({ "/mixed": get200("mixedOp31", None) }),
);
let (ir, preview) = run(&Value::Object(fixture));
let blockers = security_findings(&ir.findings, "GET /mixed");
let blockers = security_findings(&ir, "GET /mixed");
assert_eq!(
blockers.len(),
1,
@@ -528,14 +537,15 @@ fn openapi_31_public_alternative_with_malformed_sibling_does_not_go_public() {
ir.findings
);
assert_eq!(
blockers[0].location.pointer, "/security/1",
"blocker must point at the non-object item: {:?}",
blockers[0].location.pointer, "/security",
"fail-closed blocker must point at the malformed array: {:?}",
blockers[0]
);
assert_eq!(
blockers[0].message,
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
);
assert!(all_findings(&ir).iter().any(|finding| {
finding.code == "invalid_security_requirement"
&& finding.severity == ImportFindingSeverity::Warning
&& finding.location.pointer == "/security/1"
}));
assert!(
candidate(&preview, "GET /mixed")
.findings
@@ -558,7 +568,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
);
let (ir, preview) = run(&Value::Object(fixture));
for key in ["GET /one", "GET /two"] {
let blockers = security_findings(&ir.findings, key);
let blockers = security_findings(&ir, key);
assert_eq!(
blockers.len(),
1,
@@ -567,7 +577,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
);
let blocker = &blockers[0];
assert_eq!(
blocker.code, "unsupported_security_requirement",
blocker.code, "invalid_security_requirements",
"stable finding code: {blocker:?}"
);
assert_eq!(
@@ -581,7 +591,7 @@ fn swagger2_non_array_root_security_blocks_inherited_operations() {
);
assert!(
candidate(&preview, key).findings.iter().any(|finding| {
finding.code == "unsupported_security_requirement"
finding.code == "invalid_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
}),
"{key} candidate must be blocked"
@@ -604,9 +614,9 @@ fn swagger2_malformed_local_security_affects_only_that_operation() {
}),
);
let (ir, preview) = run(&Value::Object(fixture));
let blockers = security_findings(&ir.findings, "GET /bad");
let blockers = security_findings(&ir, "GET /bad");
assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings);
assert_eq!(blockers[0].code, "unsupported_security_requirement");
assert_eq!(blockers[0].code, "invalid_security_requirements");
assert_eq!(blockers[0].severity, ImportFindingSeverity::Error);
assert_eq!(
blockers[0].location.pointer, "/paths/~1bad/get/security",
@@ -614,7 +624,7 @@ fn swagger2_malformed_local_security_affects_only_that_operation() {
blockers[0]
);
assert!(
security_findings(&ir.findings, "GET /public").is_empty(),
security_findings(&ir, "GET /public").is_empty(),
"the public neighbour must not inherit a local defect: {:?}",
ir.findings
);
@@ -636,7 +646,7 @@ fn swagger2_public_alternative_with_malformed_sibling_does_not_go_public() {
json!({ "/mixed": get200("mixedOpS2", None) }),
);
let (ir, preview) = run(&Value::Object(fixture));
let blockers = security_findings(&ir.findings, "GET /mixed");
let blockers = security_findings(&ir, "GET /mixed");
assert_eq!(
blockers.len(),
1,
@@ -644,14 +654,15 @@ fn swagger2_public_alternative_with_malformed_sibling_does_not_go_public() {
ir.findings
);
assert_eq!(
blockers[0].location.pointer, "/security/1",
"blocker must point at the non-object item: {:?}",
blockers[0].location.pointer, "/security",
"fail-closed blocker must point at the malformed array: {:?}",
blockers[0]
);
assert_eq!(
blockers[0].message,
"security содержит альтернативу без объекта требований: формат не поддерживается, операция не публикуется."
);
assert!(all_findings(&ir).iter().any(|finding| {
finding.code == "invalid_security_requirement"
&& finding.severity == ImportFindingSeverity::Warning
&& finding.location.pointer == "/security/1"
}));
assert!(
candidate(&preview, "GET /mixed")
.findings
@@ -675,7 +686,7 @@ fn empty_security_array_stays_public() {
});
let (ir, preview) = run(&source);
assert!(
security_findings(&ir.findings, "GET /public").is_empty(),
security_findings(&ir, "GET /public").is_empty(),
"explicit empty override must stay public: {:?}",
ir.findings
);
+35 -35
View File
@@ -598,19 +598,19 @@ constructs:
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "security.bearer"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Bearer scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "security.basic"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "security.api_key"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "security.override_empty"
status: supported
@@ -618,9 +618,9 @@ constructs:
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "security.mixed_operations"
status: unsupported
preserved_semantics: "Only the operation carrying an effective security requirement receives the source-located blocker; operations overriding it with an empty security array remain actionable."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Effective security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable."
expected_finding: "none"
- specification_version: "OpenAPI 3.0"
construct_id: "media_type.application_xml.request"
status: unsupported
@@ -1238,19 +1238,19 @@ constructs:
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "security.bearer"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Bearer scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "security.basic"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "security.api_key"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "security.override_empty"
status: supported
@@ -1258,9 +1258,9 @@ constructs:
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "security.mixed_operations"
status: unsupported
preserved_semantics: "Only the operation carrying an effective security requirement receives the source-located blocker; operations overriding it with an empty security array remain actionable."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Effective security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable."
expected_finding: "none"
- specification_version: "OpenAPI 3.1"
construct_id: "media_type.application_xml.request"
status: unsupported
@@ -1879,18 +1879,18 @@ constructs:
- specification_version: "Swagger 2.0"
construct_id: "security.bearer"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
preserved_semantics: "Swagger OAuth2 semantics require manual setup; OAuth credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirements"
- specification_version: "Swagger 2.0"
construct_id: "security.basic"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "Swagger 2.0"
construct_id: "security.api_key"
status: unsupported
preserved_semantics: "The source security requirement is retained as a blocker until an administrator selects a compatible Auth Profile; credentials are never copied into the import preview."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding."
expected_finding: "none"
- specification_version: "Swagger 2.0"
construct_id: "security.override_empty"
status: supported
@@ -1898,9 +1898,9 @@ constructs:
expected_finding: "none"
- specification_version: "Swagger 2.0"
construct_id: "security.mixed_operations"
status: unsupported
preserved_semantics: "Only the operation carrying an effective security requirement receives the source-located blocker; operations overriding it with an empty security array remain actionable."
expected_finding: "unsupported_security_requirement"
status: supported
preserved_semantics: "Effective basic security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable."
expected_finding: "none"
- specification_version: "Swagger 2.0"
construct_id: "media_type.application_xml.request"
status: unsupported
@@ -1,9 +1,10 @@
mod legacy_auth;
mod plan_apply;
use super::*;
use crate::{
ArtifactSourceId, CreateImportPlanRequest, ImportJobKind, ImportJobSourceEnvelope, ImportPlan,
ImportPlanId,
ArtifactSourceId, CreateImportPlanRequest, ImportJobKind, ImportJobSourceEnvelope,
ImportOperationDraft, ImportPlan, ImportPlanId,
};
use crank_artifacts::ArtifactRef;
use sha2::{Digest, Sha256};
@@ -577,6 +578,8 @@ async fn apply_import_job_transaction(
}
}
legacy_auth::verify_legacy_import_auth(tx, request.workspace_id, request.operations).await?;
let mut result = ImportJobApplyResult {
application_key: request.application_key.to_owned(),
skipped: request.pre_skipped.to_vec(),
@@ -0,0 +1,144 @@
use super::*;
/// Legacy Apply has no plan-time auth snapshot, but it still must not create
/// operations from a stale or cross-workspace profile reference. Lock and
/// validate every referenced profile and its secret rows before inserting
/// any operation; ImportPlan Apply performs the stricter snapshot comparison.
pub(super) async fn verify_legacy_import_auth(
tx: &mut Transaction<'_, Postgres>,
workspace_id: &WorkspaceId,
operations: &[ImportOperationDraft],
) -> Result<(), RegistryError> {
let mut profiles = std::collections::BTreeMap::<String, Vec<String>>::new();
let mut profile_snapshots = std::collections::BTreeMap::<String, (String, Value)>::new();
for draft in operations {
if let Some(profile_id) = draft.operation.execution_config.auth_profile_ref.as_ref() {
profiles.entry(profile_id.as_str().to_owned()).or_default();
}
}
if profiles.is_empty() {
return Ok(());
}
let unavailable = |id: &str| RegistryError::ImportPlanAuthUnavailable {
auth_profile_id: id.to_owned(),
};
let profile_ids: Vec<String> = profiles.keys().cloned().collect();
for profile_id in &profile_ids {
let row = sqlx::query(
"select id, name, kind, config_json, created_at, updated_at
from auth_profiles
where workspace_id = $1 and id = $2",
)
.bind(workspace_id.as_str())
.bind(profile_id)
.fetch_optional(&mut **tx)
.await?
.ok_or_else(|| unavailable(profile_id))?;
let profile_id_value = row.try_get::<String, _>("id")?;
let profile_kind = row.try_get::<String, _>("kind")?;
let config_json = row.try_get::<Value, _>("config_json")?;
let profile = build_auth_profile(
profile_id_value,
workspace_id.as_str().to_owned(),
row.try_get::<String, _>("name")?,
profile_kind.clone(),
config_json.clone(),
row.try_get::<OffsetDateTime, _>("created_at")?,
row.try_get::<OffsetDateTime, _>("updated_at")?,
)
.map_err(|_| unavailable(profile_id))?;
if profile.kind != profile.config.kind() {
return Err(unavailable(profile_id));
}
let mut secret_ids = profile
.config
.secret_ids()
.into_iter()
.map(|secret| secret.as_str().to_owned())
.collect::<Vec<_>>();
secret_ids.sort();
secret_ids.dedup();
profiles.insert(profile_id.clone(), secret_ids);
profile_snapshots.insert(row.try_get::<String, _>("id")?, (profile_kind, config_json));
}
let secret_ids: std::collections::BTreeSet<String> =
profiles.values().flatten().cloned().collect();
for secret_id in secret_ids {
let owner = profiles
.iter()
.find_map(|(profile_id, ids)| ids.contains(&secret_id).then_some(profile_id.as_str()))
.unwrap_or_default();
let row = sqlx::query(
"select status, current_version
from secrets
where workspace_id = $1 and id = $2
for update",
)
.bind(workspace_id.as_str())
.bind(&secret_id)
.fetch_optional(&mut **tx)
.await?
.ok_or_else(|| unavailable(owner))?;
let status: String = row.try_get("status")?;
let version: i32 = row.try_get("current_version")?;
let version_present = sqlx::query_scalar::<_, bool>(
"select exists(select 1 from secret_versions where secret_id = $1 and version = $2)",
)
.bind(&secret_id)
.bind(version)
.fetch_one(&mut **tx)
.await?;
if status != "active" || !version_present {
return Err(unavailable(owner));
}
}
// Keep the established secret-then-profile lock order. The profile may
// have changed while secret rows were locked; compare its live config and
// secret set with the initial read before Apply mutates any operation.
for profile_id in profile_ids {
let row = sqlx::query(
"select id, name, kind, config_json, created_at, updated_at
from auth_profiles
where workspace_id = $1 and id = $2
for update",
)
.bind(workspace_id.as_str())
.bind(&profile_id)
.fetch_optional(&mut **tx)
.await?
.ok_or_else(|| unavailable(&profile_id))?;
let kind: String = row.try_get("kind")?;
let config: Value = row.try_get("config_json")?;
let Some((expected_kind, expected_config)) = profile_snapshots.get(&profile_id) else {
return Err(unavailable(&profile_id));
};
if &kind != expected_kind || &config != expected_config {
return Err(unavailable(&profile_id));
}
let profile = build_auth_profile(
row.try_get::<String, _>("id")?,
workspace_id.as_str().to_owned(),
row.try_get::<String, _>("name")?,
kind,
config,
row.try_get::<OffsetDateTime, _>("created_at")?,
row.try_get::<OffsetDateTime, _>("updated_at")?,
)
.map_err(|_| unavailable(&profile_id))?;
let mut live_secret_ids = profile
.config
.secret_ids()
.into_iter()
.map(|secret| secret.as_str().to_owned())
.collect::<Vec<_>>();
live_secret_ids.sort();
live_secret_ids.dedup();
if profiles.get(&profile_id) != Some(&live_secret_ids) {
return Err(unavailable(&profile_id));
}
}
Ok(())
}
@@ -60,6 +60,18 @@ pub(super) struct StoredPlanCandidate {
/// ids/status/versions.
#[serde(default)]
pub(crate) auth_snapshot: Option<StoredAuthSnapshot>,
/// Source-derived selected scheme requirement. Apply checks the live
/// profile against this independently of the captured profile snapshot,
/// closing a profile-change race between compatibility lookup and capture.
#[serde(default)]
pub(crate) auth_requirement: Option<StoredAuthRequirement>,
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub(super) struct StoredAuthRequirement {
pub(crate) kind: String,
#[serde(default)]
pub(crate) parameter_name: Option<String>,
}
/// Plan-time snapshot of one auth profile's redacted metadata.
@@ -391,6 +403,24 @@ pub(super) fn read_and_validate_plan_material(
return Err(corrupt());
}
}
match (&candidate.auth_requirement, &auth_ref, actionable) {
(Some(requirement), Some(_), true)
if requirement.kind == "unconstrained"
&& requirement.parameter_name.is_none()
&& decision.action == "new_version" => {}
(Some(requirement), Some(_), true)
if matches!(requirement.kind.as_str(), "bearer" | "basic")
&& requirement.parameter_name.is_none()
|| matches!(
requirement.kind.as_str(),
"api_key_header" | "api_key_query"
) && requirement
.parameter_name
.as_deref()
.is_some_and(|name| !name.is_empty() && name.len() <= 256) => {}
(None, None, _) | (None, Some(_), false) => {}
_ => return Err(corrupt()),
}
match decision.action.as_str() {
"create" => {
if target.is_some()
@@ -68,7 +68,7 @@ pub(super) async fn lock_and_verify_auth(
workspace_id: &WorkspaceId,
candidates: &[StoredPlanCandidate],
) -> Result<(), RegistryError> {
use material::StoredAuthSnapshot;
use material::{StoredAuthRequirement, StoredAuthSnapshot};
let unavailable = |auth_profile_id: &str| RegistryError::ImportPlanAuthUnavailable {
auth_profile_id: auth_profile_id.to_owned(),
@@ -79,6 +79,7 @@ pub(super) async fn lock_and_verify_auth(
// snapshot; a disagreement means the profile drifted during CreatePlan
// and the whole apply must be re-previewed before any mutation.
let mut snapshots: BTreeMap<String, &StoredAuthSnapshot> = BTreeMap::new();
let mut requirements: BTreeMap<String, Vec<&StoredAuthRequirement>> = BTreeMap::new();
{
use std::collections::btree_map::Entry;
for candidate in candidates {
@@ -95,6 +96,14 @@ pub(super) async fn lock_and_verify_auth(
.auth_snapshot
.as_ref()
.ok_or_else(|| unavailable(ref_id.as_str()))?;
let requirement = candidate
.auth_requirement
.as_ref()
.ok_or_else(|| unavailable(ref_id.as_str()))?;
requirements
.entry(ref_id.as_str().to_owned())
.or_default()
.push(requirement);
match snapshots.entry(ref_id.as_str().to_owned()) {
Entry::Vacant(vacant) => {
vacant.insert(snapshot);
@@ -132,6 +141,13 @@ pub(super) async fn lock_and_verify_auth(
if profile.kind != profile.config.kind() {
return Err(unavailable(profile_id));
}
if requirements.get(profile_id).is_none_or(|expected| {
expected
.iter()
.any(|requirement| !auth_config_satisfies_requirement(&profile.config, requirement))
}) {
return Err(unavailable(profile_id));
}
// Drift since the plan was confirmed (config, name, kind or
// updated_at) means the re-preview contract: typed conflict, no
// mutation.
@@ -263,3 +279,57 @@ pub(super) async fn lock_and_verify_auth(
}
Ok(())
}
fn auth_config_satisfies_requirement(
config: &crank_core::AuthConfig,
requirement: &material::StoredAuthRequirement,
) -> bool {
use crank_core::AuthConfig;
match (config, requirement.kind.as_str()) {
(_, "unconstrained") => requirement.parameter_name.is_none(),
(AuthConfig::Bearer(_), "bearer") | (AuthConfig::Basic(_), "basic") => {
requirement.parameter_name.is_none()
}
(AuthConfig::ApiKeyHeader(config), "api_key_header") => requirement
.parameter_name
.as_deref()
.is_some_and(|name| config.header_name.eq_ignore_ascii_case(name)),
(AuthConfig::ApiKeyQuery(config), "api_key_query") => requirement
.parameter_name
.as_deref()
.is_some_and(|name| config.param_name == name),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::auth_config_satisfies_requirement;
use crate::postgres::import_job::plan_apply::material::StoredAuthRequirement;
use crank_core::{ApiKeyHeaderAuthConfig, AuthConfig, SecretId};
#[test]
fn source_selected_header_name_is_rechecked_against_live_profile_config() {
let source_requirement = StoredAuthRequirement {
kind: "api_key_header".to_owned(),
parameter_name: Some("X-A".to_owned()),
};
let compatible = AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "x-a".to_owned(),
secret_id: SecretId::new("secret_a"),
});
let changed_after_resolution = AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-B".to_owned(),
secret_id: SecretId::new("secret_a"),
});
assert!(auth_config_satisfies_requirement(
&compatible,
&source_requirement
));
assert!(!auth_config_satisfies_requirement(
&changed_after_resolution,
&source_requirement
));
}
}
@@ -5,8 +5,8 @@
use super::*;
use crank_core::{
ApiKeyHeaderAuthConfig, AuthConfig, AuthKind, AuthProfile, Secret, SecretId, SecretKind,
SecretStatus,
ApiKeyHeaderAuthConfig, ApiKeyQueryAuthConfig, AuthConfig, AuthKind, AuthProfile,
BasicAuthConfig, Secret, SecretId, SecretKind, SecretStatus,
};
use crank_registry::MASTER_KEY_CIPHER_CONTRACT;
use crank_registry::{
@@ -110,6 +110,7 @@ fn auth_plan_payload(
"name": operation.name,
"state": "actionable",
"decision": { "action": "create" },
"auth_requirement": { "kind": "api_key_header", "parameter_name": "X-Apply-Key" },
"operation": operation
});
if let Some(snapshot) = snapshot {
@@ -133,6 +134,136 @@ fn auth_plan_payload(
})
}
fn typed_auth_plan_payload(
plan_id: &str,
job_id: &str,
fingerprint: &Value,
auth_profile_id: &str,
requirement_kind: &str,
parameter_name: Option<&str>,
snapshot: Value,
) -> Value {
let mut operation = plan_operation("op_typed_auth");
operation.execution_config.auth_profile_ref = Some(auth_profile_id.into());
let mut requirement = json!({ "kind": requirement_kind });
if let Some(parameter_name) = parameter_name {
requirement["parameter_name"] = json!(parameter_name);
}
json!({
"schema_version": "import-plan-v1",
"plan_reference": plan_id,
"job_id": job_id,
"fingerprint": fingerprint,
"source": { "source_id": SOURCE_ID, "digest": source_ref() },
"dependencies": [],
"selection": {
"mode": "full", "all": ["typed"], "selected": ["typed"], "excluded": []
},
"counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 },
"candidates": [{
"key": "typed",
"name": operation.name,
"state": "actionable",
"decision": { "action": "create" },
"auth_requirement": requirement,
"operation": operation,
"auth_snapshot": snapshot
}]
})
}
async fn typed_auth_base(
database: &TestDatabase,
workspace_tag: &str,
profile_id: &str,
config: AuthConfig,
) -> (
PostgresRegistry,
WorkspaceId,
ImportJobId,
Value,
AuthProfile,
Value,
) {
let registry = database.registry().await;
registry
.verify_or_register_master_key_identity(MasterKeyIdentityCandidate {
epoch: 1,
fingerprint: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
cipher_contract: MASTER_KEY_CIPHER_CONTRACT,
observed_at: &ts(),
})
.await
.unwrap();
let now = OffsetDateTime::now_utc();
let ws = create_workspace(&registry, workspace_tag).await;
let job = create_pending_openapi_job(
&registry,
&ws,
&format!("job_typed_auth_{workspace_tag}"),
now,
now + Duration::hours(1),
)
.await;
activate_source(&database.raw_pool().await, &ws, now).await;
let mut secrets = Vec::new();
for (index, secret_id) in config.secret_ids().into_iter().enumerate() {
let secret = Secret {
id: secret_id.clone(),
workspace_id: ws.clone(),
name: format!("Typed auth secret {index}"),
kind: SecretKind::Token,
status: SecretStatus::Active,
current_version: 1,
created_at: ts(),
updated_at: ts(),
last_used_at: None,
};
registry
.create_secret(CreateSecretRequest {
secret: &secret,
ciphertext: "ciphertext",
key_version: "v1",
master_key_epoch: 1,
created_by: None,
})
.await
.unwrap();
secrets.push(secret);
}
let profile = AuthProfile {
id: profile_id.into(),
workspace_id: ws.clone(),
name: "Typed auth profile".to_owned(),
kind: config.kind(),
config,
created_at: ts(),
updated_at: ts(),
};
registry
.save_auth_profile(SaveAuthProfileRequest {
workspace_id: &ws,
profile: &profile,
})
.await
.unwrap();
let snapshot = json!({
"profile_id": profile.id.as_str(),
"name": profile.name,
"kind": profile.kind,
"config": profile.config,
"updated_at": profile.updated_at.format(&Rfc3339).unwrap(),
"secrets": secrets.iter().map(|secret| json!({
"id": secret.id.as_str(),
"status": secret.status,
"current_version": secret.current_version,
"updated_at": secret.updated_at.format(&Rfc3339).unwrap()
})).collect::<Vec<_>>()
});
(registry, ws, job, fingerprint(), profile, snapshot)
}
async fn auth_base(
database: &TestDatabase,
workspace_tag: &str,
@@ -268,6 +399,67 @@ async fn apply_rejects_auth_profile_config_drift() {
database.cleanup().await;
}
#[tokio::test]
async fn apply_rechecks_live_profile_against_source_requirement_even_when_snapshot_matches() {
let database = TestDatabase::new().await;
let (registry, raw_pool, ws, job, fp, _snapshot_a) =
auth_base(&database, "a_source_requirement").await;
// Reproduce the race: source compatibility was resolved while the
// profile used X-Apply-Key, then the profile changed before the later
// snapshot capture, so the stored snapshot itself accurately says X-B.
sqlx::query(
"update auth_profiles
set config_json = jsonb_set(config_json, '{api_key_header,header_name}', '\"X-B\"'),
updated_at = now()
where workspace_id = $1 and id = $2",
)
.bind(ws.as_str())
.bind("auth_apply")
.execute(&raw_pool)
.await
.unwrap();
let profile = capture_profile_fixture(&registry, &ws).await;
let secret = registry
.get_secret(&ws, &SecretId::new("sec_apply"))
.await
.unwrap()
.unwrap()
.secret;
let snapshot_b = capture_snapshot(&profile, &secret);
assert_eq!(snapshot_b["config"]["api_key_header"]["header_name"], "X-B");
// The source-derived requirement remains X-Apply-Key even though the
// plan-time snapshot now matches the live X-B profile exactly.
let payload = auth_plan_payload("plan_auth_source_req", job.as_str(), &fp, Some(snapshot_b));
let (plan, digest) =
store_plan(&registry, &ws, &job, "plan_auth_source_req", &fp, payload).await;
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
let result = registry
.apply_import_plan(&ApplyImportPlanRequest {
workspace_id: &ws,
plan_id: &plan,
plan_digest: &digest,
idempotency_key: "idem_auth_source_req",
verified_fingerprint: &fp,
applied_at: &applied_at,
})
.await;
assert!(matches!(
result,
Err(RegistryError::ImportPlanAuthUnavailable { auth_profile_id })
if auth_profile_id == "auth_apply"
));
assert!(
registry
.get_operation_summary(&ws, &OperationId::new("op_apply_auth"))
.await
.unwrap()
.is_none()
);
database.cleanup().await;
}
async fn capture_profile_fixture(registry: &PostgresRegistry, ws: &WorkspaceId) -> AuthProfile {
registry
.get_auth_profile(ws, &crank_core::AuthProfileId::new("auth_apply"))
@@ -421,3 +613,149 @@ async fn apply_rejects_auth_candidates_missing_or_mismatched_snapshot() {
);
database.cleanup().await;
}
#[tokio::test]
async fn apply_accepts_basic_auth_with_matching_snapshot_and_source_requirement() {
let database = TestDatabase::new().await;
let profile_id = "auth_basic_apply";
let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base(
&database,
"a_basic_ok",
profile_id,
AuthConfig::Basic(BasicAuthConfig {
username_secret_id: SecretId::new("sec_basic_user"),
password_secret_id: SecretId::new("sec_basic_password"),
}),
)
.await;
let payload = typed_auth_plan_payload(
"plan_basic_ok",
job.as_str(),
&fp,
profile_id,
"basic",
None,
snapshot,
);
let (plan, digest) = store_plan(&registry, &ws, &job, "plan_basic_ok", &fp, payload).await;
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
let result = registry
.apply_import_plan(&ApplyImportPlanRequest {
workspace_id: &ws,
plan_id: &plan,
plan_digest: &digest,
idempotency_key: "idem_basic_ok",
verified_fingerprint: &fp,
applied_at: &applied_at,
})
.await
.unwrap();
assert_eq!(result.report.status, ImportPlanApplyStatus::Applied);
assert_eq!(result.report.counts.created, 1);
let summary = registry
.get_operation_summary(&ws, &OperationId::new("op_typed_auth"))
.await
.unwrap()
.unwrap();
assert_eq!(summary.current_draft_version, 1);
database.cleanup().await;
}
#[tokio::test]
async fn apply_accepts_query_key_with_matching_snapshot_and_source_requirement() {
let database = TestDatabase::new().await;
let profile_id = "auth_query_apply";
let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base(
&database,
"a_query_ok",
profile_id,
AuthConfig::ApiKeyQuery(ApiKeyQueryAuthConfig {
param_name: "access_token".to_owned(),
secret_id: SecretId::new("sec_query_key"),
}),
)
.await;
let payload = typed_auth_plan_payload(
"plan_query_ok",
job.as_str(),
&fp,
profile_id,
"api_key_query",
Some("access_token"),
snapshot,
);
let (plan, digest) = store_plan(&registry, &ws, &job, "plan_query_ok", &fp, payload).await;
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
let result = registry
.apply_import_plan(&ApplyImportPlanRequest {
workspace_id: &ws,
plan_id: &plan,
plan_digest: &digest,
idempotency_key: "idem_query_ok",
verified_fingerprint: &fp,
applied_at: &applied_at,
})
.await
.unwrap();
assert_eq!(result.report.status, ImportPlanApplyStatus::Applied);
assert_eq!(result.report.counts.created, 1);
let summary = registry
.get_operation_summary(&ws, &OperationId::new("op_typed_auth"))
.await
.unwrap()
.unwrap();
assert_eq!(summary.current_draft_version, 1);
database.cleanup().await;
}
#[tokio::test]
async fn apply_rejects_query_key_source_mismatch_with_matching_live_snapshot() {
let database = TestDatabase::new().await;
let profile_id = "auth_query_mismatch";
let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base(
&database,
"a_query_mismatch",
profile_id,
AuthConfig::ApiKeyQuery(ApiKeyQueryAuthConfig {
param_name: "access_token".to_owned(),
secret_id: SecretId::new("sec_query_mismatch"),
}),
)
.await;
let payload = typed_auth_plan_payload(
"plan_query_mismatch",
job.as_str(),
&fp,
profile_id,
"api_key_query",
Some("other_token"),
snapshot,
);
let (plan, digest) =
store_plan(&registry, &ws, &job, "plan_query_mismatch", &fp, payload).await;
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
let result = registry
.apply_import_plan(&ApplyImportPlanRequest {
workspace_id: &ws,
plan_id: &plan,
plan_digest: &digest,
idempotency_key: "idem_query_mismatch",
verified_fingerprint: &fp,
applied_at: &applied_at,
})
.await;
assert!(matches!(
result,
Err(RegistryError::ImportPlanAuthUnavailable { auth_profile_id })
if auth_profile_id == profile_id
));
assert!(
registry
.get_operation_summary(&ws, &OperationId::new("op_typed_auth"))
.await
.unwrap()
.is_none(),
"query source mismatch must fail before creating an operation"
);
database.cleanup().await;
}
+10 -4
View File
@@ -8,17 +8,20 @@ Crank умеет импортировать REST API из OpenAPI 3.x и Swagger
2. Нажмите **Импорт OpenAPI** рядом с кнопкой **Новая операция**.
3. Загрузите ровно один UTF-8 `.yaml`, `.yml` или `.json` файл размером от 1 B до 256 KiB. Вставка текста не поддерживается.
4. Нажмите **Разобрать документ**.
5. Проверьте preview: сгенерированный contract сохраняет parameter locations, body formats и варианты успешного ответа.
5. Проверьте preview: сгенерированный contract сохраняет parameter locations, body formats, варианты успешного ответа и effective security requirements.
6. Выберите нужные группы и методы.
7. Проверьте `Base URL`. Если в документе нет `servers`, укажите URL вручную.
8. Для уже существующей операции подтвердите решение отдельно: пропустить точную операцию, создать отдельную операцию с указанным вами свободным именем или добавить новую черновую версию к выбранной REST-операции. Автоматический подбор имени при применении плана не выполняется.
9. Создайте план и проверьте число выбранных, исключённых, доступных и заблокированных методов. По умолчанию полный план требует, чтобы каждый метод можно было применить; для частичного импорта явно оставьте только нужные методы.
10. Подтвердите план и примените его. Создание плана не меняет операции. Применение выполняет все выбранные изменения одной транзакцией и показывает результат для каждого метода.
10. Для защищённых методов задайте совместимый Auth Profile через import API, выбрав конкретную security alternative из вариантов preview. Незакрытое обязательное требование оставляет метод заблокированным; выбор этих привязок в UI пока недоступен.
11. Подтвердите план и примените его. Создание плана не меняет операции. Применение выполняет все выбранные изменения одной транзакцией и показывает результат для каждого метода.
## Загрузка и восстановление
Браузер отправляет выбранный файл как `multipart/form-data`; границу multipart формирует сам браузер. Файл остаётся только в памяти текущего окна импорта. При замене файла, сбросе, закрытии окна, смене workspace/языка или уходе со страницы текущий запрос отменяется, а поздний ответ игнорируется. При `pagehide` (включая BFCache) выбранный файл и Base URL очищаются: после возврата нужно выбрать файл заново, повтор старой загрузки не предлагается.
До сохранения source snapshot импорт проверяет основной документ и загруженные allowlisted `$ref`-документы вместе, включая разрешённые ссылки. Если такая проверка находит literal credential, загрузка отклоняется; значение не попадает в artifact storage, preview или текст ошибки. Безопасные внешние ссылки сохраняются как digest-проверенные immutable snapshots и не загружаются заново при Apply.
Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика загрузки показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации и внутренние пути не выводятся. Подтверждение плана показывает счётчики и решения по кандидатам, но не исходный документ и не учётные данные. Digest плана передаётся в Apply автоматически.
При неизвестном результате Apply, например после разрыва соединения, повторите запрос с теми же ссылкой на план, digest и ключом идемпотентности. Повтор после commit возвращает сохранённый отчёт и не создаёт ещё одну версию; тот же ключ с другим digest даёт конфликт. Пока Apply выполняется, повторное нажатие кнопки не отправляет вторую логическую операцию.
@@ -78,7 +81,7 @@ Semantic loss получает blocker с source location: такой candidate
Один endpoint с несколькими request formats остаётся одним Tool. Поля `content_type` и `body` выбирают одну schema и соответствующую сериализацию; неизвестный selector или body другой ветви отклоняются до отправки запроса. Для единственного формата selector не нужен. Подробный inline-file contract описан в [REST](protocols/rest.md).
Новые imports используют `normalized-ir-v4` / `preview-v4`. Сохранённые v2/v3 jobs воспроизводят сохранённый preview, а legacy-v1 использует отдельную историческую projection. Обновление importer не меняет inputs существующих jobs.
Новые imports используют `normalized-ir-v5` / `preview-v5`. Исторические v2/v3/v4 jobs воспроизводят сохранённый preview, а legacy-v1 использует отдельную историческую projection. Обновление importer не меняет inputs существующих jobs.
Другие форматы спецификаций и протоколов в Community-импорт не входят.
@@ -94,7 +97,10 @@ Semantic loss получает blocker с source location: такой candidate
| Успешные `2xx`, включая `204` | Поддерживается | Поддерживается | Каждая строка 200–299 проверяется отдельным probe; status сохраняется, а 204/205 остаются bodyless |
| Локальные и allowlisted external `$ref` | Поддерживается | Поддерживается | Разрешение только из загруженного документа или digest-проверенного snapshot; тесты не загружают fixture по сети |
| `allOf`, `oneOf`, `anyOf` | Поддерживается | Поддерживается | Совместимые `allOf` ветви объединяются; `oneOf` и `anyOf` остаются вариантами schema |
| bearer/basic/API key security requirements | Blocker до выбора Auth Profile | Blocker до выбора Auth Profile | `unsupported_security_requirement`; requirement вычисляется per-operation с наследованием document-уровня; credentials не импортируются и не попадают в preview |
| bearer/basic/API key security schemes | Поддерживается | Поддерживается для basic и API key header/query | Схемы типизируются без credential values; preview предлагает только совместимые workspace Auth Profiles с активными current secrets |
| Security requirements с несколькими alternatives | Поддерживается | Поддерживается | Каждая OR alternative сохраняется отдельно и получает собственный список совместимых Auth Profiles; unsupported alternative не блокирует доступную соседнюю alternative |
| Несколько schemes в одной security alternative (AND) | Blocker для этой alternative | Blocker для этой alternative | Составное требование сохраняет имена схем в finding; оно не становится actionable через single-scheme Auth Profile |
| OAuth2, OpenID Connect, mutual TLS и неизвестные схемы | Manual configuration | Manual configuration | Явный unsupported finding; схема не извлекает token/client secret и не становится actionable автоматически |
| security override: operation-level `security: []` | Поддерживается | Поддерживается | Явный пустой массив отключает наследование: только operations с эффективным requirement получают source-located blocker, связанные операции остаются actionable |
| Микс public/secured operations | Поддерживается | Подвергается проверке | Blocker принадлежит только затронутому candidate; global blocker не создаётся для требований одного operation |
| XML, `TRACE`, `CONNECT` | Unsupported | Unsupported | Source-located finding; blocker не становится actionable Draft |