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
+47 -20
View File
@@ -62,7 +62,7 @@ func (s *Server) webPage(fn webHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sess := s.webUser(r)
if sess == nil {
s.renderSignIn(w, "")
s.renderSignIn(w, r, "")
return
}
fn(w, r, sess)
@@ -75,7 +75,7 @@ func (s *Server) webForm(fn webHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sess := s.webUser(r)
if sess == nil {
s.renderSignIn(w, "")
s.renderSignIn(w, r, "")
return
}
if err := r.ParseForm(); err != nil || !s.checkCSRF(r, sess) {
@@ -108,7 +108,28 @@ func (s *Server) handleWebHome(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/web/vaults", http.StatusFound)
return
}
s.renderSignIn(w, "")
s.renderSignIn(w, r, "")
}
// preCookie carries a token that the sign-in form must echo. Without it a
// page elsewhere could post the form with its own credentials and sign a
// visitor in to the attacker's account, where whatever they then create
// lands. Strict, so a cross-site post does not carry it.
const preCookie = "keeporg_pre"
// preSessionToken returns this browser's sign-in form token, issuing one if
// it has none.
func (s *Server) preSessionToken(w http.ResponseWriter, r *http.Request) string {
if c, err := r.Cookie(preCookie); err == nil && len(c.Value) == 64 {
return c.Value
}
tok := secret.Token()
http.SetCookie(w, &http.Cookie{
Name: preCookie, Value: tok, Path: "/web", HttpOnly: true,
Secure: strings.HasPrefix(s.cfg.ExternalURL, "https://"),
SameSite: http.SameSiteStrictMode,
})
return tok
}
func (s *Server) handleWebSignIn(w http.ResponseWriter, r *http.Request) {
@@ -116,25 +137,28 @@ func (s *Server) handleWebSignIn(w http.ResponseWriter, r *http.Request) {
fail(w, http.StatusBadRequest, "bad_request", err.Error())
return
}
if c, err := r.Cookie(preCookie); err != nil || !secret.Equal(c.Value, r.FormValue("pre")) {
s.renderSignIn(w, r, "The sign-in form had expired. Sign in again.")
return
}
var token string
err := s.st.Update(func(st *store.State) error {
u, err := checkPassword(st, r.FormValue("username"), r.FormValue("password"),
r.FormValue("totp"))
if err != nil {
return err
}
token = s.newBrowserSession(st, u.ID, r).Token
return nil
})
err := s.signIn(r, r.FormValue("username"), r.FormValue("password"), r.FormValue("totp"),
func(st *store.State, u *store.User) error {
token = s.newBrowserSession(st, u.ID, r).Token
return nil
})
var throttled *errThrottled
switch {
case errors.As(err, &throttled):
s.renderSignIn(w, r, throttled.Error())
case errors.Is(err, errTOTPRequired):
s.renderSignIn(w, "This account needs its one-time code.")
s.renderSignIn(w, r, "This account needs its one-time code.")
case errors.Is(err, errTOTPInvalid):
s.renderSignIn(w, "That one-time code was rejected.")
s.renderSignIn(w, r, "That one-time code was rejected.")
case errors.Is(err, errBadCredentials):
s.renderSignIn(w, "Unknown user or wrong password.")
s.renderSignIn(w, r, "Unknown user or wrong password.")
case err != nil:
s.renderSignIn(w, err.Error())
s.renderSignIn(w, r, err.Error())
default:
s.setWebCookie(w, token, s.cfg.SessionTTL)
http.Redirect(w, r, "/web/vaults", http.StatusFound)
@@ -246,15 +270,18 @@ func (s *Server) handleWebDeleteVaultPage(w http.ResponseWriter, r *http.Request
func (s *Server) handleWebDeleteVault(w http.ResponseWriter, r *http.Request, sess *webSession) {
id := r.PathValue("id")
name, err := s.deleteDatabase(sess.user, id, r.FormValue("password"),
name, err := s.deleteDatabase(r, sess.user, id, r.FormValue("password"),
r.FormValue("totp"), r.FormValue("confirm_name"))
var throttled *errThrottled
switch {
case errors.As(err, &throttled):
s.renderDeleteVault(w, sess, id, throttled.Error())
case errors.Is(err, errReauth):
s.renderDeleteVault(w, sess, id, "Enter your account password.")
case errors.Is(err, errTOTPRequired):
s.renderDeleteVault(w, sess, id, "Enter your one-time code as well.")
case errors.Is(err, errTOTPInvalid):
s.renderDeleteVault(w, sess, id, "That one-time code was rejected.")
case errors.Is(err, errBadCredentials):
s.renderDeleteVault(w, sess, id, "That is not your account password.")
s.renderDeleteVault(w, sess, id, "Your account password or one-time code is wrong.")
case errors.Is(err, errConfirmName):
s.renderDeleteVault(w, sess, id, "Type the vault's name exactly as it is shown.")
case err != nil: