Files
Georgy ButaevandClaude Opus 4.7 cc76194fd6 feat(crypto): encrypt webhook_endpoints secrets at rest (phase 1 of 2)
The PHASE-2 debt the user explicitly chose 30+ commits ago in
2755a8a5 ("plaintext for now, encrypt later"). Webhook signing secrets
are needed at fire time (the worker must sign each delivery), so they
have to live somewhere — but a DB compromise should NOT reveal them.

This commit lands the encryption infrastructure and starts dual-write.
Commit 2 of 2 will backfill secret_encrypted (no-op today: prod has
zero webhook_endpoints rows), flip getEndpointSigningContext to
decrypt, drop secret_plaintext.

Cipher (src/lib/crypto/webhookSecretCipher.ts):
  AES-256-GCM with WEBHOOK_SECRET_KEY env var (32-byte hex). Ciphertext
  layout: IV(12B) || GCM tag(16B) || ciphertext. Authenticated — a
  flipped byte anywhere in the blob throws on decrypt, so a DB
  compromise can't return a forged secret to the worker (only DoS by
  deleting rows). loadKey() validates the env var at first use and
  caches the parsed key; resetCachedKey() is test-only.

Schema (migration 20260604000003, applied to remote):
  ALTER webhook_endpoints ADD COLUMN secret_encrypted BYTEA.
  Service dual-writes; reads still come from secret_plaintext so the
  worker keeps signing deliveries unchanged.

Service (webhookEndpointsService.createWebhookEndpoint):
  Dual-writes secret_plaintext + secret_encrypted on mint. If
  encryptWebhookSecret throws (env var missing/malformed), the mint
  fails — we never store a row that phase 2 can't decrypt.

Deploy checklist: new step 9 (WEBHOOK_SECRET_KEY env var) with
generation command, verification (mint a webhook endpoint and watch
for 500), rollback warning (rotating the key requires re-encryption,
mention of incident-response posture).

Tests (16):
  loadKey: cache, missing env, empty env, non-hex, wrong length
  round-trip: exact equality, len-1, 4KB, unicode, IV-randomness,
    blob layout assertion (12+16+len(plaintext))
  tampering: flipped ciphertext / IV / tag byte, truncated blob,
    wrong key → all throw

Suite: 55 suites · 717 tests · 0 failures (+1 suite, +16 tests).
tsc + eslint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 04:03:38 +02:00
..