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
This commit is contained in:
1 parent
5f17aab6ca
commit
53444bec7a
40 files changed
+2332
-254
No files matched your search
+103
-1
@@ -110,10 +110,112 @@ func migrateDatabase(ctx context.Context, db connSource, t tableNames, log *slog
|
||||
return err
|
||||
}
|
||||
return withNamedLock(ctx, db, t.migrateLockName(), migrateLockTimeout, log, func(conn *sql.Conn) error {
|
||||
return applyMigrations(ctx, conn, t, migrations, log)
|
||||
if err := applyMigrations(ctx, conn, t, migrations, log); err != nil {
|
||||
return err
|
||||
}
|
||||
return verifyAppSchema(ctx, conn, t, log)
|
||||
})
|
||||
}
|
||||
|
||||
type schemaColumn struct {
|
||||
table string
|
||||
column string
|
||||
columnType string
|
||||
}
|
||||
|
||||
var appSchema = []schemaColumn{
|
||||
{"webui_schema", "id", "tinyint"},
|
||||
{"webui_schema", "version", "int"},
|
||||
{"webui_users", "id", "int"},
|
||||
{"webui_users", "username", "varchar(64)"},
|
||||
{"webui_users", "role", "varchar(8)"},
|
||||
{"webui_users", "password_hash", "varchar(255)"},
|
||||
{"webui_users", "created_at", "datetime(6)"},
|
||||
{"webui_users", "updated_at", "datetime(6)"},
|
||||
{"webui_users", "last_login_at", "datetime(6)"},
|
||||
{"webui_users", "deleted_at", "datetime(6)"},
|
||||
{"webui_sessions", "token_hash", "binary(32)"},
|
||||
{"webui_sessions", "user_id", "int"},
|
||||
{"webui_sessions", "created_at", "datetime(6)"},
|
||||
{"webui_sessions", "last_seen_at", "datetime(6)"},
|
||||
{"webui_sessions", "expires_at", "datetime(6)"},
|
||||
{"webui_sessions", "origin", "varchar(45)"},
|
||||
{"webui_sessions", "user_agent", "varchar(255)"},
|
||||
{"webui_zone_delegations", "zone", "varchar(255)"},
|
||||
{"webui_zone_delegations", "user_id", "int"},
|
||||
{"webui_zone_delegations", "created_at", "datetime(6)"},
|
||||
{"webui_zone_delegations", "created_by", "int"},
|
||||
{"webui_audit", "id", "bigint"},
|
||||
{"webui_audit", "time", "datetime(6)"},
|
||||
{"webui_audit", "user_id", "int"},
|
||||
{"webui_audit", "actor", "varchar(64)"},
|
||||
{"webui_audit", "origin", "varchar(45)"},
|
||||
{"webui_audit", "operation", "varchar(32)"},
|
||||
{"webui_audit", "status", "smallint"},
|
||||
{"webui_audit", "error", "varchar(255)"},
|
||||
{"webui_audit", "zone", "varchar(255)"},
|
||||
{"webui_audit", "record_id", "int"},
|
||||
{"webui_audit", "target_user_id", "int"},
|
||||
{"webui_audit", "before_value", "json"},
|
||||
{"webui_audit", "after_value", "json"},
|
||||
}
|
||||
|
||||
var appTables = []string{"webui_schema", "webui_users", "webui_sessions", "webui_zone_delegations", "webui_audit"}
|
||||
|
||||
func verifyAppSchema(ctx context.Context, q dbtx, t tableNames, log *slog.Logger) error {
|
||||
args := make([]any, len(appTables))
|
||||
for i, name := range appTables {
|
||||
args[i] = t.prefix + name
|
||||
}
|
||||
rows, err := q.QueryContext(ctx, "SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN (?, ?, ?, ?, ?)", args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read application schema: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
found := map[string]string{}
|
||||
for rows.Next() {
|
||||
var table, column, columnType string
|
||||
if err := rows.Scan(&table, &column, &columnType); err != nil {
|
||||
return fmt.Errorf("read application schema: %w", err)
|
||||
}
|
||||
found[strings.ToLower(table)+"."+strings.ToLower(column)] = strings.ToLower(columnType)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return fmt.Errorf("read application schema: %w", err)
|
||||
}
|
||||
for _, c := range appSchema {
|
||||
table := t.prefix + c.table
|
||||
got, ok := found[strings.ToLower(table)+"."+c.column]
|
||||
if !ok {
|
||||
return fmt.Errorf("application schema: table %s has no column %s", table, c.column)
|
||||
}
|
||||
if got != c.columnType {
|
||||
return fmt.Errorf("application schema: column %s of table %s has type %s, expected %s", c.column, table, got, c.columnType)
|
||||
}
|
||||
}
|
||||
if err := verifyAppForeignKeys(ctx, q, t); err != nil {
|
||||
return err
|
||||
}
|
||||
log.DebugContext(ctx, "application schema verified")
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyAppForeignKeys(ctx context.Context, q dbtx, t tableNames) error {
|
||||
var n int
|
||||
err := q.QueryRowContext(ctx, "SELECT COUNT(DISTINCT TABLE_NAME, COLUMN_NAME) FROM information_schema.KEY_COLUMN_USAGE "+
|
||||
"WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME = ? AND REFERENCED_COLUMN_NAME = 'id' "+
|
||||
"AND ((TABLE_NAME = ? AND COLUMN_NAME = 'user_id') OR (TABLE_NAME = ? AND COLUMN_NAME IN ('user_id', 'created_by')) "+
|
||||
"OR (TABLE_NAME = ? AND COLUMN_NAME IN ('user_id', 'target_user_id')))",
|
||||
t.prefix+"webui_users", t.prefix+"webui_sessions", t.prefix+"webui_zone_delegations", t.prefix+"webui_audit").Scan(&n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read application foreign keys: %w", err)
|
||||
}
|
||||
if n != 5 {
|
||||
return fmt.Errorf("application schema: expected 5 foreign keys to %swebui_users.id, found %d", t.prefix, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func schemaVersion(ctx context.Context, q dbtx, t tableNames) (int, error) {
|
||||
var count int
|
||||
if err := q.QueryRowContext(ctx,
|
||||
|
||||
Reference in new issue
Block a user