Vault

Secrets kept encrypted in your database and read back in SQL by the roles you allow, shaped like Supabase Vault.

The vault keeps secrets (API keys, webhook signing secrets, passwords for other systems) encrypted in your database, and gives their values back in SQL to the roles you allow. It is shaped like Supabase Vault, so the same SQL works:

SQL
-- keep a secret: its id comes back
select vault.create_secret('sk_live_...', 'stripe_secret_key', 'Stripe, live mode');

-- read it, as the service role or a role granted the view
select decrypted_secret from vault.decrypted_secrets where name = 'stripe_secret_key';

-- change its value, name or description (what is null stays as it was)
select vault.update_secret('<id>', 'sk_live_new...');
select vault.update_secret('<id>', null, 'stripe_key', 'renamed');

Both database modes, Postgres 16 and the Chronos engine, store and read secrets the same way, with no extension.

In SQL

ObjectWhat it is
vault.secretsid uuid, name text (unique when set), description text, secret text (the ciphertext, base64), key_id uuid, nonce bytea, created_at, updated_at
vault.decrypted_secretsthe same columns and decrypted_secret text, the value
vault.create_secret(new_secret text, new_name text = null, new_description text = '', new_key_id uuid = null)returns the new secret's uuid
vault.update_secret(secret_id uuid, new_secret text = null, new_name text = null, new_description text = null, new_key_id uuid = null)changes what is not null; a new value is sealed with a new nonce

To delete a secret, delete its row: delete from vault.secrets where id = '<id>'.

Who can read values

By default only the database's own user and service_role (the service key) use the vault: service_role may read vault.decrypted_secrets and vault.secrets, delete secrets, and call create_secret and update_secret. anon and authenticated can't reach the vault schema at all, so nothing in it is served by /rest/v1, and no role but the database's own user ever reads vault.keys.

Grant the view to another role as in Supabase:

SQL
grant usage on schema vault to reporting;
grant select on vault.decrypted_secrets to reporting;

To use a secret from a request made with a user's token, read it in a security definer function owned by a role that may read the view, and give that function an empty search path:

SQL
create function public.charge(amount integer) returns text
language plpgsql security definer set search_path = '' as $$
declare key text;
begin
  select decrypted_secret into key from vault.decrypted_secrets where name = 'stripe_secret_key';
  -- ... use key; never return it
  return 'ok';
end $$;

Over HTTP and in the dashboard

/platform/v1/vault/secrets (the service key, or a team member):

MethodPathWho
GET/vault/secretseveryone on the teamid, name, description, key id, created_at, updated_at (Unix ms). Never a value.
POST/vault/secrets {secret, name, description}admins201 and the secret without its value; a name already used is 409
GET/vault/secrets/{id}everyone on the teamone, without its value
PATCH/vault/secrets/{id} {secret?, name?, description?}adminswhat is sent changes
DELETE/vault/secrets/{id}admins204
POST/vault/secrets/{id}/revealadmins{id, name, decrypted_secret}
GET/vault/keysadminsthe keys and the rotation's progress (see Rotating the key)
POST/vault/keys/rotateadmins202: begins a rotation, or carries on with one that stopped; 409 while one runs

A value is at most 64 KB over HTTP; a name at most 256 bytes, a description 1 KB. Every change and every reveal goes in the audit log with who made it (a reveal by a viewer or operator is refused, and logged too). Values are never written to Forkbase's log or put in an error message.

The dashboard's Database, Vault page lists the secrets and lets admins add, edit, reveal and delete them. Admins also see the keys there, and Rotate key starts a rotation and shows its progress.

Rotating the key

Each key has an id and a status, and each secret records the id of the key that sealed it (vault.secrets.key_id):

