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
+34
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
@@ -43,6 +44,10 @@ type Config struct {
LogFile string `json:"log_file"`
// Notifications turns the tray's desktop notifications on or off.
Notifications bool `json:"notifications"`
// AllowInsecureHTTP permits an http:// server_url that is not this
// machine. Without TLS the password, the session token and every vault
// cross the network readable and alterable by anyone on the path.
AllowInsecureHTTP bool `json:"allow_insecure_http"`
}
// ErrNoConfig means the client has not been logged in yet.
@@ -134,6 +139,10 @@ func (c *Config) Validate() error {
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return fmt.Errorf("server_url %q must be an http:// or https:// URL", c.ServerURL)
}
if u.Scheme == "http" && !isLoopback(u.Hostname()) && !c.AllowInsecureHTTP {
return fmt.Errorf("server_url %q is not encrypted; use https://, "+
"or set allow_insecure_http if you really mean it", c.ServerURL)
}
}
if c.CAFile != "" {
if _, err := os.Stat(c.CAFile); err != nil {
@@ -143,6 +152,31 @@ func (c *Config) Validate() error {
return nil
}
func isLoopback(host string) bool {
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}
// CheckVerificationURL makes sure a page the server asks the client to open
// in a browser is on that server. A compromised server must not be able to
// make the desktop open whatever it likes — a file: URL, a network share
// that runs a program, another application's URL scheme.
func CheckVerificationURL(serverURL, raw string) error {
srv, err := url.Parse(serverURL)
if err != nil {
return err
}
u, err := url.Parse(raw)
if err != nil || u.Scheme != srv.Scheme || !strings.EqualFold(u.Host, srv.Host) ||
u.User != nil || u.Path != strings.TrimRight(srv.Path, "/")+"/device" {
return fmt.Errorf("the server asked to open %q, which is not its own approval page", raw)
}
return nil
}
// API returns a client for the configured server, signed in with the stored
// token when there is one.
func (c *Config) API() (*API, error) {