FORGE-503 Refuse or detach, not 500, when deleting a database credential secret #1331

Merged
gmackie merged 1 commit from FORGE-503-secret-rm-500 into main 2026-12-05 07:01:54 +00:00
Owner

Fixes FORGE-503 (https://tasks.gmac.io/gmacko/issue/FORGE-503): forge secret rm DATABASE_URL_LOCAL --stage production for app fabforge returned a bodyless HTTP 500.

Root cause

forge db create (and forge db rotate-password) store the stage's DATABASE_URL_LOCAL and link it from node_database_bindings.credential_secret_id. That foreign key has no ON DELETE action, so DELETE /api/fg/secrets/:key did a bare db.delete(secrets), Postgres raised 23503 (node_database_bindings_credential_secret_id_secrets_id_fk), and the unhandled throw became a 500 with no body.

Evidence (read-only, metadata only): fabforge production's DATABASE_URL_LOCAL row (id 59952bbd…) is the credential_secret_id of binding 3426d2ae… (fabforge_production, created 2026-05-23 08:36:49, one second before the secret). It is the only FK into secrets; 38 bindings in production carry a credential, so every forge db create app hits this. All 15 fabforge production rows use the same age: format, so decryption, key versions and row shape are not the cause. The regression test reproduces the exact 23503 against the old route.

Fix

  • packages/api/src/lib/secret-delete.ts: deleteSecret() looks up bindings that use the secret as their credential. Without consent it returns in_use; with detachDatabaseBindings it nulls credential_secret_id and deletes the row in one atomic() write. A 23503 from a race is mapped to in_use too.
  • DELETE /api/fg/secrets/:key: 409 with an error naming the binding (db, node, stage) and the way forward; ?detachDatabase=true detaches and deletes (200 with detachedBindings).
  • forge secret rm --detach-database sends that flag.
  • tRPC secret.delete uses the same helper (CONFLICT instead of an internal error).
  • GET /api/fg/secrets/:key: 422 naming the key when the row cannot be decrypted, instead of a 500.

forge secret get FORGEJO_API_TOKEN --stage production returns 0 today (checked 2026-12-05, value not printed), so the GET symptom did not reproduce; the GET change is defensive.

Tests

  • New apps/web/src/app/api/fg/secrets/[id]/route.database.test.ts: 409 + row kept, 422 on undecryptable row, detach + delete. Fails on the old route (23503 / decrypt throw), passes now on Postgres and the D1 lane.
  • cli.database.test.ts with the CLI built from this branch, api/web secret unit tests, api secret database tests, go test ./cmd/fg/commands -run Secret, typecheck for api and web: all pass.

Data remediation after deploy (not run): forge secret rm DATABASE_URL_LOCAL --stage production --detach-database from the fabforge repo. fabforge is on D1 now; the binding fabforge_production then falls back to the stage's DATABASE_URL, which fabforge doesn't have, so forge db url/connect for it stops working, which is the intent.

🤖 Generated with Claude Code

Fixes FORGE-503 (https://tasks.gmac.io/gmacko/issue/FORGE-503): `forge secret rm DATABASE_URL_LOCAL --stage production` for app fabforge returned a bodyless HTTP 500. ## Root cause `forge db create` (and `forge db rotate-password`) store the stage's `DATABASE_URL_LOCAL` and link it from `node_database_bindings.credential_secret_id`. That foreign key has no ON DELETE action, so `DELETE /api/fg/secrets/:key` did a bare `db.delete(secrets)`, Postgres raised 23503 (`node_database_bindings_credential_secret_id_secrets_id_fk`), and the unhandled throw became a 500 with no body. Evidence (read-only, metadata only): fabforge production's `DATABASE_URL_LOCAL` row (id 59952bbd…) is the `credential_secret_id` of binding 3426d2ae… (`fabforge_production`, created 2026-05-23 08:36:49, one second before the secret). It is the only FK into `secrets`; 38 bindings in production carry a credential, so every `forge db create` app hits this. All 15 fabforge production rows use the same `age:` format, so decryption, key versions and row shape are not the cause. The regression test reproduces the exact 23503 against the old route. ## Fix - `packages/api/src/lib/secret-delete.ts`: `deleteSecret()` looks up bindings that use the secret as their credential. Without consent it returns `in_use`; with `detachDatabaseBindings` it nulls `credential_secret_id` and deletes the row in one `atomic()` write. A 23503 from a race is mapped to `in_use` too. - `DELETE /api/fg/secrets/:key`: 409 with an error naming the binding (db, node, stage) and the way forward; `?detachDatabase=true` detaches and deletes (200 with `detachedBindings`). - `forge secret rm --detach-database` sends that flag. - tRPC `secret.delete` uses the same helper (CONFLICT instead of an internal error). - `GET /api/fg/secrets/:key`: 422 naming the key when the row cannot be decrypted, instead of a 500. `forge secret get FORGEJO_API_TOKEN --stage production` returns 0 today (checked 2026-12-05, value not printed), so the GET symptom did not reproduce; the GET change is defensive. ## Tests - New `apps/web/src/app/api/fg/secrets/[id]/route.database.test.ts`: 409 + row kept, 422 on undecryptable row, detach + delete. Fails on the old route (23503 / decrypt throw), passes now on Postgres and the D1 lane. - `cli.database.test.ts` with the CLI built from this branch, api/web secret unit tests, api secret database tests, `go test ./cmd/fg/commands -run Secret`, typecheck for api and web: all pass. Data remediation after deploy (not run): `forge secret rm DATABASE_URL_LOCAL --stage production --detach-database` from the fabforge repo. fabforge is on D1 now; the binding `fabforge_production` then falls back to the stage's DATABASE_URL, which fabforge doesn't have, so `forge db url/connect` for it stops working, which is the intent. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-503 Refuse or detach, not 500, when deleting a database credential secret
All checks were successful
Registry deps hash / inputs (pull_request) Successful in 40s
Registry deps hash / rebuild (pull_request) Successful in 4m56s
Registry deps hash / pnpm-deps-hash (pull_request) Successful in 20s
CI / superseded (pull_request) Successful in 31s
CI / gitleaks (pull_request) Successful in 1m10s
CI / storybook (pull_request) Successful in 1m55s
CI / database-d1 (pull_request) Has been skipped
CI / unit-web (pull_request) Successful in 1m24s
CI / checks (pull_request) Successful in 2m46s
CI / go (pull_request) Successful in 1m10s
CI / web-build (pull_request) Successful in 5m2s
CI / database (pull_request) Successful in 4m34s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 25s
Preview / deploy (pull_request) Has been skipped
Preview / cleanup (pull_request) Has been skipped
Preview / e2e (pull_request) Has been skipped
Preview / release (pull_request) Has been skipped
227ec8a803
`forge db create` and `forge db rotate-password` point the binding's
node_database_bindings.credential_secret_id at the stage's DATABASE_URL_LOCAL
row. That foreign key has no ON DELETE action, so DELETE /api/fg/secrets/:key
raised an unhandled 23503 and the route returned a bodyless 500.

DELETE now returns 409 naming the database binding(s) that use the secret.
With detachDatabase=true (forge secret rm --detach-database) it unlinks the
binding and deletes the row in one atomic() write. The tRPC secret.delete
shares the helper. GET returns 422 naming the key instead of a 500 when a row
cannot be decrypted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gmackie changed title from WIP: FORGE-503 Refuse or detach, not 500, when deleting a database credential secret to FORGE-503 Refuse or detach, not 500, when deleting a database credential secret 2026-12-05 06:50:55 +00:00
Sign in to join this conversation.
No description provided.