Merge concurrent saves on the server, with vaults created from the web

Why: the point of a sync server is that two people can work at once. Picking
a winner was all this one could do while it could not read a vault.

What, end to end:
- A vault is created at /web or with `keeporg vault -create`: the server
  builds an empty KDBX 4 file, encrypts it with the password from the form,
  and stores it as version 1. An existing database can adopt a stored key
  with `keeporg db -id <id> -store-password`, which is verified against the
  stored file before it is accepted.
- A stale push to a `merge` vault is no longer refused. The server opens the
  stored version and the pushed one in memory, folds them together, and
  stores the re-encrypted result. Clients push as usual and pull what comes
  back. Any failure answers 409 merge_failed and the client keeps its copy
  aside, so a failed merge still loses nothing.
- At rest: vault contents stay KDBX-encrypted as always. A merge vault's
  password is kept AES-256-GCM-wrapped under a master key that lives outside
  the data directory, bound to the vault id, never returned by the API. A
  stolen data directory is ciphertext; a stolen data directory plus the
  master key is not. The server runs without a master key and simply refuses
  to create or merge vaults.

This moves the server inside the trust boundary for vaults it can merge,
which is inherent to merging and is stated plainly in the README, the
handover notes and the web form itself. Key-less strategies are unchanged and
remain the default for a database created without a password.

Proved: two clients editing the same version are merged rather than refused,
the newer edit of one entry wins, a merge failure leaves the stored vault
untouched, `merge` is refused without a stored key, the wrapped key never
appears in state.json or the API, and the web interface rejects a form
without its CSRF token, withholds its cookie over plain http, and only offers
groups the user administers. Checked by hand against the real thing: a vault
created in a browser, edited by keepassxc-cli on two sides from the same
version, merged by the server, and both entries read back out with
keepassxc-cli. make check, make test-race, android lint and both .deb builds
are clean.
This commit is contained in:
2026-10-29 17:34:47 +01:00
parent 4d2f0b047b
commit a9308a3e89
33 changed files with 2168 additions and 88 deletions
+45 -7
View File
@@ -15,6 +15,7 @@ import (
"time"
"cc.tiredsysadmin.keeporg/internal/blob"
"cc.tiredsysadmin.keeporg/internal/kdbx"
"cc.tiredsysadmin.keeporg/internal/secret"
"cc.tiredsysadmin.keeporg/internal/store"
)
@@ -29,6 +30,14 @@ type Config struct {
TLSCert string `json:"tls_cert,omitempty"`
TLSKey string `json:"tls_key,omitempty"`
OIDC OIDCConfig `json:"oidc"`
// MasterKeyFile holds the key that wraps the vault keys the server keeps
// in order to merge. It belongs outside DataDir: a copy of the data
// directory alone must not be enough to open a vault.
MasterKeyFile string `json:"master_key_file"`
// VaultKDF is the work factor of vaults this server creates. The server
// pays it three times per merge, and so does every client on every open.
VaultKDF kdbx.KDFParams `json:"vault_kdf"`
}
// Defaults fills in every value a deployment may leave unset.
@@ -49,16 +58,23 @@ func (c *Config) Defaults() {
if c.MaxDBSize == 0 {
c.MaxDBSize = 32 << 20
}
if c.MasterKeyFile == "" {
c.MasterKeyFile = "/etc/keeporg/master.key"
}
if c.VaultKDF.MemoryKiB == 0 {
c.VaultKDF = kdbx.DefaultKDF
}
}
type Server struct {
cfg Config
st *store.Store
blobs *blob.Store
hub *Hub
oidc *oidcProvider
mux *http.ServeMux
log *log.Logger
cfg Config
st *store.Store
blobs *blob.Store
hub *Hub
oidc *oidcProvider
keyring *secret.Keyring
mux *http.ServeMux
log *log.Logger
}
// New opens the server's data directory and wires up its routes.
@@ -73,6 +89,13 @@ func New(cfg Config, lg *log.Logger) (*Server, error) {
return nil, err
}
s := &Server{cfg: cfg, st: st, blobs: blobs, hub: NewHub(), mux: http.NewServeMux(), log: lg}
// Without a master key the server still serves every strategy that does
// not need to read a vault; only merging and vault creation are refused.
if kr, kerr := secret.LoadKeyring(cfg.MasterKeyFile); kerr != nil {
lg.Printf("no master key, so this server cannot create or merge vaults: %v", kerr)
} else {
s.keyring = kr
}
if cfg.OIDC.Enabled() {
s.oidc = newOIDCProvider(cfg.OIDC, cfg.ExternalURL+"/oidc/callback")
}
@@ -120,6 +143,16 @@ func (s *Server) routes() {
m.HandleFunc("POST /api/v1/auth/device/start", s.handleDeviceStart)
m.HandleFunc("POST /api/v1/auth/device/poll", s.handleDevicePoll)
// The web interface: sign in, see your vaults, create one.
m.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/web", http.StatusFound)
})
m.HandleFunc("GET /web", s.handleWebHome)
m.HandleFunc("POST /web/signin", s.handleWebSignIn)
m.HandleFunc("POST /web/signout", s.handleWebSignOut)
m.HandleFunc("GET /web/vaults", s.handleWebVaults)
m.HandleFunc("POST /web/vaults", s.handleWebCreateVault)
// Browser-facing pages: device approval, invite registration, OIDC.
m.HandleFunc("GET /device", s.handleDevicePage)
m.HandleFunc("POST /device", s.handleDeviceApprove)
@@ -134,6 +167,10 @@ func (s *Server) routes() {
m.HandleFunc("PUT /api/v1/databases/{id}/blob", s.authed(s.handlePutBlob))
m.HandleFunc("GET /api/v1/events", s.authed(s.handleEvents))
// Vaults: a database the server created, and can merge.
m.HandleFunc("POST /api/v1/vaults", s.authed(s.handleCreateVault))
m.HandleFunc("POST /api/v1/databases/{id}/key", s.authed(s.handleSetVaultKey))
// Administration.
m.HandleFunc("POST /api/v1/databases", s.authed(s.handleCreateDatabase))
m.HandleFunc("PATCH /api/v1/databases/{id}", s.authed(s.handlePatchDatabase))
@@ -237,6 +274,7 @@ func (s *Server) newSession(st *store.State, userID, label string) *store.Sessio
Token: secret.Token(),
UserID: userID,
Label: label,
CSRF: secret.Token(),
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(s.cfg.SessionTTL),
}