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:
co-authored by
Claude Opus 5.5
parent
f8f634fbf7
commit
4ffc434b1b
+86
-16
@@ -30,6 +30,11 @@ type fileState struct {
|
||||
Version int64 `json:"version"`
|
||||
SHA256 string `json:"sha256"`
|
||||
ConflictMarker string `json:"conflict_marker,omitempty"`
|
||||
// Retired marks a database this device no longer has access to. Its
|
||||
// file stays where it is, and its name stays taken: a database created
|
||||
// later under the same name must not adopt — and upload — a file that
|
||||
// belonged to another.
|
||||
Retired bool `json:"retired,omitempty"`
|
||||
}
|
||||
|
||||
// Status is a human-readable account of one mirrored database.
|
||||
@@ -192,12 +197,12 @@ func (m *Mirror) SyncAll(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
// A database that is no longer assigned stops being tracked. Its file is
|
||||
// left alone: deleting a user's local copy of a password database is not
|
||||
// this program's decision.
|
||||
for id := range m.st {
|
||||
// A database that is no longer assigned stops being synchronized. Its
|
||||
// file is left alone — deleting a user's local copy of a password
|
||||
// database is not this program's decision — and its name stays retired.
|
||||
for id, st := range m.st {
|
||||
if !seen[id] {
|
||||
delete(m.st, id)
|
||||
st.Retired = true
|
||||
delete(m.status, id)
|
||||
}
|
||||
}
|
||||
@@ -263,7 +268,7 @@ func (m *Mirror) reconcile(ctx context.Context, db Database) error {
|
||||
return nil
|
||||
|
||||
case actPull:
|
||||
return m.pull(ctx, db, st, path)
|
||||
return m.pull(ctx, db, st, path, localSHA)
|
||||
|
||||
case actPush, actPushForMerge:
|
||||
return m.push(ctx, db, st, path, local, false)
|
||||
@@ -276,7 +281,7 @@ func (m *Mirror) reconcile(ctx context.Context, db Database) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.pull(ctx, db, st, path); err != nil {
|
||||
if err := m.pull(ctx, db, st, path, localSHA); err != nil {
|
||||
return err
|
||||
}
|
||||
why := "the server copy was newer, your save was kept as " + filepath.Base(copyPath)
|
||||
@@ -306,7 +311,15 @@ func (m *Mirror) reconcile(ctx context.Context, db Database) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mirror) pull(ctx context.Context, db Database, st *fileState, path string) error {
|
||||
// errChangedDuringSync means the local file was saved while a pass was
|
||||
// deciding about it. Nothing is overwritten; the next pass sees the save.
|
||||
var errChangedDuringSync = errors.New("the file was saved during synchronization; trying again")
|
||||
|
||||
// pull replaces the local file with the server copy. expectSHA is the digest
|
||||
// of the local file the decision was made on ("" for none): if the file is
|
||||
// different now, a KeePass application saved it in the meantime, and
|
||||
// replacing it would lose that save without a trace.
|
||||
func (m *Mirror) pull(ctx context.Context, db Database, st *fileState, path, expectSHA string) error {
|
||||
data, version, err := m.api.Pull(ctx, db.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -314,6 +327,13 @@ func (m *Mirror) pull(ctx context.Context, db Database, st *fileState, path stri
|
||||
if version == 0 || len(data) == 0 {
|
||||
return nil // nothing stored yet
|
||||
}
|
||||
now, exists, err := readFileIfExists(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (exists && digest(now) != expectSHA) || (!exists && expectSHA != "") {
|
||||
return errChangedDuringSync
|
||||
}
|
||||
if err := writeFileAtomic(path, data); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -332,7 +352,7 @@ func (m *Mirror) push(ctx context.Context, db Database, st *fileState, path stri
|
||||
// The server folded our save into someone else's. What it holds
|
||||
// now is neither copy, so take it.
|
||||
m.log.Printf("%s: server merged version %d (%s)", db.Name, res.Version, res.MergeSummary)
|
||||
if err := m.pull(ctx, db, st, path); err != nil {
|
||||
if err := m.pull(ctx, db, st, path, digest(data)); err != nil {
|
||||
return err
|
||||
}
|
||||
m.setStatus(db, "merged", res.MergeSummary)
|
||||
@@ -341,7 +361,7 @@ func (m *Mirror) push(ctx context.Context, db Database, st *fileState, path stri
|
||||
if res.SHA256 != digest(data) {
|
||||
// Whatever the server stored differs from what we sent, so the
|
||||
// local file is no longer what the vault holds.
|
||||
return m.pull(ctx, db, st, path)
|
||||
return m.pull(ctx, db, st, path, digest(data))
|
||||
}
|
||||
if !res.Unchanged {
|
||||
m.log.Printf("%s: pushed version %d", db.Name, res.Version)
|
||||
@@ -371,7 +391,7 @@ func (m *Mirror) push(ctx context.Context, db Database, st *fileState, path stri
|
||||
if cerr != nil {
|
||||
return cerr
|
||||
}
|
||||
if perr := m.pull(ctx, db, st, path); perr != nil {
|
||||
if perr := m.pull(ctx, db, st, path, digest(data)); perr != nil {
|
||||
return perr
|
||||
}
|
||||
why := "the server copy was newer, your save was kept as " + filepath.Base(copyPath)
|
||||
@@ -386,7 +406,7 @@ func (m *Mirror) push(ctx context.Context, db Database, st *fileState, path stri
|
||||
if cerr != nil {
|
||||
return cerr
|
||||
}
|
||||
if perr := m.pull(ctx, db, st, path); perr != nil {
|
||||
if perr := m.pull(ctx, db, st, path, digest(data)); perr != nil {
|
||||
return perr
|
||||
}
|
||||
why := "you have read-only access, your save was kept as " + filepath.Base(copyPath)
|
||||
@@ -406,6 +426,8 @@ func (m *Mirror) trackedState(db Database) *fileState {
|
||||
m.st[db.ID] = st
|
||||
return st
|
||||
}
|
||||
// Access came back: the file and what was last agreed are still valid.
|
||||
st.Retired = false
|
||||
if st.File == "" {
|
||||
st.File = m.fileNameFor(db)
|
||||
}
|
||||
@@ -413,6 +435,13 @@ func (m *Mirror) trackedState(db Database) *fileState {
|
||||
want := m.fileNameFor(db)
|
||||
old := filepath.Join(m.dir, st.File)
|
||||
if _, err := os.Stat(old); err == nil && want != st.File {
|
||||
// Never onto a file that is already there: os.Rename would
|
||||
// replace it, and it may be someone else's vault.
|
||||
if _, err := os.Lstat(filepath.Join(m.dir, want)); err == nil {
|
||||
m.log.Printf("%s: not renaming %s to %s, which already exists", db.Name, st.File, want)
|
||||
st.Name = db.Name
|
||||
return st
|
||||
}
|
||||
if err := os.Rename(old, filepath.Join(m.dir, want)); err != nil {
|
||||
m.log.Printf("%s: rename %s to %s: %v", db.Name, st.File, want, err)
|
||||
return st
|
||||
@@ -425,7 +454,9 @@ func (m *Mirror) trackedState(db Database) *fileState {
|
||||
}
|
||||
|
||||
// fileNameFor turns a database name into a filename, keeping it unique
|
||||
// within the mirror directory.
|
||||
// within the mirror directory. Names compare without regard to case, since
|
||||
// macOS and Windows file systems do: "work" and "Work" are one file there,
|
||||
// and two databases sharing it would upload each other.
|
||||
func (m *Mirror) fileNameFor(db Database) string {
|
||||
base := sanitize(db.Name)
|
||||
if base == "" {
|
||||
@@ -433,7 +464,7 @@ func (m *Mirror) fileNameFor(db Database) string {
|
||||
}
|
||||
name := base + ".kdbx"
|
||||
for id, st := range m.st {
|
||||
if id != db.ID && st.File == name {
|
||||
if id != db.ID && strings.EqualFold(st.File, name) {
|
||||
return fmt.Sprintf("%s-%s.kdbx", base, shortID(db.ID))
|
||||
}
|
||||
}
|
||||
@@ -521,7 +552,8 @@ func writeFileAtomic(path string, data []byte) error {
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
// sanitize keeps a database name usable as a filename on every platform.
|
||||
// sanitize keeps a database name usable as a filename on every platform,
|
||||
// and out of the names this client makes for itself.
|
||||
func sanitize(name string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range strings.TrimSpace(name) {
|
||||
@@ -533,7 +565,45 @@ func sanitize(name string) string {
|
||||
b.WriteRune('-')
|
||||
}
|
||||
}
|
||||
return strings.Trim(strings.TrimSuffix(b.String(), ".kdbx"), " .-")
|
||||
out := b.String()
|
||||
for strings.Contains(out, "..") {
|
||||
out = strings.ReplaceAll(out, "..", ".")
|
||||
}
|
||||
out = strings.Trim(strings.TrimSuffix(out, ".kdbx"), " .-")
|
||||
// A database called "Work.server-v7" or "Work.conflict-…" would collide
|
||||
// with the side copies kept for "Work".
|
||||
for _, reserved := range []string{".server-v", ".conflict-", ".keeporg"} {
|
||||
out = undotFold(out, reserved)
|
||||
}
|
||||
// Windows maps these to devices, whatever the extension.
|
||||
stem, _, _ := strings.Cut(out, ".")
|
||||
if windowsDevice[strings.ToUpper(stem)] {
|
||||
out += "-db"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
var windowsDevice = map[string]bool{
|
||||
"CON": true, "PRN": true, "AUX": true, "NUL": true,
|
||||
"COM1": true, "COM2": true, "COM3": true, "COM4": true, "COM5": true,
|
||||
"COM6": true, "COM7": true, "COM8": true, "COM9": true,
|
||||
"LPT1": true, "LPT2": true, "LPT3": true, "LPT4": true, "LPT5": true,
|
||||
"LPT6": true, "LPT7": true, "LPT8": true, "LPT9": true,
|
||||
}
|
||||
|
||||
// undotFold turns the leading dot of every case-insensitive occurrence of
|
||||
// marker into a dash, keeping the name's own spelling.
|
||||
func undotFold(s, marker string) string {
|
||||
lower, low := []byte(strings.ToLower(s)), strings.ToLower(marker)
|
||||
out := []byte(s)
|
||||
for i := 0; ; {
|
||||
j := strings.Index(string(lower[i:]), low)
|
||||
if j < 0 {
|
||||
return string(out)
|
||||
}
|
||||
out[i+j] = '-'
|
||||
i += j + len(marker)
|
||||
}
|
||||
}
|
||||
|
||||
func shortID(id string) string {
|
||||
|
||||
Reference in New Issue
Block a user