Repository navigation
feat: realm key store - zero-config mfa seed encryption + optional secrets kek - #3255
Conversation
…crets kek - generalize auth_keys with a JWK use discriminator (sig | enc); per-realm oct enc keys are minted lazily, mirroring the existing RSA signing keys (port promoted to core/key IKeyRepository, adapter KeyRepositoryAdapter) - MFA TOTP seeds encrypt under the user's realm enc key via self-describing v1.<key_id>.<blob> blobs (core/key RealmCipher, key-id addressed decrypt with realm binding assert) - MFA_ENCRYPTION_KEY is removed while still unreleased; MFA_ENABLED=true is now sufficient to enable MFA - optional SECRETS_ENCRYPTION_KEY (base64, 32 bytes) wraps ALL auth_keys material at rest (realm signing private keys included) as wrapped.v1. blobs, with lazy wrap-on-read adoption, loud failure on a missing KEK, and a one-time production boot warning when unset - use hygiene enforced at every sig consumer: signer + authorize grant request JWKUse.SIGNATURE, the verifier rejects a kid resolving to a non-sig key, and both JWKS surfaces filter use=sig - auth_keys.use ALTER folded into unreleased migration 1783856507391 (both dialects; run/revert/re-run verified on mysql + postgres)
|
Note Reviews pausedUse the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR replaces configuration-based MFA encryption with per-realm encryption keys, adds JWK usage discrimination, introduces KEK wrapping and ChangesRealm Key Store and MFA encryption
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant UserAuthenticatorService
participant RealmCipher
participant KeyRepositoryAdapter
participant auth_keys
UserAuthenticatorService->>RealmCipher: encrypt(realmId, TOTP seed)
RealmCipher->>KeyRepositoryAdapter: resolve enc key
KeyRepositoryAdapter->>auth_keys: create or load realm key
auth_keys-->>RealmCipher: key id and material
RealmCipher-->>UserAuthenticatorService: versioned encrypted blob
UserAuthenticatorService->>RealmCipher: decrypt(blob, realmId)
RealmCipher->>KeyRepositoryAdapter: resolve embedded key id
RealmCipher-->>UserAuthenticatorService: seed or verification failure
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
authup
@authup/client-web
@authup/server-core
@authup/access
@authup/client-web-kit
@authup/client-web-kit-theme
@authup/client-web-nuxt
@authup/client-web-theme
@authup/core-http-kit
@authup/core-kit
@authup/core-realtime-kit
@authup/errors
@authup/i18n
@authup/kit
@authup/server-adapter-kit
@authup/server-adapter-node
@authup/server-adapter-socket-io
@authup/server-adapter-web
@authup/server-kit
@authup/server-test-kit
@authup/specs
commit: |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@apps/server-core/src/adapters/database/migrations/postgres/1783856507391-Default.ts`:
- Around line 112-125: Keep the existing 1783856507391-Default migrations
immutable by removing the auth_keys.use schema changes from both
apps/server-core/src/adapters/database/migrations/postgres/1783856507391-Default.ts
(lines 112-125) and
apps/server-core/src/adapters/database/migrations/mysql/1783856507391-Default.ts
(lines 113-126), then add a new follow-up migration that adds auth_keys.use with
the same definition and reverses it in down().
In `@apps/server-core/src/core/entities/user-authenticator/service.ts`:
- Around line 740-747: Update the decryption catch block in the authenticator
service around this.cipher.decrypt to return false only for expected AuthupError
validation/decryption failures, using isAuthupError from `@authup/errors`.
Re-throw all other errors so infrastructure failures, including database
connection errors, propagate as internal server errors instead of being treated
as failed verification attempts.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e62d0b7e-5b72-49f2-90a6-340e187ed149
📒 Files selected for processing (49)
.agents/architecture.mdapps/server-core/src/adapters/database/domains/key/entity.tsapps/server-core/src/adapters/database/migrations/mysql/1783856507391-Default.tsapps/server-core/src/adapters/database/migrations/postgres/1783856507391-Default.tsapps/server-core/src/adapters/http/controllers/workflows/jwks/handlers/read.tsapps/server-core/src/adapters/http/controllers/workflows/jwks/module.tsapps/server-core/src/adapters/http/controllers/workflows/token/types.tsapps/server-core/src/app/modules/config/constants.tsapps/server-core/src/app/modules/config/normalize.tsapps/server-core/src/app/modules/config/read/env.tsapps/server-core/src/app/modules/config/types.tsapps/server-core/src/app/modules/config/validator.tsapps/server-core/src/app/modules/http/modules/controller.tsapps/server-core/src/app/modules/oauth2/constants.tsapps/server-core/src/app/modules/oauth2/module.tsapps/server-core/src/app/modules/oauth2/repositories/key/repository.tsapps/server-core/src/core/entities/user-authenticator/service.tsapps/server-core/src/core/entities/user-authenticator/types.tsapps/server-core/src/core/index.tsapps/server-core/src/core/key/constants.tsapps/server-core/src/core/key/index.tsapps/server-core/src/core/key/realm-cipher.tsapps/server-core/src/core/key/types.tsapps/server-core/src/core/key/wrap.tsapps/server-core/src/core/oauth2/grant-types/authorize.tsapps/server-core/src/core/oauth2/grant-types/types.tsapps/server-core/src/core/oauth2/index.tsapps/server-core/src/core/oauth2/key/types.tsapps/server-core/src/core/oauth2/token/signer/module.tsapps/server-core/src/core/oauth2/token/verifier/module.tsapps/server-core/test/unit/app/modules/oauth2/key-repository.spec.tsapps/server-core/test/unit/config/index.spec.tsapps/server-core/test/unit/core/entities/user-authenticator/service.spec.tsapps/server-core/test/unit/core/helpers/fake-key-repository.tsapps/server-core/test/unit/core/helpers/index.tsapps/server-core/test/unit/core/key/realm-cipher.spec.tsapps/server-core/test/unit/core/key/wrap.spec.tsapps/server-core/test/unit/core/oauth2/grant-types/authorize.spec.tsapps/server-core/test/unit/core/oauth2/token/signer/module.spec.tsapps/server-core/test/unit/core/oauth2/token/verifier/module.spec.tsapps/server-core/test/unit/http/controllers/entities/user-authenticator-email.spec.tsapps/server-core/test/unit/http/controllers/entities/user-authenticator.spec.tsapps/server-core/test/unit/http/controllers/workflows/key-store.spec.tsapps/server-core/test/unit/http/controllers/workflows/token/grant-password-mfa.spec.tsapps/server-core/test/unit/http/controllers/workflows/token/id-token-claims.spec.tsapps/server-core/test/unit/http/controllers/workflows/token/mfa-login-ticket.spec.tsdocs/src/guide/deployment/configuration-server-core.mdpackages/core-kit/src/domains/key/type.tspackages/specs/src/json-web-key/constants.ts
💤 Files with no reviewable changes (7)
- apps/server-core/test/unit/http/controllers/entities/user-authenticator-email.spec.ts
- apps/server-core/src/core/oauth2/key/types.ts
- apps/server-core/test/unit/http/controllers/workflows/token/id-token-claims.spec.ts
- apps/server-core/test/unit/http/controllers/entities/user-authenticator.spec.ts
- apps/server-core/test/unit/http/controllers/workflows/token/mfa-login-ticket.spec.ts
- apps/server-core/src/core/oauth2/index.ts
- apps/server-core/test/unit/http/controllers/workflows/token/grant-password-mfa.spec.ts
|
@coderabbitai pause |
✅ Action performedReviews paused. |
…st, realm required encrypt(plain, realmId) / decrypt(blob, realmId): the payload is the verb's subject (shape-aligned with ISymmetricCipher.encrypt(plain) plus a scope argument), and the previously optional decrypt realm becomes REQUIRED so the foreign-realm binding assert can never be skipped - every consumer (MFA devices, plan-070 client/IdP secrets) knows its entity's realm.
…blob-semantics errors RealmCipher.decrypt now throws a dedicated RealmCipherBlobError (Symbol-marked, isRealmCipherBlobError guard) for malformed blobs, unknown/foreign/non-enc key references, and failed GCM authentication; verifyTotp maps ONLY that class to a plain verification failure. Infrastructure errors - a database outage during key resolution, KEK misconfiguration - bubble as 500s instead of silently burning throttle attempts (spurious lockout risk flagged in review). Note the review's isAuthupError suggestion would not have covered the KEK case: that failure IS an AuthupError but is infrastructure, not blob semantics.
Summary
Implements plan 069: generalizes
auth_keysinto a per-realm key store, makes MFA seed encryption zero-config, and adds an optional KEK that wraps all stored key material at rest.auth_keysgains a JWKusediscriminator (sig|enc, RFC 7517 §4.2; existing rows defaultsig). Per-realmenckeys (32-byte oct) are minted lazily on first use, mirroring the existing RSA signing keys. Port promoted tocore/key/(IKeyRepository), adapter renamedKeyRepositoryAdapter.v1.<key_id>.<blob>blobs (RealmCipher— key-id-addressed decrypt with realm-binding assert; unknown/foreign key fails closed as a plain verification failure).MFA_ENCRYPTION_KEYis removed (never released — no deployments, no legacy-blob path);MFA_ENABLED=trueis now sufficient.SECRETS_ENCRYPTION_KEY(base64, 32 bytes): wraps ALLauth_keysmaterial at rest — realm JWT signing private keys included — aswrapped.v1.blobs. Existing plaintext rows wrap lazily on read; a wrapped row met without a KEK fails loud; production boots without one log a single warning. Unset = Keycloak/authentik parity posture (both store OTP secrets and signing keys plaintext).usehygiene is load-bearing: the signer supports HMAC (oct) signing keys, so without filtering it could sign tokens with a realm's MFA enc key. Signer + authorize grant (at_hash alg) requestJWKUse.SIGNATURE, the verifier rejects akidresolving to a non-sig key, and both JWKS surfaces filteruse = sig— all pinned by tests.Migration
auth_keys.useALTER folded into the unreleased1783856507391migration (both dialects) per the fold-per-release convention. This PR should merge before the next server-core release — that keeps theMFA_ENCRYPTION_KEYremoval a clean deletion and the fold window open.Testing
check:typesgreen.core/keywrap + RealmCipher unit specs; KEK wrap/unwrap matrix over an isolated in-memory DataSource (test/unit/app/modules/oauth2/key-repository.spec.ts); http pin for zero-config enrollment + JWKS enc-key exclusion; verifier enc-kidrejection.run→ 12×revert→runverified on MySQL and Postgres;usecolumn present in both schemas.Follow-up (plan 070): consume the cipher seam for client
secret_encrypted(dormant todos), IdPclient_secret, LDAP bind password.Summary by CodeRabbit
New Features
secretsEncryptionKeyconfiguration to protect stored key material.Documentation
Tests