- test(ui): avoid a literal em dash in the em dash check - fix(cli): accept any positive int64 audit count and share the last active admin rule - fix(api): limit audit before_id to 2^53-1 - fix(api): audit unmatched api requests, bound every request body and record audit write failures - fix(ui): recheck the account in the login transaction and require existing zones on zone pages - fix(api): recheck the acting admin and the stored password hash in change transactions - fix(db): keep committed outcomes on lock release failure, narrow the records contract and trace sql statements - fix(db): make the initial migration restartable and verify the application schema - fix(auth): never move last_seen_at backwards on concurrent session touches - test(api): cover lock release failure after commit and handler tracing without secrets - fix(test): redact credentials from mysql setup errors - fix(audit): retain addressed objects on admin refusals - test(db): cover migration restarts and records contract ranges in mysql - test(api): verify account races and audit rollback in mysql - fix(auth): reject password changes after account deletion - docs: document unknown api audit entries - fix(audit): bound failure retries and redact sensitive sql parameters - fix(test): isolate docker resources and cover image cli commands - test(auth): expect unauthorized after an account deletion race - fix(db): verify application column widths and foreign keys
201 lines
7.2 KiB
Go
201 lines
7.2 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func expectInternalErrorRow(t *testing.T, res webResult, operation string) auditEntry {
|
|
t.Helper()
|
|
e := expectOneAudit(t, res, operation, http.StatusInternalServerError)
|
|
if res.audit[0].inTx || e.Error.String != msgInternalError || e.Before != nil || e.After != nil {
|
|
t.Fatalf("%s: %+v", operation, res.audit[0])
|
|
}
|
|
return e
|
|
}
|
|
|
|
func TestAPIAuditFailureOnReadsAndRefusals(t *testing.T) {
|
|
h := newWebHarness(t)
|
|
admin := h.user("admin", roleAdmin)
|
|
alice := h.user("alice", roleUser)
|
|
at, ut := h.login(admin), h.login(alice)
|
|
h.store.failOn["InsertAudit"] = errors.New("audit down")
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res := h.api("GET", "/account", at, "")
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
expectInternalErrorRow(t, res, opAccountRead)
|
|
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res = h.api("PUT", "/zones/example.org./users/bob", ut, "")
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
if e := expectInternalErrorRow(t, res, opDelegationGrant); e.Zone.String != testZone {
|
|
t.Fatalf("%+v", e)
|
|
}
|
|
|
|
delete(h.store.failTimes, "InsertAudit")
|
|
res = h.api("GET", "/account", at, "")
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
if len(res.audit) != 0 || strings.Contains(res.body, "admin") {
|
|
t.Fatal("persistent failure")
|
|
}
|
|
if !strings.Contains(h.logs.String(), "audit insert of the internal error failed") {
|
|
t.Fatal("second failure not logged")
|
|
}
|
|
}
|
|
|
|
func TestAPIAuditWriteFailureIsAlwaysInternal(t *testing.T) {
|
|
h, token := usersHarness(t)
|
|
bob := h.users["bob"]
|
|
for _, cause := range []error{driver.ErrBadConn, errDatabase, errors.New("constraint")} {
|
|
h.store.failOn["InsertAudit"] = cause
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res := h.api("PUT", "/users/bob/role", token, `{"role":"admin"}`)
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
if e := expectInternalErrorRow(t, res, opUserRole); e.TargetUserID.Int64 != bob.ID {
|
|
t.Fatalf("%v: %+v", cause, e)
|
|
}
|
|
if h.stored("bob").Role != roleUser {
|
|
t.Fatalf("%v: change kept without its audit row", cause)
|
|
}
|
|
}
|
|
if status, msg := failureStatus(&auditWriteError{err: driver.ErrBadConn}); status != 500 || msg != msgInternalError {
|
|
t.Fatal(status, msg)
|
|
}
|
|
if status, _ := failureStatus(driver.ErrBadConn); status != 503 {
|
|
t.Fatal("operation connection errors must stay 503", status)
|
|
}
|
|
delete(h.store.failTimes, "InsertAudit")
|
|
calls := h.store.called("InsertAudit")
|
|
res := h.api("PUT", "/users/bob/role", token, `{"role":"admin"}`)
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
if len(res.audit) != 0 || h.store.called("InsertAudit")-calls != 2 || h.stored("bob").Role != roleUser {
|
|
t.Fatal("transactional audit failure should attempt exactly one fallback")
|
|
}
|
|
}
|
|
|
|
func TestAPIUserCreateRollbackClearsTheTarget(t *testing.T) {
|
|
h, token := usersHarness(t)
|
|
h.store.failOn["InsertAudit"] = errors.New("audit down")
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res := h.api("POST", "/users", token, `{"username":"newbie","role":"user","password":"long-enough-pw"}`)
|
|
expectError(t, res, 500, `{"error":"internal error"}`)
|
|
if e := expectInternalErrorRow(t, res, opUserCreate); e.TargetUserID.Valid {
|
|
t.Fatalf("rolled back insert kept as target: %+v", e)
|
|
}
|
|
if _, err := h.store.UserByUsername(t.Context(), "newbie"); !errors.Is(err, errRowNotFound) {
|
|
t.Fatal("user kept")
|
|
}
|
|
h.store.failOn["CreateUser"] = errors.New("x")
|
|
res = h.api("POST", "/users", token, `{"username":"newbie","role":"user","password":"long-enough-pw"}`)
|
|
if e := expectNoChange(t, res, opUserCreate, 500); e.TargetUserID.Valid {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
|
|
func TestLoginAuditFailures(t *testing.T) {
|
|
h := newWebHarness(t)
|
|
alice := h.user("alice", roleUser)
|
|
h.store.failOn["InsertAudit"] = errors.New("audit down")
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res := h.postLogin(loginForm("alice", "wrong-password", ""), "")
|
|
if res.status != 500 || !strings.Contains(res.body, msgLoginUnavailable) {
|
|
t.Fatal(res.status)
|
|
}
|
|
if e := expectInternalErrorRow(t, res, opLogin); e.UserID.Int64 != alice.ID {
|
|
t.Fatal(e)
|
|
}
|
|
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
h.store.failOn["UserByUsername"] = errDatabase
|
|
res = h.postLogin(loginForm("alice", testPassword, ""), "")
|
|
if res.status != 500 {
|
|
t.Fatal("unavailable answered after the audit failed", res.status)
|
|
}
|
|
expectInternalErrorRow(t, res, opLogin)
|
|
delete(h.store.failOn, "UserByUsername")
|
|
|
|
h.store.failOn["InsertAudit"] = driver.ErrBadConn
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res = h.postLogin(loginForm("alice", testPassword, ""), "")
|
|
if res.status != 500 || len(res.header.Values("Set-Cookie")) != 0 || h.store.sessionCount(alice.ID) != 0 {
|
|
t.Fatal("transactional audit failure", res.status)
|
|
}
|
|
expectInternalErrorRow(t, res, opLogin)
|
|
delete(h.store.failTimes, "InsertAudit")
|
|
calls := h.store.called("InsertAudit")
|
|
res = h.postLogin(loginForm("alice", testPassword, ""), "")
|
|
if res.status != 500 || len(res.audit) != 0 || h.store.called("InsertAudit")-calls != 2 {
|
|
t.Fatal("transactional login audit failure should attempt exactly one fallback")
|
|
}
|
|
|
|
for range 5 {
|
|
h.s.limiter.fail("alice", "192.0.2.10")
|
|
}
|
|
h.store.failOn["InsertAudit"] = errors.New("audit down")
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res = h.postLogin(loginForm("alice", testPassword, ""), "")
|
|
if res.status != 500 || !strings.Contains(res.body, msgLoginUnavailable) {
|
|
t.Fatal("rate limited", res.status)
|
|
}
|
|
expectInternalErrorRow(t, res, opLogin)
|
|
delete(h.store.failTimes, "InsertAudit")
|
|
if res := h.postLogin(loginForm("alice", testPassword, ""), ""); res.status != 500 || len(res.audit) != 0 {
|
|
t.Fatal("persistent failure", res.status)
|
|
}
|
|
delete(h.store.failOn, "InsertAudit")
|
|
|
|
token := h.login(alice)
|
|
h.store.failOn["InsertAudit"] = driver.ErrBadConn
|
|
h.store.failTimes["InsertAudit"] = 1
|
|
res = h.do(webRequest{method: "POST", path: "/logout", token: token})
|
|
if res.status != 500 || h.store.sessionCount(alice.ID) != 1 {
|
|
t.Fatal("logout", res.status)
|
|
}
|
|
expectInternalErrorRow(t, res, opLogout)
|
|
}
|
|
|
|
func TestAuditReadHidesRefusedTargets(t *testing.T) {
|
|
h := newWebHarness(t)
|
|
admin := h.user("admin", roleAdmin)
|
|
alice := h.user("alice", roleUser)
|
|
h.user("bob", roleUser)
|
|
at, ut := h.login(admin), h.login(alice)
|
|
if res := h.api("GET", "/users/bob", ut, ""); res.status != 403 {
|
|
t.Fatal(res.status)
|
|
}
|
|
if res := h.api("PUT", "/account/password", ut, `{"current":"secret-password","password":"own-new-password"}`); res.status != 204 {
|
|
t.Fatal(res.status)
|
|
}
|
|
targets := func(token string) map[string]*string {
|
|
var body struct {
|
|
Entries []struct {
|
|
Operation string `json:"operation"`
|
|
TargetUser *string `json:"target_user"`
|
|
} `json:"entries"`
|
|
}
|
|
res := h.api("GET", "/audit", token, "")
|
|
if err := json.Unmarshal([]byte(res.body), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := map[string]*string{}
|
|
for _, e := range body.Entries {
|
|
out[e.Operation] = e.TargetUser
|
|
}
|
|
return out
|
|
}
|
|
user, adm := targets(ut), targets(at)
|
|
if user[opUserRead] != nil || user[opPasswordChange] == nil || *user[opPasswordChange] != "alice" {
|
|
t.Fatalf("user view %v", user)
|
|
}
|
|
if adm[opUserRead] == nil || *adm[opUserRead] != "bob" {
|
|
t.Fatalf("admin view %v", adm)
|
|
}
|
|
}
|