StatusMeaning
activeNew values are sealed with it. There is one.
retiringA rotation is moving its secrets to the active key. They still decrypt with it meanwhile.
retiredNo secret uses it any more, and its material (the subkeys' pads) has been overwritten with nothing.

A rotation makes a new key and makes it active, in one transaction, and the old one becomes retiring. A worker in Forkbase then seals every secret on a retiring key again with the active key, 100 at a time, each in a statement of its own with a fresh nonce; a secret's value, name and updated_at don't change. When no secret is left on the old key, it is retired. While this runs:

  • Reads keep working. vault.decrypted_secrets opens each secret with its own key_id, old or new.
  • Writes go to the new key. vault.create_secret, and vault.update_secret with a new value, seal with the active key, so a secret written during the rotation is already moved.
  • A stop is not a problem. All the progress is in the rows: a secret on the new key is skipped. If Forkbase stops or crashes halfway, it carries on at its next start. POST /vault/keys/rotate carries on with a stopped rotation too.

Start one from the dashboard, over HTTP, or from the command line:

shell
forkbase vault rotate          # waits until the old key is retired; --no-wait returns at once
forkbase vault keys            # each key's status and secrets, and the rotation's progress
curl -X POST "$FORKBASE_URL/platform/v1/vault/keys/rotate" -H "apikey: $SERVICE_KEY" -H "Authorization: Bearer $SERVICE_KEY"

GET /platform/v1/vault/keys answers:

JSON
{
  "keys": [
    { "id": "…", "status": "retiring", "secrets": 120, "created_at": 1765000000000, "retired_at": null },
    { "id": "…", "status": "active", "secrets": 130, "created_at": 1765090000000, "retired_at": null }
  ],
  "rotation": { "state": "running", "remaining": 120, "resealed": 130, "unreadable": [], "error": null,
                "started_at": 1765090000000, "finished_at": null }
}

state is running (this server's worker is moving secrets), stopped (begun and not finished: it was stopped, it runs on another server, or a secret can't be opened) or idle. A secret whose row was changed outside the vault's functions can't be opened by its key, so it can't be moved: its id is in unreadable, error says so, and its key stays retiring. Delete that secret or give it a new value (vault.update_secret), then rotate again to finish.

Only admins and the service key see the keys or rotate them; the request is in the audit log. Nothing in the progress is a value.

Like Supabase Vault. Supabase's pgsodium keys have ids and a status (default, valid, invalid, expired), and each secret records its key_id; you rotate by making a key and moving secrets to it with vault.update_secret(id, new_key_id => ...). Here active plays default, retiring a key still valid, and retired an expired key, and Forkbase does the moving for you. update_secret's new_key_id still works, but only with the active key.

What retiring removes. A retired key's row keeps its id and times, and its pads are overwritten. A backup or dump made before then, Postgres's old row versions until they are vacuumed, and the Chronos engine's history of the database still hold the old key along with the secrets it sealed then. Rotating protects what is written from now on; it doesn't change what a copy taken earlier can read. A dev branch has its own copy of the vault's keys, and rotating on main doesn't change it.

How it is encrypted

Postgres has no built-in encryption function without an extension, and Forkbase uses none, so the cipher is written in plain SQL from what both Postgres 16 and the Chronos engine have (sha256, get_byte, #):

  • The key. When Forkbase first starts on a database it makes a 32-byte key from the operating system's random source, derives two subkeys from it with HMAC-SHA256 (one to encrypt, one to authenticate), and stores only those subkeys' HMAC pads in vault.keys. The key itself is not kept anywhere.
  • Encryption. Each value is XORed with a key stream of HMAC-SHA256(encryption key, nonce ‖ block number) blocks: SHA-256 used as a pseudorandom function in counter mode. Each secret gets a fresh 16-byte nonce on every write.
  • Authentication. HMAC-SHA256(authentication key, id ‖ nonce ‖ ciphertext) is stored after the ciphertext (encrypt-then-MAC). A row whose ciphertext, nonce or id was changed, or moved from another row, does not open: decrypted_secret is null and the dashboard and API say it can't be decrypted.

This is a sound construction, but it is SQL, not a constant-time library: decrypting a 64 KB value takes about a second on the Chronos engine and a tenth of that on Postgres. Keep values to keys and passwords.

What it protects against

  • Roles that may not read it. anon, authenticated, agents' roles and any role you have not granted the view read nothing, through the API or SQL: not the values, not the ciphertext, not the key.
  • A copy of vault.secrets alone. An export of that table, a replica or logical copy of it, a screenshot of the table editor, a select * pasted in a ticket, or the table's data on a disk or backup without vault.keys holds only ciphertext.
  • Values in places they shouldn't be. The API never lists values, never logs them, and a reveal is an audited, admin-only action.
  • Changed rows. A ciphertext altered or swapped between rows fails its seal instead of decrypting to something else.

What it does not protect against

  • The database's own user and superusers. The key is in the same database (it has to be, for SQL to decrypt), in a table only the database's own user reads. Whoever is that user, or a superuser, can read every value. This is the same as anyone with that access calling vault.decrypted_secrets.
  • A full dump or backup. pg_dump of the whole database, a Forkbase backup, or a copy of the Chronos data folder contains vault.keys too, so it can decrypt the secrets. Keep backups as private as the database. (Supabase keeps its root key outside the database; without an extension there is no place for SQL to reach a key outside it.)
  • SQL text in logs. select vault.create_secret('the value') puts the value in the statement. With log_statement = 'all' it is in Postgres's log. The API and dashboard send values as bind parameters, not in the statement text.
  • Roles you grant. A role granted vault.decrypted_secrets reads every value in it; there is no per-secret grant. Read single secrets in a definer function, as above, when a role should get only one.

Notes

  • Several Forkbase servers on one Postgres share the database's vault key. If two start on a new database at the same moment, one key is made (the other server uses it). A database where two were made before 0.9.0 keeps the oldest active and moves the other's secrets to it at the next start. A rotation runs on the server it was started on; the others see it as stopped until it ends.
  • Agents' worlds in chronos mode are copies of the database: the vault is in them too, readable by the same roles (agents' roles are not granted it).
  • Coming from Supabase. A database restored from Supabase may already have a vault schema from the supabase_vault extension; Forkbase's migration then stops at start with an error saying vault.keys or vault.secrets exists. Read your secrets out first (select name, decrypted_secret from vault.decrypted_secrets on Supabase), drop the schema, start Forkbase, and create them again.

Something wrong or missing? Edit this page on GitHub or open an issue.

Vault · Forkbase