feat(import): bind OpenAPI security schemes safely

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

No files matched your search

Generated
+13 -13
View File
@@ -221,9 +221,9 @@ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "aws-lc-rs"
version = "1.17.3"
version = "1.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00bdb5da18dac48ca2cc7cd4a98e533e8635a58e2361d13a1a4ee3888e0d72f1"
checksum = "b281d307588d634de920874890732659e2e7672f72b5e10e81badc1a8a83621e"
dependencies = [
"aws-lc-sys",
"zeroize",
@@ -231,9 +231,9 @@ dependencies = [
[[package]]
name = "aws-lc-sys"
version = "0.43.0"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43103168cc76fe62678a375e722fc9cb3a0146159ac5828bc4f0dfd755c2224c"
checksum = "9bff6c3b54fad79a2e60b8102caf565819711497c1f5f092f49508e2f5c31b27"
dependencies = [
"cc",
"cmake",
@@ -1142,7 +1142,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -2671,7 +2671,7 @@ dependencies = [
"once_cell",
"socket2 0.5.10",
"tracing",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -3010,14 +3010,14 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
name = "rustls"
version = "0.23.41"
version = "0.23.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f"
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
dependencies = [
"aws-lc-rs",
"log",
@@ -3069,7 +3069,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -3080,9 +3080,9 @@ checksum = "f87165f0995f63a9fbeea62b64d10b4d9d8e78ec6d7d51fb2125fda7bb36788f"
[[package]]
name = "rustls-webpki"
version = "0.103.13"
version = "0.103.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e"
checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
dependencies = [
"aws-lc-rs",
"ring",
@@ -4473,7 +4473,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -2,9 +2,10 @@
title: '2.10 — Привязать security schemes через безопасный import API'
type: feature
created: '2026-09-13'
status: blocked
status: done
baseline_revision: c562d5368d810d1730f5d392ae0c6c106143a97c
review_loop_iteration: 0
followup_review_recommended: false
followup_review_recommended: true
context:
- '_bmad-output/project-context.md'
- '_bmad-output/implementation-artifacts/epic-2-context.md'
@@ -73,13 +74,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):**
@@ -149,6 +150,23 @@ deferred: []
## Review Triage Log
### 2026-09-27 — Review pass
- verdicts: 13 findings — high 9, medium 1, low 2, false 1, maybe-false 0
- findings:
- `[high]` `[patch]` camelCase `clientSecret`/`accessToken` не распознаются — `sensitive_key` приводит регистр, но сравнивает только целые слова или `_`-границы; literal может остаться в source. Исправить распознавание и покрыть canary. В `security_literals.rs` добавлена проверка camelCase; canary-тест проходит.
- `[high]` `[patch]` числовые/boolean credential values пропускаются — `walk` обрабатывает лишь строки, хотя значения под чувствительным ключом тоже являются literal; заменить их безопасным redaction marker и покрыть тестом. В `walk` скаляры под sensitive context заменяются `[REDACTED]`; null оставлен без finding, тест проходит.
- `[high]` `[patch]` JWT в generic example пропускается — путь и значение могут не содержать текстовых маркеров; добавить строгую JWT-эвристику и canary-тест. `is_jwt` проверяет форму и длины сегментов; JWT canary и benign `1.2.3` проверены.
- `[high]` `[patch]` secret в query URL пропускается — проверяется userinfo, но не ключи query вроде `access_token`; отклонять такой source до persistence и проверять отсутствие canary в IR. Добавлена проверка чувствительных query keys с redaction; OAuth URL fixture проверяет и безопасный, и секретный вариант.
- `[high]` `[patch]` Cookie/Set-Cookie example пропускается — response header example без слова `token` не получает sensitive context; покрыть header/cookie примеры. Добавлен контекст Cookie/Set-Cookie и canary-тест.
- `[medium]` `[patch]` credential как ключ объекта example остаётся в pointer/source tree — для распознаваемого token-shaped ключа значение редактируется, а имя ключа нет; редактировать весь example object и указывать безопасный parent pointer. JWT-shaped key приводит к redaction всего example object с parent pointer; canary-тест проходит.
- `[low]` `[patch]` transaction-level auth error не локализуется — `From<RegistryError>` всегда выдаёт английский текст, хотя `create_openapi_import` знает locale; преобразовать этот вариант на границе import API. В import Apply вариант RegistryError преобразуется с locale; Ru integration test проходит.
- `[low]` `[patch]` документация не уточняет zero-based `alternative_index` — код индексирует `Vec` напрямую; добавить одну фразу. В `docs/openapi-import.md` добавлено явное правило нулевой индексации.
- `[high]` `[patch]` AC-12 не проверяет перечисленные literal paths — тесты не содержат camelCase/JWT/query URL/Cookie; добавить canary-fixtures к исправлению сканера. `scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples` и OAuth URL fixture добавлены и прошли.
- `[high]` `[patch]` тест redacted bearer hint не проверяет safe finding — сейчас проверяется только отсутствие canary в IR; добавить проверку source location и сериализованного finding. Проверяются точный safe pointer, код finding и сериализованный report без canary.
- `[high]` `[patch]` edge-case слой подтвердил camelCase/JWT пропуски — тот же `sensitive_key` и `looks_secret_value`, что в строках 1 и 3; покрывается тем же исправлением. Закрыто общей scanner patch и canary-тестами строк 1 и 3.
- `[high]` `[patch]` query API-key parameter с произвольным example пропускается — имя параметра и example обходятся отдельно, `in: query` не включает sensitive context; связать context с именем `api_key` и добавить тест. Сканер использует sibling `name`/`in` для example context; canary-тест проходит.
- `[false]` `[reject]` API rejection вместо redacted preview finding не означает отсутствия finding — `normalize.rs` создаёт `secret_literal_redacted` в IR, а admin API до persistence отклоняет опасный upload с безопасной ошибкой; контракт не требует сохранять небезопасный source ради preview.
## 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.
@@ -157,12 +175,15 @@ Pure importer хранит schemes/requirements, но не читает Auth Pro
## Verification
**Commands (не запускались; production code не изменён):**
- `cargo test -p crank-import --all-targets` — PASS: 99 tests; security metadata, inheritance, OR/AND, Swagger 2.0, redaction, JWT/cookie/query credential canaries.
- `cargo test -p admin-api --test integration -- --test-threads=1` — PASS: 88 integration tests; upload, mappings, workspace isolation, Secret availability, replay и digest drift.
- `cargo test -p crank-runtime --all-targets` — PASS: 83 tests; существующий trusted auth path не менялся.
- `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1` — PASS: 5 tests; transaction revalidation и no-partial-Draft race.
- `just rust-code-health`, `just dependencies`, `just fmt-check`, `just clippy` — PASS после разнесения oversized модулей, обновления `rustls` в `Cargo.lock` до 0.23.45 и исправления формата теста.
- `just verify` — PASS: полный workspace gate завершился с exit 0 (через pipe/`tee`). Первый прогон остановился на file-size gate, второй — на устаревшей advisory `RUSTSEC-2026-0285`, третий — на `clippy`, четвёртый — на `rustfmt`, пятый и шестой запуск упали в `crank-artifacts::real_process_write_limit_leaves_no_partial_final_blob` из-за перенаправления stdout в обычный файл: дочерний тест ставит `RLIMIT_FSIZE=1`, и его собственный вывод попал под лимит. Это воспроизведено отдельной командой с редиректом; без него scoped target прошёл 7/7. Финальный полный прогон через pipe/`tee` прошёл; код теста не менялся.
- После review patch повторный `just verify` — PASS (exit 0): все workspace tests, fmt, clippy, code health и dependency gate. Включены новые scanner canaries и исправленная локализация ошибки binding.
- `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.
- `cargo test -p crank-runtime --all-targets` — auth resolution и trusted injection.
- `just verify` — обязательный полный gate после реализации.
**Matrix test audit:** Schemes, Effective security и Alternatives покрыты `crates/crank-import/tests/security_schemes.rs`; Binding, Credential availability, Shared Secret и Stale source — `apps/admin-api/tests/integration/openapi_import/security.rs` и `crates/crank-registry/tests/integration/import_auth_binding.rs`; Secret source — importer scanner tests, primary/external upload integration и telemetry canary tests. Все указанные story tests и полный workspace gate запускались и прошли.
## Auto Run Result
@@ -183,3 +204,13 @@ Blocking condition: intent gap
Основание остановки: правило `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.”
Это пробел нормативного scope, а не внешнее действие оператора. AC не требуют покупки домена, выдачи реального API key или действий в vendor console; `awaiting-operator` к данной остановке не применяется.
### 2026-09-27 — повторный Auto Run Result
Status: done
Реализованы typed security schemes и OR/AND requirements в IR/preview, безопасный выбор Auth Profile при import, повторная проверка Secret/profile в транзакции перед созданием Draft, redaction credential literals и совместимость исторического replay. Небезопасный source отклоняется до сохранения artifacts. Документирован API contract; ATDD сценарии и матрица AC покрыты тестами.
Review: 13 findings; 12 исправлены (9 high, 1 medium, 2 low), 1 отклонён как false с объяснением в Review Triage Log. Отложенных findings и открытых intent gaps нет. Повторный `just verify` прошёл с exit 0 после всех review patches; importer 99/99, Admin API integration 88/88, registry auth-binding 5/5, runtime 83/83.
Остаточный риск: распознавание произвольных секретов без явного контекста остаётся эвристическим. Рекомендована независимая follow-up проверка scanner coverage при дальнейшем расширении форматов. Внешних действий оператора для AC этой истории нет.
@@ -0,0 +1,137 @@
---
stepsCompleted: ['step-01-preflight-and-context', 'step-02-generation-mode', 'step-03-test-strategy', 'step-04c-aggregate', 'step-05-validate-and-complete']
lastStep: 'step-05-validate-and-complete'
lastSaved: '2026-09-27'
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:
- 'crates/crank-import/tests/security_schemes.rs'
- 'apps/admin-api/tests/integration/openapi_import/security.rs'
- 'apps/admin-api/tests/integration/openapi_import/external_references.rs'
- 'crates/crank-registry/tests/integration/import_auth_binding.rs'
inputDocuments:
- '_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.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'
- 'Cargo.toml'
- 'apps/admin-api/Cargo.toml'
- 'crates/crank-import/Cargo.toml'
- 'crates/crank-runtime/Cargo.toml'
acceptanceCriteria:
- id: AC-1
idSource: generated
text: '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.'
- id: AC-2
idSource: generated
text: 'Given global security requirement; When Operation не задаёт собственный override; Then candidate наследует global requirement согласно specification semantics; And operation-level requirement или explicit no-auth корректно заменяет global rule.'
- id: AC-3
idSource: generated
text: 'Given security requirement с несколькими alternatives; When source определяет OR-семантику; Then preview сохраняет все допустимые alternatives и позволяет выбрать поддерживаемый вариант; And importer не объединяет alternatives в ложное обязательное AND.'
- id: AC-4
idSource: generated
text: 'Given security requirement, требующий несколько schemes одновременно; When source определяет AND-семантику; Then candidate сохраняет составное требование, если runtime contract его поддерживает; And иначе affected Operation получает blocker finding с перечислением несохраняемой semantics.'
- id: AC-5
idSource: generated
text: 'Given bearer, basic или API-key requirement; When формируется preview; Then администратору предлагается выбрать совместимый Auth Profile из текущего workspace либо оставить явный unresolved binding; And unresolved required authentication блокирует actionable Draft/Apply для affected Operation.'
- id: AC-6
idSource: generated
text: '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.'
- id: AC-7
idSource: generated
text: '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.'
- id: AC-8
idSource: generated
text: '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.'
- id: AC-9
idSource: generated
text: 'Given Auth Profile отсутствует/удалён, принадлежит другому workspace либо любой необходимый связанный Secret недоступен по существующим правилам active/current version; When preview либо Apply проверяет binding; Then reference отклоняется до создания Draft; And ошибка не раскрывает metadata foreign profile.'
- id: AC-10
idSource: generated
text: 'Given профили A и B используют один активный Secret с доступной текущей версией; When проверяются binding этих профилей; Then оба binding допустимы при выполнении остальных проверок; And после отключения общего Secret оба binding отклоняются до создания Draft; тест с disabled Secret не является доказательством независимого отзыва Auth Profile, который остаётся вне scope.'
- id: AC-11
idSource: generated
text: 'Given исходная security scheme изменилась после preview; When старый ImportPlan пытаются применить к новому source; Then plan digest mismatch блокирует Apply; And требуется новый preview и явное подтверждение Auth Profile mappings.'
- id: AC-12
idSource: generated
text: '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` проходят.'
---
# ATDD Checklist — Story 2.10: Security Schemes and Secrets
## Preflight
- **Approval/source:** the approved, frozen acceptance contract is the Story 2.10 spec. Its recorded 2026-09-13 decision defines binding availability through the existing active/current Secret rules; it does not add independent Auth Profile revocation.
- **Story identity:** ID `2.10`; canonical key `2-10-security-schemes-secrets` from the spec filename.
- **Stack and harness:** repository is Rust backend plus an existing UI. This story's approved scope is API/parser/runtime, so primary level is Rust unit/integration/API contract; UI/E2E belongs to Story 2.11. `cargo test` and `just verify` are configured.
- **Risk and design:** P0, FR-10, E2-R2 (score 9), seed scenarios `2.10-API-A` and `2.10-API-B`. The epic test design calls for preserving security semantics and rejecting unsupported/foreign bindings, with secret canaries absent from API/IR/plan/export surfaces.
- **Implementation observation:** security metadata now flows through typed IR/preview; Admin API accepts profile mappings, rejects unsafe literals before persistence, and validates binding availability within Apply. Current verification evidence is recorded below and is scoped to the commands actually run.
- **Generation mode:** backend/API source analysis. No browser recording; no Story 2.11 UI scaffolds.
- **Contract boundary:** implementation added focused tests in the existing importer, Admin API and registry harnesses. No skipped or placeholder tests are used as evidence.
## Acceptance Criteria Snapshot and Scenario Mapping
AC IDs below are deterministic IDs assigned in source order because the spec does not declare `AC-<n>` IDs. The exact snapshot is also stored in frontmatter.
| Scenario | AC | Test level / priority | Primary reusable test path | Required scenario and current status |
|---|---|---|---|---|
| 2.10-API-A1 | AC-1 | Importer unit / P0 | `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes`; `swagger2_security_definitions_project_basic_and_api_key_header_and_query` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: OpenAPI typed scheme metadata and Swagger 2.0 basic/API-key locations are projected. |
| 2.10-API-A2 | AC-2 | Importer unit / P0 | `security_requirements_inherit_and_operation_empty_array_disables_auth` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: global inheritance, multi-scheme operation requirement, explicit empty override. |
| 2.10-API-A3 | AC-3 | Importer + preview API / P0 | `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes`; `selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in importer and Admin API integration suites: OR alternatives remain distinct and selected supported option applies. |
| 2.10-API-A4 | AC-4 | Importer unit + preview API / P0 | `security_requirements_inherit_and_operation_empty_array_disables_auth`; `security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: AND composition is retained and unsupported composition becomes a blocker. |
| 2.10-API-B1 | AC-5 | Admin API integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current`; `selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in Admin API integration suite: choices are workspace-scoped, supported mapping applies, unresolved required auth is blocked. |
| 2.10-API-B2 | AC-6 | Importer + API/registry integration / P0 | `ac_6_never_copies_literal_secret_canaries_into_normalized_ir` in `crates/crank-import/tests/security_schemes.rs`; `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in `apps/admin-api/tests/integration/openapi_import/security.rs`; `no_auth_requires_no_profile_reference` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in importer, Admin API and registry suites. Tests assert canary/ciphertext/hash absence from IR/serialized Operation and no profile ref for no-auth. |
| 2.10-API-A5 | AC-7 | Importer unit / P0 | `unsupported_security_schemes_create_blockers_without_copying_credentials` in `crates/crank-import/tests/security_schemes.rs` | **PASS** in importer suite: OAuth/OIDC/mTLS/unknown produce explicit unsupported/blocker findings without credential extraction. |
| 2.10-API-A6 | AC-8 | Importer + Admin API integration / P0 | `scanner_finds_examples_defaults_extensions_and_url_userinfo_without_values`; `scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples`; `scanner_keeps_version_examples_and_null_credential_defaults`; `unsafe_bearer_format_canary_is_redacted_and_not_projected` in `crates/crank-import/tests/security_schemes.rs`; `literal_credential_is_rejected_before_any_import_artifact_or_job_is_created` in Admin API `security.rs`; `external_literal_credential_is_rejected_before_snapshot_persistence` in `external_references.rs` | **PASS** in importer/Admin API suites: canaries redact from findings and literal credentials are rejected before primary or external source persistence. |
| 2.10-API-B3 | AC-9 | Admin API + registry integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in Admin API `security.rs`; `missing_and_foreign_profiles_have_the_same_safe_error_and_insert_no_operation`; `apply_rejects_kind_parameter_and_missing_current_version_before_mutation` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in Admin API and registry suites. Foreign/missing errors are indistinguishable and no Draft mutation occurs. |
| 2.10-API-B4 | AC-10 | Admin API + registry integration / P0 | `auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current` in Admin API `security.rs`; `apply_accepts_current_compatible_profile_and_shared_secret_then_rejects_disabled_secret` in `crates/crank-registry/tests/integration/import_auth_binding.rs` | **PASS** in Admin API and registry suites. Shared Secret permits both profiles while active and rejects them when disabled; independent profile revocation is not asserted. |
| 2.10-API-B5 | AC-11 | Admin API integration / P0 | `changed_security_scheme_after_preview_fails_digest_check_without_draft_mutation` in `apps/admin-api/tests/integration/openapi_import/security.rs` | **PASS** in Admin API integration suite: stale digest blocks Apply without creating Draft. |
| 2.10-API-GATE | AC-12 | Suite / P0 | `cargo test -p crank-import --all-targets`; `cargo test -p admin-api --test integration openapi_import -- --test-threads=1`; `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1`; `just verify` | Importer, Admin API and registry suites **PASS**; final `just verify` **PASS**. Earlier failed attempts were repaired; the unrelated artifact subprocess failure was caused by redirecting stdout to a regular file and disappeared when the gate ran through a pipe. |
### Reuse inventory (not a claim of AC coverage)
- Importer behavior patterns: `crates/crank-import/tests/security_schemes.rs`, `swagger2_contract.rs`, `unit.rs`, `preview.rs`, and `http_replay.rs`.
- API/import persistence patterns: `apps/admin-api/tests/integration/openapi_import/http_contract.rs` and `external_references.rs`.
- Runtime auth injection patterns: `crates/crank-runtime/tests/stages.rs` and `integration/confirmation.rs`.
- The AC rows point to the actual tests observed in the current implementation. Importer, Admin API and registry test runs are complete; broad `just verify` has now passed.
## Test Strategy and TDD State
- **Primary level:** importer unit tests for pure OpenAPI semantics; Admin API + registry integration for profile scope, secret availability, transaction revalidation, and no-Draft guarantees; runtime tests only for unchanged trusted credential injection behavior.
- **Duplicate coverage:** do not duplicate existing general reference, source digest, redaction, or runtime-auth tests. Add assertions only for the Story 2.10 security contract and its import boundary.
- **Fixture requirements:** OAS 3.0, OAS 3.1, and Swagger 2.0 docs; deterministic secret canaries in example/default/extension/userinfo/header-example positions; workspace A/B profiles sharing one Secret; active/current and disabled Secret states; source mutation after preview. Never use customer credentials.
- **Assertion order:** primary assertion checks the exact promised value/state (typed scheme/effective requirement, blocker, absence of Draft, or digest rejection) before broad serialized-object checks. Setup results from an unimplemented endpoint remain opaque until the criterion assertion.
- **TDD record:** the initial two active tests genuinely failed before implementation (historical RED below); after implementation both now pass. No skipped tests are counted as RED or GREEN evidence.
## Handoff
- Story-level importer, Admin API and registry evidence is recorded above; all scenarios mapped to AC-1..AC-11 passed in those suites.
- AC-12 is satisfied: full `just verify` completed successfully. Earlier gate failures were fixed or traced to the output-redirection harness.
- Runtime suite: root reports `cargo test -p crank-runtime --all-targets` passed (83 tests).
## Historical RED Evidence (Before Implementation)
`cargo test -p crank-import --test normalization_details -- --nocapture` failed as intended: 11 passed, 2 failed, 0 ignored. AC-1 failed because `WorkspaceBearer` was absent from operation projection; AC-6 failed because `atdd-secret-canary-2-10` appeared in normalized IR. These failures were fixed by implementation and the tests now pass.
## Verification Results (Current Worktree)
| Command | Result | Interpretation |
|---|---|---|
| `cargo test -p crank-import --all-targets` | **PASS** — 99 tests passed, 0 failed, 0 ignored. | Includes security normalization, OpenAPI/Swagger mapping and redaction cases after review fixes. |
| `cargo test -p admin-api --test integration openapi_import -- --test-threads=1` | **PASS** — 24 passed, 0 failed, 0 ignored; 64 filtered out. | Includes mapping choices, literal-credential rejection, workspace/profile revalidation, shared Secret availability and digest drift. |
| `cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1` | **PASS** — 5 passed, 0 failed, 0 ignored; 95 filtered out. | Includes transaction-level profile, Secret state, no-auth and no-partial-mutation checks. |
| `cargo test -p crank-runtime --all-targets` | **PASS** — 83 passed, 0 failed, 0 ignored (result reported by root). | Runtime regression suite is green; no security scheme behavior is implemented in the runtime crate. |
| `just verify` | **PASS** — full workspace gate finished with exit 0, including fmt, clippy, dependencies and all workspace tests. | Earlier failures were corrected; artifact subprocess needed a pipe instead of regular-file stdout. |
| `git diff --check` | **PASS**. | No whitespace errors in current diff. |
## Completion Summary
- Story ID/key: `2.10` / `2-10-security-schemes-secrets`.
- AC snapshot: 12 source criteria mapped one-to-one to scenario IDs.
- Tests: security-specific tests were added to existing importer, Admin API and registry suites. The scenario table maps each AC to exact test names and package-level evidence.
- Current evidence after review fixes: importer 99/99, Admin API integration 88/88, registry auth-binding integration 5/5. Historical RED is retained separately; repeat full `just verify` passed.
@@ -0,0 +1,81 @@
{
"story_id": "2.10",
"story_key": "2-10-security-schemes-secrets",
"story_file": "_bmad-output/implementation-artifacts/spec-2-10-security-schemes-secrets.md",
"checklist": "_bmad-output/test-artifacts/atdd-checklist-2-10-security-schemes-secrets.md",
"tdd_phase": "GREEN",
"acceptance_criteria_total": 12,
"active_red_tests": 0,
"historical_red_tests": [
{
"scenario": "2.10-API-A1",
"acceptance_criterion": "AC-1",
"test": "security_schemes::ac_1_projects_openapi_security_scheme_identity_onto_operation_metadata",
"result": "FAIL_BEFORE_IMPLEMENTATION_THEN_PASS",
"observed": "operation projection does not contain WorkspaceBearer"
},
{
"scenario": "2.10-API-B2",
"acceptance_criterion": "AC-6",
"test": "security_schemes::ac_6_never_copies_literal_secret_canaries_into_normalized_ir",
"result": "FAIL_BEFORE_IMPLEMENTATION_THEN_PASS",
"observed": "normalized IR contains atdd-secret-canary-2-10"
}
],
"pending_acceptance_criteria": [],
"generated_test_files": [
"crates/crank-import/tests/security_schemes.rs",
"apps/admin-api/tests/integration/openapi_import/security.rs",
"apps/admin-api/tests/integration/openapi_import/external_references.rs",
"crates/crank-registry/tests/integration/import_auth_binding.rs"
],
"existing_suite_baseline": {
"command": "cargo test -p crank-import --all-targets",
"result": "PASS",
"passed": 99,
"failed": 0,
"ignored": 0,
"timing": "current implementation"
},
"other_passed_suites": [
{
"command": "cargo test -p admin-api --test integration openapi_import -- --test-threads=1",
"result": "PASS",
"passed": 24,
"failed": 0,
"ignored": 0,
"filtered_out": 64
},
{
"command": "cargo test -p crank-registry --test integration import_auth_binding -- --test-threads=1",
"result": "PASS",
"passed": 5,
"failed": 0,
"ignored": 0,
"filtered_out": 95
},
{
"command": "cargo test -p crank-runtime --all-targets",
"result": "PASS",
"passed": 83,
"failed": 0,
"ignored": 0,
"reported_by": "root"
}
],
"other_checks": [
{
"command": "just verify",
"result": "PASS",
"detail": "Repeat full workspace gate after review fixes exited 0; earlier file-size, advisory, fmt/clippy and stdout-redirection failures were resolved."
},
{
"command": "git diff --check",
"result": "PASS"
}
],
"all_tests_skipped": false,
"expected_to_fail": false,
"e2e_tests": 0,
"note": "Historical RED evidence is preserved; all active acceptance tests and the final just verify gate pass. No skipped scaffold is counted as RED."
}
+31
View File
@@ -542,17 +542,48 @@ pub struct OpenApiImportPreviewResponse {
pub job_id: String,
pub expires_at: String,
pub preview: crank_import::rest::ImportPreview,
/// Compatible Auth Profiles visible to the current workspace, grouped by
/// operation and security alternative. Profile credentials are never
/// included.
pub auth_profile_choices: Vec<OpenApiAuthProfileChoice>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiAuthProfileChoice {
pub operation_key: String,
pub alternative_index: usize,
pub scheme_names: Vec<String>,
pub supported: bool,
pub profiles: Vec<OpenApiAuthProfileOption>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiAuthProfileOption {
pub id: String,
pub name: String,
pub kind: AuthKind,
}
#[derive(Clone, Debug, Deserialize)]
pub struct OpenApiImportCreatePayload {
pub selected_operation_keys: Vec<String>,
#[serde(default)]
pub auth_mappings: Vec<OpenApiAuthMapping>,
#[serde(default)]
pub server_url: Option<String>,
#[serde(default = "default_openapi_conflict_mode")]
pub conflict_mode: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OpenApiAuthMapping {
pub operation_key: String,
pub alternative_index: usize,
/// `None` is an explicit unresolved mapping. It is never actionable for a
/// required authentication alternative.
pub auth_profile_id: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportCreateResponse {
pub created: Vec<OpenApiImportCreatedOperation>,
+51
View File
@@ -135,6 +135,41 @@ impl ApiError {
"OpenAPI document is invalid or unsupported"
}
}
(_, "literal_credentials_rejected") => {
if russian {
"документ OpenAPI содержит literal credential и отклонён"
} else {
"OpenAPI document contains literal credential material and was rejected"
}
}
(_, "auth_mapping_required") => {
if russian {
"для требования аутентификации выберите доступный профиль"
} else {
"an available Auth Profile mapping is required for this authentication requirement"
}
}
(_, "auth_mapping_unsupported") => {
if russian {
"это требование аутентификации не поддерживается импортом"
} else {
"this authentication requirement is not supported by import"
}
}
(_, "auth_mapping_invalid") => {
if russian {
"привязка аутентификации в запросе импорта некорректна"
} else {
"OpenAPI import authentication mapping is invalid"
}
}
(_, "auth_binding_unavailable") => {
if russian {
"выбранный профиль аутентификации недоступен для этого импорта"
} else {
"selected Auth Profile is unavailable for this import"
}
}
(_, "no_methods") => {
if russian {
"документ OpenAPI не содержит поддерживаемых методов"
@@ -195,6 +230,18 @@ impl ApiError {
}
}
pub(crate) fn openapi_auth_binding_unavailable(locale: OpenApiUploadLocale) -> Self {
let message = if locale == OpenApiUploadLocale::Ru {
"выбранный профиль аутентификации недоступен для этого импорта"
} else {
"selected Auth Profile is unavailable for this import"
};
Self::unprocessable_with_context(
message,
json!({ "error_code": "openapi_auth_binding_unavailable" }),
)
}
pub(crate) fn rate_limited_with_context(message: impl Into<String>, context: Value) -> Self {
Self::RateLimited {
message: message.into(),
@@ -675,6 +722,10 @@ impl From<RegistryError> for ApiError {
"value": value,
}),
),
RegistryError::ImportAuthBindingUnavailable => Self::unprocessable_with_context(
"selected Auth Profile is unavailable for this import",
json!({ "error_code": "openapi_auth_binding_unavailable" }),
),
RegistryError::YamlImportJobNotFound { job_id } => Self::validation_with_context(
format!("yaml import job {job_id} was not found"),
json!({ "job_id": job_id }),
+105 -9
View File
@@ -12,8 +12,8 @@ use crank_import::rest::{
use crank_registry::{
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
CreateArtifactSourceRequest, CreateImportJobRequest, FinishImportJobRequest,
ImportConflictMode, ImportJobApplyResult, ImportJobId, ImportJobKind, ImportJobSourceEnvelope,
ImportJobStatus, ImportOperationDraft, RegistryError,
ImportAuthRequirement, ImportConflictMode, ImportJobApplyResult, ImportJobId, ImportJobKind,
ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, RegistryError,
};
use serde_json::json;
use sha2::{Digest, Sha256};
@@ -23,7 +23,7 @@ use tracing::{info, instrument};
use crate::{
error::ApiError,
service::{
AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
AdminService, OpenApiAuthMapping, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
OpenApiImportCreatedOperation, OpenApiImportPreviewResponse, OpenApiImportSkippedOperation,
OpenApiUpload, OpenApiUploadLocale, OperationPayload, new_prefixed_id,
},
@@ -31,6 +31,7 @@ use crate::{
mod external_references;
mod job_contract;
mod security;
use external_references::ImportReplayContext;
use job_contract::{
SourceDetachGuard, import_job_dependencies, import_job_normalization_config, import_job_source,
@@ -52,6 +53,14 @@ impl AdminService {
self.ensure_workspace_exists(workspace_id).await?;
validate_openapi_upload(&upload)?;
let literal_findings = crank_import::rest::scan_secret_literals(&upload.bytes)
.map_err(|_| ApiError::openapi_upload(upload.locale, "invalid_document"))?;
if !literal_findings.is_empty() {
return Err(ApiError::openapi_upload(
upload.locale,
"literal_credentials_rejected",
));
}
let OpenApiUpload {
bytes,
mime_type,
@@ -105,6 +114,14 @@ impl AdminService {
let mut dependencies = self
.materialize_external_reference_snapshots(workspace_id, &verified.bytes, now)
.await;
if dependencies.secret_literal_rejected {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(
locale,
"literal_credentials_rejected",
));
}
let parsed = match parse_verified_preview(
verified.bytes,
artifact.artifact_ref().digest_hex().to_owned(),
@@ -132,6 +149,17 @@ impl AdminService {
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(locale, "no_methods"));
}
let auth_profile_choices = match self
.openapi_auth_profile_choices(workspace_id, &parsed.preview)
.await
{
Ok(choices) => choices,
Err(error) => {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(error);
}
};
let source_envelope = ImportJobSourceEnvelope {
source_id,
digest: artifact.artifact_ref().clone(),
@@ -184,6 +212,7 @@ impl AdminService {
expires_at: expires_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
auth_profile_choices,
preview: parsed.preview,
})
}
@@ -247,6 +276,15 @@ impl AdminService {
));
}
let finished_at = OffsetDateTime::now_utc();
let mut auth_mappings = BTreeMap::new();
for mapping in &payload.auth_mappings {
if auth_mappings
.insert(mapping.operation_key.clone(), mapping)
.is_some()
{
return Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"));
}
}
let application_key = openapi_application_key(&payload)?;
let conflict_mode = if payload.conflict_mode == "skip" {
ImportConflictMode::Skip
@@ -425,6 +463,20 @@ impl AdminService {
candidates.insert(operation.key.clone(), operation);
}
}
if auth_mappings.iter().any(|(operation_key, mapping)| {
!selected.contains(operation_key)
|| candidates.get(operation_key).is_none_or(|candidate| {
mapping.alternative_index >= candidate.security_requirements.alternatives.len()
})
}) {
return self
.fail_openapi_import_or_replay(
&replay_context,
"auth_mapping_invalid",
ApiError::openapi_upload(locale, "auth_mapping_invalid"),
)
.await;
}
let mut skipped = Vec::new();
let mut operations = Vec::new();
@@ -438,6 +490,26 @@ impl AdminService {
});
continue;
};
let (auth_profile_ref, auth_requirement) = match self
.resolve_openapi_auth_binding(
workspace_id,
candidate,
auth_mappings.get(&operation_key).copied(),
locale,
)
.await
{
Ok(binding) => binding,
Err(error) => {
return self
.fail_openapi_import_or_replay(
&replay_context,
"auth_binding_unavailable",
error,
)
.await;
}
};
let mut draft =
operation_draft_from_candidate(candidate, payload.server_url.as_deref());
attach_import_findings(&mut draft, candidate);
@@ -459,7 +531,7 @@ impl AdminService {
idempotency: None,
safety: None,
approval_policy: None,
auth_profile_ref: None,
auth_profile_ref,
headers: BTreeMap::new(),
},
tool_description: draft.tool_description,
@@ -468,6 +540,7 @@ impl AdminService {
operations.push(ImportOperationDraft {
operation_key: candidate.key.clone(),
operation,
auth_requirement,
});
}
@@ -482,7 +555,13 @@ impl AdminService {
pre_skipped: &skipped,
finished_at: &finished_at,
})
.await?;
.await
.map_err(|error| match error {
RegistryError::ImportAuthBindingUnavailable => {
ApiError::openapi_auth_binding_unavailable(locale)
}
error => error.into(),
})?;
Ok(openapi_import_response(applied))
}
@@ -639,11 +718,13 @@ fn import_replay_contract(
if normalizer == NORMALIZER_VERSION && projection == PROJECTION_VERSION {
return Ok(ImportReplayContract::Current);
}
// Historical preview payloads are replayed as their persisted projection.
// They must never be parsed again with the current v4 semantics.
// Historical v2-v4 preview payloads are replayed as their persisted
// projection. They must never be parsed again with current v5 semantics.
if !matches!(
(normalizer, projection),
("normalized-ir-v2", "preview-v2") | ("normalized-ir-v3", "preview-v3")
("normalized-ir-v2", "preview-v2")
| ("normalized-ir-v3", "preview-v3")
| ("normalized-ir-v4", "preview-v4")
) {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
@@ -817,7 +898,15 @@ fn preview_has_blocker(
.flat_map(|group| group.operations.iter())
.filter(|operation| selected_operation_keys.contains(&operation.key))
.flat_map(|operation| operation.findings.iter())
.any(|finding| finding.severity == ImportFindingSeverity::Error)
.any(|finding| {
finding.severity == ImportFindingSeverity::Error
&& !matches!(
finding.code.as_str(),
"unsupported_security_and_requirement"
| "unsupported_security_scheme"
| "unresolved_security_scheme"
)
})
}
fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result<String, ApiError> {
@@ -826,8 +915,15 @@ fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result<Strin
.iter()
.cloned()
.collect::<BTreeSet<_>>();
let mut auth_mappings = payload.auth_mappings.clone();
auth_mappings.sort_by(|left, right| {
left.operation_key
.cmp(&right.operation_key)
.then(left.alternative_index.cmp(&right.alternative_index))
});
let canonical = serde_json::to_vec(&json!({
"selected_operation_keys": selected_operation_keys,
"auth_mappings": auth_mappings,
"server_url": payload.server_url.as_deref(),
"conflict_mode": payload.conflict_mode.as_str(),
}))
@@ -4,6 +4,7 @@ use tracing::warn;
pub(super) struct MaterializedDependencies {
pub(super) snapshots: Vec<crank_import::rest::ExternalDocumentSnapshot>,
pub(super) secret_literal_rejected: bool,
envelopes: Vec<ImportJobSourceEnvelope>,
canonical_uris: Vec<String>,
guards: Vec<SourceDetachGuard>,
@@ -21,6 +22,7 @@ impl MaterializedDependencies {
fn empty() -> Self {
Self {
snapshots: Vec::new(),
secret_literal_rejected: false,
envelopes: Vec::new(),
canonical_uris: Vec::new(),
guards: Vec::new(),
@@ -207,6 +209,26 @@ impl AdminService {
);
continue;
};
match crank_import::rest::scan_secret_literals(&bytes) {
Ok(findings) if !findings.is_empty() => {
materialization_failure(
"security_scan",
"literal_credentials_rejected",
result.snapshots.len(),
);
result.secret_literal_rejected = true;
break;
}
Ok(_) => {}
Err(_) => {
materialization_failure(
"security_scan",
"invalid_document",
result.snapshots.len(),
);
continue;
}
}
let Ok(references) = crank_import::rest::external_reference_uris(
&document,
&self.external_reference_normalization,
@@ -368,6 +390,14 @@ impl AdminService {
}
let document = String::from_utf8(verified.bytes)
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
let findings = crank_import::rest::scan_secret_literals(document.as_bytes())
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
if !findings.is_empty() {
return Err(ApiError::openapi_upload(
locale,
"literal_credentials_rejected",
));
}
let digest =
crank_import::rest::SourceDigest::parse(dependency.digest.digest_hex().to_owned())
.map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?;
@@ -0,0 +1,195 @@
use super::*;
use crate::service::{OpenApiAuthProfileChoice, OpenApiAuthProfileOption};
use crank_core::{AuthConfig, AuthProfile, AuthProfileId};
impl AdminService {
pub(super) async fn openapi_auth_profile_choices(
&self,
workspace_id: &WorkspaceId,
preview: &crank_import::rest::ImportPreview,
) -> Result<Vec<OpenApiAuthProfileChoice>, ApiError> {
let mut profiles = self
.registry
.list_auth_profiles(workspace_id)
.await?
.into_iter()
.filter(|profile| profile.kind == profile.config.kind())
.collect::<Vec<_>>();
let mut available_profiles = Vec::with_capacity(profiles.len());
for profile in profiles.drain(..) {
if auth_profile_is_available(&self.registry, workspace_id, &profile).await? {
available_profiles.push(profile);
}
}
let mut choices = Vec::new();
for operation in preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
{
for (alternative_index, alternative) in operation
.security_requirements
.alternatives
.iter()
.enumerate()
{
let scheme = (alternative.schemes.len() == 1)
.then(|| operation.security_schemes.get(&alternative.schemes[0]))
.flatten();
let requirement = scheme.and_then(import_auth_requirement);
let supported = alternative.supported
&& (alternative.schemes.is_empty() || requirement.is_some());
let options = requirement
.as_ref()
.map(|requirement| {
available_profiles
.iter()
.filter(|profile| auth_profile_matches(profile, requirement))
.map(|profile| OpenApiAuthProfileOption {
id: profile.id.as_str().to_owned(),
name: profile.name.clone(),
kind: profile.kind,
})
.collect()
})
.unwrap_or_default();
choices.push(OpenApiAuthProfileChoice {
operation_key: operation.key.clone(),
alternative_index,
scheme_names: alternative.schemes.clone(),
supported,
profiles: options,
});
}
}
Ok(choices)
}
pub(super) async fn resolve_openapi_auth_binding(
&self,
workspace_id: &WorkspaceId,
candidate: &ImportOperationCandidate,
mapping: Option<&OpenApiAuthMapping>,
locale: OpenApiUploadLocale,
) -> Result<(Option<AuthProfileId>, Option<ImportAuthRequirement>), ApiError> {
let alternatives = &candidate.security_requirements.alternatives;
if alternatives.is_empty() {
return if mapping.is_none() {
Ok((None, None))
} else {
Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"))
};
}
let mapping =
mapping.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?;
let alternative = alternatives
.get(mapping.alternative_index)
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_invalid"))?;
if !alternative.supported {
return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported"));
}
if alternative.schemes.is_empty() {
return if mapping.auth_profile_id.is_none() {
Ok((None, None))
} else {
Err(ApiError::openapi_upload(locale, "auth_mapping_invalid"))
};
}
if alternative.schemes.len() != 1 {
return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported"));
}
let scheme = candidate
.security_schemes
.get(&alternative.schemes[0])
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?;
let requirement = import_auth_requirement(scheme)
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?;
let profile_id = mapping
.auth_profile_id
.as_deref()
.filter(|value| !value.is_empty())
.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?;
let profile_id = AuthProfileId::new(profile_id.to_owned());
let profile = self
.registry
.get_auth_profile(workspace_id, &profile_id)
.await?
.ok_or_else(|| ApiError::openapi_auth_binding_unavailable(locale))?;
if !auth_profile_matches(&profile, &requirement)
|| !auth_profile_is_available(&self.registry, workspace_id, &profile).await?
{
return Err(ApiError::openapi_auth_binding_unavailable(locale));
}
Ok((Some(profile_id), Some(requirement)))
}
}
fn import_auth_requirement(
scheme: &crank_import::rest::NormalizedSecurityScheme,
) -> Option<ImportAuthRequirement> {
use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation};
match &scheme.kind {
NormalizedSecuritySchemeKind::Http { scheme, .. }
if scheme.eq_ignore_ascii_case("bearer") =>
{
Some(ImportAuthRequirement::Bearer)
}
NormalizedSecuritySchemeKind::Http { scheme, .. }
if scheme.eq_ignore_ascii_case("basic") =>
{
Some(ImportAuthRequirement::Basic)
}
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Header,
parameter_name,
..
} => Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: parameter_name.clone(),
}),
NormalizedSecuritySchemeKind::ApiKey {
location: SecurityParameterLocation::Query,
parameter_name,
..
} => Some(ImportAuthRequirement::ApiKeyQuery {
parameter_name: parameter_name.clone(),
}),
_ => None,
}
}
fn auth_profile_matches(profile: &AuthProfile, requirement: &ImportAuthRequirement) -> bool {
if profile.kind != profile.config.kind() {
return false;
}
match (requirement, &profile.config) {
(ImportAuthRequirement::Bearer, AuthConfig::Bearer(_))
| (ImportAuthRequirement::Basic, AuthConfig::Basic(_)) => true,
(
ImportAuthRequirement::ApiKeyHeader { parameter_name },
AuthConfig::ApiKeyHeader(config),
) => config.header_name.eq_ignore_ascii_case(parameter_name),
(
ImportAuthRequirement::ApiKeyQuery { parameter_name },
AuthConfig::ApiKeyQuery(config),
) => config.param_name == *parameter_name,
_ => false,
}
}
async fn auth_profile_is_available(
registry: &crank_registry::PostgresRegistry,
workspace_id: &WorkspaceId,
profile: &AuthProfile,
) -> Result<bool, ApiError> {
for secret_id in profile.config.secret_ids() {
if registry
.get_current_secret_version(workspace_id, secret_id)
.await?
.is_none()
{
return Ok(false);
}
}
Ok(true)
}
@@ -1,9 +1,15 @@
use admin_api::service::{
AdminServiceBuilder, OpenApiImportCreatePayload, OpenApiUpload, OpenApiUploadLocale,
AdminServiceBuilder, AuthProfilePayload, OpenApiAuthMapping, OpenApiImportCreatePayload,
OpenApiImportCreateResponse, OpenApiUpload, OpenApiUploadLocale, SecretPayload,
};
use crank_core::WorkspaceId;
use crank_registry::ImportJobStatus;
use crank_core::{
AuthConfig, AuthKind, BearerAuthConfig, SecretId, SecretKind, Workspace, WorkspaceId,
WorkspaceStatus,
};
use crank_registry::{CreateWorkspaceRequest, ImportJobStatus};
use serde_json::json;
use serial_test::serial;
use sha2::Digest;
use super::common::{
test_auth_settings, test_registry, test_secret_crypto, test_service, test_storage_root,
@@ -11,6 +17,7 @@ use super::common::{
mod external_references;
mod http_contract;
mod security;
const OPENAPI3: &str = r#"
openapi: 3.0.3
@@ -78,11 +85,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"
"normalized-ir-v5"
);
assert_eq!(
preview_job.preview_payload["normalization"]["projection_version"],
"preview-v4"
"preview-v5"
);
assert!(preview_job.preview_payload["normalization"]["ir_fingerprint"].is_string());
@@ -92,6 +99,7 @@ async fn previews_openapi_and_creates_draft_operations() {
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -110,6 +118,7 @@ async fn previews_openapi_and_creates_draft_operations() {
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -162,6 +171,7 @@ async fn previews_openapi_and_creates_draft_operations() {
&skip_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -184,6 +194,7 @@ async fn previews_openapi_and_creates_draft_operations() {
&rename_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "rename".to_owned(),
},
@@ -213,6 +224,7 @@ async fn unknown_selected_operations_are_persisted_in_the_canonical_replay() {
.unwrap();
let payload = OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned(), "GET /missing".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
};
@@ -254,6 +266,7 @@ async fn concurrent_openapi_import_replays_the_same_atomic_result() {
let job_id = preview.job_id.as_str().into();
let payload = OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "rename".to_owned(),
};
@@ -308,6 +321,7 @@ async fn concurrent_openapi_import_replays_the_same_atomic_result() {
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -349,6 +363,7 @@ async fn apply_fails_closed_when_the_preview_parser_contract_drifts() {
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "rename".to_owned(),
},
@@ -425,6 +440,7 @@ async fn apply_fails_closed_for_each_normalization_contract_field_and_digest_sha
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "rename".to_owned(),
}
@@ -493,6 +509,7 @@ async fn legacy_job_without_normalization_contract_replays_until_expiry() {
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -547,6 +564,7 @@ paths:
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /valid".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -634,6 +652,7 @@ paths:
&valid_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /valid".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -652,6 +671,7 @@ paths:
&full_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /valid".to_owned(), "GET /broken".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "rename".to_owned(),
},
@@ -733,6 +753,7 @@ components:
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /referenced".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -99,6 +99,7 @@ paths:
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /items".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -117,6 +118,73 @@ paths:
assert_eq!(active_sources, 0);
}
#[tokio::test]
#[serial]
async fn external_literal_credential_is_rejected_before_snapshot_persistence() {
let canary = "external-atdd-secret-canary-2-10";
let app = Router::new().route(
"/schemas.yaml",
get(move || async move {
format!(
"Item: {{ type: object, properties: {{ token: {{ type: string, example: '{canary}' }} }} }}"
)
}),
);
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 origin = format!("http://{address}");
let registry = test_registry().await;
let service = test_service_with_external_references(
registry.clone(),
test_storage_root("openapi_import_external_literal_credential"),
vec![format!("{origin}/")],
);
let workspace_id = WorkspaceId::new("ws_default");
let upload = OpenApiUpload {
bytes: format!(
r#"
openapi: 3.1.0
info: {{ title: External secret canary }}
paths:
/items:
get:
responses:
'200':
description: ok
content:
application/json:
schema: {{ $ref: '{origin}/schemas.yaml#/Item' }}
"#
)
.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
};
let error = service
.preview_openapi_import(&workspace_id, upload)
.await
.unwrap_err();
assert!(!format!("{error:?}").contains(canary));
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();
let import_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!(dependency_sources, 0);
assert_eq!(import_jobs, 0);
}
#[tokio::test]
#[serial]
async fn missing_external_snapshot_fails_closed_before_draft_mutation() {
@@ -188,6 +256,7 @@ paths:
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /items".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -270,6 +339,7 @@ paths:
&corrupt_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /items".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -522,6 +592,7 @@ async fn exact_v2_contract_replays_persisted_preview_without_v3_or_dependency_re
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -592,6 +663,84 @@ async fn exact_v3_contract_replays_persisted_preview_without_v4_or_dependency_re
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
)
.await
.unwrap();
assert_eq!(result.created.len(), 1);
let completed = registry
.get_import_job(&workspace_id, &job_id)
.await
.unwrap()
.unwrap();
assert_eq!(completed.status, ImportJobStatus::Completed);
}
#[tokio::test]
#[serial]
async fn exact_v4_contract_replays_legacy_preview_without_v5_reparse_or_dependency_reads() {
use sha2::Digest;
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_v4_compatibility"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace_id, openapi_upload())
.await
.unwrap();
let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into();
let mut job = registry
.get_import_job(&workspace_id, &job_id)
.await
.unwrap()
.unwrap();
let missing_dependency = serde_json::json!([{
"source_id": "src_openapi_dep_v4_must_not_be_read",
"digest": format!("sha256:{}", "0".repeat(64)),
"canonical_uri": "https://schemas.example.test/v4.yaml"
}]);
let mut persisted_preview = job.preview_payload["preview"].clone();
for group in persisted_preview["groups"].as_array_mut().unwrap() {
for operation in group["operations"].as_array_mut().unwrap() {
let object = operation.as_object_mut().unwrap();
object.remove("security_requirements");
object.remove("security_schemes");
}
}
let expected_preview_digest = format!(
"{:x}",
sha2::Sha256::digest(serde_json::to_vec(&persisted_preview).unwrap())
);
job.preview_payload["preview"] = persisted_preview;
job.preview_payload["preview_digest"] = serde_json::json!(expected_preview_digest);
job.preview_payload["normalization"]["normalizer_version"] =
serde_json::json!("normalized-ir-v4");
job.preview_payload["normalization"]["projection_version"] = serde_json::json!("preview-v4");
job.preview_payload["normalization"]["ir_fingerprint"] = serde_json::json!("a".repeat(64));
job.preview_payload["dependency_snapshots"] = missing_dependency;
sqlx::query("update import_jobs set preview_payload = $1 where workspace_id = $2 and id = $3")
.bind(&job.preview_payload)
.bind(workspace_id.as_str())
.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 /v2/latest".to_owned()],
auth_mappings: vec![],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
@@ -108,6 +108,7 @@ async fn http_contract_preview_apply_and_replay_preserve_json_multipart_schemas_
let payload = OpenApiImportCreatePayload {
selected_operation_keys: vec![candidate.key.clone()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
};
@@ -199,6 +200,7 @@ async fn xml_and_unsupported_media_block_apply_without_registry_mutation() {
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec![candidate.key.clone()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -0,0 +1,659 @@
use super::*;
#[tokio::test]
#[serial]
async fn literal_credential_is_rejected_before_any_import_artifact_or_job_is_created() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_literal_credential_rejected"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let canary = "atdd-secret-canary-2-10-api";
let upload = OpenApiUpload {
bytes: format!(
r#"
openapi: 3.0.3
info: {{ title: Secret canary }}
paths:
/items:
get:
responses:
'200': {{ description: ok }}
x-api-key: {canary}
"#
)
.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
};
let error = service
.preview_openapi_import(&workspace_id, upload)
.await
.unwrap_err();
assert!(!format!("{error:?}").contains(canary));
let import_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();
let artifact_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();
assert_eq!(import_jobs, 0);
assert_eq!(artifact_sources, 0);
}
#[tokio::test]
#[serial]
async fn auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current()
{
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_auth_profile_binding"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "shared-openapi-token".to_owned(),
kind: SecretKind::Token,
value: json!("integration-secret-canary"),
},
None,
)
.await
.unwrap();
let ciphertext = registry
.get_current_secret_version(&workspace_id, &secret.id)
.await
.unwrap()
.unwrap()
.secret_version
.ciphertext;
let secret_hash = format!("{:x}", sha2::Sha256::digest(b"integration-secret-canary"));
let profile_a = create_bearer_profile(&service, &workspace_id, "profile-a", &secret.id).await;
let profile_b = create_bearer_profile(&service, &workspace_id, "profile-b", &secret.id).await;
let preview = service
.preview_openapi_import(&workspace_id, two_protected_operations_upload())
.await
.unwrap();
assert_eq!(preview.auth_profile_choices.len(), 2);
for choice in &preview.auth_profile_choices {
assert!(choice.supported);
assert_eq!(choice.profiles.len(), 2);
}
let response = serde_json::to_string(&preview).unwrap();
assert!(!response.contains("integration-secret-canary"));
let applied = service
.create_openapi_import(
&workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /a".to_owned(), "GET /b".to_owned()],
auth_mappings: vec![
OpenApiAuthMapping {
operation_key: "GET /a".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile_a.id.as_str().to_owned()),
},
OpenApiAuthMapping {
operation_key: "GET /b".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile_b.id.as_str().to_owned()),
},
],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await
.unwrap();
assert_eq!(applied.created.len(), 2);
for created in &applied.created {
let version = service
.get_operation_version(
&workspace_id,
&created.operation_id.as_str().into(),
created.version,
)
.await
.unwrap();
let profile_ref = version
.snapshot
.execution_config
.auth_profile_ref
.as_ref()
.unwrap();
let serialized_operation = serde_json::to_string(&version.snapshot).unwrap();
assert!(serialized_operation.contains(profile_ref.as_str()));
assert!(!serialized_operation.contains("integration-secret-canary"));
assert!(!serialized_operation.contains(&ciphertext));
assert!(!serialized_operation.contains(&secret_hash));
}
let changed_mapping_replay = service
.create_openapi_import(
&workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /a".to_owned(), "GET /b".to_owned()],
auth_mappings: vec![
OpenApiAuthMapping {
operation_key: "GET /a".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile_b.id.as_str().to_owned()),
},
OpenApiAuthMapping {
operation_key: "GET /b".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile_a.id.as_str().to_owned()),
},
],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await;
assert!(changed_mapping_replay.is_err());
assert_eq!(
service.list_operations(&workspace_id).await.unwrap().len(),
2
);
let now = time::OffsetDateTime::now_utc();
let foreign_workspace_id = WorkspaceId::new("ws_openapi_auth_foreign");
let foreign_workspace = Workspace {
id: foreign_workspace_id.clone(),
slug: "openapi-auth-foreign".to_owned(),
display_name: "OpenAPI Auth Foreign".to_owned(),
status: WorkspaceStatus::Active,
settings: json!({}),
created_at: now,
updated_at: now,
};
registry
.create_workspace(CreateWorkspaceRequest {
workspace: &foreign_workspace,
})
.await
.unwrap();
let foreign_secret = service
.create_secret(
&foreign_workspace_id,
None,
SecretPayload {
name: "foreign-token".to_owned(),
kind: SecretKind::Token,
value: json!("foreign-secret-canary"),
},
None,
)
.await
.unwrap();
let foreign_profile = create_bearer_profile(
&service,
&foreign_workspace_id,
"foreign-profile",
&foreign_secret.id,
)
.await;
let choices_preview = service
.preview_openapi_import(&workspace_id, two_protected_operations_upload())
.await
.unwrap();
assert!(
choices_preview
.auth_profile_choices
.iter()
.flat_map(|choice| &choice.profiles)
.all(|profile| profile.id != foreign_profile.id.as_str())
);
let missing_error =
apply_first_protected_mapping(&service, &workspace_id, "auth_missing_profile")
.await
.unwrap_err();
let foreign_error =
apply_first_protected_mapping(&service, &workspace_id, foreign_profile.id.as_str())
.await
.unwrap_err();
assert_eq!(format!("{missing_error:?}"), format!("{foreign_error:?}"));
assert!(!format!("{foreign_error:?}").contains(foreign_profile.id.as_str()));
sqlx::query("update secrets set status = 'disabled' where workspace_id = $1 and id = $2")
.bind(workspace_id.as_str())
.bind(secret.id.as_str())
.execute(registry.pool())
.await
.unwrap();
let disabled_preview = service
.preview_openapi_import(&workspace_id, two_protected_operations_upload())
.await
.unwrap();
assert!(
disabled_preview
.auth_profile_choices
.iter()
.all(|choice| choice.profiles.is_empty())
);
let before_disabled_apply = service.list_operations(&workspace_id).await.unwrap().len();
let disabled_error = service
.create_openapi_import_with_locale(
&workspace_id,
&disabled_preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /a".to_owned()],
auth_mappings: vec![OpenApiAuthMapping {
operation_key: "GET /a".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile_a.id.as_str().to_owned()),
}],
server_url: None,
conflict_mode: "skip".to_owned(),
},
OpenApiUploadLocale::Ru,
)
.await
.unwrap_err();
match &disabled_error {
admin_api::error::ApiError::Unprocessable { message, context } => {
assert_eq!(
message,
"выбранный профиль аутентификации недоступен для этого импорта"
);
assert_eq!(
context.as_ref().unwrap()["error_code"],
"openapi_auth_binding_unavailable"
);
}
other => panic!("expected localized auth binding error, got {other:?}"),
}
let disabled_shared_profile_error =
apply_first_protected_mapping(&service, &workspace_id, profile_b.id.as_str())
.await
.unwrap_err();
assert_eq!(
format!("{disabled_shared_profile_error:?}"),
format!("{missing_error:?}")
);
assert_eq!(
service.list_operations(&workspace_id).await.unwrap().len(),
before_disabled_apply
);
}
#[tokio::test]
#[serial]
async fn selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics()
{
let registry = test_registry().await;
let service = test_service(
registry,
test_storage_root("openapi_import_auth_alternatives"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "or-token".to_owned(),
kind: SecretKind::Token,
value: json!("or-profile-secret"),
},
None,
)
.await
.unwrap();
let profile = create_bearer_profile(&service, &workspace_id, "or-bearer", &secret.id).await;
let preview = service
.preview_openapi_import(&workspace_id, security_alternatives_upload())
.await
.unwrap();
let choices = preview
.auth_profile_choices
.iter()
.filter(|choice| choice.operation_key == "GET /choice")
.collect::<Vec<_>>();
assert_eq!(choices.len(), 2);
assert!(choices[0].supported);
assert_eq!(choices[0].profiles.len(), 1);
assert!(!choices[1].supported);
let either = preview
.auth_profile_choices
.iter()
.filter(|choice| choice.operation_key == "GET /either")
.collect::<Vec<_>>();
assert_eq!(either.len(), 2);
assert!(either[1].supported);
let applied = service
.create_openapi_import(
&workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec![
"GET /choice".to_owned(),
"GET /either".to_owned(),
"GET /public".to_owned(),
],
auth_mappings: vec![
OpenApiAuthMapping {
operation_key: "GET /choice".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile.id.as_str().to_owned()),
},
OpenApiAuthMapping {
operation_key: "GET /either".to_owned(),
alternative_index: 1,
auth_profile_id: None,
},
],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await
.unwrap();
assert_eq!(applied.created.len(), 3);
for created in &applied.created {
let version = service
.get_operation_version(
&workspace_id,
&created.operation_id.as_str().into(),
created.version,
)
.await
.unwrap();
let auth_profile_ref = version.snapshot.execution_config.auth_profile_ref;
match created.operation_key.as_str() {
"GET /choice" => assert_eq!(auth_profile_ref, Some(profile.id.clone())),
"GET /either" | "GET /public" => assert!(auth_profile_ref.is_none()),
other => panic!("unexpected operation {other}"),
}
}
}
#[tokio::test]
#[serial]
async fn changed_security_scheme_after_preview_fails_digest_check_without_draft_mutation() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_security_scheme_drift"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let secret = service
.create_secret(
&workspace_id,
None,
SecretPayload {
name: "drift-test-token".to_owned(),
kind: SecretKind::Token,
value: json!("scheme-drift-profile-secret"),
},
None,
)
.await
.unwrap();
let profile =
create_bearer_profile(&service, &workspace_id, "drift-test-profile", &secret.id).await;
let bearer_preview = service
.preview_openapi_import(&workspace_id, security_scheme_upload(false))
.await
.unwrap();
let api_key_preview = service
.preview_openapi_import(&workspace_id, security_scheme_upload(true))
.await
.unwrap();
let bearer_job: crank_registry::ImportJobId = bearer_preview.job_id.as_str().into();
let api_key_job: crank_registry::ImportJobId = api_key_preview.job_id.as_str().into();
let api_key_source = registry
.get_import_job(&workspace_id, &api_key_job)
.await
.unwrap()
.unwrap()
.preview_payload["source"]
.clone();
sqlx::query(
"update import_jobs
set preview_payload = jsonb_set(
jsonb_set(preview_payload, '{source,source_id}', $1, false),
'{source,digest}', $2, false
)
where workspace_id = $3 and id = $4",
)
.bind(&api_key_source["source_id"])
.bind(&api_key_source["digest"])
.bind(workspace_id.as_str())
.bind(bearer_job.as_str())
.execute(registry.pool())
.await
.unwrap();
let error = service
.create_openapi_import(
&workspace_id,
&bearer_job,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /secure".to_owned()],
auth_mappings: vec![OpenApiAuthMapping {
operation_key: "GET /secure".to_owned(),
alternative_index: 0,
auth_profile_id: Some(profile.id.as_str().to_owned()),
}],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await
.unwrap_err();
match error {
admin_api::error::ApiError::Unprocessable { context, .. } => assert_eq!(
context.unwrap()["error_code"],
"openapi_upload.source_integrity"
),
other => panic!("expected source integrity error, got {other:?}"),
}
let job = registry
.get_import_job(&workspace_id, &bearer_job)
.await
.unwrap()
.unwrap();
assert_eq!(
job.error_text.as_deref(),
Some("import_replay_verification_failed")
);
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
}
#[tokio::test]
#[serial]
async fn auth_mapping_for_unselected_operation_is_rejected_without_draft_mutation() {
let registry = test_registry().await;
let service = test_service(
registry,
test_storage_root("openapi_import_unselected_auth_mapping"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace_id, two_protected_operations_upload())
.await
.unwrap();
assert!(
service
.create_openapi_import(
&workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /a".to_owned()],
auth_mappings: vec![OpenApiAuthMapping {
operation_key: "GET /b".to_owned(),
alternative_index: 0,
auth_profile_id: None,
}],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await
.is_err()
);
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
}
fn two_protected_operations_upload() -> OpenApiUpload {
OpenApiUpload {
bytes: br#"
openapi: 3.0.3
info: { title: Protected API }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
bearerAuth: { type: http, scheme: bearer }
security:
- bearerAuth: []
paths:
/a:
get:
responses: { '200': { description: ok } }
/b:
get:
responses: { '200': { description: ok } }
"#
.to_vec(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
}
}
fn security_alternatives_upload() -> OpenApiUpload {
OpenApiUpload {
bytes: br#"
openapi: 3.0.3
info: { title: Security alternatives }
servers: [{ url: https://api.example.test }]
components:
securitySchemes:
bearerAuth: { type: http, scheme: bearer }
oauth: { type: oauth2, flows: { implicit: { authorizationUrl: https://auth.example.test, scopes: {} } } }
security:
- bearerAuth: []
- oauth: []
paths:
/choice:
get:
responses: { '200': { description: ok } }
/either:
get:
security:
- bearerAuth: []
- {}
responses: { '200': { description: ok } }
/public:
get:
security: []
responses: { '200': { description: ok } }
"#
.to_vec(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
}
}
fn security_scheme_upload(api_key: bool) -> OpenApiUpload {
let security_scheme = if api_key {
"auth: { type: apiKey, in: header, name: X-Token }"
} else {
"auth: { type: http, scheme: bearer }"
};
OpenApiUpload {
bytes: format!(
"openapi: 3.0.3\ninfo: {{ title: Scheme drift }}\nservers: [{{ url: https://api.example.test }}]\ncomponents:\n securitySchemes:\n {security_scheme}\nsecurity:\n - auth: []\npaths:\n /secure:\n get:\n responses: {{ '200': {{ description: ok }} }}\n"
)
.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
}
}
async fn create_bearer_profile(
service: &admin_api::service::AdminService,
workspace_id: &WorkspaceId,
name: &str,
secret_id: &SecretId,
) -> crank_core::AuthProfile {
service
.create_auth_profile(
workspace_id,
AuthProfilePayload {
name: name.to_owned(),
kind: AuthKind::Bearer,
config: AuthConfig::Bearer(BearerAuthConfig {
header_name: "Authorization".to_owned(),
secret_id: secret_id.clone(),
}),
},
None,
)
.await
.unwrap()
}
async fn apply_first_protected_mapping(
service: &admin_api::service::AdminService,
workspace_id: &WorkspaceId,
auth_profile_id: &str,
) -> Result<OpenApiImportCreateResponse, admin_api::error::ApiError> {
let preview = service
.preview_openapi_import(workspace_id, two_protected_operations_upload())
.await?;
service
.create_openapi_import(
workspace_id,
&preview.job_id.as_str().into(),
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /a".to_owned()],
auth_mappings: vec![OpenApiAuthMapping {
operation_key: "GET /a".to_owned(),
alternative_index: 0,
auth_profile_id: Some(auth_profile_id.to_owned()),
}],
server_url: None,
conflict_mode: "skip".to_owned(),
},
)
.await
}
@@ -512,6 +512,7 @@ async fn apply_rereads_the_scoped_verified_source_and_expiry_detaches_it() {
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /health".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -245,6 +245,7 @@ async fn apply_health_operation(
job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /health".to_owned()],
auth_mappings: vec![],
server_url: None,
conflict_mode: "skip".to_owned(),
},
@@ -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: Default::default(),
operation_id: operation.operation_id.clone(),
suggested_name: name,
suggested_display_name: display_name,
+18 -9
View File
@@ -3,33 +3,41 @@ mod legacy_mapping;
mod legacy_payload;
mod legacy_schema;
mod mapping;
mod metadata_findings;
pub mod model;
mod naming;
mod normalize;
mod normalize_coverage;
mod normalize_finding_validation;
mod normalize_limits;
mod normalize_preview;
mod normalize_projection;
mod normalize_schema;
mod openapi3;
mod openapi3_findings;
mod openapi3_legacy;
mod payload;
mod recommendations;
mod reference;
mod schema;
mod security;
mod security_literals;
mod swagger2;
mod swagger2_findings;
mod swagger2_legacy;
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,
@@ -37,3 +45,4 @@ pub use normalize::{
reference_uris, validate_normalized_ir,
};
pub use payload::operation_draft_from_candidate;
pub use security_literals::{SecretLiteralFinding, scan_secret_literals};
+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>,
+31 -90
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;
@@ -63,9 +61,9 @@ pub fn external_reference_uris(
pub fn preview_document_legacy_v1(document: &str) -> Result<ImportPreview, ImportParseError> {
let root = decode_legacy_v1(document)?;
let mut parsed = match root.get("openapi") {
Some(_) => openapi3::parse_document_legacy_v1(&root)?,
Some(_) => super::openapi3_legacy::parse_document_legacy_v1(&root)?,
None if root.get("swagger").and_then(Value::as_str) == Some("2.0") => {
swagger2::parse_document_legacy_v1(&root)?
super::swagger2_legacy::parse_document_legacy_v1(&root)?
}
None => return Err(ImportParseError::UnsupportedDocument),
};
@@ -82,7 +80,7 @@ pub fn preview_document_legacy_v1(document: &str) -> Result<ImportPreview, Impor
operation.response_schema = Some(resolve_local_ref(&root, &schema, 0));
}
}
Ok(preview_from_document(parsed))
Ok(super::normalize_preview::preview_from_document(parsed))
}
fn decode_legacy_v1(document: &str) -> Result<Value, ImportParseError> {
@@ -135,9 +133,11 @@ 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 redacted_literals = super::security_literals::inspect_and_redact(&mut root);
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,13 +146,15 @@ pub fn normalize_verified_bundle(
}
None => return Err(ImportParseError::UnsupportedDocument),
};
super::metadata_findings::collect_secret_like_examples(
&root,
"",
false,
&mut parsed.findings,
&mut parsed.internal_finding_locations,
);
for literal in redacted_literals {
parsed.findings.push(super::recommendations::document_finding(
"secret_literal_redacted",
"Документ содержит literal credential; значение удалено из normalized representation.",
));
parsed
.internal_finding_locations
.push(Some(literal.location));
}
if parsed.operations.is_empty()
&& !parsed
@@ -174,79 +176,10 @@ 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_preview::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)
}
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);
}
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);
}
@@ -272,6 +205,7 @@ fn canonicalize(
reference_graph: crate::rest::model::ResolvedReferenceGraph,
resolution_findings: Vec<NormalizedFinding>,
) -> Result<NormalizedIr, ImportParseError> {
let security_schemes = document.security_schemes.clone();
let source = ImportSourcePreview {
format: document.format,
version: document.version,
@@ -571,6 +505,8 @@ fn canonicalize(
key: operation.key,
method: operation.method,
path: operation.path,
security_requirements: operation.security_requirements,
security_schemes: security_schemes.clone(),
operation_id: operation.operation_id,
summary: operation.summary,
description: operation.description,
@@ -762,6 +698,7 @@ fn canonicalize(
unresolved_references,
reference_graph,
source,
security_schemes: document.security_schemes,
operations,
findings,
coverage,
@@ -771,7 +708,11 @@ fn canonicalize(
}
pub fn validate_normalized_ir(ir: &NormalizedIr) -> Result<(), ImportParseError> {
if ir.normalizer_version != NORMALIZER_VERSION || ir.projection_version != PROJECTION_VERSION {
let current =
ir.normalizer_version == NORMALIZER_VERSION && ir.projection_version == PROJECTION_VERSION;
let historical_v4 = ir.normalizer_version == super::model::HISTORICAL_NORMALIZER_VERSION
&& ir.projection_version == super::model::HISTORICAL_PROJECTION_VERSION;
if !current && !historical_v4 {
return Err(ImportParseError::InvalidDocument);
}
validate_reference_graph(ir)?;
@@ -924,7 +865,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),
@@ -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 {
let current =
ir.normalizer_version == NORMALIZER_VERSION && ir.projection_version == PROJECTION_VERSION;
let historical_v4 = ir.normalizer_version == super::model::HISTORICAL_NORMALIZER_VERSION
&& ir.projection_version == super::model::HISTORICAL_PROJECTION_VERSION;
if !current && !historical_v4 {
return Err(ImportParseError::InvalidDocument);
}
let mut source_nodes = BTreeMap::new();
@@ -94,6 +98,12 @@ pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseErro
.flat_map(|operation| &operation.findings),
)
.collect::<Vec<_>>();
if findings.iter().any(|finding| {
is_security_finding_code(&finding.code)
&& !super::normalize_finding_validation::matches_source(finding, ir, &source_nodes)
}) {
return Err(ImportParseError::InvalidDocument);
}
for finding in &findings {
if super::normalize_finding_validation::matches_source(finding, ir, &source_nodes)
&& finding.construct_id == format!("{version}:{}", finding.location.pointer)
@@ -145,6 +155,20 @@ pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseErro
Ok(())
}
fn is_security_finding_code(code: &str) -> bool {
matches!(
code,
"invalid_security_requirements"
| "invalid_security_requirement"
| "unresolved_security_scheme"
| "unsupported_security_and_requirement"
| "unsupported_security_scheme"
| "unsupported_security_requirements"
| "invalid_security_scopes"
| "unexpected_security_scopes"
)
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct ExpectedCoverage {
location: SourceLocation,
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use crate::rest::model::{
CanonicalSourceNode, CanonicalSourceValue, ImportFindingSeverity, NormalizedFinding,
NormalizedIr,
NormalizedIr, NormalizedOperation, NormalizedSecuritySchemeKind, SecurityRequirementsSource,
};
pub(super) fn matches_source(
@@ -56,11 +56,158 @@ pub(super) fn matches_source(
"secret_like_example" if finding.severity == ImportFindingSeverity::Warning => {
pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value))
}
"secret_literal_redacted" if finding.severity == ImportFindingSeverity::Warning => {
matches!(&node.value, CanonicalSourceValue::String(value) if value == "[REDACTED]")
}
"invalid_security_requirements"
| "invalid_security_requirement"
| "unresolved_security_scheme"
| "unsupported_security_and_requirement"
| "unsupported_security_scheme"
| "unsupported_security_requirements"
| "invalid_security_scopes"
| "unexpected_security_scopes" => security_finding(finding, ir, nodes),
"invalid_server" if is_error => invalid_server(pointer, node),
_ => false,
}
}
fn security_finding(
finding: &NormalizedFinding,
ir: &NormalizedIr,
nodes: &BTreeMap<String, &CanonicalSourceNode>,
) -> bool {
let Some(operation) = ir
.operations
.iter()
.find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str()))
else {
return false;
};
let Some(requirements_pointer) = requirements_pointer(operation) else {
return false;
};
let pointer = finding.location.pointer.as_str();
let node = nodes.get(pointer).copied();
match finding.code.as_str() {
"invalid_security_requirements" => {
finding.severity == ImportFindingSeverity::Error
&& pointer == requirements_pointer
&& node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Array(_)))
}
"invalid_security_requirement" => {
finding.severity == ImportFindingSeverity::Warning
&& alternative_at(operation, pointer).is_some_and(|_| {
node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Object(_)))
})
}
"unresolved_security_scheme" => {
finding.severity == ImportFindingSeverity::Warning
&& scheme_at(operation, pointer)
.is_some_and(|name| node.is_some() && !ir.security_schemes.contains_key(name))
}
"unsupported_security_and_requirement" => {
finding.severity == ImportFindingSeverity::Warning
&& alternative_at(operation, pointer)
.is_some_and(|alternative| alternative.schemes.len() > 1)
&& node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Object(_)))
}
"unsupported_security_scheme" => {
finding.severity == ImportFindingSeverity::Warning
&& scheme_at(operation, pointer).is_some_and(|name| {
let Some(scheme) = ir.security_schemes.get(name) else {
return false;
};
node.is_some()
&& matches!(
&scheme.kind,
NormalizedSecuritySchemeKind::Unsupported { .. }
| NormalizedSecuritySchemeKind::ApiKey {
location: crate::rest::model::SecurityParameterLocation::Cookie,
..
}
)
})
}
"unsupported_security_requirements" => {
finding.severity == ImportFindingSeverity::Error
&& pointer == requirements_pointer
&& !operation.security_requirements.alternatives.is_empty()
&& operation
.security_requirements
.alternatives
.iter()
.all(|alternative| !alternative.supported)
&& node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Array(_)))
}
"invalid_security_scopes" | "unexpected_security_scopes" => {
if finding.severity != ImportFindingSeverity::Warning {
return false;
}
let Some(name) = scheme_at(operation, pointer) else {
return false;
};
let Some(node) = node else {
return false;
};
let valid_scopes = matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))));
if finding.code == "invalid_security_scopes" {
!valid_scopes
} else {
let nonempty = matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty() && items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_))));
let oauth_scoped = ir.security_schemes.get(name).is_some_and(|scheme| {
matches!(
&scheme.kind,
NormalizedSecuritySchemeKind::Unsupported {
scheme_type: Some(scheme_type)
} if matches!(scheme_type.as_str(), "oauth2" | "openIdConnect")
)
});
nonempty && !oauth_scoped
}
}
_ => false,
}
}
fn requirements_pointer(operation: &NormalizedOperation) -> Option<String> {
match operation.security_requirements.source {
SecurityRequirementsSource::Document => Some("/security".to_owned()),
SecurityRequirementsSource::Operation => {
Some(format!("{}/security", operation.location.pointer))
}
SecurityRequirementsSource::None => None,
}
}
fn alternative_at<'a>(
operation: &'a NormalizedOperation,
pointer: &str,
) -> Option<&'a crate::rest::model::SecurityRequirementAlternative> {
operation
.security_requirements
.alternatives
.iter()
.find(|alternative| {
alternative
.source_location
.as_ref()
.is_some_and(|location| location.pointer == pointer)
})
}
fn scheme_at<'a>(operation: &'a NormalizedOperation, pointer: &str) -> Option<&'a str> {
let (alternative_pointer, scheme) = pointer.rsplit_once('/')?;
let alternative = alternative_at(operation, alternative_pointer)?;
let name = scheme.replace("~1", "/").replace("~0", "~");
alternative
.schemes
.iter()
.find(|candidate| **candidate == name)
.map(String::as_str)
}
fn parameter_style(
pointer: &str,
node: &CanonicalSourceNode,
@@ -0,0 +1,97 @@
use std::collections::{BTreeMap, BTreeSet};
use crate::rest::model::{
ImportFinding, ImportGroupPreview, ImportPreview, ImportSourcePreview, NormalizedIr,
NormalizedSecurityScheme, RestImportDocument,
};
pub(super) 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,
&ir.security_schemes,
operations.iter(),
findings,
false,
)
}
pub(super) 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.security_schemes,
document.operations.iter(),
document.findings,
true,
)
}
fn preview_from_operations<'a>(
source: &ImportSourcePreview,
security_schemes: &BTreeMap<String, NormalizedSecurityScheme>,
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 {
super::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::new(),
})
.operations
.push(candidate);
}
super::normalize::sort_findings(&mut findings);
ImportPreview {
source: source.clone(),
groups: groups.into_values().collect(),
findings,
}
}
@@ -13,6 +13,7 @@ pub(super) fn operation_from_normalized(operation: &NormalizedOperation) -> Rest
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(),
+10 -222
View File
@@ -27,6 +27,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
let mut findings = Vec::new();
let mut internal_finding_locations = Vec::new();
let servers = parse_servers(root.get("servers"), "/servers");
let security_schemes = super::security::schemes(root, false);
append_findings_with_locations(
&mut findings,
&mut internal_finding_locations,
@@ -156,6 +157,13 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
let mut located_operation_findings =
super::openapi3_findings::operation(operation_value, &operation_pointer);
let security_requirements = super::security::collect_operation_requirements(
root,
operation_value,
&operation_pointer,
&security_schemes,
&mut located_operation_findings,
);
located_operation_findings.extend(super::openapi3_findings::path_parameters(
path,
&operation_parameters,
@@ -169,6 +177,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)
@@ -210,6 +219,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
version,
title,
servers,
security_schemes,
operations,
findings,
internal_finding_locations,
@@ -223,228 +233,6 @@ fn deduplicate_parameters(parameters: &mut Vec<RestImportParameter>) {
parameters.reverse();
}
pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, ImportParseError> {
let version = root
.get("openapi")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let title = root
.pointer("/info/title")
.and_then(Value::as_str)
.unwrap_or("Imported API")
.to_owned();
let servers = legacy_servers(root.get("servers"));
let mut findings = Vec::new();
if servers.is_empty() {
findings.push(document_finding(
"missing_servers",
"В документе не указаны servers, base URL нужно будет выбрать вручную.",
));
} else if servers.len() > 1 {
findings.push(document_finding(
"multiple_servers",
"В документе несколько servers, при импорте нужно выбрать нужный base URL.",
));
}
let mut operations = Vec::new();
let paths = root
.get("paths")
.and_then(Value::as_object)
.ok_or(ImportParseError::UnsupportedDocument)?;
for (path, path_item) in paths {
let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1"));
let path_parameters = legacy_parameters(
root,
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
for method_name in ["get", "post", "put", "patch", "delete"] {
let Some(operation) = path_item.get(method_name) else {
continue;
};
let Some(method) = super::http_methods::from_lower(method_name) else {
continue;
};
let operation_pointer = format!("{path_pointer}/{method_name}");
let mut parameters = path_parameters.clone();
parameters.extend(legacy_parameters(
root,
operation.get("parameters"),
&format!("{operation_pointer}/parameters"),
));
operations.push(RestImportOperation {
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
operation_id: operation
.get("operationId")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
summary: operation
.get("summary")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
description: operation
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
tags: legacy_tags(operation.get("tags")),
parameters,
request_body_schema: legacy_request_body_schema(root, operation),
request_body_schema_location: None,
request_body_variants: Vec::new(),
response_schema: legacy_response_schema(root, operation),
response_schema_location: None,
response_variants: Vec::new(),
servers: legacy_servers(operation.get("servers")),
findings: legacy_operation_findings(operation),
internal_finding_locations: Vec::new(),
});
}
}
Ok(RestImportDocument {
format: "openapi".to_owned(),
version,
title,
servers,
operations,
findings,
internal_finding_locations: Vec::new(),
})
}
fn legacy_servers(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(|item| item.get("url").and_then(Value::as_str))
.map(|url| url.trim_end_matches('/').to_owned())
.collect()
})
.unwrap_or_default()
}
fn legacy_tags(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_str)
.map(ToOwned::to_owned)
.collect()
})
.unwrap_or_default()
}
fn legacy_parameters(
root: &Value,
value: Option<&Value>,
base_pointer: &str,
) -> Vec<RestImportParameter> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item = crate::rest::normalize::resolve_local_ref(root, item, 0);
let location = match item.get("in").and_then(Value::as_str)? {
"path" => RestParameterLocation::Path,
"query" => RestParameterLocation::Query,
"header" => RestParameterLocation::Header,
_ => return None,
};
Some(RestImportParameter {
name: item.get("name").and_then(Value::as_str)?.to_owned(),
location,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false)
|| location == RestParameterLocation::Path,
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: item.get("schema").map(|schema| {
crate::rest::normalize::resolve_local_ref(root, schema, 0)
}),
source_location: crate::rest::model::SourceLocation {
pointer: format!("{base_pointer}/{index}"),
},
schema_source_location: None,
})
})
.collect()
})
.unwrap_or_default()
}
fn legacy_request_body_schema(root: &Value, operation: &Value) -> Option<Value> {
let body = crate::rest::normalize::resolve_local_ref(root, operation.get("requestBody")?, 0);
let content = body.get("content")?.as_object()?;
for content_type in ["application/json", "application/*+json"] {
if let Some(schema) = content
.get(content_type)
.and_then(|media| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
content
.iter()
.find(|(content_type, _)| content_type.contains("json"))
.and_then(|(_, media)| media.get("schema"))
.map(|schema| crate::rest::normalize::resolve_local_ref(root, schema, 0))
}
fn legacy_response_schema(root: &Value, operation: &Value) -> Option<Value> {
let responses = operation.get("responses")?.as_object()?;
for code in ["200", "201", "202", "default"] {
let Some(response) = responses.get(code) else {
continue;
};
let response = crate::rest::normalize::resolve_local_ref(root, response, 0);
let Some(content) = response.get("content").and_then(Value::as_object) else {
continue;
};
for content_type in ["application/json", "application/*+json"] {
if let Some(schema) = content
.get(content_type)
.and_then(|media| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
if let Some(schema) = content
.iter()
.find(|(content_type, _)| content_type.contains("json"))
.and_then(|(_, media)| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
None
}
fn legacy_operation_findings(operation: &Value) -> Vec<ImportFinding> {
if operation.get("requestBody").is_some()
&& legacy_request_body_schema(&Value::Null, operation).is_none()
{
vec![document_finding(
"unsupported_request_body",
"У метода есть requestBody, но JSON schema не найдена.",
)]
} else {
Vec::new()
}
}
struct ParsedParameters {
parameters: Vec<RestImportParameter>,
findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>,
@@ -0,0 +1,236 @@
use serde_json::Value;
use crate::rest::{
model::{
ImportFinding, RestImportDocument, RestImportOperation, RestImportParameter,
RestParameterLocation,
},
normalize::ImportParseError,
recommendations::document_finding,
};
pub(super) fn parse_document_legacy_v1(
root: &Value,
) -> Result<RestImportDocument, ImportParseError> {
let version = root
.get("openapi")
.and_then(Value::as_str)
.map(ToOwned::to_owned);
let title = root
.pointer("/info/title")
.and_then(Value::as_str)
.unwrap_or("Imported API")
.to_owned();
let servers = legacy_servers(root.get("servers"));
let mut findings = Vec::new();
if servers.is_empty() {
findings.push(document_finding(
"missing_servers",
"В документе не указаны servers, base URL нужно будет выбрать вручную.",
));
} else if servers.len() > 1 {
findings.push(document_finding(
"multiple_servers",
"В документе несколько servers, при импорте нужно выбрать нужный base URL.",
));
}
let mut operations = Vec::new();
let paths = root
.get("paths")
.and_then(Value::as_object)
.ok_or(ImportParseError::UnsupportedDocument)?;
for (path, path_item) in paths {
let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1"));
let path_parameters = legacy_parameters(
root,
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
for method_name in ["get", "post", "put", "patch", "delete"] {
let Some(operation) = path_item.get(method_name) else {
continue;
};
let Some(method) = super::http_methods::from_lower(method_name) else {
continue;
};
let operation_pointer = format!("{path_pointer}/{method_name}");
let mut parameters = path_parameters.clone();
parameters.extend(legacy_parameters(
root,
operation.get("parameters"),
&format!("{operation_pointer}/parameters"),
));
operations.push(RestImportOperation {
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)
.map(ToOwned::to_owned),
summary: operation
.get("summary")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
description: operation
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
tags: legacy_tags(operation.get("tags")),
parameters,
request_body_schema: legacy_request_body_schema(root, operation),
request_body_schema_location: None,
request_body_variants: Vec::new(),
response_schema: legacy_response_schema(root, operation),
response_schema_location: None,
response_variants: Vec::new(),
servers: legacy_servers(operation.get("servers")),
findings: legacy_operation_findings(operation),
internal_finding_locations: Vec::new(),
});
}
}
Ok(RestImportDocument {
format: "openapi".to_owned(),
version,
title,
servers,
security_schemes: Default::default(),
operations,
findings,
internal_finding_locations: Vec::new(),
})
}
fn legacy_servers(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(|item| item.get("url").and_then(Value::as_str))
.map(|url| url.trim_end_matches('/').to_owned())
.collect()
})
.unwrap_or_default()
}
fn legacy_tags(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_str)
.map(ToOwned::to_owned)
.collect()
})
.unwrap_or_default()
}
fn legacy_parameters(
root: &Value,
value: Option<&Value>,
base_pointer: &str,
) -> Vec<RestImportParameter> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item = crate::rest::normalize::resolve_local_ref(root, item, 0);
let location = match item.get("in").and_then(Value::as_str)? {
"path" => RestParameterLocation::Path,
"query" => RestParameterLocation::Query,
"header" => RestParameterLocation::Header,
_ => return None,
};
Some(RestImportParameter {
name: item.get("name").and_then(Value::as_str)?.to_owned(),
location,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false)
|| location == RestParameterLocation::Path,
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: item.get("schema").map(|schema| {
crate::rest::normalize::resolve_local_ref(root, schema, 0)
}),
source_location: crate::rest::model::SourceLocation {
pointer: format!("{base_pointer}/{index}"),
},
schema_source_location: None,
})
})
.collect()
})
.unwrap_or_default()
}
fn legacy_request_body_schema(root: &Value, operation: &Value) -> Option<Value> {
let body = crate::rest::normalize::resolve_local_ref(root, operation.get("requestBody")?, 0);
let content = body.get("content")?.as_object()?;
for content_type in ["application/json", "application/*+json"] {
if let Some(schema) = content
.get(content_type)
.and_then(|media| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
content
.iter()
.find(|(content_type, _)| content_type.contains("json"))
.and_then(|(_, media)| media.get("schema"))
.map(|schema| crate::rest::normalize::resolve_local_ref(root, schema, 0))
}
fn legacy_response_schema(root: &Value, operation: &Value) -> Option<Value> {
let responses = operation.get("responses")?.as_object()?;
for code in ["200", "201", "202", "default"] {
let Some(response) = responses.get(code) else {
continue;
};
let response = crate::rest::normalize::resolve_local_ref(root, response, 0);
let Some(content) = response.get("content").and_then(Value::as_object) else {
continue;
};
for content_type in ["application/json", "application/*+json"] {
if let Some(schema) = content
.get(content_type)
.and_then(|media| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
if let Some(schema) = content
.iter()
.find(|(content_type, _)| content_type.contains("json"))
.and_then(|(_, media)| media.get("schema"))
{
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
None
}
fn legacy_operation_findings(operation: &Value) -> Vec<ImportFinding> {
if operation.get("requestBody").is_some()
&& legacy_request_body_schema(&Value::Null, operation).is_none()
{
vec![document_finding(
"unsupported_request_body",
"У метода есть requestBody, но JSON schema не найдена.",
)]
} else {
Vec::new()
}
}
+5 -1
View File
@@ -10,7 +10,8 @@ use serde_json::json;
use crate::rest::{
mapping::{BodyFieldMapping, ParameterFieldMapping, input_mapping, output_mapping},
model::{
ImportOperationCandidate, RestImportCandidate, RestImportOperation, RestParameterLocation,
ImportOperationCandidate, NormalizedSecurityScheme, RestImportCandidate,
RestImportOperation, RestParameterLocation,
},
naming::{fallback_operation_seed, title_from_name, unique_name},
recommendations::{candidate_recommendations, operation_recommendations},
@@ -21,6 +22,7 @@ use crate::rest::{
pub fn candidate_from_operation(
operation: &RestImportOperation,
security_schemes: &BTreeMap<String, NormalizedSecurityScheme>,
document_servers: &[String],
used_names: &mut BTreeSet<String>,
) -> ImportOperationCandidate {
@@ -154,6 +156,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,
+340
View File
@@ -0,0 +1,340 @@
use std::collections::BTreeMap;
use serde_json::Value;
use super::model::{
NormalizedSecurityRequirements, NormalizedSecurityScheme, NormalizedSecuritySchemeKind,
SecurityParameterLocation, SecurityRequirementAlternative, SecurityRequirementsSource,
SourceLocation,
};
use super::recommendations::{document_blocker, document_finding};
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()
}
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)) => 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 ")
}
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 {
(None, SecurityRequirementsSource::None, String::new())
};
let mut findings = Vec::new();
let Some(value) = value else {
return (
NormalizedSecurityRequirements {
source,
alternatives: Vec::new(),
},
findings,
);
};
let Some(alternatives) = 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;
for (alternative_index, alternative) in alternatives.iter().enumerate() {
let alternative_pointer = format!("{pointer}/{alternative_index}");
let Some(requirement) = alternative.as_object() else {
let source_location = SourceLocation {
pointer: alternative_pointer,
};
findings.push((
document_finding(
"invalid_security_requirement",
"Security alternative имеет неверную структуру и недоступна для выбора.",
),
source_location.clone(),
));
projected.push(SecurityRequirementAlternative {
schemes: Vec::new(),
supported: false,
findings: vec!["invalid_security_requirement".to_owned()],
source_location: Some(source_location),
});
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 !projected.is_empty() && supported_alternatives == 0 {
findings.push((
document_blocker(
"unsupported_security_requirements",
"Ни одну authentication alternative нельзя безопасно выбрать; импорт операции заблокирован.",
),
SourceLocation { pointer },
));
}
(
NormalizedSecurityRequirements {
source,
alternatives: projected,
},
findings,
)
}
pub(super) fn collect_operation_requirements(
root: &Value,
operation: &Value,
pointer: &str,
schemes: &BTreeMap<String, NormalizedSecurityScheme>,
findings: &mut Vec<(super::model::ImportFinding, SourceLocation)>,
) -> NormalizedSecurityRequirements {
let (requirements, security_findings) =
effective_requirements(root, operation, pointer, schemes);
findings.extend(security_findings);
requirements
}
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")
)
}
@@ -0,0 +1,287 @@
use serde_json::Value;
use super::model::SourceLocation;
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SecretLiteralFinding {
pub location: SourceLocation,
}
/// Scans UTF-8 JSON or YAML and returns source locations without literal values.
pub fn scan_secret_literals(
bytes: &[u8],
) -> Result<Vec<SecretLiteralFinding>, super::normalize::ImportParseError> {
let document = std::str::from_utf8(bytes)
.map_err(|_| super::normalize::ImportParseError::InvalidDocument)?;
let mut root = super::normalize::decode(document)?;
Ok(inspect_and_redact(&mut root))
}
pub(super) fn inspect_and_redact(root: &mut Value) -> Vec<SecretLiteralFinding> {
let mut findings = Vec::new();
walk(root, "", false, false, &mut findings, true, false);
findings
}
fn walk(
value: &mut Value,
pointer: &str,
sensitive_context: bool,
example_context: bool,
findings: &mut Vec<SecretLiteralFinding>,
redact: bool,
cookie_context: bool,
) {
if example_context
&& value
.as_object()
.is_some_and(|fields| fields.keys().any(|key| is_jwt(key)))
{
findings.push(SecretLiteralFinding {
location: SourceLocation {
pointer: pointer.to_owned(),
},
});
if redact {
*value = Value::String("[REDACTED]".to_owned());
}
return;
}
match value {
Value::Object(fields) => {
let header_parameter = fields
.get("in")
.and_then(Value::as_str)
.is_some_and(|location| location.eq_ignore_ascii_case("header"));
let parameter_name = fields.get("name").and_then(Value::as_str).unwrap_or("");
let named_sensitive_parameter =
sensitive_key(parameter_name) && fields.get("in").and_then(Value::as_str).is_some();
let named_cookie_header = is_cookie_header(parameter_name);
for (key, child) in fields {
let child_pointer = format!(
"{pointer}/{}",
super::normalize_coverage::escape_pointer(key)
);
let property_definition = (pointer.contains("/properties/")
|| pointer.ends_with("/properties"))
&& !key.to_ascii_lowercase().starts_with("x-");
let sensitive = sensitive_context
|| (sensitive_key(key)
&& !property_definition
&& !is_oauth_flow_name(pointer, key));
let example = example_context
|| matches!(
key.to_ascii_lowercase().as_str(),
"example" | "examples" | "default"
);
let header_example = header_parameter
&& matches!(
key.to_ascii_lowercase().as_str(),
"example" | "examples" | "default"
);
let named_header = is_cookie_header(key);
let credential_example = matches!(
key.to_ascii_lowercase().as_str(),
"example" | "examples" | "default"
) && (named_sensitive_parameter
|| (cookie_context && !property_definition)
|| (named_cookie_header && !property_definition));
walk(
child,
&child_pointer,
sensitive || header_example || credential_example,
example,
findings,
redact,
cookie_context || named_cookie_header || named_header,
);
}
}
Value::Array(items) => {
for (index, child) in items.iter_mut().enumerate() {
walk(
child,
&format!("{pointer}/{index}"),
sensitive_context,
example_context,
findings,
redact,
cookie_context,
);
}
}
Value::String(string) => {
let userinfo = url_userinfo_range(string);
let query_secret = sensitive_query_value(string);
let likely_example_secret = (example_context
&& (looks_secret_like(pointer, string) || is_jwt(string)))
|| looks_secret_value(string)
|| ((pointer.ends_with("/bearerFormat") || pointer.ends_with("/x-format-hint"))
&& looks_secret_like(pointer, string));
if sensitive_context || likely_example_secret || userinfo.is_some() || query_secret {
findings.push(SecretLiteralFinding {
location: SourceLocation {
pointer: pointer.to_owned(),
},
});
if redact {
if query_secret {
*string = "[REDACTED]".to_owned();
} else if let Some((start, end)) = userinfo {
string.replace_range(start..end, "[REDACTED]");
} else {
*string = "[REDACTED]".to_owned();
}
}
}
}
Value::Number(_) | Value::Bool(_) if sensitive_context => {
findings.push(SecretLiteralFinding {
location: SourceLocation {
pointer: pointer.to_owned(),
},
});
if redact {
*value = Value::String("[REDACTED]".to_owned());
}
}
_ => {}
}
}
fn sensitive_key(key: &str) -> bool {
let mut normalized = String::new();
let mut previous_lower = false;
for ch in key.chars() {
if ch.is_ascii_uppercase() && previous_lower {
normalized.push('_');
}
if ch == '-' || ch == ' ' {
normalized.push('_');
} else {
normalized.push(ch.to_ascii_lowercase());
}
previous_lower = ch.is_ascii_lowercase() || ch.is_ascii_digit();
}
let key = normalized;
if matches!(key.as_str(), "authorization_url" | "token_url") {
return false;
}
[
"token",
"secret",
"secret_hash",
"secrethash",
"password",
"password_hash",
"passwordhash",
"passwd",
"api_key",
"apikey",
"authorization",
"credential",
"credentials",
"access_key",
"private_key",
"privatekey",
"ciphertext",
]
.iter()
.any(|marker| {
key == *marker
|| key.ends_with(&format!("_{marker}"))
|| key.starts_with(&format!("{marker}_"))
})
}
fn is_oauth_flow_name(parent_pointer: &str, key: &str) -> bool {
if !parent_pointer.ends_with("/flows") {
return false;
}
let compact_key: String = key
.chars()
.filter(|character| character.is_ascii_alphanumeric())
.map(|character| character.to_ascii_lowercase())
.collect();
matches!(
compact_key.as_str(),
"implicit" | "password" | "clientcredentials" | "authorizationcode"
)
}
fn is_cookie_header(value: &str) -> bool {
value.eq_ignore_ascii_case("cookie") || value.eq_ignore_ascii_case("set-cookie")
}
fn is_jwt(value: &str) -> bool {
let mut parts = value.split('.');
let Some(header) = parts.next() else {
return false;
};
let Some(payload) = parts.next() else {
return false;
};
let Some(signature) = parts.next() else {
return false;
};
parts.next().is_none()
&& header.starts_with("eyJ")
&& payload.starts_with("eyJ")
&& header.len() >= 8
&& payload.len() >= 16
&& signature.len() >= 12
&& [header, payload, signature].iter().all(|part| {
!part.is_empty()
&& part
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-')
})
}
fn sensitive_query_value(value: &str) -> bool {
let Some(query_start) = value.find('?') else {
return false;
};
value[query_start + 1..]
.split('#')
.next()
.unwrap_or_default()
.split(['&', ';'])
.any(|pair| {
let (key, value) = pair.split_once('=').unwrap_or((pair, ""));
!value.is_empty() && sensitive_key(key)
})
}
fn looks_secret_like(pointer: &str, value: &str) -> bool {
let pointer = pointer.to_ascii_lowercase();
let value = value.to_ascii_lowercase();
[
"token",
"secret",
"password",
"api_key",
"apikey",
"authorization",
]
.iter()
.any(|marker| pointer.contains(marker) || value.contains(marker))
|| value.starts_with("bearer ")
|| value.starts_with("sk-")
}
fn looks_secret_value(value: &str) -> bool {
let value = value.to_ascii_lowercase();
value.starts_with("bearer ") || value.starts_with("sk-")
}
fn url_userinfo_range(value: &str) -> Option<(usize, usize)> {
let scheme_end = value.find("://")? + 3;
let authority_end = value[scheme_end..]
.find('/')
.map(|index| scheme_end + index)
.unwrap_or(value.len());
let authority = &value[scheme_end..authority_end];
let at = authority.find('@')?;
(at > 0).then_some((scheme_end, scheme_end + at))
}
+11 -195
View File
@@ -21,6 +21,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
.unwrap_or("Imported API")
.to_owned();
let servers = swagger_servers(root);
let security_schemes = super::security::schemes(root, true);
let mut findings = Vec::new();
let mut internal_finding_locations = Vec::new();
append_parameter_findings(
@@ -146,6 +147,13 @@ 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 = super::security::collect_operation_requirements(
root,
operation_value,
&operation_pointer,
&security_schemes,
&mut located_operation_findings,
);
located_operation_findings.extend(parsed_parameter_findings);
located_operation_findings.extend(super::swagger2_findings::path_parameters(
path,
@@ -160,6 +168,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)
@@ -199,6 +208,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
version: Some("2.0".to_owned()),
title,
servers,
security_schemes,
operations,
findings,
internal_finding_locations,
@@ -212,201 +222,7 @@ fn deduplicate_parameters(parameters: &mut Vec<RestImportParameter>) {
parameters.reverse();
}
pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, ImportParseError> {
let title = root
.pointer("/info/title")
.and_then(Value::as_str)
.unwrap_or("Imported API")
.to_owned();
let servers = swagger_servers(root);
let mut findings = Vec::new();
if servers.is_empty() {
findings.push(document_finding(
"missing_servers",
"В Swagger 2.0 документе не указаны host/schemes, base URL нужно будет выбрать вручную.",
));
}
let mut operations = Vec::new();
let paths = root
.get("paths")
.and_then(Value::as_object)
.ok_or(ImportParseError::UnsupportedDocument)?;
for (path, path_item) in paths {
let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1"));
let path_parameters = legacy_parameters(
root,
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
for method_name in ["get", "post", "put", "patch", "delete"] {
let Some(operation) = path_item.get(method_name) else {
continue;
};
let Some(method) = super::http_methods::from_lower(method_name) else {
continue;
};
let operation_pointer = format!("{path_pointer}/{method_name}");
let mut parameters = path_parameters.clone();
parameters.extend(legacy_parameters(
root,
operation.get("parameters"),
&format!("{operation_pointer}/parameters"),
));
let request_body_schema = parameters
.iter()
.find(|parameter| parameter.name == "body")
.and_then(|parameter| parameter.schema.clone());
let parameters = parameters
.into_iter()
.filter(|parameter| parameter.name != "body")
.collect();
operations.push(RestImportOperation {
key: format!("{} {}", method_name.to_uppercase(), path),
method,
path: path.clone(),
operation_id: operation
.get("operationId")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
summary: operation
.get("summary")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
description: operation
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
tags: legacy_tags(operation.get("tags")),
parameters,
request_body_schema,
request_body_schema_location: None,
request_body_variants: Vec::new(),
response_schema: legacy_response_schema(root, operation),
response_schema_location: None,
response_variants: Vec::new(),
servers: Vec::new(),
findings: super::swagger2_findings::legacy_operation(path, operation),
internal_finding_locations: Vec::new(),
});
}
}
Ok(RestImportDocument {
format: "swagger".to_owned(),
version: Some("2.0".to_owned()),
title,
servers,
operations,
findings,
internal_finding_locations: Vec::new(),
})
}
fn legacy_tags(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_str)
.map(ToOwned::to_owned)
.collect()
})
.unwrap_or_default()
}
fn legacy_parameters(
root: &Value,
value: Option<&Value>,
base_pointer: &str,
) -> Vec<RestImportParameter> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item = crate::rest::normalize::resolve_local_ref(root, item, 0);
let raw_location = item.get("in").and_then(Value::as_str)?;
let pointer = format!("{base_pointer}/{index}");
if raw_location == "body" {
return Some(RestImportParameter {
name: "body".to_owned(),
location: RestParameterLocation::Query,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false),
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: item.get("schema").map(|schema| {
crate::rest::normalize::resolve_local_ref(root, schema, 0)
}),
source_location: crate::rest::model::SourceLocation { pointer },
schema_source_location: None,
});
}
let location = match raw_location {
"path" => RestParameterLocation::Path,
"query" => RestParameterLocation::Query,
"header" => RestParameterLocation::Header,
_ => return None,
};
Some(RestImportParameter {
name: item.get("name").and_then(Value::as_str)?.to_owned(),
location,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false)
|| location == RestParameterLocation::Path,
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: legacy_parameter_schema(root, &item),
source_location: crate::rest::model::SourceLocation { pointer },
schema_source_location: None,
})
})
.collect()
})
.unwrap_or_default()
}
fn legacy_parameter_schema(root: &Value, parameter: &Value) -> Option<Value> {
if let Some(schema) = parameter.get("schema") {
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
let mut schema = serde_json::Map::new();
for key in ["type", "format", "items", "enum", "default", "description"] {
if let Some(value) = parameter.get(key) {
schema.insert(
key.to_owned(),
crate::rest::normalize::resolve_local_ref(root, value, 0),
);
}
}
(!schema.is_empty()).then_some(Value::Object(schema))
}
fn legacy_response_schema(root: &Value, operation: &Value) -> Option<Value> {
let responses = operation.get("responses")?.as_object()?;
for code in ["200", "201", "202", "default"] {
let Some(response) = responses.get(code) else {
continue;
};
let response = crate::rest::normalize::resolve_local_ref(root, response, 0);
if let Some(schema) = response.get("schema") {
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
None
}
fn swagger_servers(root: &Value) -> Vec<String> {
pub(super) fn swagger_servers(root: &Value) -> Vec<String> {
let Some(host) = root.get("host").and_then(Value::as_str) else {
return Vec::new();
};
@@ -0,0 +1,206 @@
use serde_json::Value;
use super::swagger2::swagger_servers;
use crate::rest::{
model::{RestImportDocument, RestImportOperation, RestImportParameter, RestParameterLocation},
normalize::ImportParseError,
recommendations::document_finding,
};
pub(super) fn parse_document_legacy_v1(
root: &Value,
) -> Result<RestImportDocument, ImportParseError> {
let title = root
.pointer("/info/title")
.and_then(Value::as_str)
.unwrap_or("Imported API")
.to_owned();
let servers = swagger_servers(root);
let mut findings = Vec::new();
if servers.is_empty() {
findings.push(document_finding(
"missing_servers",
"В Swagger 2.0 документе не указаны host/schemes, base URL нужно будет выбрать вручную.",
));
}
let mut operations = Vec::new();
let paths = root
.get("paths")
.and_then(Value::as_object)
.ok_or(ImportParseError::UnsupportedDocument)?;
for (path, path_item) in paths {
let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1"));
let path_parameters = legacy_parameters(
root,
path_item.get("parameters"),
&format!("{path_pointer}/parameters"),
);
for method_name in ["get", "post", "put", "patch", "delete"] {
let Some(operation) = path_item.get(method_name) else {
continue;
};
let Some(method) = super::http_methods::from_lower(method_name) else {
continue;
};
let operation_pointer = format!("{path_pointer}/{method_name}");
let mut parameters = path_parameters.clone();
parameters.extend(legacy_parameters(
root,
operation.get("parameters"),
&format!("{operation_pointer}/parameters"),
));
let request_body_schema = parameters
.iter()
.find(|parameter| parameter.name == "body")
.and_then(|parameter| parameter.schema.clone());
let parameters = parameters
.into_iter()
.filter(|parameter| parameter.name != "body")
.collect();
operations.push(RestImportOperation {
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)
.map(ToOwned::to_owned),
summary: operation
.get("summary")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
description: operation
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
tags: legacy_tags(operation.get("tags")),
parameters,
request_body_schema,
request_body_schema_location: None,
request_body_variants: Vec::new(),
response_schema: legacy_response_schema(root, operation),
response_schema_location: None,
response_variants: Vec::new(),
servers: Vec::new(),
findings: super::swagger2_findings::legacy_operation(path, operation),
internal_finding_locations: Vec::new(),
});
}
}
Ok(RestImportDocument {
format: "swagger".to_owned(),
version: Some("2.0".to_owned()),
title,
servers,
security_schemes: Default::default(),
operations,
findings,
internal_finding_locations: Vec::new(),
})
}
fn legacy_tags(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_str)
.map(ToOwned::to_owned)
.collect()
})
.unwrap_or_default()
}
fn legacy_parameters(
root: &Value,
value: Option<&Value>,
base_pointer: &str,
) -> Vec<RestImportParameter> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.enumerate()
.filter_map(|(index, item)| {
let item = crate::rest::normalize::resolve_local_ref(root, item, 0);
let raw_location = item.get("in").and_then(Value::as_str)?;
let pointer = format!("{base_pointer}/{index}");
if raw_location == "body" {
return Some(RestImportParameter {
name: "body".to_owned(),
location: RestParameterLocation::Query,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false),
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: item.get("schema").map(|schema| {
crate::rest::normalize::resolve_local_ref(root, schema, 0)
}),
source_location: crate::rest::model::SourceLocation { pointer },
schema_source_location: None,
});
}
let location = match raw_location {
"path" => RestParameterLocation::Path,
"query" => RestParameterLocation::Query,
"header" => RestParameterLocation::Header,
_ => return None,
};
Some(RestImportParameter {
name: item.get("name").and_then(Value::as_str)?.to_owned(),
location,
required: item
.get("required")
.and_then(Value::as_bool)
.unwrap_or(false)
|| location == RestParameterLocation::Path,
description: item
.get("description")
.and_then(Value::as_str)
.map(ToOwned::to_owned),
schema: legacy_parameter_schema(root, &item),
source_location: crate::rest::model::SourceLocation { pointer },
schema_source_location: None,
})
})
.collect()
})
.unwrap_or_default()
}
fn legacy_parameter_schema(root: &Value, parameter: &Value) -> Option<Value> {
if let Some(schema) = parameter.get("schema") {
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
let mut schema = serde_json::Map::new();
for key in ["type", "format", "items", "enum", "default", "description"] {
if let Some(value) = parameter.get(key) {
schema.insert(
key.to_owned(),
crate::rest::normalize::resolve_local_ref(root, value, 0),
);
}
}
(!schema.is_empty()).then_some(Value::Object(schema))
}
fn legacy_response_schema(root: &Value, operation: &Value) -> Option<Value> {
let responses = operation.get("responses")?.as_object()?;
for code in ["200", "201", "202", "default"] {
let Some(response) = responses.get(code) else {
continue;
};
let response = crate::rest::normalize::resolve_local_ref(root, response, 0);
if let Some(schema) = response.get("schema") {
return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0));
}
}
None
}
@@ -0,0 +1,780 @@
mod security_schemes {
use crank_import::rest::{
ImportFindingSeverity, ImportParseError, NormalizationConfig, NormalizedSecuritySchemeKind,
SecurityParameterLocation, SecurityRequirementsSource, normalize_verified_document,
preview_from_ir, scan_secret_literals, validate_normalized_ir,
};
fn normalize_document(
document: &str,
config: &NormalizationConfig,
) -> Result<crank_import::rest::NormalizedIr, ImportParseError> {
normalize_verified_document(
document,
crank_import::rest::SourceDigest::parse("b".repeat(64)).unwrap(),
config,
)
}
#[test]
fn ac_1_projects_openapi_security_scheme_identity_onto_operation_metadata() {
let document = r#"
openapi: 3.1.0
info: { title: Security metadata }
components:
securitySchemes:
WorkspaceBearer:
type: http
scheme: bearer
bearerFormat: JWT
paths:
/private:
get:
operationId: private
security: [{ WorkspaceBearer: [] }]
responses: { '200': { description: ok } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let operation = ir
.operations
.iter()
.find(|operation| operation.path == "/private")
.unwrap();
let operation_contract = serde_json::to_string(operation).unwrap();
// This assertion deliberately inspects the operation projection, not the
// canonical source tree, so copying the original document is insufficient.
assert!(operation_contract.contains("WorkspaceBearer"));
assert!(operation_contract.contains("bearer"));
assert!(operation_contract.contains("JWT"));
}
#[test]
fn ac_6_never_copies_literal_secret_canaries_into_normalized_ir() {
const CANARY: &str = "atdd-secret-canary-2-10";
let document = format!(
r#"
openapi: 3.0.3
info: {{ title: Secret canary }}
components:
securitySchemes:
Bearer:
type: http
scheme: bearer
x-example-token: {CANARY}
x-credentials: {CANARY}
x-ciphertext: {CANARY}
paths:
/private:
get:
security: [{{ Bearer: [] }}]
responses:
'200':
description: ok
content:
application/json:
example: {{ token: {CANARY} }}
"#
);
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
let normalized = serde_json::to_string(&ir).unwrap();
assert!(
!normalized.contains(CANARY),
"normalized IR must not retain literal credential canaries"
);
}
#[test]
fn security_requirements_inherit_and_operation_empty_array_disables_auth() {
let document = r#"
openapi: 3.0.3
info: { title: Requirements }
components:
securitySchemes:
Key: { type: apiKey, in: header, name: X-Workspace-Key }
security: [{ Key: [] }]
paths:
/inherited:
get: { responses: { '200': { description: ok } } }
/public:
get:
security: []
responses: { '200': { description: ok } }
/and:
get:
security: [{ Key: [], Other: [] }]
responses: { '200': { description: ok } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let inherited = ir
.operations
.iter()
.find(|operation| operation.path == "/inherited")
.unwrap();
assert_eq!(
inherited.security_requirements.source,
SecurityRequirementsSource::Document
);
assert_eq!(
inherited.security_requirements.alternatives[0].schemes,
["Key"]
);
let public = ir
.operations
.iter()
.find(|operation| operation.path == "/public")
.unwrap();
assert_eq!(
public.security_requirements.source,
SecurityRequirementsSource::Operation
);
assert!(public.security_requirements.alternatives.is_empty());
let and = ir
.operations
.iter()
.find(|operation| operation.path == "/and")
.unwrap();
assert!(
and.findings
.iter()
.any(|finding| finding.code == "unsupported_security_and_requirement")
);
assert_eq!(
and.security_requirements.alternatives[0].schemes,
["Key", "Other"]
);
}
#[test]
fn security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes() {
let document = r#"
openapi: 3.1.0
info: { title: Alternatives }
components:
securitySchemes:
Bearer:
type: http
scheme: bearer
bearerFormat: JWT
User:
type: apiKey
in: query
name: access_token
CookieKey:
type: apiKey
in: cookie
name: session
security:
- Bearer: []
- User: []
- CookieKey: []
paths:
/private:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let first = preview_from_ir(&ir);
let second = preview_from_ir(&ir);
assert_eq!(
serde_json::to_vec(&first).unwrap(),
serde_json::to_vec(&second).unwrap()
);
let candidate = &first.groups[0].operations[0];
assert_eq!(candidate.security_requirements.alternatives.len(), 3);
assert!(
matches!(candidate.security_schemes["Bearer"].kind, NormalizedSecuritySchemeKind::Http { ref format_hint, .. } if format_hint.as_deref() == Some("JWT"))
);
assert!(
matches!(candidate.security_schemes["User"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } if parameter_name == "access_token")
);
assert!(
matches!(candidate.security_schemes["CookieKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Cookie, ref parameter_name, .. } if parameter_name == "session")
);
assert!(!candidate.security_requirements.alternatives[2].supported);
}
#[test]
fn swagger2_security_definitions_project_basic_and_api_key_header_and_query() {
let document = r#"
swagger: '2.0'
info: { title: Swagger security }
securityDefinitions:
BasicAuth: { type: basic }
HeaderKey: { type: apiKey, in: header, name: X-Api-Key }
QueryKey: { type: apiKey, in: query, name: api_key }
security: [{ BasicAuth: [] }, { HeaderKey: [] }]
paths:
/private:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
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, 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 == "api_key")
);
assert_eq!(ir.operations[0].security_requirements.alternatives.len(), 2);
}
#[test]
fn unsupported_security_schemes_create_blockers_without_copying_credentials() {
let document = r#"
openapi: 3.0.3
info: { title: Unsupported }
components:
securitySchemes:
OAuth: { type: oauth2, flows: {} }
Oidc: { type: openIdConnect, openIdConnectUrl: https://issuer.test }
Mutual: { type: mutualTLS }
paths:
/private:
get:
security: [{ OAuth: [read] }, { Oidc: [] }, { Mutual: [] }, { Unknown: [] }]
responses: { '200': { description: ok } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let codes = ir.operations[0]
.findings
.iter()
.map(|finding| finding.code.as_str())
.collect::<Vec<_>>();
assert!(codes.contains(&"unsupported_security_scheme"));
assert!(codes.contains(&"unresolved_security_scheme"));
assert!(!codes.contains(&"invalid_security_scopes"));
assert!(!codes.contains(&"unexpected_security_scopes"));
assert!(ir.operations[0].findings.iter().any(|finding| {
finding.code == "unsupported_security_requirements"
&& finding.severity == ImportFindingSeverity::Error
}));
assert!(
ir.operations[0]
.security_requirements
.alternatives
.iter()
.all(|alternative| !alternative.supported)
);
}
#[test]
fn normalized_ir_rejects_unrelated_security_findings_and_accepts_valid_security_ir() {
let document = r#"
openapi: 3.0.3
info: { title: Validation }
components:
securitySchemes:
OAuth: { type: oauth2, flows: {} }
paths:
/private:
get:
security: [{ OAuth: [] }]
responses: { '200': { description: ok } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
validate_normalized_ir(&ir).unwrap();
let mut tampered = ir;
let finding = tampered.operations[0]
.findings
.iter_mut()
.find(|finding| finding.code == "unsupported_security_scheme")
.unwrap();
finding.location.pointer = "/info/title".to_owned();
finding.construct_id = "3.0.3:/info/title".to_owned();
assert_eq!(
validate_normalized_ir(&tampered),
Err(ImportParseError::InvalidDocument)
);
}
#[test]
fn unsupported_or_alternative_does_not_block_a_supported_choice() {
let document = r#"
openapi: 3.0.3
info: { title: Mixed alternatives }
components:
securitySchemes:
Bearer: { type: http, scheme: bearer }
OAuth: { type: oauth2, flows: {} }
security: [{ OAuth: [] }, { Bearer: [] }]
paths:
/private:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let operation = &ir.operations[0];
assert_eq!(operation.security_requirements.alternatives.len(), 2);
assert!(!operation.security_requirements.alternatives[0].supported);
assert!(operation.security_requirements.alternatives[1].supported);
assert!(
!operation
.findings
.iter()
.any(|finding| finding.code == "unsupported_security_requirements")
);
assert!(operation.findings.iter().any(|finding| {
finding.code == "unsupported_security_scheme"
&& finding.severity == ImportFindingSeverity::Warning
}));
}
#[test]
fn openapi_invalid_http_scopes_are_selection_aware_and_valid_or_sibling_survives() {
let document = r#"
openapi: 3.0.3
info: { title: Invalid HTTP scopes }
components:
securitySchemes:
Bearer: { type: http, scheme: bearer }
security:
- Bearer: [read]
- Bearer: "read"
- Bearer: [1]
- Bearer: []
paths:
/private:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let operation = &ir.operations[0];
let alternatives = &operation.security_requirements.alternatives;
assert_eq!(alternatives.len(), 4);
assert!(
alternatives[..3]
.iter()
.all(|alternative| !alternative.supported)
);
assert!(alternatives[3].supported);
assert!(operation.findings.iter().any(|finding| {
finding.code == "unexpected_security_scopes"
&& finding.severity == ImportFindingSeverity::Warning
}));
assert!(operation.findings.iter().any(|finding| {
finding.code == "invalid_security_scopes"
&& finding.severity == ImportFindingSeverity::Warning
}));
assert!(
!operation
.findings
.iter()
.any(|finding| { finding.code == "unsupported_security_requirements" })
);
}
#[test]
fn swagger_invalid_api_key_scopes_do_not_hide_valid_or_sibling() {
let document = r#"
swagger: '2.0'
info: { title: Invalid Swagger scopes }
securityDefinitions:
HeaderKey: { type: apiKey, in: header, name: X-Api-Key }
BasicAuth: { type: basic }
security:
- HeaderKey: [read]
- BasicAuth: []
paths:
/private:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let operation = &ir.operations[0];
assert!(!operation.security_requirements.alternatives[0].supported);
assert!(operation.security_requirements.alternatives[1].supported);
assert!(operation.findings.iter().any(|finding| {
finding.code == "unexpected_security_scopes"
&& finding.severity == ImportFindingSeverity::Warning
}));
assert!(
!operation
.findings
.iter()
.any(|finding| { finding.code == "unsupported_security_requirements" })
);
}
#[test]
fn scanner_finds_examples_defaults_extensions_and_url_userinfo_without_values() {
const CANARIES: [&str; 5] = [
"example-token-secret",
"default-password-secret",
"extension-api-key-secret",
"url-userinfo-secret",
"header-example-secret",
];
let document = r#"
openapi: 3.0.3
info: { title: Redaction }
servers: [{ url: 'https://operator:url-userinfo-secret@example.test' }]
components:
securitySchemes:
Bearer: { type: http, scheme: bearer, x-example-token: extension-api-key-secret }
paths:
/private:
get:
parameters:
- name: token
in: header
example: header-example-secret
schema: { type: string, default: default-password-secret }
responses:
'200':
description: ok
content:
application/json:
example: { token: example-token-secret }
"#;
let findings = scan_secret_literals(document.as_bytes()).unwrap();
assert!(findings.len() >= CANARIES.len());
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let serialized = serde_json::to_string(&ir).unwrap();
assert!(CANARIES.iter().all(|canary| !serialized.contains(canary)));
let report = serde_json::to_string(&findings).unwrap();
assert!(CANARIES.iter().all(|canary| !report.contains(canary)));
}
#[test]
fn scanner_does_not_mistake_password_or_token_schema_properties_for_credentials() {
let document = r#"
openapi: 3.0.3
info: { title: Password fields }
paths:
/login:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
password: { type: string, format: password }
access_token: { type: string }
responses: { '204': { description: ok } }
"#;
assert!(
scan_secret_literals(document.as_bytes())
.unwrap()
.is_empty()
);
}
#[test]
fn scanner_keeps_version_examples_and_null_credential_defaults() {
let document = r#"
openapi: 3.0.3
info: { title: Benign examples }
components:
securitySchemes:
Bearer: { type: http, scheme: bearer, clientSecret: null }
paths:
/version:
get:
responses:
'200':
description: ok
content:
text/plain:
example: 1.2.3
"#;
assert!(
scan_secret_literals(document.as_bytes())
.unwrap()
.is_empty()
);
}
#[test]
fn scanner_allows_oauth_endpoint_urls_but_redacts_secret_query_parameters() {
let safe_document = r#"
openapi: 3.0.3
info: { title: OAuth URLs }
components:
securitySchemes:
OAuth:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://auth.example.test/authorize
tokenUrl: https://auth.example.test/token
scopes: {}
password:
tokenUrl: https://auth.example.test/password-token
scopes: {}
clientCredentials:
tokenUrl: https://auth.example.test/client-token
scopes: {}
"#;
assert!(
scan_secret_literals(safe_document.as_bytes())
.unwrap()
.is_empty()
);
let secret_document = safe_document
.replace(
"https://auth.example.test/token",
"https://auth.example.test/token?access_token=oauth-url-canary",
)
.replace(
"https://auth.example.test/password-token",
"https://auth.example.test/password-token?access_token=password-flow-canary",
)
.replace(
"https://auth.example.test/client-token",
"https://auth.example.test/client-token?access_token=client-flow-canary",
);
let findings = scan_secret_literals(secret_document.as_bytes()).unwrap();
assert!(findings.iter().any(|finding| {
finding.location.pointer
== "/components/securitySchemes/OAuth/flows/authorizationCode/tokenUrl"
}));
assert!(findings.iter().any(|finding| {
finding.location.pointer == "/components/securitySchemes/OAuth/flows/password/tokenUrl"
}));
assert!(findings.iter().any(|finding| {
finding.location.pointer
== "/components/securitySchemes/OAuth/flows/clientCredentials/tokenUrl"
}));
let report = serde_json::to_string(&findings).unwrap();
assert!(!report.contains("oauth-url-canary"));
assert!(!report.contains("password-flow-canary"));
assert!(!report.contains("client-flow-canary"));
}
#[test]
fn scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples() {
const CANARIES: [&str; 8] = [
"982734651",
"true",
"opaque-query-canary",
"cookie-session-canary",
"set-cookie-canary",
"query-param-canary",
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl",
"opaque-jwt-object-key-canary",
];
const JWT_KEY: &str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl";
let document = format!(
r#"
openapi: 3.0.3
info: {{ title: Contextual redaction }}
servers: [{{ url: 'https://api.test/items?access_token=opaque-query-canary' }}]
components:
securitySchemes:
Custom: {{ type: http, scheme: bearer, x-clientSecret: 982734651, x-accessToken: true }}
paths:
/login:
post:
parameters:
- name: api_key
in: query
schema: {{ type: string }}
example: query-param-canary
- name: Cookie
in: header
schema: {{ type: string }}
example: cookie-session-canary
responses:
'200':
description: ok
headers:
Set-Cookie:
schema: {{ type: string }}
example: set-cookie-canary
content:
application/json:
example: {{{JWT_KEY}: opaque-jwt-object-key-canary}}
examples:
compact:
value: {{{JWT_KEY}}}
"#
);
let findings = scan_secret_literals(document.as_bytes()).unwrap();
let location_pointers: Vec<_> = findings
.iter()
.map(|finding| finding.location.pointer.as_str())
.collect();
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/x-clientSecret"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/x-accessToken"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/servers/0/url"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/parameters/0/example"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/parameters/1/example"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/headers/Set-Cookie/example"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer.ends_with("/content/application~1json/example"))
);
assert!(
location_pointers
.iter()
.any(|pointer| pointer
.ends_with("/content/application~1json/examples/compact/value"))
);
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
let serialized = serde_json::to_string(&ir).unwrap();
for canary in CANARIES {
assert!(!serialized.contains(canary), "IR retained canary {canary}");
}
let redacted_locations: Vec<_> = ir
.findings
.iter()
.filter(|finding| finding.code == "secret_literal_redacted")
.map(|finding| finding.location.pointer.as_str())
.collect();
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/x-clientSecret"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/x-accessToken"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/servers/0/url"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/parameters/0/example"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/parameters/1/example"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/headers/Set-Cookie/example"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer.ends_with("/content/application~1json/example"))
);
assert!(
redacted_locations
.iter()
.any(|pointer| pointer
.ends_with("/content/application~1json/examples/compact/value"))
);
let report = serde_json::to_string(&ir.findings).unwrap();
assert!(report.contains("secret_literal_redacted"));
for canary in CANARIES {
assert!(
!report.contains(canary),
"finding report retained canary {canary}"
);
}
}
#[test]
fn unsafe_bearer_format_canary_is_redacted_and_not_projected() {
const CANARY: &str = "Bearer sk-format-canary";
let document = format!(
r#"
openapi: 3.0.3
info: {{ title: Format redaction }}
components:
securitySchemes:
Bearer:
type: http
scheme: bearer
bearerFormat: "{CANARY}"
paths:
/private:
get: {{ security: [{{ Bearer: [] }}], responses: {{ '200': {{ description: ok }} }} }}
"#
);
let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap();
let serialized = serde_json::to_string(&ir).unwrap();
assert!(!serialized.contains(CANARY));
let scan_findings = scan_secret_literals(document.as_bytes()).unwrap();
assert!(scan_findings.iter().any(|finding| {
finding.location.pointer == "/components/securitySchemes/Bearer/bearerFormat"
}));
let redaction_findings: Vec<_> = ir
.findings
.iter()
.filter(|finding| finding.code == "secret_literal_redacted")
.collect();
assert_eq!(redaction_findings.len(), 1);
assert_eq!(
redaction_findings[0].location.pointer,
"/components/securitySchemes/Bearer/bearerFormat"
);
let report = serde_json::to_string(&ir.findings).unwrap();
assert!(report.contains("secret_literal_redacted"));
assert!(report.contains("/components/securitySchemes/Bearer/bearerFormat"));
assert!(!report.contains(CANARY));
}
#[test]
fn historical_v4_ir_without_security_fields_remains_valid() {
let document = r#"
openapi: 3.0.3
info: { title: Replay }
paths:
/old:
get: { responses: { '200': { description: ok } } }
"#;
let ir = normalize_document(document, &NormalizationConfig::default()).unwrap();
let mut value = serde_json::to_value(ir).unwrap();
value["normalizer_version"] = serde_json::json!("normalized-ir-v4");
value["projection_version"] = serde_json::json!("preview-v4");
value.as_object_mut().unwrap().remove("security_schemes");
for operation in value["operations"].as_array_mut().unwrap() {
operation
.as_object_mut()
.unwrap()
.remove("security_schemes");
operation
.as_object_mut()
.unwrap()
.remove("security_requirements");
}
let replay: crank_import::rest::NormalizedIr = serde_json::from_value(value).unwrap();
validate_normalized_ir(&replay).unwrap();
assert!(
replay.operations[0]
.security_requirements
.alternatives
.is_empty()
);
}
}
+2
View File
@@ -136,6 +136,8 @@ pub enum RegistryError {
ImportJobNotFound { job_id: String },
#[error("import job {job_id} was already applied with different parameters")]
ImportJobAlreadyApplied { job_id: String },
#[error("import auth profile binding is unavailable")]
ImportAuthBindingUnavailable,
#[error("unsupported enum representation for field {field}")]
InvalidEnumRepresentation { field: &'static str },
#[error("invalid numeric value for field {field}: {value}")]
+23 -21
View File
@@ -47,14 +47,15 @@ pub mod requests {
CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest,
CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest,
DecideApprovalRequest, DetachArtifactSourceRequest, ExpireApprovalRequest,
FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode, ImportJobSourceEnvelope,
ImportOperationDraft, ListApprovalRequestsQuery, ListArtifactSourcesQuery,
ListInvocationLogsQuery, ListProductEventsQuery, MasterKeyIdentityCandidate,
PublishAgentRequest, PublishRequest, RecordOnboardingCompletionRequest,
RecordOnboardingMilestoneRequest, RecoverAdminPasswordRequest, RotateSecretRequest,
SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest,
SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest,
UpdateAgentSummaryRequest, UpdateWorkspaceRequest, UsageQuery,
FinishApprovalRequest, FinishImportJobRequest, ImportAuthRequirement, ImportConflictMode,
ImportJobSourceEnvelope, ImportOperationDraft, ListApprovalRequestsQuery,
ListArtifactSourcesQuery, ListInvocationLogsQuery, ListProductEventsQuery,
MasterKeyIdentityCandidate, PublishAgentRequest, PublishRequest,
RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest,
RecoverAdminPasswordRequest, RotateSecretRequest, SaveAgentBindingsRequest,
SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, SaveDescriptorMetadataRequest,
SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, UpdateAgentSummaryRequest,
UpdateWorkspaceRequest, UsageQuery,
};
}
@@ -82,19 +83,20 @@ pub use model::{
CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest,
CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest,
DecideApprovalRequest, DescriptorKind, DescriptorMetadata, DetachArtifactSourceRequest,
ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode,
ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, ImportJobKind,
ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, InvitationRecord,
InvocationHistoryLoss, InvocationHistoryLossCategory, InvocationHistoryWriteOutcome,
InvocationLogRecord, InvocationRetentionOutcome, InvocationRetentionPolicy,
InvocationRetentionStatus, ListApprovalRequestsQuery, ListArtifactSourcesQuery,
ListInvocationLogsQuery, ListProductEventsQuery, MASTER_KEY_CIPHER_CONTRACT,
MAX_ARTIFACT_CLAIM_RECOVERY_BATCH, MAX_ARTIFACT_SOURCE_PAGE_SIZE, MasterKeyIdentityCandidate,
MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus, MembershipRecord,
OnboardingMilestoneResult, OnboardingPresentationMilestone, OperationAgentRef,
OperationSampleMetadata, OperationStateExpectation, OperationSummary, OperationUsageSummary,
OperationVersionRecord, Page, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest,
PublishRequest, PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest,
ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest, ImportAuthRequirement,
ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId,
ImportJobKind, ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft,
InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory,
InvocationHistoryWriteOutcome, InvocationLogRecord, InvocationRetentionOutcome,
InvocationRetentionPolicy, InvocationRetentionStatus, ListApprovalRequestsQuery,
ListArtifactSourcesQuery, ListInvocationLogsQuery, ListProductEventsQuery,
MASTER_KEY_CIPHER_CONTRACT, MAX_ARTIFACT_CLAIM_RECOVERY_BATCH, MAX_ARTIFACT_SOURCE_PAGE_SIZE,
MasterKeyIdentityCandidate, MasterKeyIdentityRecord, MasterKeyRotationRecord,
MasterKeyRotationStatus, MembershipRecord, OnboardingMilestoneResult,
OnboardingPresentationMilestone, OperationAgentRef, OperationSampleMetadata,
OperationStateExpectation, OperationSummary, OperationUsageSummary, OperationVersionRecord,
Page, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest, PublishRequest,
PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest,
RecordOnboardingMilestoneRequest, RecoverAdminPasswordRequest, RegistryOperation,
RotateSecretRequest, SampleKind, SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest,
SaveAuthProfileRequest, SaveDescriptorMetadataRequest, SaveSampleMetadataRequest,
+30 -7
View File
@@ -1,13 +1,13 @@
use crank_artifacts::ArtifactRef;
use crank_core::{
Agent, AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ApprovalRequest,
ApprovalRequestId, ApprovalRequestStatus, AuthProfile, DescriptorId, ExecutionErrorCode,
ExportMode, InvitationToken, InvocationLevel, InvocationLog, InvocationLogId, InvocationSource,
InvocationStatus, MembershipRole, OnboardingProjection, Operation, OperationId,
OperationSecurityLevel, OperationStatus, PlatformApiKey, PlatformApiKeyId, ProductEvent,
ProductEventId, ProductEventKind, Protocol, SampleId, Secret, SecretId, SecretVersion,
ToolSelectionPolicy, UsagePeriod, UsageRollup, User, UserId, UserSessionId, Workspace,
WorkspaceId,
ApprovalRequestId, ApprovalRequestStatus, AuthKind, AuthProfile, DescriptorId,
ExecutionErrorCode, ExportMode, InvitationToken, InvocationLevel, InvocationLog,
InvocationLogId, InvocationSource, InvocationStatus, MembershipRole, OnboardingProjection,
Operation, OperationId, OperationSecurityLevel, OperationStatus, PlatformApiKey,
PlatformApiKeyId, ProductEvent, ProductEventId, ProductEventKind, Protocol, SampleId, Secret,
SecretId, SecretVersion, ToolSelectionPolicy, UsagePeriod, UsageRollup, User, UserId,
UserSessionId, Workspace, WorkspaceId,
};
use crank_mapping::MappingSet;
use crank_schema::Schema;
@@ -648,6 +648,29 @@ pub enum ImportConflictMode {
pub struct ImportOperationDraft {
pub operation_key: String,
pub operation: RegistryOperation,
/// The effective single-scheme requirement selected for this operation.
/// The registry checks it against the referenced profile inside Apply.
pub auth_requirement: Option<ImportAuthRequirement>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ImportAuthRequirement {
Bearer,
Basic,
ApiKeyHeader { parameter_name: String },
ApiKeyQuery { parameter_name: String },
}
impl ImportAuthRequirement {
pub const fn kind(&self) -> AuthKind {
match self {
Self::Bearer => AuthKind::Bearer,
Self::Basic => AuthKind::Basic,
Self::ApiKeyHeader { .. } => AuthKind::ApiKeyHeader,
Self::ApiKeyQuery { .. } => AuthKind::ApiKeyQuery,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -1,6 +1,8 @@
use super::*;
use crate::{ArtifactSourceId, ImportJobSourceEnvelope};
use crank_artifacts::ArtifactRef;
use crank_core::{AuthConfig, AuthProfile, AuthProfileId, SecretId};
use std::collections::BTreeMap;
const APPLICATION_RESULT_KEY: &str = "_crank_application_result";
const IMPORT_JOB_CLEANUP_BATCH: u32 = 128;
@@ -388,6 +390,10 @@ async fn apply_import_job_transaction(
}
}
// Validate the complete binding set before the first Operation insert.
// This transaction-level check closes the gap between preview and Apply.
validate_import_auth_bindings(tx, request.workspace_id, request.operations).await?;
let mut result = ImportJobApplyResult {
application_key: request.application_key.to_owned(),
skipped: request.pre_skipped.to_vec(),
@@ -485,6 +491,190 @@ async fn apply_import_job_transaction(
Ok(result)
}
async fn validate_import_auth_bindings(
tx: &mut Transaction<'_, Postgres>,
workspace_id: &WorkspaceId,
operations: &[crate::ImportOperationDraft],
) -> Result<(), RegistryError> {
let mut requirements_by_profile = BTreeMap::<String, Vec<&crate::ImportAuthRequirement>>::new();
for draft in operations {
match (
draft.auth_requirement.as_ref(),
draft.operation.execution_config.auth_profile_ref.as_ref(),
) {
(None, None) => {}
(Some(requirement), Some(profile_id)) => requirements_by_profile
.entry(profile_id.as_str().to_owned())
.or_default()
.push(requirement),
_ => return Err(RegistryError::ImportAuthBindingUnavailable),
}
}
if requirements_by_profile.is_empty() {
return Ok(());
}
// Read the current workspace's profile config first to discover Secret
// references. Secret rows are locked in stable order before profile rows,
// matching save_auth_profile's secret-then-profile write order.
let mut observed_profiles = BTreeMap::<String, AuthProfile>::new();
let mut secret_ids = BTreeMap::<String, SecretId>::new();
for profile_id in requirements_by_profile.keys() {
let profile = read_import_auth_profile(tx, workspace_id, profile_id, false)
.await?
.ok_or(RegistryError::ImportAuthBindingUnavailable)?;
let requirements = &requirements_by_profile[profile_id];
if !requirements
.iter()
.all(|requirement| auth_profile_matches(profile_id, &profile, requirement))
{
return Err(RegistryError::ImportAuthBindingUnavailable);
}
for secret_id in profile.config.secret_ids() {
secret_ids
.entry(secret_id.as_str().to_owned())
.or_insert_with(|| secret_id.clone());
}
observed_profiles.insert(profile_id.clone(), profile);
}
for secret_id in secret_ids.values() {
let row = sqlx::query(
"select s.status,
exists (
select 1 from secret_versions sv
where sv.secret_id = s.id and sv.version = s.current_version
) as has_current_version
from secrets s
where s.workspace_id = $1 and s.id = $2
for update of s",
)
.bind(workspace_id.as_str())
.bind(secret_id.as_str())
.fetch_optional(&mut **tx)
.await?;
let Some(row) = row else {
return Err(RegistryError::ImportAuthBindingUnavailable);
};
if row.try_get::<String, _>("status")? != "active"
|| !row.try_get::<bool, _>("has_current_version")?
{
return Err(RegistryError::ImportAuthBindingUnavailable);
}
}
// The profile may have changed while its former Secret rows were being
// locked. Lock profile rows only after Secrets (the order used by profile
// updates), then compare the binding config and revalidate compatibility.
for (profile_id, observed) in &observed_profiles {
let current = read_import_auth_profile(tx, workspace_id, profile_id, true)
.await?
.ok_or(RegistryError::ImportAuthBindingUnavailable)?;
if current.id != observed.id
|| current.workspace_id != observed.workspace_id
|| current.kind != observed.kind
|| current.config != observed.config
|| !requirements_by_profile[profile_id]
.iter()
.all(|requirement| auth_profile_matches(profile_id, &current, requirement))
{
return Err(RegistryError::ImportAuthBindingUnavailable);
}
}
Ok(())
}
async fn read_import_auth_profile(
tx: &mut Transaction<'_, Postgres>,
workspace_id: &WorkspaceId,
profile_id: &str,
lock: bool,
) -> Result<Option<AuthProfile>, RegistryError> {
let query = if lock {
"select id, workspace_id, name, kind, config_json, created_at, updated_at
from auth_profiles
where workspace_id = $1 and id = $2
for update"
} else {
"select id, workspace_id, name, kind, config_json, created_at, updated_at
from auth_profiles
where workspace_id = $1 and id = $2"
};
let row = sqlx::query(query)
.bind(workspace_id.as_str())
.bind(profile_id)
.fetch_optional(&mut **tx)
.await?;
row.map(|row| {
super::build_auth_profile(
row.try_get("id")?,
row.try_get("workspace_id")?,
row.try_get("name")?,
row.try_get("kind")?,
row.try_get("config_json")?,
row.try_get("created_at")?,
row.try_get("updated_at")?,
)
})
.transpose()
}
fn auth_profile_matches(
expected_profile_id: &str,
profile: &AuthProfile,
requirement: &crate::ImportAuthRequirement,
) -> bool {
if profile.id != AuthProfileId::new(expected_profile_id)
|| profile.kind != profile.config.kind()
|| profile.kind != requirement.kind()
{
return false;
}
match (requirement, &profile.config) {
(crate::ImportAuthRequirement::Bearer, AuthConfig::Bearer(_))
| (crate::ImportAuthRequirement::Basic, AuthConfig::Basic(_)) => true,
(
crate::ImportAuthRequirement::ApiKeyHeader { parameter_name },
AuthConfig::ApiKeyHeader(config),
) => {
is_valid_auth_header_name(&config.header_name)
&& is_valid_auth_header_name(parameter_name)
&& config.header_name.eq_ignore_ascii_case(parameter_name)
}
(
crate::ImportAuthRequirement::ApiKeyQuery { parameter_name },
AuthConfig::ApiKeyQuery(config),
) => config.param_name == *parameter_name,
_ => false,
}
}
fn is_valid_auth_header_name(value: &str) -> bool {
value.len() <= 128
&& !value.is_empty()
&& value.bytes().all(|byte| {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
})
}
fn validate_source_envelope(
source: &ImportJobSourceEnvelope,
preview_payload: &Value,
@@ -5,6 +5,7 @@ mod integration {
mod artifact_sources;
mod common;
mod credential_touch;
mod import_auth_binding;
mod master_key_identity;
mod migrations;
mod observability;
@@ -0,0 +1,570 @@
use super::common::*;
use std::{
fs,
os::unix::fs::PermissionsExt,
path::PathBuf,
sync::atomic::{AtomicU64, Ordering},
};
use crank_artifacts::{ArtifactRef, ArtifactStore};
use crank_core::{
ApiKeyHeaderAuthConfig, AuthConfig, AuthKind, AuthProfile, BearerAuthConfig, ExecutionConfig,
Secret, SecretKind, SecretStatus, Workspace, WorkspaceId, WorkspaceStatus,
};
use crank_registry::{
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
CreateArtifactSourceRequest, CreateImportJobRequest, CreateSecretRequest,
CreateWorkspaceRequest, ImportAuthRequirement, ImportConflictMode, ImportJobId, ImportJobKind,
ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, MASTER_KEY_CIPHER_CONTRACT,
MasterKeyIdentityCandidate, PostgresRegistry, RegistryError, RegistryOperation,
SaveAuthProfileRequest,
};
use serde_json::json;
use time::OffsetDateTime;
use tokio::time::{Duration as TokioDuration, sleep};
static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);
struct ImportSourceRoot(PathBuf);
impl ImportSourceRoot {
fn new() -> Self {
let path = std::env::temp_dir().join(format!(
"crank-registry-import-auth-{}-{}",
std::process::id(),
NEXT_ROOT.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir(&path).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
Self(path)
}
}
impl Drop for ImportSourceRoot {
fn drop(&mut self) {
let _ = fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700));
let _ = fs::remove_dir_all(&self.0);
}
}
fn timestamp() -> OffsetDateTime {
OffsetDateTime::now_utc()
}
async fn create_workspace(registry: &PostgresRegistry, id: &str) -> WorkspaceId {
let now = timestamp();
let workspace_id = WorkspaceId::new(id);
registry
.create_workspace(CreateWorkspaceRequest {
workspace: &Workspace {
id: workspace_id.clone(),
slug: id.replace('_', "-"),
display_name: id.to_owned(),
status: WorkspaceStatus::Active,
settings: json!({}),
created_at: now,
updated_at: now,
},
})
.await
.unwrap();
workspace_id
}
async fn create_secret(registry: &PostgresRegistry, id: &str, workspace_id: &WorkspaceId) {
let now = timestamp();
registry
.verify_or_register_master_key_identity(MasterKeyIdentityCandidate {
epoch: 1,
fingerprint: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
cipher_contract: MASTER_KEY_CIPHER_CONTRACT,
observed_at: &now,
})
.await
.unwrap();
let secret = Secret {
id: id.into(),
workspace_id: workspace_id.clone(),
name: format!("secret-{id}"),
kind: SecretKind::Token,
status: SecretStatus::Active,
current_version: 1,
created_at: now,
updated_at: now,
last_used_at: None,
};
registry
.create_secret(CreateSecretRequest {
secret: &secret,
ciphertext: "opaque-test-ciphertext",
key_version: "test-key-v1",
master_key_epoch: 1,
created_by: None,
})
.await
.unwrap();
}
fn profile(
id: &str,
workspace_id: &WorkspaceId,
kind: AuthKind,
config: AuthConfig,
) -> AuthProfile {
let now = timestamp();
AuthProfile {
id: id.into(),
workspace_id: workspace_id.clone(),
name: format!("profile-{id}"),
kind,
config,
created_at: now,
updated_at: now,
}
}
async fn save_profile(registry: &PostgresRegistry, profile: &AuthProfile) {
registry
.save_auth_profile(SaveAuthProfileRequest {
workspace_id: &profile.workspace_id,
profile,
})
.await
.unwrap();
}
async fn create_import_job(
registry: &PostgresRegistry,
root: &ImportSourceRoot,
job_number: usize,
) -> (ImportJobId, ArtifactSourceId, ArtifactRef) {
let store = ArtifactStore::open(&root.0).unwrap();
let artifact = store.put_registered(b"openapi: 3.1.0\n").unwrap();
let source_id = ArtifactSourceId::new(format!("src_import_auth_{job_number}"));
let now = timestamp();
let workspace_id = test_workspace_id();
registry
.create_artifact_source(CreateArtifactSourceRequest {
workspace_id: &workspace_id,
source_id: &source_id,
artifact: &artifact,
mime_type: "application/yaml",
sensitivity: ArtifactSourceSensitivity::Internal,
created_at: now,
})
.await
.unwrap();
let source = ImportJobSourceEnvelope {
source_id: source_id.clone(),
digest: artifact.artifact_ref().clone(),
};
let payload = json!({
"source": {
"source_id": source.source_id.as_str(),
"digest": source.digest.as_str(),
}
});
let id = ImportJobId::new(format!("job_import_auth_{job_number}"));
registry
.create_import_job(CreateImportJobRequest {
id: &id,
workspace_id: &workspace_id,
kind: ImportJobKind::OpenApi,
source_format: "openapi",
source_version: Some("v5"),
status: ImportJobStatus::Pending,
source: &source,
preview_payload: &payload,
created_at: &now,
expires_at: &(now + time::Duration::hours(1)),
})
.await
.unwrap();
(id, source_id, artifact.artifact_ref().clone())
}
fn operation(id: &str, profile_id: Option<&str>) -> RegistryOperation {
let mut operation = test_operation(id, 1, crank_core::OperationStatus::Draft);
operation.execution_config = ExecutionConfig {
auth_profile_ref: profile_id.map(Into::into),
..operation.execution_config
};
operation
}
async fn apply(
registry: &PostgresRegistry,
job_id: &ImportJobId,
drafts: &[ImportOperationDraft],
) -> Result<crank_registry::ImportJobApplyResult, RegistryError> {
let workspace_id = test_workspace_id();
let now = timestamp();
registry
.apply_import_job(ApplyImportJobRequest {
id: job_id,
workspace_id: &workspace_id,
application_key: "test-app-key",
conflict_mode: ImportConflictMode::Rename,
operations: drafts,
pre_skipped: &[],
finished_at: &now,
})
.await
}
async fn operation_count(database: &TestDatabase, ids: &[&str]) -> i64 {
let pool = database.raw_pool().await;
let count = sqlx::query_scalar::<_, i64>("select count(*) from operations where id = any($1)")
.bind(ids)
.fetch_one(&pool)
.await
.unwrap();
pool.close().await;
count
}
#[tokio::test]
async fn apply_accepts_current_compatible_profile_and_shared_secret_then_rejects_disabled_secret() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let workspace_id = test_workspace_id();
let secret_id = "secret_import_shared";
create_secret(&registry, secret_id, &workspace_id).await;
for profile_id in ["auth_import_a", "auth_import_b"] {
save_profile(
&registry,
&profile(
profile_id,
&workspace_id,
AuthKind::ApiKeyHeader,
AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "x-api-key".to_owned(),
secret_id: secret_id.into(),
}),
),
)
.await;
}
let root = ImportSourceRoot::new();
let (job_id, _, _) = create_import_job(&registry, &root, 1).await;
let drafts = [
ImportOperationDraft {
operation_key: "first".to_owned(),
operation: operation("op_import_auth_first", Some("auth_import_a")),
auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Api-Key".to_owned(),
}),
},
ImportOperationDraft {
operation_key: "second".to_owned(),
operation: operation("op_import_auth_second", Some("auth_import_b")),
auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Api-Key".to_owned(),
}),
},
];
let result = apply(&registry, &job_id, &drafts).await.unwrap();
assert_eq!(result.created.len(), 2);
assert_eq!(
operation_count(
&database,
&["op_import_auth_first", "op_import_auth_second"]
)
.await,
2
);
let pool = database.raw_pool().await;
sqlx::query("update secrets set status = 'disabled' where id = $1")
.bind(secret_id)
.execute(&pool)
.await
.unwrap();
pool.close().await;
let (job_id, _, _) = create_import_job(&registry, &root, 2).await;
let disabled_error = apply(&registry, &job_id, &drafts).await.unwrap_err();
assert_eq!(
disabled_error.to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(
operation_count(
&database,
&["op_import_auth_first", "op_import_auth_second"]
)
.await,
2
);
database.cleanup().await;
}
#[tokio::test]
async fn missing_and_foreign_profiles_have_the_same_safe_error_and_insert_no_operation() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let foreign_workspace = create_workspace(&registry, "ws_import_auth_foreign").await;
create_secret(&registry, "secret_import_foreign", &foreign_workspace).await;
save_profile(
&registry,
&profile(
"auth_import_foreign",
&foreign_workspace,
AuthKind::Bearer,
AuthConfig::Bearer(BearerAuthConfig {
header_name: "Authorization".to_owned(),
secret_id: "secret_import_foreign".into(),
}),
),
)
.await;
let root = ImportSourceRoot::new();
let (missing_job, _, _) = create_import_job(&registry, &root, 3).await;
let missing = [ImportOperationDraft {
operation_key: "missing".to_owned(),
operation: operation("op_import_auth_missing", Some("auth_import_absent")),
auth_requirement: Some(ImportAuthRequirement::Bearer),
}];
let missing_error = apply(&registry, &missing_job, &missing).await.unwrap_err();
let (foreign_job, _, _) = create_import_job(&registry, &root, 4).await;
let foreign = [ImportOperationDraft {
operation_key: "foreign".to_owned(),
operation: operation("op_import_auth_foreign_ref", Some("auth_import_foreign")),
auth_requirement: Some(ImportAuthRequirement::Bearer),
}];
let foreign_error = apply(&registry, &foreign_job, &foreign).await.unwrap_err();
assert_eq!(
missing_error.to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(missing_error.to_string(), foreign_error.to_string());
assert_eq!(
operation_count(
&database,
&["op_import_auth_missing", "op_import_auth_foreign_ref"]
)
.await,
0
);
database.cleanup().await;
}
#[tokio::test]
async fn apply_rejects_kind_parameter_and_missing_current_version_before_mutation() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let workspace_id = test_workspace_id();
create_secret(&registry, "secret_import_mismatch", &workspace_id).await;
save_profile(
&registry,
&profile(
"auth_import_mismatch",
&workspace_id,
AuthKind::ApiKeyHeader,
AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: "secret_import_mismatch".into(),
}),
),
)
.await;
let root = ImportSourceRoot::new();
let bad_requirements = [
ImportAuthRequirement::Bearer,
ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Different".to_owned(),
},
];
for (index, requirement) in bad_requirements.into_iter().enumerate() {
let (job_id, _, _) = create_import_job(&registry, &root, 5 + index).await;
let operation_id = format!("op_import_auth_mismatch_{index}");
let draft = [ImportOperationDraft {
operation_key: operation_id.clone(),
operation: operation(&operation_id, Some("auth_import_mismatch")),
auth_requirement: Some(requirement),
}];
let error = apply(&registry, &job_id, &draft).await.unwrap_err();
assert_eq!(
error.to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(operation_count(&database, &[&operation_id]).await, 0);
}
let pool = database.raw_pool().await;
sqlx::query("update secrets set current_version = 2 where id = $1")
.bind("secret_import_mismatch")
.execute(&pool)
.await
.unwrap();
pool.close().await;
let (job_id, _, _) = create_import_job(&registry, &root, 7).await;
let draft = [ImportOperationDraft {
operation_key: "missing-current".to_owned(),
operation: operation(
"op_import_auth_missing_current",
Some("auth_import_mismatch"),
),
auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Api-Key".to_owned(),
}),
}];
let error = apply(&registry, &job_id, &draft).await.unwrap_err();
assert_eq!(
error.to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(
operation_count(&database, &["op_import_auth_missing_current"]).await,
0
);
database.cleanup().await;
}
#[tokio::test]
async fn no_auth_requires_no_profile_reference() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let root = ImportSourceRoot::new();
let (job_id, _, _) = create_import_job(&registry, &root, 8).await;
let draft = [ImportOperationDraft {
operation_key: "inconsistent".to_owned(),
operation: operation("op_import_auth_no_requirement", Some("auth_nonexistent")),
auth_requirement: None,
}];
let error = apply(&registry, &job_id, &draft).await.unwrap_err();
assert_eq!(
error.to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(
operation_count(&database, &["op_import_auth_no_requirement"]).await,
0
);
database.cleanup().await;
}
#[tokio::test]
async fn apply_waiting_on_secret_disable_rejects_without_partial_draft_mutation() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let workspace_id = test_workspace_id();
create_secret(&registry, "secret_import_race", &workspace_id).await;
save_profile(
&registry,
&profile(
"auth_import_race",
&workspace_id,
AuthKind::ApiKeyHeader,
AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: "secret_import_race".into(),
}),
),
)
.await;
let root = ImportSourceRoot::new();
let (job_id, _, _) = create_import_job(&registry, &root, 9).await;
let drafts = vec![
ImportOperationDraft {
operation_key: "race-first".to_owned(),
operation: operation("op_import_auth_race_first", Some("auth_import_race")),
auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Api-Key".to_owned(),
}),
},
ImportOperationDraft {
operation_key: "race-second".to_owned(),
operation: operation("op_import_auth_race_second", Some("auth_import_race")),
auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader {
parameter_name: "X-Api-Key".to_owned(),
}),
},
];
let pool = database.raw_pool().await;
let mut disable_tx = pool.begin().await.unwrap();
sqlx::query("select id from secrets where workspace_id = $1 and id = $2 for update")
.bind(workspace_id.as_str())
.bind("secret_import_race")
.fetch_one(&mut *disable_tx)
.await
.unwrap();
let concurrent_registry = registry.clone();
let concurrent_job_id = job_id.clone();
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
let apply_task = tokio::spawn(async move {
let _ = started_tx.send(());
let workspace_id = test_workspace_id();
let finished_at = timestamp();
concurrent_registry
.apply_import_job(ApplyImportJobRequest {
id: &concurrent_job_id,
workspace_id: &workspace_id,
application_key: "race-app-key",
conflict_mode: ImportConflictMode::Rename,
operations: &drafts,
pre_skipped: &[],
finished_at: &finished_at,
})
.await
});
started_rx.await.unwrap();
// Use a separate pool because the transaction holding the lock occupies
// the raw pool's only connection.
let observer_pool = database.raw_pool().await;
let mut waiting_for_secret_lock = false;
for _ in 0..100 {
waiting_for_secret_lock = sqlx::query_scalar::<_, bool>(
"select exists (
select 1 from pg_stat_activity
where wait_event_type = 'Lock'
and query like 'select s.status,%'
and query like '%for update of s%'
)",
)
.fetch_one(&observer_pool)
.await
.unwrap();
if waiting_for_secret_lock {
break;
}
sleep(TokioDuration::from_millis(10)).await;
}
assert!(
waiting_for_secret_lock,
"Apply should be waiting on the locked Secret row"
);
observer_pool.close().await;
sqlx::query("update secrets set status = 'disabled' where workspace_id = $1 and id = $2")
.bind(workspace_id.as_str())
.bind("secret_import_race")
.execute(&mut *disable_tx)
.await
.unwrap();
disable_tx.commit().await.unwrap();
let result = apply_task.await.unwrap();
assert_eq!(
result.unwrap_err().to_string(),
"import auth profile binding is unavailable"
);
assert_eq!(
operation_count(
&database,
&["op_import_auth_race_first", "op_import_auth_race_second"]
)
.await,
0
);
pool.close().await;
database.cleanup().await;
}
+10
View File
@@ -22,6 +22,16 @@ Crank умеет импортировать REST API из OpenAPI 3.x и Swagger
Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации, digest, source ID и внутренние пути не выводятся. Повторное нажатие **Создать черновики** во время выполнения не создаёт вторую мутацию.
## Контракт API импорта и аутентификация
В workspace API доступны `POST /imports/openapi/preview` (multipart upload) и `POST /imports/openapi/{job_id}/create` (JSON). Preview сохраняет effective security requirements по операциям и предлагает `auth_profile_choices`: для каждой OR-alternative он указывает индекс, имена схем, признак `supported` и только совместимые профили текущего workspace. Варианты профиля содержат ID, имя и тип; значения Secret и другие credential material не возвращаются.
При создании черновиков отправляйте `auth_mappings` с одной записью на выбранную защищённую операцию: `{ "operation_key": "GET /items", "alternative_index": 0, "auth_profile_id": "auth_…" }`. `alternative_index` начинается с нуля. Внешний список security alternatives означает OR; все схемы внутри одной alternative означают AND. Текущий runtime применяет одну Auth Profile, поэтому неподдерживаемый AND-вариант нельзя выбрать. Для явно пустой alternative можно передать `auth_profile_id: null`; отсутствие mappings остаётся совместимым для операций без security requirements. Неуказанное или unresolved required mapping блокирует создание Draft.
API принимает Bearer, Basic и API key в header/query, если соответствующая Auth Profile принадлежит текущему workspace, параметры схемы совпадают и все связанные Secrets активны с доступной current version. Cookie API key, OAuth2, OpenID Connect, mutual TLS, неизвестные схемы и несовместимые AND-варианты отображаются как unsupported и требуют ручной конфигурации. Apply повторяет проверку профиля и Secret внутри registry transaction; отсутствующие, foreign, несовместимые профили и недоступные Secrets возвращают одну sanitized ошибку без metadata профиля.
Изменение требований безопасности после preview делает старый preview непригодным для применения: digest включает выбранные mapping-значения, а исходный документ повторно проверяется перед Apply. Literal credentials в поддерживаемых source locations отклоняются до сохранения upload или внешнего snapshot; ответ содержит только безопасный error code и не включает значение. Отдельный экран выбора Auth Profile в мастере относится к Story 2.11.
## Что создается
Для каждого выбранного метода Crank создает отдельную операцию в статусе черновика: