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.
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package client
|
|
|
|
// action is what the agent does to one database on one pass.
|
|
type action int
|
|
|
|
const (
|
|
actNothing action = iota
|
|
// actPull writes the server copy over the local file.
|
|
actPull
|
|
// actPush uploads the local file against the version it was based on.
|
|
actPush
|
|
// actForcePush uploads the local file and overwrites a newer server copy.
|
|
actForcePush
|
|
// actServerWins keeps the local file as a conflict copy, then pulls.
|
|
actServerWins
|
|
// actRestore is actServerWins forced on the client by read-only access.
|
|
actRestore
|
|
// actManual writes the server copy beside the local file and waits for a
|
|
// human.
|
|
actManual
|
|
// actPushForMerge sends the local file to a server that will fold the
|
|
// two copies together and hand back the result.
|
|
actPushForMerge
|
|
)
|
|
|
|
func (a action) String() string {
|
|
switch a {
|
|
case actPull:
|
|
return "pull"
|
|
case actPush:
|
|
return "push"
|
|
case actForcePush:
|
|
return "force-push"
|
|
case actServerWins:
|
|
return "server-wins"
|
|
case actRestore:
|
|
return "restore"
|
|
case actManual:
|
|
return "manual-conflict"
|
|
case actPushForMerge:
|
|
return "push-for-merge"
|
|
}
|
|
return "nothing"
|
|
}
|
|
|
|
// situation is everything the agent knows about one database at one moment.
|
|
type situation struct {
|
|
LocalExists bool
|
|
// LocalSHA is the digest of the file on disk now.
|
|
LocalSHA string
|
|
// SyncedSHA and SyncedVersion are what this client last agreed with the
|
|
// server. An empty SyncedSHA means this client has never synced the file.
|
|
SyncedSHA string
|
|
SyncedVersion int64
|
|
RemoteSHA string
|
|
RemoteVersion int64
|
|
CanWrite bool
|
|
Strategy string
|
|
// ConflictSeen is set when this exact manual conflict was already
|
|
// reported, so it is not reported again on every pass.
|
|
ConflictSeen bool
|
|
}
|
|
|
|
// decide is the whole client-side synchronization policy, kept pure so the
|
|
// table of cases can be read and tested in one place.
|
|
func decide(s situation) action {
|
|
localChanged := s.LocalExists && s.LocalSHA != s.SyncedSHA
|
|
remoteChanged := s.RemoteVersion != s.SyncedVersion || s.RemoteSHA != s.SyncedSHA
|
|
|
|
switch {
|
|
case !s.LocalExists:
|
|
if s.RemoteVersion > 0 {
|
|
return actPull
|
|
}
|
|
return actNothing
|
|
|
|
case localChanged && !remoteChanged:
|
|
if !s.CanWrite {
|
|
return actRestore
|
|
}
|
|
return actPush
|
|
|
|
case !localChanged && remoteChanged:
|
|
return actPull
|
|
|
|
case localChanged && remoteChanged:
|
|
switch {
|
|
case !s.CanWrite:
|
|
return actRestore
|
|
case s.Strategy == "merge":
|
|
// The server holds this vault's key and merges the two copies,
|
|
// so the right move is to send ours and take what comes back.
|
|
return actPushForMerge
|
|
case s.Strategy == "client-wins":
|
|
return actForcePush
|
|
case s.Strategy == "manual":
|
|
if s.ConflictSeen {
|
|
return actNothing
|
|
}
|
|
return actManual
|
|
default:
|
|
return actServerWins
|
|
}
|
|
}
|
|
return actNothing
|
|
}
|