#136 feat(secrets): version history and rollback for secrets
Originally created by: thomijasir
Summary
Adds per-secret version history and rollback to the server, CLI, and web UI. Closes [#33].
Until now every write bumped version but overwrote the ciphertext, so a bad rotation or a broken import could not be undone. Each write now also stores its version, and a rollback saves an earlier value as a new version. History is never rewritten, so a rollback can itself be rolled back.
secrets (one row per key, current value)
+secret_versions (one row per version, the current one included)
+ environment_id, key, version PK, FK -> secrets ON DELETE CASCADE
+ ciphertext, nonces, wrapped_key NULL once retention expires the version
+ change_type legacy | created | updated | imported | rolled_back
+ source_version, actor_*, created_at
Every write goes through one function, inside the caller's BEGIN IMMEDIATE transaction:
set / import / project init / rollback
repository::write_version
upsert secrets (ON CONFLICT DO UPDATE, never INSERT OR REPLACE)
insert secret_versions
expire ciphertext beyond secret_history_versions (default 20)
rollback(version, expectedVersion)
current changed since expectedVersion? -> 409 SECRET_VERSION_CONFLICT
target is current / missing / expired? -> 409 / 404 / 410
decrypt at target version, encrypt at current + 1, write_version, audit
The ciphertext AAD binds the version number, so a rollback re-encrypts instead of copying old ciphertext.
What users get:
dopbase secret history <ENV> <KEY> [--limit N] [--before V]
dopbase secret get <ENV> <KEY> --version N [--reveal]
dopbase secret rollback <ENV> <KEY> [VERSION] [--yes] # no VERSION: pick from a list
<SecretsPanel>
row: [reveal] [History] [edit] [delete]
<SecretHistoryDialog size="xl"> versions, reveal one (30 s), roll back with inline confirm
:::text
GET /api/v1/environments/{id}/secrets/{key}/versions?limit=&before=
POST /api/v1/environments/{id}/secrets/{key}/versions/{version}/reveal
POST /api/v1/environments/{id}/secrets/{key}/rollback { version, expectedVersion }
Listing has the same access as secret list and returns metadata only. Reveal and rollback need a project manager, plus the CSRF header and a recent password confirmation for browser sessions. Runner and AI agent tokens are refused. Both actions are audited as secret.version_revealed and secret.rolled_back, without values.
Two web UI fixes came out of review, because both blocked the new dialogs:
<DbModal>
- z-50 for every dialog (last mounted wins)
+ layer="page" | "auth" | "session" -> z-50 / z-[60] / z-[70]
+ session expired -> every non-session dialog hides and emits close
- "Session expired" and the password prompt could open behind the update or History dialog. They now always stack on top.
- Saving with an expired session left the update dialog open, with a "Could not save the secret." banner behind it. Now only "Session expired" stays on screen.
- The update dialog shows the key as a read-only field with its version badge, not a loose code chip.
Where this differs from the original [#33], which is updated to match:
- Deleting a secret, an environment, or a project deletes the history too. That was the simplest option. Restoring deleted secrets and rolling back a whole environment, the way Doppler rolls back a config, are tracked in [#135].
- Manual redaction is out. Retention is the only way a value is removed.
- History reads are not audited, the same as
secret list. - From the CLI, rollback doesn't ask for the password again. The server only demands recent authentication from browser sessions, and
importanddeletebehave the same way, so--yeskeeps working in scripts.
Compatibility
- Migration
0018_secret_versionsadds the table and copies each existing secret in as alegacyversion. No existing columns change. - Existing endpoints keep their shape. The new ones are additive.
- New setting
secret_history_versions(2 to 1000, default 20), also settable asDOPBASE_SECRET_HISTORY_VERSIONS. New instances get it uncommented in the generatedserver.toml. A lower value applies to a secret the next time it is saved. - Behavior change: deleting a secret now also deletes its stored versions. The delete confirmation says so.
- Rekeying the master key and restoring backups both carry
secret_versions. Older backups get the backfill when restore migrates them.
Merge danger. This is a one-way door for data: once deployed, versions start accumulating, and rolling the release back means leaving or dropping that table. The code itself reverts cleanly, and the migration's down file only drops secret_versions. Blast radius: secret writes. Every set, import, and init now writes two rows plus a pruning update in the same transaction, and a bug there would fail writes rather than corrupt them. Storage grows by about one value plus a few hundred bytes per retained version.
Verification
bun run testpasses: UI 253 tests, all Rust suites (CLI suite 223 passed, 1 ignored), and the docs tests.bun run lint(eslint pluscargo clippy --all-targets --all-features -D warnings) andbun run typecheckpass.- Every one of the 20 commits passes
cargo clippy --all-targets -D warningson its own, and every UI commit passestsc -bandvue-tsc -b, checked in a clean worktree. bun run format:checkreports two files this branch doesn't touch:CONTRIBUTING.mdand an untracked local.claude/settings.local.json.
New tests, before and after:
| Test | Before | After |
|---|---|---|
rollback_saves_the_earlier_value_as_a_new_version |
no endpoint | v1 to v3, roll back to 1 -> v4 holds v1's value, v1 to v3 unchanged, runtime serves v1 |
rollback_refuses_bad_targets_without_writing |
- | current 409, missing 404, stale expectedVersion 409, tampered ciphertext 500, nothing written |
retention_keeps_values_for_the_newest_versions_only |
- | 22 writes -> 20 restorable, v1 and v2 answer 410 |
rollback_and_version_reveal_need_a_confirmed_project_manager |
- | no CSRF 403, stale confirmation 403, runner and agent 403, history readable by agent |
deleting_a_secret_removes_its_history |
- | delete and replace import purge history, a re-created key starts at v1 |
secret_versions_migration_keeps_existing_values_as_legacy_versions |
- | pre-0018 secret becomes a legacy version with its ciphertext |
| backups lifecycle and cross-key restore | restore only | history restored, rollback works after rekey |
secret_rollback_offers_restorable_versions_in_a_terminal |
- | PTY picker skips the current version, confirm restores v1 |
DbModal layers |
session dialog could sit behind | z-order by layer, page dialogs close on expiry |
Manual, in the browser against a local debug build of the server:
- History dialog: listed v1 to v3, revealed v1 (
postgres://db-v1, hid after 30 s), rolled back to v2, and the table moved to v4 with "Rolled back from v2". - Session expiry: opened Update secret, revoked the browser session in SQLite, clicked Save. The update dialog closed, only "Session expired" stayed with no error banner behind it, and "Sign in again" went to login with the environment as the redirect.
Checklist
- [x] I kept this pull request focused on one problem.
- [x] I added or updated tests for behavior changes, or explained why tests are not needed.
- [x] I updated the documentation and
CHANGELOG.mdwhen public behavior changed, or explained why they are not needed. - [x] I updated relevan documentation inside
/docs
Originally posted by: thomijasir
Screenshot Secrets History and Rollback
Ticket changed by: dopbase