Files
ReactorcoremeltdownandClaude Opus 5.5 4ffc434b1b 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>
2026-11-02 15:38:32 +01:00

139 lines
5.1 KiB
Go

package server
import (
"net/http"
"net/url"
"strings"
"testing"
"time"
"cc.tiredsysadmin.keeporg/internal/store"
)
const newAdminPassword = "a-much-better-password"
func TestChangePasswordRequiresTheCurrentOne(t *testing.T) {
h := newHarness(t)
tok, _, _ := h.login("admin", adminPassword, "")
status, body := h.call("POST", "/api/v1/auth/password", tok, map[string]string{
"current_password": "not-the-password", "new_password": newAdminPassword})
if status != http.StatusUnauthorized || body["code"] != "bad_credentials" {
t.Fatalf("wrong current password: %d %v", status, body)
}
// And the password really did not change.
if _, s, _ := h.login("admin", adminPassword, ""); s != http.StatusOK {
t.Fatal("the old password stopped working after a failed change")
}
}
func TestChangePasswordRejectsAWeakOrUnchangedPassword(t *testing.T) {
h := newHarness(t)
tok, _, _ := h.login("admin", adminPassword, "")
for _, next := range []string{"short", adminPassword} {
status, body := h.call("POST", "/api/v1/auth/password", tok, map[string]string{
"current_password": adminPassword, "new_password": next})
if status != http.StatusBadRequest {
t.Fatalf("new password %q: %d %v", next, status, body)
}
}
}
func TestChangePasswordSwapsThePasswordAndRevokesOtherSessions(t *testing.T) {
h := newHarness(t)
keep, _, _ := h.login("admin", adminPassword, "")
elsewhere, _, _ := h.login("admin", adminPassword, "")
status, body := h.call("POST", "/api/v1/auth/password", keep, map[string]string{
"current_password": adminPassword, "new_password": newAdminPassword})
if status != http.StatusOK {
t.Fatalf("change: %d %v", status, body)
}
if _, s, _ := h.login("admin", adminPassword, ""); s != http.StatusUnauthorized {
t.Fatal("the old password still works")
}
if _, s, _ := h.login("admin", newAdminPassword, ""); s != http.StatusOK {
t.Fatal("the new password does not work")
}
// The session that made the change survives; the other one does not.
if s, _ := h.call("GET", "/api/v1/auth/whoami", keep, nil); s != http.StatusOK {
t.Fatalf("the calling session was revoked: %d", s)
}
if s, _ := h.call("GET", "/api/v1/auth/whoami", elsewhere, nil); s != http.StatusUnauthorized {
t.Fatalf("another session survived the password change: %d", s)
}
}
func TestChangePasswordRequiresTheSecondFactorWhenEnrolled(t *testing.T) {
h := newHarness(t)
tok, _, _ := h.login("admin", adminPassword, "")
sec := h.enrollTOTP(t, tok, adminPassword)
status, body := h.call("POST", "/api/v1/auth/password", tok, map[string]string{
"current_password": adminPassword, "new_password": newAdminPassword})
if status != http.StatusUnauthorized || body["code"] != "totp_required" {
t.Fatalf("change without a code: %d %v", status, body)
}
code := codeAt(t, sec, 0)
if s, b := h.call("POST", "/api/v1/auth/password", tok, map[string]string{
"current_password": adminPassword, "new_password": newAdminPassword,
"totp": code}); s != http.StatusOK {
t.Fatalf("change with a code: %d %v", s, b)
}
}
func TestWebChangesThePassword(t *testing.T) {
h := newHarness(t)
b := newBrowser(t, h)
csrf := b.signIn()
// The form is on the account page, and it is guarded like the others.
_, page := b.get("/web/account")
if !strings.Contains(page, "Change password") {
t.Fatalf("no password form on the account page:\n%s", page)
}
if status, _ := b.post("/web/account", url.Values{"action": {"password"},
"current_password": {adminPassword}, "new_password": {newAdminPassword},
"new_password2": {newAdminPassword}}); status != http.StatusForbidden {
t.Fatalf("a form without a CSRF token returned %d", status)
}
_, page = b.post("/web/account", url.Values{"csrf": {csrf}, "action": {"password"},
"current_password": {adminPassword}, "new_password": {newAdminPassword},
"new_password2": {"something-else"}})
if !strings.Contains(page, "do not match") {
t.Fatalf("mismatch not reported:\n%s", page)
}
_, page = b.post("/web/account", url.Values{"csrf": {csrf}, "action": {"password"},
"current_password": {adminPassword}, "new_password": {newAdminPassword},
"new_password2": {newAdminPassword}})
if !strings.Contains(page, "Password changed") {
t.Fatalf("no confirmation:\n%s", page)
}
if _, s, _ := h.login("admin", newAdminPassword, ""); s != http.StatusOK {
t.Fatal("the new password does not work")
}
}
// An account that exists only through single sign-on has no password to
// change, and must not be able to acquire one this way.
func TestChangePasswordRefusesAnSSOOnlyAccount(t *testing.T) {
h := newHarness(t)
if err := h.srv.Store().Update(func(st *store.State) error {
st.Users["sso"] = &store.User{ID: "sso", Username: "ssouser",
OIDCIssuer: "https://id.example", OIDCSubject: "s"}
st.Sessions["sso-token"] = &store.Session{Token: "sso-token", UserID: "sso",
ExpiresAt: time.Now().Add(time.Hour)}
return nil
}); err != nil {
t.Fatal(err)
}
status, body := h.call("POST", "/api/v1/auth/password", "sso-token", map[string]string{
"current_password": "", "new_password": newAdminPassword})
if status != http.StatusUnauthorized {
t.Fatalf("an account with no password set one: %d %v", status, body)
}
}