Repository navigation
Encryption
form.md can encrypt its data directory at rest with a 256-bit key that the operator holds outside the data directory. With a key, the database, its write-ahead log, the uploaded files, import uploads, secret.key and the ACME certificate cache are stored encrypted, so a copy of the disk or of the data directory alone reveals no page text and no file contents. Encryption is off until you give the server a key. There is no recovery without the key.
| Path | What lives there |
|---|---|
internal/crypt/key.go |
Key parsing, KeyPath (where the key is looked for), Load (checks the key against encryption.json), subkey derivation |
internal/crypt/stream.go |
Chunked AES-256-GCM for files and small blobs (Writer, Open, Seal, Unseal, WriteFile, ReadFile) |
internal/cryptvfs/vfs.go |
The SQLite shim VFS that encrypts database, WAL and journal pages with XTS-AES-256 |
internal/store/store.go |
Registers the VFS as formmd-crypt and opens the database through it when a key is set |
internal/ops/encrypt.go |
form-md encrypt |
DATADIR/encryption.json |
Written when a directory becomes encrypted: format, algorithms and a key check value (never the key) |
KeyPath looks in this order:
-
FORMMD_KEY_FILE, when set. - Otherwise a systemd credential named
form-md.key, that is$CREDENTIALS_DIRECTORY/form-md.key, when that file exists.
The key file holds 32 bytes written as 64 hex characters (what form-md keygen prints) or as base64.
form-md keygenprints a new key and touches nothing else. Store a copy of the key somewhere other than your backups: a backup of an encrypted instance is useless without it.
Create the key, readable by root only:
sudo sh -c 'umask 077; /usr/local/bin/form-md keygen > /etc/form-md/form-md.key'Give it to the service as a credential with a drop-in, so the service user never needs read access to the file:
sudo mkdir -p /etc/systemd/system/form-md.service.d
sudo tee /etc/systemd/system/form-md.service.d/key.conf >/dev/null <<'EOF'
[Service]
LoadCredential=form-md.key:/etc/form-md/form-md.key
EOF
sudo systemctl daemon-reloadThe installer already started the service once, so the data directory holds a database. Encrypt it with the server stopped. Run as root, the command hands every file in the data directory back to the directory's owner (form-md), so nothing else is needed before starting the service:
sudo systemctl stop form-md
sudo env FORMMD_DATA=/var/lib/form-md FORMMD_KEY_FILE=/etc/form-md/form-md.key /usr/local/bin/form-md encrypt
sudo systemctl start form-md
journalctl -u form-md -n 20Look for the line at-rest encryption on database=xts-aes-256 files=aes-256-gcm. Then check with form-md doctor (see Operations), which should report ok encryption on, key ... matches.
Instead of the credential you can put FORMMD_KEY_FILE=/etc/form-md/form-md.key in /etc/form-md/env; the file must then be readable by the form-md user.
A server started with a key on an empty data directory writes encryption.json and encrypts from the first write; no encrypt step is needed.
form-md keygen > ~/form-md.key && chmod 600 ~/form-md.key
FORMMD_KEY_FILE=~/form-md.key FORMMD_DATA=./data form-mdIt needs the key (FORMMD_KEY_FILE or the credential) and refuses when there is no database yet or when a running server holds the data directory lock. On a plain directory it:
- checkpoints the WAL and writes an encrypted copy of the database with
VACUUM INTOtoform-md.db.encrypting, - runs
PRAGMA integrity_checkon the copy and stops if it fails, - seals every file under
files/,tmp/andacme/, andsecret.key(files that are already sealed are skipped), and deletes the unfinished temporary files an interrupted upload or import leaves behind (names starting with a dot, such as.upload-*and.import-*), since nothing reads them again, - makes the copy readable by its owner only (mode 0600), renames it over
form-md.db, removes the old-waland-shm, and writesencryption.json, - when run as root, hands every file in the data directory back to the directory's owner.
It prints encrypted the database and N files (M already encrypted, K unfinished temporary files removed). If it stops partway, run it again.
On a directory that is already encrypted it checks the key against encryption.json (a wrong key stops it), takes the lock, seals any file under those directories, and secret.key, that is still plain, skipping the sealed ones, and removes leftover temporary files the same way. This is what to run when form-md doctor reports N files are not encrypted:
sudo systemctl stop form-md
sudo env FORMMD_DATA=/var/lib/form-md FORMMD_KEY_FILE=/etc/form-md/form-md.key /usr/local/bin/form-md encrypt
sudo systemctl start form-md| Data | How |
|---|---|
form-md.db |
Every page, in 4096-byte units, with XTS-AES-256 tweaked by its file offset, through the formmd-crypt VFS |
form-md.db-wal |
The page inside every WAL frame, with its own XTS key |
| Rollback journal | Page records, with their own XTS key |
| SQLite temporary files | AES-CTR with a random key per open file; temporary storage is kept in memory |
files/ (uploads) |
Chunked AES-256-GCM: a FMDENC1 header and 16-byte salt, then 64 KiB chunks, each with its own nonce and tag; the last chunk is marked, so a cut-off file does not read as complete. The file key is derived from the salt, so every file has its own, and ranged reads decrypt only the chunks they touch |
tmp/ (import uploads) |
The same chunked format |
secret.key |
The same chunked format |
acme/ (certificate cache) |
The same chunked format |
| Backup archives | The database inside is the encrypted snapshot and the files are copied as stored, so a backup needs the same key |
All subkeys (database, WAL, journal, files, key check) are derived from the one master key with HKDF-SHA256.
- File sizes, the number of files, timestamps, and the names of files in
files/(an id plus the original extension). - The WAL file header and each WAL frame header (page number, database size, salts and the frame checksums).
- The
-shmfile (SQLite's WAL index). -
encryption.json,form-md.lockand the backup manifestform-md-backup.json(version, time, counts, size and whether the backup is encrypted). - Tampering with database pages is not detected as such. XTS keeps page contents secret but carries no authentication tag, so a page changed on disk decrypts to noise, which SQLite then reports as a damaged database (
PRAGMA integrity_check,form-md doctor). Files, uploads andsecret.keyuse AES-GCM and refuse to open when changed. - Copies made before encryption was turned on: earlier backup archives, a directory kept aside by
restore, or snapshots of the disk.form-md encryptconverts only the data directory; delete or re-create older plain backups yourself. - Anything outside the data directory: data in memory, traffic on the network (use TLS-and-ACME), and pages kept in browsers for offline use (see Offline).
serve, backup, doctor and admin reset-owner check the key against the data directory first; serve exits with status 2 and logs encryption, the other commands print the message and exit with status 1 (doctor reports it as a failed check). restore checks the key only when the backup is encrypted, against the backup's own encryption.json, and says this backup is encrypted: set FORMMD_KEY_FILE to its key when none is given.
| Situation | Message |
|---|---|
| Directory is encrypted, no key given | this data directory is encrypted: set FORMMD_KEY_FILE to the key file |
Key does not match encryption.json
|
the key does not match this data directory |
| Key file is not 32 bytes of hex or base64 | the key file must hold 32 bytes as 64 hex characters or base64 (form-md keygen makes one) |
| Key given, directory holds a plain database | a key is set but this data directory is not encrypted: stop the server and run form-md encrypt first |
| A sealed file is damaged or was sealed with another key | crypt: the encrypted data is damaged or the key is wrong |
A plain file sits where an encrypted one is expected (copied in by hand, or left from before encrypt) |
crypt: this file is not encrypted although the instance is; run form-md doctor (the file is not served; form-md encrypt seals it) |
| The encrypted database does not use 4096-byte pages | an encrypted database must use 4096-byte pages (found N) |
Losing the key loses the data. There is no recovery path, no command to change the key, and no command to turn encryption off.
- Anything written to the data directory that holds user content goes through the store's key:
Key.Writer,Key.WriteFileorKey.Sealto write,Key.Open,Key.ReadFileorKey.Unsealto read. These pass data through unchanged when encryption is off, so the same code serves both modes. With a key set, readers refuse a plain file (crypt.ErrNotSealed) rather than serve something that bypassed encryption; the only place that reads plain files under a key isform-md encrypt, which seals them. - The database file is created with mode 0600 and the store chmods it to 0600 on every start.
- The database is opened only through
store.Openorops.openDB, which select theformmd-cryptVFS when a key is set. A second connection opened some other way reads ciphertext. - A new directory of user content in the data directory is added to
carriedDirsininternal/ops/backup.go;form-md backupcopies those directories andform-md encryptseals them. - The key never goes into the data directory, the database or a log line.
form-md doctorwarns when the key file sits inside the data directory.
- Restoring a plain backup into an instance that has a key set gives a plain directory, and the server then refuses to start with the "not encrypted" message. Run
form-md encryptafter such a restore.
form.md
Running an instance
Using it
- Data-views
- Offline
- API-and-agents
- Form-family
- Context-and-knowledge
- Terminal-and-Claude-Code
- Extensions
Changing the code