← Back to Pull requests

feat(crypto): key enrollment per verified identity, create-once (#175) #300

Closed
unknown closed this pull request without merging on Nov 27, 2026 16:15 UTC
View on GitHub
unknown unknown commented on Nov 23, 2026 23:53 UTC
Author

What and why



Closes #175 (P33). ADR 0004 decides the design: one key per tenant, created once; replacing it is an explicit, audited rotation. A second device, a cleared browser or a worker restart can no longer silently re-key a tenant.

  • PUT /api/v1/drive/enrollment is create-once (put_if_absent). The record carries key_id (SHA-256 of the raw key, from the new TenantPublicKey::fingerprint()), the key's own suite and created_at_unix. The same key again succeeds; a different key is a 409, audited crypto.key_enrollment "denied". A stored record that fails verification is a backend fault (502, audited "failed"), as enrolled_suite treats one, not another device's key.

  • POST /api/v1/drive/enrollment/rotate refuses (403, audited) until P30 wires in the fresh second factor. Worker initialization never calls it.

  • verify_enrollment_record(stored, policy) refuses a record with unknown fields, no creation time, a key that doesn't parse, a suite the policy refuses or that isn't the key's own, or a key_id that isn't the key's fingerprint. Records written before this change are refused.

  • The browser key store is keyed by verified identity (iss, sub, tid), and AADs use the session's tid. A stored row that doesn't match the session is refused. Database version 2 leaves the old device row in place, unread; data sealed under its random UUID is unsupported pre-v1 data.

  • The worker reports the enrollment result. On "refused", KeySetupGate shows a second-device refusal with no way into the drive. Its acknowledgement is per identity, and a device without an HPKE key gets one later without losing its master key.

  • Notes for reviewers:

  • Replacing an HPK1 key with an HPK2 key (the P36 migration) is allowed only where HPK1 is still accepted, so never in production, and is audited as "migrated". It reads the record and then writes it unconditionally (VaultStore has no compare-and-swap), so two devices migrating at once both succeed and the last write wins. A comment in put_enrollment records this development-only race.

  • Anything that seals to an enrolled key should read it through verify_enrollment_record (#289). #280 also edits worker.ts; whichever lands second rebases onto loadDeviceKeys(identity).

  • The research-result test after a refused re-enrollment waits on #177.

  • Clearing old development records



    Enrollment records written before this PR have no key_id or created_at_unix, so they fail verification, and every enrollment for that tenant is a 502 until the record is deleted. Any deployment holding such records needs them cleared before it runs this change. Deleting a record removes only the enrolled key, not files or datasets.

  • Local-directory vault: delete $STORAGE_LOCAL_DIR/control/enrollments/.json.

  • Simulated TEE stack: docker compose -f docker-compose.simulated-tee.yml exec enclave-api sh -c 'rm -f /var/lib/quackxide/vault/control/enrollments/.json'.

  • In-memory vault (neither STORAGE_LOCAL_DIR nor STORAGE_BUCKET set): restart platform-api.


  • The browser enrolls again on the next drive load.

    How it was tested



  • audit_coverage.rs: first enrollment and same-key retry succeed; a different key returns 409, is audited "denied" and leaves the record unchanged; rotation returns 403 and is audited; a record written before this PR returns 502 and is audited "failed".

  • platform-api unit tests: verify_enrollment_record refuses a key id mismatch, a wrong or unknown suite, an invalid key, a zero creation time, a production-refused HPK1 key and a pre-#175 record.

  • platform-crypto: the fingerprint is the lowercase hex SHA-256 of the raw key.

  • envelope_vectors.rs and dataset_upload_api.rs: adjusted for create-once; the P36 migration test passes.

  • keystore.test.ts (fake-indexeddb): per-identity storage, the session's tid required, mismatched rows refused, version 1 to 2 upgrade without using the legacy row, round-trip with an encrypted HPKE key.

  • worker.test.ts: keys bound to the session identity, no keys without a session, enrolled vs. refused reporting, an HPKE key added later with the same master key.

  • KeySetupGate.test.tsx: the second-device refusal shows no button or checkbox.

  • Checklist



  • scripts/check.sh passes locally

  • Every new refusal has a negative test

  • Every new security decision emits a SECURITY_AUDIT_EVENT covered by audit_coverage.rs

  • Fails closed on missing configuration, key material, or attestation

  • No new cryptographic primitives; suites stay behind their type aliases

  • Synthetic or public data only (docs/DATA_POLICY.md)

  • Docs and CHANGELOG.md updated for user-visible changes

  • Architectural decision recorded in docs/adr/ if one was made
  • Diff: 597 lines (+447 / -150) across 7 files Reviewer: abendrothj Review duration: 5302m
    All Vouch review confidence checks passed
    Residual risk 0.34 = Change Risk (0.78) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.78 · 597 lines diff across 7 files
    0.78
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.383
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.078
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #300"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 78.2% (HIGH) · Higher = larger blast radius
    78.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 33.8% · Safe to merge
    33.8%