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
+44
-6
@@ -25,6 +25,7 @@ const (
|
||||
msgDelegationAbsent = "delegation not found"
|
||||
msgWrongPassword = "current password is wrong"
|
||||
msgConfirmMismatch = "confirmation does not match"
|
||||
msgPasswordChanged = "the password was changed by another request"
|
||||
)
|
||||
|
||||
func frameworkAPIHandlers(s *WebServer) map[string]apiHandler {
|
||||
@@ -123,6 +124,18 @@ func activeTargetForUpdate(ctx context.Context, c *apiContext, tx webTx, usernam
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func lockActingAdmin(ctx context.Context, c *apiContext, tx webTx) ([]userRow, error) {
|
||||
admins, err := tx.LockActiveAdmins(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !slices.ContainsFunc(admins, func(u userRow) bool { return u.ID == c.Principal.User.ID }) {
|
||||
c.Server.log.WarnContext(ctx, "actor is no longer an active admin", "operation", c.Entry.Operation, "user", c.Principal.User.Username)
|
||||
return nil, &apiError{Status: http.StatusForbidden, Message: msgAdminRequired}
|
||||
}
|
||||
return admins, nil
|
||||
}
|
||||
|
||||
func (c *apiContext) recordChange(before, after []byte) {
|
||||
c.Entry.Before, c.Entry.After = before, after
|
||||
}
|
||||
@@ -182,11 +195,14 @@ func handlePasswordChange(ctx context.Context, c *apiContext) (apiResponse, erro
|
||||
c.setTargetUser(c.Principal.User)
|
||||
err = s.store.InTx(ctx, func(tx webTx) error {
|
||||
u, err := tx.UserByIDForUpdate(ctx, c.Principal.User.ID)
|
||||
if errors.Is(err, errRowNotFound) || (err == nil && u.DeletedAt.Valid) {
|
||||
return &apiError{Status: http.StatusUnauthorized, Message: msgAuthRequired}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if u.DeletedAt.Valid {
|
||||
return errUserNotFound()
|
||||
if u.PasswordHash != c.Principal.User.PasswordHash {
|
||||
return &apiError{Status: http.StatusConflict, Message: msgPasswordChanged}
|
||||
}
|
||||
if err := tx.SetPassword(ctx, u.ID, hash, c.Now); err != nil {
|
||||
return err
|
||||
@@ -242,6 +258,9 @@ func handleDelegationGrant(ctx context.Context, c *apiContext) (apiResponse, err
|
||||
return apiResponse{}, err
|
||||
}
|
||||
err = c.Server.store.InZoneTx(ctx, c.Zone, c.Server.log, func(tx webTx) error {
|
||||
if _, err := lockActingAdmin(ctx, c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := requireZone(ctx, tx.ZoneExists, c.Zone); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -275,6 +294,9 @@ func handleDelegationRevoke(ctx context.Context, c *apiContext) (apiResponse, er
|
||||
return apiResponse{}, err
|
||||
}
|
||||
err = c.Server.store.InZoneTx(ctx, c.Zone, c.Server.log, func(tx webTx) error {
|
||||
if _, err := lockActingAdmin(ctx, c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
u, err := activeTargetForUpdate(ctx, c, tx, username)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -379,6 +401,9 @@ func handleUserCreate(ctx context.Context, c *apiContext) (apiResponse, error) {
|
||||
}
|
||||
var created userRow
|
||||
err = s.store.InTx(ctx, func(tx webTx) error {
|
||||
if _, err := lockActingAdmin(ctx, c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
u, err := tx.CreateUser(ctx, req.Username, req.Role, hash, c.Now)
|
||||
if errors.Is(err, errUsernameTaken) {
|
||||
return &apiError{Status: http.StatusConflict, Message: msgUsernameTaken}
|
||||
@@ -392,6 +417,7 @@ func handleUserCreate(ctx context.Context, c *apiContext) (apiResponse, error) {
|
||||
return c.commitChange(ctx, tx, http.StatusCreated)
|
||||
})
|
||||
if err != nil {
|
||||
c.Entry.TargetUserID = sql.NullInt64{}
|
||||
return apiResponse{}, err
|
||||
}
|
||||
return apiResponse{Status: http.StatusCreated, Body: userListItemOf(created, 0)}, nil
|
||||
@@ -451,7 +477,7 @@ type userRoleRequest struct {
|
||||
}
|
||||
|
||||
func lockAdminsAndTarget(ctx context.Context, c *apiContext, tx webTx, username string) (userRow, []userRow, error) {
|
||||
admins, err := tx.LockActiveAdmins(ctx)
|
||||
admins, err := lockActingAdmin(ctx, c, tx)
|
||||
if err != nil {
|
||||
return userRow{}, nil, err
|
||||
}
|
||||
@@ -518,6 +544,9 @@ func handleUserPassword(ctx context.Context, c *apiContext) (apiResponse, error)
|
||||
return apiResponse{}, err
|
||||
}
|
||||
err = s.store.InTx(ctx, func(tx webTx) error {
|
||||
if _, err := lockActingAdmin(ctx, c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
u, err := activeTargetForUpdate(ctx, c, tx, username)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -637,6 +666,15 @@ func auditItemOf(r auditRow) auditItem {
|
||||
}
|
||||
}
|
||||
|
||||
func auditItemFor(p principal, r auditRow) auditItem {
|
||||
item := auditItemOf(r)
|
||||
refused := !r.Status.Valid || r.Status.Int64 >= 400
|
||||
if !p.isAdmin() && r.TargetUserID.Valid && r.TargetUserID.Int64 != p.User.ID && refused {
|
||||
item.TargetUser = nil
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func parseAuditFilter(q url.Values) (auditFilter, error) {
|
||||
f := auditFilter{Limit: auditDefaultLimit}
|
||||
if v := q.Get("zone"); v != "" {
|
||||
@@ -671,7 +709,7 @@ func parseAuditFilter(q url.Values) (auditFilter, error) {
|
||||
return auditFilter{}, badParameter("outcome")
|
||||
}
|
||||
if v := q.Get("before_id"); v != "" {
|
||||
n, err := parsePositive(v, 1<<62)
|
||||
n, err := parsePositive(v, auditMaxBeforeID)
|
||||
if err != nil {
|
||||
return auditFilter{}, badParameter("before_id")
|
||||
}
|
||||
@@ -735,7 +773,7 @@ func handleAuditRead(ctx context.Context, c *apiContext) (apiResponse, error) {
|
||||
resp.NextBeforeID = &next
|
||||
break
|
||||
}
|
||||
resp.Entries = append(resp.Entries, auditItemOf(r))
|
||||
resp.Entries = append(resp.Entries, auditItemFor(c.Principal, r))
|
||||
}
|
||||
return okJSON(resp), nil
|
||||
}
|
||||
@@ -1100,7 +1138,7 @@ func (h zoneHandlers) createZone(ctx context.Context, c *apiContext) (apiRespons
|
||||
if err != nil {
|
||||
return apiResponse{}, err
|
||||
}
|
||||
if err := zoneAPIAuditChange(c, nil, zoneAPIAuditRows(rows, deleted), true); err != nil {
|
||||
if err := zoneAPIAuditChange(c, zoneAPIAuditRows(nil, deleted), zoneAPIAuditRows(rows, nil), true); err != nil {
|
||||
return apiResponse{}, err
|
||||
}
|
||||
return zoneAPISummary(zone, rows, c.Now, http.StatusCreated), nil
|
||||
|
||||
Reference in new issue
Block a user