- 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
344 lines
10 KiB
Go
344 lines
10 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type auditEntry struct {
|
|
ID int64
|
|
Time time.Time
|
|
UserID sql.NullInt64
|
|
Actor string
|
|
Origin string
|
|
Operation string
|
|
Status sql.NullInt64
|
|
Error sql.NullString
|
|
Zone sql.NullString
|
|
RecordID sql.NullInt64
|
|
TargetUserID sql.NullInt64
|
|
Before []byte
|
|
After []byte
|
|
}
|
|
|
|
const (
|
|
opLogin = "login"
|
|
opLogout = "logout"
|
|
opAccountRead = "account_read"
|
|
opPasswordChange = "password_change"
|
|
opZoneList = "zone_list"
|
|
opZoneRead = "zone_read"
|
|
opZoneCreate = "zone_create"
|
|
opZoneDelete = "zone_delete"
|
|
opRecordList = "record_list"
|
|
opRecordCreate = "record_create"
|
|
opRecordUpdate = "record_update"
|
|
opRecordDelete = "record_delete"
|
|
opRecordBulkDelete = "record_bulk_delete"
|
|
opSOAUpdate = "soa_update"
|
|
opZoneCheck = "zone_check"
|
|
opCheckAll = "check_all"
|
|
opZoneExport = "zone_export"
|
|
opZoneImportPreview = "zone_import_preview"
|
|
opZoneImport = "zone_import"
|
|
opDelegationList = "delegation_list"
|
|
opDelegationGrant = "delegation_grant"
|
|
opDelegationRevoke = "delegation_revoke"
|
|
opUserList = "user_list"
|
|
opUserRead = "user_read"
|
|
opUserCreate = "user_create"
|
|
opUserRole = "user_role"
|
|
opUserPassword = "user_password"
|
|
opUserDelete = "user_delete"
|
|
opAuditRead = "audit_read"
|
|
opAPIUnknown = "api_unknown"
|
|
)
|
|
|
|
const (
|
|
cliActor = reservedUsername
|
|
cliOrigin = "local"
|
|
auditTimeLayout = "2006-01-02T15:04:05.000000Z"
|
|
defaultBodyLimit = 64 << 10
|
|
importBodyLimit = 2 << 20
|
|
apiPrefix = "/api/v1"
|
|
auditDefaultLimit = 50
|
|
auditMaxLimit = 200
|
|
auditMaxBeforeID = 1<<53 - 1
|
|
)
|
|
|
|
type auditOperation struct {
|
|
Name string
|
|
Kind string
|
|
}
|
|
|
|
var auditOperations = []auditOperation{
|
|
{opAccountRead, auditKindRead},
|
|
{opLogin, auditKindChange},
|
|
{opLogout, auditKindChange},
|
|
{opPasswordChange, auditKindChange},
|
|
{opZoneList, auditKindRead},
|
|
{opZoneRead, auditKindRead},
|
|
{opZoneCreate, auditKindChange},
|
|
{opZoneDelete, auditKindChange},
|
|
{opRecordList, auditKindRead},
|
|
{opRecordCreate, auditKindChange},
|
|
{opRecordUpdate, auditKindChange},
|
|
{opRecordDelete, auditKindChange},
|
|
{opRecordBulkDelete, auditKindChange},
|
|
{opSOAUpdate, auditKindChange},
|
|
{opZoneCheck, auditKindRead},
|
|
{opCheckAll, auditKindRead},
|
|
{opZoneExport, auditKindRead},
|
|
{opZoneImportPreview, auditKindRead},
|
|
{opZoneImport, auditKindChange},
|
|
{opDelegationList, auditKindRead},
|
|
{opDelegationGrant, auditKindChange},
|
|
{opDelegationRevoke, auditKindChange},
|
|
{opUserList, auditKindRead},
|
|
{opUserRead, auditKindRead},
|
|
{opUserCreate, auditKindChange},
|
|
{opUserRole, auditKindChange},
|
|
{opUserPassword, auditKindChange},
|
|
{opUserDelete, auditKindChange},
|
|
{opAuditRead, auditKindRead},
|
|
{opAPIUnknown, auditKindRead},
|
|
}
|
|
|
|
var auditChangeOperations = operationsOfKind(auditKindChange)
|
|
|
|
var zoneOperations = []string{
|
|
opZoneList, opZoneRead, opZoneCreate, opZoneDelete,
|
|
opRecordList, opRecordCreate, opRecordUpdate, opRecordDelete, opRecordBulkDelete, opSOAUpdate,
|
|
opZoneCheck, opCheckAll, opZoneExport, opZoneImportPreview, opZoneImport,
|
|
}
|
|
|
|
func operationsOfKind(kind string) []string {
|
|
var out []string
|
|
for _, o := range auditOperations {
|
|
if o.Kind == kind {
|
|
out = append(out, o.Name)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func knownOperation(name string) bool {
|
|
return slices.ContainsFunc(auditOperations, func(o auditOperation) bool { return o.Name == name })
|
|
}
|
|
|
|
type apiRoute struct {
|
|
Method string
|
|
Path string
|
|
Operation string
|
|
Access string
|
|
BodyLimit int64
|
|
}
|
|
|
|
var apiRoutes = []apiRoute{
|
|
{"GET", "/account", opAccountRead, accessSession, defaultBodyLimit},
|
|
{"PUT", "/account/password", opPasswordChange, accessSession, defaultBodyLimit},
|
|
{"GET", "/zones", opZoneList, accessSession, defaultBodyLimit},
|
|
{"POST", "/zones", opZoneCreate, accessAdmin, defaultBodyLimit},
|
|
{"GET", "/zones/{zone}", opZoneRead, accessZone, defaultBodyLimit},
|
|
{"DELETE", "/zones/{zone}", opZoneDelete, accessAdmin, defaultBodyLimit},
|
|
{"GET", "/zones/{zone}/records", opRecordList, accessZone, defaultBodyLimit},
|
|
{"POST", "/zones/{zone}/records", opRecordCreate, accessZone, defaultBodyLimit},
|
|
{"PUT", "/zones/{zone}/records/{id}", opRecordUpdate, accessZone, defaultBodyLimit},
|
|
{"DELETE", "/zones/{zone}/records/{id}", opRecordDelete, accessZone, defaultBodyLimit},
|
|
{"POST", "/zones/{zone}/records/delete", opRecordBulkDelete, accessZone, defaultBodyLimit},
|
|
{"PUT", "/zones/{zone}/soa", opSOAUpdate, accessZone, defaultBodyLimit},
|
|
{"GET", "/zones/{zone}/check", opZoneCheck, accessZone, defaultBodyLimit},
|
|
{"GET", "/check", opCheckAll, accessSession, defaultBodyLimit},
|
|
{"GET", "/zones/{zone}/export", opZoneExport, accessZone, defaultBodyLimit},
|
|
{"POST", "/zones/{zone}/import/preview", opZoneImportPreview, accessZone, importBodyLimit},
|
|
{"POST", "/zones/{zone}/import/apply", opZoneImport, accessZone, importBodyLimit},
|
|
{"GET", "/zones/{zone}/users", opDelegationList, accessAdmin, defaultBodyLimit},
|
|
{"PUT", "/zones/{zone}/users/{username}", opDelegationGrant, accessAdmin, defaultBodyLimit},
|
|
{"DELETE", "/zones/{zone}/users/{username}", opDelegationRevoke, accessAdmin, defaultBodyLimit},
|
|
{"GET", "/users", opUserList, accessAdmin, defaultBodyLimit},
|
|
{"POST", "/users", opUserCreate, accessAdmin, defaultBodyLimit},
|
|
{"GET", "/users/{username}", opUserRead, accessAdmin, defaultBodyLimit},
|
|
{"PUT", "/users/{username}/role", opUserRole, accessAdmin, defaultBodyLimit},
|
|
{"PUT", "/users/{username}/password", opUserPassword, accessAdmin, defaultBodyLimit},
|
|
{"DELETE", "/users/{username}", opUserDelete, accessAdmin, defaultBodyLimit},
|
|
{"GET", "/audit", opAuditRead, accessSession, defaultBodyLimit},
|
|
}
|
|
|
|
type pageRoute struct {
|
|
Method string
|
|
Path string
|
|
Access string
|
|
Page string
|
|
}
|
|
|
|
var pageRoutes = []pageRoute{
|
|
{"GET", "/login", accessNone, "login"},
|
|
{"POST", "/login", accessNone, ""},
|
|
{"POST", "/logout", accessSession, ""},
|
|
{"GET", "/", accessSession, "zones"},
|
|
{"GET", "/zones/{zone}", accessZone, "zone"},
|
|
{"GET", "/zones/{zone}/import", accessZone, "import"},
|
|
{"GET", "/audit", accessSession, "audit"},
|
|
{"GET", "/account", accessSession, "account"},
|
|
{"GET", "/users", accessAdmin, "users"},
|
|
{"GET", "/users/{username}", accessAdmin, "user"},
|
|
{"GET", "/version", accessNone, ""},
|
|
{"GET", "/health", accessNone, ""},
|
|
{"GET", "/static/{hash}/...", accessNone, ""},
|
|
}
|
|
|
|
func routeHasZone(path string) bool {
|
|
return strings.Contains(path, "{zone}")
|
|
}
|
|
|
|
func apiAuditEntry(now time.Time, p principal, origin, operation, zone string) auditEntry {
|
|
e := auditEntry{
|
|
Time: now,
|
|
UserID: sql.NullInt64{Int64: p.User.ID, Valid: true},
|
|
Actor: p.User.Username,
|
|
Origin: origin,
|
|
Operation: operation,
|
|
}
|
|
if zone != "" {
|
|
e.Zone = sql.NullString{String: zone, Valid: true}
|
|
}
|
|
return e
|
|
}
|
|
|
|
func loginActor(submitted string) string {
|
|
return printableText(submitted, auditMaxActorRunes)
|
|
}
|
|
|
|
func loginAuditEntry(now time.Time, operation, submitted string, user *userRow, origin string) auditEntry {
|
|
e := auditEntry{Time: now, Actor: loginActor(submitted), Origin: origin, Operation: operation}
|
|
if user != nil {
|
|
e.UserID = sql.NullInt64{Int64: user.ID, Valid: true}
|
|
}
|
|
return e
|
|
}
|
|
|
|
func finishAuditEntry(e *auditEntry, status int, message string) {
|
|
e.Status = sql.NullInt64{Int64: int64(status), Valid: true}
|
|
e.Error = sql.NullString{}
|
|
if status >= 400 {
|
|
e.Error = sql.NullString{String: message, Valid: true}
|
|
}
|
|
}
|
|
|
|
func cliAuditEntry(now time.Time, operation string, target userRow, before, after *userRow) auditEntry {
|
|
e := auditEntry{
|
|
Time: now,
|
|
Actor: cliActor,
|
|
Origin: cliOrigin,
|
|
Operation: operation,
|
|
TargetUserID: sql.NullInt64{Int64: target.ID, Valid: true},
|
|
}
|
|
if before != nil {
|
|
e.Before = auditUserJSON(*before)
|
|
}
|
|
if after != nil {
|
|
e.After = auditUserJSON(*after)
|
|
}
|
|
return e
|
|
}
|
|
|
|
func formatAuditTime(t time.Time) string {
|
|
return t.UTC().Format(auditTimeLayout)
|
|
}
|
|
|
|
func formatAuditNullTime(t sql.NullTime) *string {
|
|
if !t.Valid {
|
|
return nil
|
|
}
|
|
s := formatAuditTime(t.Time)
|
|
return &s
|
|
}
|
|
|
|
type auditUserValue struct {
|
|
ID int64 `json:"id"`
|
|
Username string `json:"username"`
|
|
Role string `json:"role"`
|
|
CreatedAt string `json:"created_at"`
|
|
DeletedAt *string `json:"deleted_at"`
|
|
}
|
|
|
|
type auditRecordValue struct {
|
|
ID int64 `json:"id"`
|
|
Zone string `json:"zone"`
|
|
Name string `json:"name"`
|
|
TTL *int64 `json:"ttl"`
|
|
RecordType string `json:"record_type"`
|
|
Content *string `json:"content"`
|
|
}
|
|
|
|
type auditDelegationValue struct {
|
|
Zone string `json:"zone"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
func mustAuditJSON(v any) []byte {
|
|
b, _ := json.Marshal(v)
|
|
return b
|
|
}
|
|
|
|
func auditUserValueOf(u userRow) auditUserValue {
|
|
return auditUserValue{ID: u.ID, Username: u.Username, Role: u.Role, CreatedAt: formatAuditTime(u.CreatedAt), DeletedAt: formatAuditNullTime(u.DeletedAt)}
|
|
}
|
|
|
|
func auditUserJSON(u userRow) []byte {
|
|
return mustAuditJSON(auditUserValueOf(u))
|
|
}
|
|
|
|
func auditRecordValueOf(r recordRow) auditRecordValue {
|
|
v := auditRecordValue{ID: r.ID, Zone: r.Zone, Name: r.Name, RecordType: r.RecordType}
|
|
if r.TTL.Valid {
|
|
ttl := r.TTL.Int64
|
|
v.TTL = &ttl
|
|
}
|
|
if r.Content.Valid {
|
|
content := r.Content.String
|
|
v.Content = &content
|
|
}
|
|
return v
|
|
}
|
|
|
|
func auditRecordJSON(r recordRow) []byte {
|
|
return mustAuditJSON(auditRecordValueOf(r))
|
|
}
|
|
|
|
func auditRecordsJSON(rows []recordRow) []byte {
|
|
values := make([]auditRecordValue, 0, len(rows))
|
|
for _, r := range rows {
|
|
values = append(values, auditRecordValueOf(r))
|
|
}
|
|
return mustAuditJSON(values)
|
|
}
|
|
|
|
func auditDelegationJSON(zone, username string) []byte {
|
|
return mustAuditJSON(auditDelegationValue{Zone: zone, Username: username})
|
|
}
|
|
|
|
func auditDelegationsJSON(rows []delegationRow) []byte {
|
|
values := make([]auditDelegationValue, 0, len(rows))
|
|
for _, d := range rows {
|
|
values = append(values, auditDelegationValue{Zone: d.Zone, Username: d.Username})
|
|
}
|
|
return mustAuditJSON(values)
|
|
}
|
|
|
|
func auditVisibleTo(p principal, e auditEntry, delegated map[string]bool) bool {
|
|
if p.isAdmin() {
|
|
return true
|
|
}
|
|
if e.UserID.Valid && e.UserID.Int64 == p.User.ID {
|
|
return true
|
|
}
|
|
return e.Zone.Valid && delegated[e.Zone.String]
|
|
}
|