/** * Forms module envelope encryption service. * * Lifts a per-tenant data-encryption key (DEK) wrapped under a master KMS key, * caches the unwrapped DEK in-memory, and uses it to encrypt/decrypt * submission revision payloads with AES-256-GCM. Ciphertext is self-describing * — the header carries the format version and the key version, so older * revisions remain decryptable across rotations. * * The `data` column on `forms_form_submission_revision` is encrypted by THIS * service, NOT by the global `findWithDecryption` pipeline. Phase 1c spec * (Cross-Cutting Concerns: encryption) is the source of truth for the format. * * Ciphertext layout: * version(2B big-endian) | key_version(2B big-endian) | iv(12B) | ciphertext | tag(16B) * Format version is `0x0001` for v1; if/when the format changes, bump and * dispatch on the leading two bytes. * * KMS integration & env vars: * The per-tenant DEK is wrapped under a master key. Three postures exist, * selected by `resolveKmsAdapter(...)`: * * 1. `EnvMasterKeyKmsAdapter` (production-viable, default when configured) — * wraps/unwraps with a real 256-bit master key supplied via * `FORMS_ENCRYPTION_MASTER_KEY` (base64 or hex, must decode to exactly 32 * bytes). This models the standard posture where a secret manager / cloud * KMS decrypts the master key into the environment at boot. * 2. Operator-supplied cloud KMS adapter (AWS KMS, GCP KMS, HashiCorp Vault, * …) — injected via DI through the `kmsAdapter` constructor slot or the * `setKmsAdapterFactory(...)` hook. No cloud SDK ships with this module. * 3. `DevDeterministicKmsAdapter` (DEV ONLY) — derives a deterministic wrap * key from `FORMS_ENCRYPTION_KMS_KEY_ID`. Adequate for local development, * tests, and CI but NEVER for production PHI. * * Production guard: when `NODE_ENV === 'production'` and no real master key / * adapter is configured, `resolveKmsAdapter(...)` throws * `INSECURE_KMS_IN_PRODUCTION` rather than protecting PHI with the dev * adapter (spec `.ai/specs/2026-05-21-...gap-analysis.md` W1 / risk R-1). * * Env vars: * - `FORMS_ENCRYPTION_MASTER_KEY` — 32-byte master key, base64 or hex. When * set and valid, the env master-key adapter is used. * - `FORMS_ENCRYPTION_KMS_KEY_ID` — dev-only wrap-key seed for the * deterministic fallback adapter. * * All adapters emit the same wrapped-DEK envelope (iv | ciphertext | tag), so * wrapped keys are format-compatible: a DEK wrapped by the dev adapter still * unwraps with the dev adapter, and likewise for the env adapter. */ import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto' import type { EntityManager } from '@mikro-orm/postgresql' import type { CompiledFormVersion } from './form-version-compiler' import { FormsEncryptionKey } from '../data/entities' const FORMAT_VERSION_V1 = 0x0001 const HEADER_LENGTH = 4 // 2 bytes format version + 2 bytes key version const IV_LENGTH = 12 const TAG_LENGTH = 16 const DEK_LENGTH = 32 const ALGORITHM = 'aes-256-gcm' as const const DEFAULT_DEK_TTL_MS = 60 * 60 * 1000 // 1 hour const DEFAULT_DEK_CACHE_MAX = 256 export class FormsEncryptionError extends Error { readonly code: string constructor(code: string, message: string) { super(message) this.name = 'FormsEncryptionError' this.code = code } } export interface KmsAdapter { /** Wraps a fresh 32-byte DEK under the master key (returns ciphertext). */ wrap(dek: Buffer): Promise /** Unwraps a stored wrapped DEK (returns plaintext). */ unwrap(wrapped: Buffer): Promise } /** * DEV-ONLY KMS adapter — derives a deterministic AES-256-GCM "wrap" key from * `FORMS_ENCRYPTION_KMS_KEY_ID`. Provides envelope encryption for local * development, tests, and CI. Operators MUST replace this with a real KMS * adapter before production rollout. */ export class DevDeterministicKmsAdapter implements KmsAdapter { private readonly wrapKey: Buffer constructor(kmsKeyId: string) { if (!kmsKeyId || typeof kmsKeyId !== 'string') { throw new FormsEncryptionError( 'KMS_KEY_ID_MISSING', 'FORMS_ENCRYPTION_KMS_KEY_ID is required (dev fallback derives the wrap key from this id).', ) } this.wrapKey = createHash('sha256').update(`forms-enc-v1::${kmsKeyId}`).digest() } async wrap(dek: Buffer): Promise { if (dek.length !== DEK_LENGTH) { throw new FormsEncryptionError('DEK_INVALID_LENGTH', 'DEK must be 32 bytes.') } const iv = randomBytes(IV_LENGTH) const cipher = createCipheriv(ALGORITHM, this.wrapKey, iv) const ciphertext = Buffer.concat([cipher.update(dek), cipher.final()]) const tag = cipher.getAuthTag() return Buffer.concat([iv, ciphertext, tag]) } async unwrap(wrapped: Buffer): Promise { if (wrapped.length !== IV_LENGTH + DEK_LENGTH + TAG_LENGTH) { throw new FormsEncryptionError('WRAPPED_DEK_INVALID_LENGTH', 'Wrapped DEK has unexpected length.') } const iv = wrapped.subarray(0, IV_LENGTH) const ciphertext = wrapped.subarray(IV_LENGTH, IV_LENGTH + DEK_LENGTH) const tag = wrapped.subarray(IV_LENGTH + DEK_LENGTH) const decipher = createDecipheriv(ALGORITHM, this.wrapKey, iv) decipher.setAuthTag(tag) return Buffer.concat([decipher.update(ciphertext), decipher.final()]) } } /** * Production-viable KMS adapter — wraps/unwraps the per-tenant DEK with an * externally-supplied 256-bit master key (NOT derived from an id string). * * The master key is read from `FORMS_ENCRYPTION_MASTER_KEY` and may be encoded * as base64 or hex; it MUST decode to exactly 32 bytes. This models the * standard posture where a secret manager / cloud KMS decrypts the master key * into the environment at boot. Wrap uses AES-256-GCM and emits the same * `iv | ciphertext | tag` envelope as the dev adapter, so stored wrapped-DEKs * are format-compatible. */ export class EnvMasterKeyKmsAdapter implements KmsAdapter { private readonly masterKey: Buffer constructor(masterKey: Buffer) { if (masterKey.length !== DEK_LENGTH) { throw new FormsEncryptionError( 'MASTER_KEY_INVALID_LENGTH', `Master key must be exactly ${DEK_LENGTH} bytes, received ${masterKey.length}.`, ) } this.masterKey = masterKey } /** * Builds an adapter from a base64- or hex-encoded master key string. * Throws `FormsEncryptionError('MASTER_KEY_INVALID', ...)` when the value is * missing, undecodable, or does not yield exactly 32 bytes. */ static fromEncoded(encoded: string | undefined | null): EnvMasterKeyKmsAdapter { if (!encoded || typeof encoded !== 'string' || encoded.trim().length === 0) { throw new FormsEncryptionError( 'MASTER_KEY_INVALID', 'FORMS_ENCRYPTION_MASTER_KEY is required and must be a base64- or hex-encoded 32-byte key.', ) } const decoded = decodeMasterKey(encoded.trim()) if (!decoded) { throw new FormsEncryptionError( 'MASTER_KEY_INVALID', 'FORMS_ENCRYPTION_MASTER_KEY must be a valid base64 or hex string.', ) } if (decoded.length !== DEK_LENGTH) { throw new FormsEncryptionError( 'MASTER_KEY_INVALID_LENGTH', `FORMS_ENCRYPTION_MASTER_KEY must decode to exactly ${DEK_LENGTH} bytes, got ${decoded.length}.`, ) } return new EnvMasterKeyKmsAdapter(decoded) } async wrap(dek: Buffer): Promise { if (dek.length !== DEK_LENGTH) { throw new FormsEncryptionError('DEK_INVALID_LENGTH', 'DEK must be 32 bytes.') } const iv = randomBytes(IV_LENGTH) const cipher = createCipheriv(ALGORITHM, this.masterKey, iv) const ciphertext = Buffer.concat([cipher.update(dek), cipher.final()]) const tag = cipher.getAuthTag() return Buffer.concat([iv, ciphertext, tag]) } async unwrap(wrapped: Buffer): Promise { if (wrapped.length !== IV_LENGTH + DEK_LENGTH + TAG_LENGTH) { throw new FormsEncryptionError('WRAPPED_DEK_INVALID_LENGTH', 'Wrapped DEK has unexpected length.') } const iv = wrapped.subarray(0, IV_LENGTH) const ciphertext = wrapped.subarray(IV_LENGTH, IV_LENGTH + DEK_LENGTH) const tag = wrapped.subarray(IV_LENGTH + DEK_LENGTH) const decipher = createDecipheriv(ALGORITHM, this.masterKey, iv) decipher.setAuthTag(tag) try { return Buffer.concat([decipher.update(ciphertext), decipher.final()]) } catch (error) { const message = error instanceof Error ? error.message : 'Unknown unwrap error' throw new FormsEncryptionError('DEK_UNWRAP_FAILED', `Failed to unwrap DEK: ${message}`) } } } /** * Pluggable cloud-KMS hook. * * Operators integrating AWS KMS, GCP KMS, HashiCorp Vault, or any other key * manager implement the `KmsAdapter` interface (`wrap`/`unwrap`) and register a * factory here at boot — e.g. from the app's DI bootstrap — WITHOUT editing * this module or adding cloud SDK dependencies to it. When a factory is * registered it takes precedence over the env-var resolution below. * * Example (in app bootstrap): * import { setKmsAdapterFactory } from '@open-mercato/forms/.../encryption-service' * setKmsAdapterFactory(() => new MyAwsKmsAdapter({ keyId: process.env.AWS_KMS_KEY_ARN })) */ export type KmsAdapterFactory = (env: NodeJS.ProcessEnv) => KmsAdapter let registeredKmsAdapterFactory: KmsAdapterFactory | null = null /** Registers an operator-supplied cloud-KMS adapter factory (or clears it with `null`). */ export function setKmsAdapterFactory(factory: KmsAdapterFactory | null): void { registeredKmsAdapterFactory = factory } /** * Selects the active KMS adapter and enforces the production safety guard. * * Resolution order: * 1. operator-registered factory (`setKmsAdapterFactory`) — always wins; * 2. `EnvMasterKeyKmsAdapter` when `FORMS_ENCRYPTION_MASTER_KEY` is set; * 3. `DevDeterministicKmsAdapter` (dev fallback). * * Throws `INSECURE_KMS_IN_PRODUCTION` when running in production with only the * dev fallback available — refusing to protect PHI with the dev adapter. */ export function resolveKmsAdapter(env: NodeJS.ProcessEnv = process.env): KmsAdapter { if (registeredKmsAdapterFactory) { return registeredKmsAdapterFactory(env) } const masterKey = env.FORMS_ENCRYPTION_MASTER_KEY if (masterKey && masterKey.trim().length > 0) { return EnvMasterKeyKmsAdapter.fromEncoded(masterKey) } if (env.NODE_ENV === 'production') { throw new FormsEncryptionError( 'INSECURE_KMS_IN_PRODUCTION', 'Refusing to encrypt forms PHI with the DEV-ONLY deterministic KMS adapter in production. ' + 'Set FORMS_ENCRYPTION_MASTER_KEY to a 32-byte base64/hex master key, or inject a real ' + 'cloud-KMS adapter via setKmsAdapterFactory(...) / the kmsAdapter DI slot.', ) } const kmsKeyId = env.FORMS_ENCRYPTION_KMS_KEY_ID ?? '' return new DevDeterministicKmsAdapter(kmsKeyId || 'forms-dev-fallback-key-id') } function decodeMasterKey(encoded: string): Buffer | null { if (/^[0-9a-fA-F]+$/.test(encoded) && encoded.length % 2 === 0) { const hex = Buffer.from(encoded, 'hex') if (hex.length === encoded.length / 2) return hex } if (/^[A-Za-z0-9+/=_-]+$/.test(encoded)) { const base64 = Buffer.from(encoded, 'base64') if (base64.length > 0) return base64 } return null } export type EncryptionServiceOptions = { emFactory: () => EntityManager kmsAdapter?: KmsAdapter cacheMax?: number cacheTtlMs?: number /** Override the clock for tests. */ now?: () => number } type DekCacheEntry = { dek: Buffer expiresAt: number } export interface EncryptionService { encrypt(organizationId: string, plaintext: Buffer): Promise decrypt(organizationId: string, ciphertext: Buffer): Promise currentKeyVersion(organizationId: string): Promise rotate(organizationId: string): Promise } export class FormsEncryptionService implements EncryptionService { private readonly emFactory: () => EntityManager private readonly kms: KmsAdapter private readonly cacheMax: number private readonly cacheTtlMs: number private readonly now: () => number private readonly dekCache = new Map() private readonly currentVersionCache = new Map() constructor(options: EncryptionServiceOptions) { this.emFactory = options.emFactory this.cacheMax = options.cacheMax ?? DEFAULT_DEK_CACHE_MAX this.cacheTtlMs = options.cacheTtlMs ?? DEFAULT_DEK_TTL_MS this.now = options.now ?? (() => Date.now()) // An explicitly injected adapter always wins over env resolution; otherwise // resolveKmsAdapter selects the env master-key adapter or dev fallback and // enforces the production guard (refuses the dev adapter in production). this.kms = options.kmsAdapter ?? resolveKmsAdapter(process.env) } async encrypt(organizationId: string, plaintext: Buffer): Promise { const keyVersion = await this.currentKeyVersion(organizationId) const dek = await this.getDek(organizationId, keyVersion) const iv = randomBytes(IV_LENGTH) const cipher = createCipheriv(ALGORITHM, dek, iv) const body = Buffer.concat([cipher.update(plaintext), cipher.final()]) const tag = cipher.getAuthTag() const header = Buffer.alloc(HEADER_LENGTH) header.writeUInt16BE(FORMAT_VERSION_V1, 0) header.writeUInt16BE(keyVersion, 2) return Buffer.concat([header, iv, body, tag]) } async decrypt(organizationId: string, ciphertext: Buffer): Promise { if (ciphertext.length < HEADER_LENGTH + IV_LENGTH + TAG_LENGTH) { throw new FormsEncryptionError('CIPHERTEXT_TOO_SHORT', 'Ciphertext too short to be a valid envelope.') } const formatVersion = ciphertext.readUInt16BE(0) if (formatVersion !== FORMAT_VERSION_V1) { throw new FormsEncryptionError( 'UNSUPPORTED_FORMAT_VERSION', `Unsupported envelope format version ${formatVersion}.`, ) } const keyVersion = ciphertext.readUInt16BE(2) const iv = ciphertext.subarray(HEADER_LENGTH, HEADER_LENGTH + IV_LENGTH) const tag = ciphertext.subarray(ciphertext.length - TAG_LENGTH) const body = ciphertext.subarray(HEADER_LENGTH + IV_LENGTH, ciphertext.length - TAG_LENGTH) const dek = await this.getDek(organizationId, keyVersion) const decipher = createDecipheriv(ALGORITHM, dek, iv) decipher.setAuthTag(tag) try { return Buffer.concat([decipher.update(body), decipher.final()]) } catch (error) { const message = error instanceof Error ? error.message : 'Unknown decryption error' throw new FormsEncryptionError('DECRYPTION_FAILED', `Failed to decrypt envelope: ${message}`) } } async currentKeyVersion(organizationId: string): Promise { const cached = this.currentVersionCache.get(organizationId) if (typeof cached === 'number') return cached const em = this.emFactory() const active = await em.findOne( FormsEncryptionKey, { organizationId, retiredAt: null }, { orderBy: { keyVersion: 'desc' } }, ) if (active) { this.currentVersionCache.set(organizationId, active.keyVersion) return active.keyVersion } const created = await this.createInitialKey(em, organizationId) this.currentVersionCache.set(organizationId, created.keyVersion) return created.keyVersion } async rotate(organizationId: string): Promise { const em = this.emFactory() const current = await em.findOne( FormsEncryptionKey, { organizationId, retiredAt: null }, { orderBy: { keyVersion: 'desc' } }, ) const nextVersion = (current?.keyVersion ?? 0) + 1 const dek = randomBytes(DEK_LENGTH) const wrapped = await this.kms.wrap(dek) const fresh = em.create(FormsEncryptionKey, { organizationId, keyVersion: nextVersion, wrappedDek: wrapped, createdAt: new Date(this.now()), }) if (current) { current.retiredAt = new Date(this.now()) } em.persist(fresh) if (current) em.persist(current) await em.flush() this.currentVersionCache.set(organizationId, nextVersion) this.cacheDek(this.dekCacheKey(organizationId, nextVersion), dek) return nextVersion } /** * Test/maintenance hook: drop all in-memory cached DEKs. */ resetCache(): void { this.dekCache.clear() this.currentVersionCache.clear() } // -------------------------------------------------------------------------- // Internals // -------------------------------------------------------------------------- private async createInitialKey( em: EntityManager, organizationId: string, ): Promise { const dek = randomBytes(DEK_LENGTH) const wrapped = await this.kms.wrap(dek) const row = em.create(FormsEncryptionKey, { organizationId, keyVersion: 1, wrappedDek: wrapped, createdAt: new Date(this.now()), }) em.persist(row) await em.flush() this.cacheDek(this.dekCacheKey(organizationId, 1), dek) return row } private async getDek(organizationId: string, keyVersion: number): Promise { const cacheKey = this.dekCacheKey(organizationId, keyVersion) const cached = this.dekCache.get(cacheKey) if (cached && cached.expiresAt > this.now()) { // Refresh LRU position on hit. this.dekCache.delete(cacheKey) this.dekCache.set(cacheKey, cached) return cached.dek } if (cached) this.dekCache.delete(cacheKey) const em = this.emFactory() const row = await em.findOne(FormsEncryptionKey, { organizationId, keyVersion }) if (!row) { throw new FormsEncryptionError( 'DEK_NOT_FOUND', `Encryption key not found for organization ${organizationId} version ${keyVersion}.`, ) } const wrapped = ensureBuffer(row.wrappedDek) const dek = await this.kms.unwrap(wrapped) this.cacheDek(cacheKey, dek) return dek } private dekCacheKey(organizationId: string, keyVersion: number): string { return `${organizationId}:${keyVersion}` } private cacheDek(cacheKey: string, dek: Buffer): void { while (this.dekCache.size >= this.cacheMax) { const oldest = this.dekCache.keys().next().value if (!oldest) break this.dekCache.delete(oldest) } this.dekCache.set(cacheKey, { dek, expiresAt: this.now() + this.cacheTtlMs }) } } function ensureBuffer(value: Buffer | Uint8Array | string): Buffer { if (Buffer.isBuffer(value)) return value if (value instanceof Uint8Array) return Buffer.from(value) if (typeof value === 'string') return Buffer.from(value, 'binary') throw new FormsEncryptionError('WRAPPED_DEK_INVALID_TYPE', 'Wrapped DEK has unsupported representation.') } /** * Redaction helper used by the logger middleware for R1 mitigation. * * Walks the payload at the root level and replaces any field marked * `x-om-sensitive: true` in the compiled form version with `"[REDACTED]"`. * Designed for shallow form payloads; nested sensitive structures should be * handled by the field type's own redactor in future phases. */ export function redactSensitive( compiled: CompiledFormVersion, payload: Record, ): Record { if (!payload || typeof payload !== 'object') return payload const out: Record = {} for (const [key, value] of Object.entries(payload)) { const descriptor = compiled.fieldIndex[key] if (descriptor?.sensitive) { out[key] = '[REDACTED]' } else { out[key] = value } } return out }