Shipkeep is an open-source platform to deploy apps on your own servers. Its control plane keeps everything that matters in one PostgreSQL database, plus one key that encrypts the secrets in it. An instance backup holds both. A restore brings the whole installation back as it was when the backup was made.

The restore checks the archive carefully before it touches anything. A forged archive is refused, and so are a changed one and a cut one. All of that has tests, and they pass.

Then backups gained an S3 destination, and I filed a question I could not answer yet: which archive is the restore checking? Not whether it is genuine: whether it is the one you asked for. The restore could not tell.

What the archive proved

A backup is an age file, encrypted to a public key. The identity that opens it stays off the server. So creating a backup needs nothing secret, and only whoever holds the identity can read one.

age authenticates every chunk of the file and its end: a changed or truncated archive fails to decrypt. But age says nothing about who wrote it. Anyone who knows the public key can encrypt to it, and the public key sits on the server. A forged archive would reach pg_restore, which runs its statements as the database owner.

So the first line of the plaintext is a manifest: the format, a version, the creation time, the newest database migration, and the key. It ends with a tag, an HMAC-SHA256 of those fields under a key derived from the identity with HKDF. The server keeps that derived key, but not the identity. A restore derives the same key from the identity it is given, and refuses an archive whose tag does not match, before a byte of it reaches pg_restore.

Forged, changed, truncated: all refused. It felt complete.

What it did not prove

The issue was one paragraph long. The tag proves that a server holding this installation's backup key made the archive. It does not prove that the archive is the one the operator asked for.

Backups go to an S3 bucket, or to files you keep somewhere. Anyone who can write there can copy an older, perfectly genuine archive of the same installation under the name you are about to restore:

  • the storage provider, or anyone who gets the bucket's credentials;
  • anyone on the path, when the storage is reached over plain HTTP. That is allowed for a storage on the same machine, with a setting that says so.

Every check passes, because the archive is genuine. It is just older. The restore then brings back the installation as it was then:

  • A password changed since works again.
  • An API token revoked since is valid again.
  • A member removed since is back.

That is a rollback attack, signed by your own server.

The only defense was in the guide. The restore printed the archive's creation time, and the guide asked you to check it. I went back to see where that line was printed: by the last step, the one that restores the key. By then the database had already been replaced.

Binding the name

The fix is the same idea as an audience claim in a token. The signature has to cover what the thing is for, not only who made it.

Version 2 of the manifest carries the name the archive was created under: the S3 object's name, or the file's name. The tag covers it:

const lines = [format, String(version), createdAt, migration, kek]; if (version === 2) lines.push(name); return createHmac('sha256', key).update(lines.join('\n')).digest('base64'); 

The restore is told which name it was asked for, and compares:

This archive was created as shipkeep-20261101-100000-0badc0de.age, not shipkeep-20261123-100000-3f9a1c2e.age: it is not restored. 

The version is one of the tagged fields, and that matters. Without it, someone could take the name out of a version 2 manifest, call it version 1, and keep the tag. With it, the tag no longer matches. A test does exactly that and expects a refusal.

Archives made by earlier releases have no name. They still restore, because refusing them would make every existing backup useless. The restore says that it cannot tell which archive it is, and prints the creation time.

The tests serve an archive created as A under the name B. Both restore steps refuse it, and the key file stays as it was. Turning the comparison off makes two tests fail.

The review found three more

I opened the pull request, and the automated review came back with three findings. All three were right.

Bash ate a newline

backup.sh took the archive's name from its path:

name=$(basename -- "$2") 

Command substitution strips trailing newlines. A file named A.age followed by a newline became A.age, which passes the name check. Two different files then share one authenticated name. The script now uses a parameter expansion, which keeps every byte:

name=${2##*/} 

The newline stays in the name, and the name check refuses it. My first check of this fix passed for the wrong reason: I built the test path with $(printf ...), which strips the newline too. Checked again with $'A.age\n', the old line gives A.age and the new one keeps the newline.

The check came too late

The name was checked when the database step opened the archive. By then, the script had already stopped the control plane and prepared an empty database to restore into. A refused archive left the service stopped, which is exactly what you do not want while someone is playing with your bucket.

There is now a separate step, backup.js verify NAME. It reads the whole archive: manifest, tag, name, and every chunk to the end. It runs before the confirmation prompt, before anything stops:

  1. Fetch the archive.
  2. Verify it, whole. A refusal says "nothing was changed".
  3. Ask you to type replace.
  4. Stop the control plane, restore the database into a new one, swap it, then the key.

Reading the archive twice costs one more decryption pass, made before anything has changed. A test damages the last bytes of an archive and expects verify to refuse it; without the full read, that test fails.

The docs promised too much

My new paragraph in the guide said that someone who can write where archives are kept can no longer swap one. The review pointed out two holes in that sentence.

  • Versions: archives from earlier releases have no name, so they are not covered.
  • Names: a name says which archive it is, not how old it is. If you reuse a name, an older archive under that same name still passes. Archives sent to S3 get the time and four random bytes in their name. For local files, the guide shows a name with the date in it.

The guide now says both, in those words. An honest limit is more useful than a broad claim.

What I take from it

  • Authenticity is not identity. "Who made this?" and "Is this the one I asked for?" are different questions. If the second matters, put the answer under the signature.
  • Check before you change anything. A refusal that arrives after the first destructive step still costs you something. Here, it left the control plane down.
  • Write the limits down. The first version of my docs claimed more than the code did. Nobody had to attack anything for that to be a bug.

Where it stands

  • Merged on main (#565, closing #439), in the next release. Backups made by v0.1.0-alpha.7 and earlier still restore, with a warning.
  • The design is in ADR 0026, and the operator's side in the installation guide under "Back up and restore".

Where you can help

  • You know backup formats or applied crypto: read the ADR and tell me what else the tag should cover.
  • You have a disposable Ubuntu 24.04 VM: install, back up, restore into a fresh install, and tell me where it breaks.
  • First contribution: the good first issues each say what to change and when it is done.

And a question for the comments: how do your backups prove they are the one you asked for, and not just one of yours?