Files
bryanpedini 53444bec7a test(ui): avoid a literal em dash in the em dash check
- 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
2026-11-19 15:46:12 +01:00

126 lines
2.6 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"context"
"errors"
"io"
"log/slog"
"strings"
)
const LevelTrace = slog.Level(-8)
var errInvalidLogLevel = errors.New("invalid log level")
var logLevelNames = []struct {
level slog.Level
name string
}{
{slog.LevelError, "ERROR"},
{slog.LevelWarn, "WARN"},
{slog.LevelInfo, "INFO"},
{slog.LevelDebug, "DEBUG"},
{LevelTrace, "TRACE"},
}
var redactedLogKeys = map[string]bool{
"password": true,
"db_password": true,
"password_hash": true,
"token": true,
"session_token": true,
"cookie": true,
"dsn": true,
"secret": true,
}
const redactedLogValue = "[redacted]"
func parseLogLevel(s string) (slog.Level, error) {
for _, l := range logLevelNames {
if strings.EqualFold(s, l.name) && isASCII(s) {
return l.level, nil
}
}
return 0, errInvalidLogLevel
}
func logLevelName(level slog.Level) string {
for _, l := range logLevelNames {
if l.level == level {
return l.name
}
}
return level.String()
}
func newLogger(w io.Writer, level slog.Level) *slog.Logger {
return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{
Level: level,
ReplaceAttr: replaceLogAttr,
}))
}
func replaceLogAttr(groups []string, a slog.Attr) slog.Attr {
if redactedLogKeys[strings.ToLower(a.Key)] {
return slog.String(a.Key, redactedLogValue)
}
if len(groups) != 0 {
return a
}
switch a.Key {
case slog.LevelKey:
if level, ok := a.Value.Any().(slog.Level); ok {
a.Value = slog.StringValue(logLevelName(level))
}
case slog.TimeKey:
if a.Value.Kind() == slog.KindTime {
a.Value = slog.TimeValue(a.Value.Time().UTC())
}
}
return a
}
func logTrace(ctx context.Context, log *slog.Logger, msg string, args ...any) {
log.Log(ctx, LevelTrace, msg, args...)
}
func traceFunc(ctx context.Context, log *slog.Logger, fn string) func() {
logTrace(ctx, log, "function entry", "func", fn)
return func() {
logTrace(ctx, log, "function exit", "func", fn)
}
}
type traceLoggerKey struct{}
func withTraceLogger(ctx context.Context, log *slog.Logger) context.Context {
if log.Enabled(ctx, LevelTrace) {
return context.WithValue(ctx, traceLoggerKey{}, log)
}
return ctx
}
func traceStore(ctx context.Context, operation string) func() {
if log, ok := ctx.Value(traceLoggerKey{}).(*slog.Logger); ok {
return traceFunc(ctx, log, operation)
}
return func() {}
}
func discardLogger() *slog.Logger {
return slog.New(slog.DiscardHandler)
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= 0x80 {
return false
}
}
return true
}