Keep server data in an encrypted database, and harden every part

Storage. The server keeps its data in SQLite (a file in the data
directory, the default) or PostgreSQL (database, KEEPORG_DATABASE_URL or
-database). Every row, the KDBX versions included, is sealed with
AES-256-GCM under a key derived from the master key and named by an HMAC.
The master key is required to start and a different one is refused. A data
directory from 0.10 or earlier is imported on first start. Several servers
may share one PostgreSQL database. Go goes to 1.26 for the pure-Go
drivers (modernc.org/sqlite, jackc/pgx).

Hardening, after an audit of the server, both clients and the deployment:
- Single sign-on approves a device only after an explicit Approve in the
  browser that started it, bound by a cookie and PKCE; identities are never
  linked by username, by verified email only when allowed, never to admins.
- Sign-in attempts are limited per address and per account, passwords are
  checked outside the store lock with uniform timing, one-time codes work
  once, and starting two-factor needs the password.
- Merge vaults bound KDF parameters, payload size and XML shape, check
  pushes before storing them, and run at most two merges at once. A version
  is written in the same transaction as its metadata.
- Tokens in URLs only on the event stream, which ends on revocation; CSP,
  frame-ancestors, no-store and HSTS; sign-in CSRF; 404 for databases
  without access; new trusted_proxies.
- Clients open only the server's approval page, keep case-variant and
  retired file names apart, never overwrite a save made during a pull,
  require https except locally, send the stream token in a header, bound
  their reads and escape notifications and terminal output.
- The .deb creates the master key, systemd is sandboxed further, nginx sets
  modern TLS and HSTS, and CI deploys from main only.

Server 0.12.0, desktop 0.6.0, Android 0.4.0 (versionCode 4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-02 15:38:32 +01:00
co-authored by Claude Opus 5.5
parent f8f634fbf7
commit 4ffc434b1b
77 changed files with 4192 additions and 776 deletions
+90
View File
@@ -0,0 +1,90 @@
package client
import (
"context"
"io"
"log"
"os"
"path/filepath"
"testing"
"time"
)
func TestSanitizeKeepsClearOfTheClientsOwnNames(t *testing.T) {
for in, want := range map[string]string{
"Team Vault": "Team Vault",
"../../etc/passwd": "etc-passwd",
"Work.server-v7": "Work-server-v7",
"Work.CONFLICT-1": "Work-CONFLICT-1",
"nul": "nul-db",
"COM3.backup": "COM3.backup-db",
".keeporg-state": "keeporg-state",
} {
if got := sanitize(in); got != want {
t.Errorf("sanitize(%q) = %q, want %q", in, got, want)
}
}
}
func TestFileNamesDifferingOnlyInCaseDoNotShareAFile(t *testing.T) {
m := &Mirror{st: map[string]*fileState{"a": {Name: "Work", File: "Work.kdbx"}}}
if got := m.fileNameFor(Database{ID: "bbbbbbbb", Name: "work"}); got == "work.kdbx" {
t.Fatalf("a case variant got the same file on a case-insensitive disk: %s", got)
}
}
// The attack: a database the user lost access to leaves its file behind; an
// administrator elsewhere creates an empty database of the same name and
// grants it to the user. The leftover must not be adopted and uploaded.
func TestARetiredFileIsNotAdoptedByANewDatabase(t *testing.T) {
s := newStack(t)
s.sync()
v, _ := s.remote()
if v == 0 {
s.write("Team Vault.kdbx", "the original vault")
s.sync()
}
ctx := context.Background()
if err := s.admin.DeleteDatabase(ctx, s.dbID, password, ""); err != nil {
t.Fatal(err)
}
s.sync()
impostor, err := s.admin.CreateDatabase(ctx, "Team Vault", "everyone", "")
if err != nil {
t.Fatal(err)
}
s.sync()
if _, v, err := s.admin.Pull(ctx, impostor.ID); err != nil || v != 0 {
t.Fatalf("the leftover file was uploaded into the new database: version %d, %v", v, err)
}
if got := s.read("Team Vault.kdbx"); got != "the original vault" {
t.Fatalf("the leftover file changed: %q", got)
}
}
func TestPullDoesNotOverwriteASaveMadeDuringSync(t *testing.T) {
dir := t.TempDir()
m := &Mirror{dir: dir, log: log.New(io.Discard, "", 0)}
path := filepath.Join(dir, "v.kdbx")
os.WriteFile(path, []byte("saved just now"), 0o600)
// The decision was made on the earlier content.
now, _, _ := readFileIfExists(path)
if digest(now) == digest([]byte("earlier")) {
t.Fatal("test setup")
}
s := newStack(t)
m.api = s.admin
if _, err := s.admin.Push(context.Background(), s.dbID, 0, []byte("server copy"), false); err != nil {
t.Fatal(err)
}
m.st, m.status = map[string]*fileState{}, map[string]*Status{}
st := &fileState{File: "v.kdbx"}
err := m.pull(context.Background(), Database{ID: s.dbID, Name: "v"}, st, path, digest([]byte("earlier")))
if err != errChangedDuringSync {
t.Fatalf("pull over a fresh save: %v", err)
}
if b, _ := os.ReadFile(path); string(b) != "saved just now" {
t.Fatalf("the save was overwritten: %q", b)
}
_ = time.Second
}