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
@@ -1,6 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -18,7 +20,21 @@ import (
|
||||
//
|
||||
// Any failure answers with a conflict instead, so the client falls back to
|
||||
// keeping its copy aside rather than losing an edit.
|
||||
func (s *Server) mergePush(w http.ResponseWriter, u *store.User, id string, basedOn int64, incoming []byte) {
|
||||
func (s *Server) mergePush(w http.ResponseWriter, r *http.Request, u *store.User, id string, basedOn int64, incoming []byte) {
|
||||
// A merge runs the vault's key derivation three times and holds two
|
||||
// decrypted copies; only a few run at once, and a push that cannot get
|
||||
// a turn soon is told to retry rather than piling up.
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
select {
|
||||
case s.merges <- struct{}{}:
|
||||
defer func() { <-s.merges }()
|
||||
case <-ctx.Done():
|
||||
w.Header().Set("Retry-After", "5")
|
||||
fail(w, http.StatusServiceUnavailable, "busy", "the server is busy merging; try again shortly")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
wrapped string
|
||||
current int64
|
||||
@@ -36,7 +52,7 @@ func (s *Server) mergePush(w http.ResponseWriter, u *store.User, id string, base
|
||||
w.Header().Set("ETag", etag(current))
|
||||
w.Header().Set("X-KeepOrg-Strategy", string(store.StrategyMerge))
|
||||
writeJSON(w, http.StatusConflict, map[string]any{
|
||||
"code": "merge_failed", "error": err.Error(),
|
||||
"code": "merge_failed", "error": "the server could not merge this save; the server log says why",
|
||||
"current_version": current, "merge_strategy": store.StrategyMerge,
|
||||
})
|
||||
return
|
||||
@@ -48,7 +64,8 @@ func (s *Server) mergePush(w http.ResponseWriter, u *store.User, id string, base
|
||||
watchers []string
|
||||
stale bool
|
||||
)
|
||||
if err := s.st.Update(func(st *store.State) error {
|
||||
if err := s.st.UpdateWith(func(st *store.State) error {
|
||||
stale = false
|
||||
d := st.Databases[id]
|
||||
if d == nil {
|
||||
return errors.New("no such database")
|
||||
@@ -59,15 +76,18 @@ func (s *Server) mergePush(w http.ResponseWriter, u *store.User, id string, base
|
||||
return nil
|
||||
}
|
||||
next = d.Version + 1
|
||||
if err := s.blobs.Put(id, next, merged); err != nil {
|
||||
return err
|
||||
}
|
||||
d.Version, d.SHA256, d.Size = next, sum, int64(len(merged))
|
||||
d.UpdatedAt, d.UpdatedBy = time.Now(), u.Username
|
||||
watchers = st.Watchers(id)
|
||||
return nil
|
||||
}, func(ctx context.Context, tx *sql.Tx) error {
|
||||
if stale {
|
||||
return nil
|
||||
}
|
||||
return s.blobs.PutTx(ctx, tx, id, next, merged)
|
||||
}); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
s.log.Printf("merge %s: %v", id, err)
|
||||
fail(w, http.StatusInternalServerError, "internal", "the merge could not be stored; try again")
|
||||
return
|
||||
}
|
||||
if stale {
|
||||
|
||||
Reference in New Issue
Block a user