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
+81
-29
@@ -268,13 +268,28 @@ func (s *WebServer) renderPage(w http.ResponseWriter, status int, page string, d
|
||||
|
||||
func (s *WebServer) pageFailure(w http.ResponseWriter, r *http.Request, err error) {
|
||||
s.log.ErrorContext(r.Context(), "page request failed", "path", r.URL.Path, "error", err)
|
||||
if isDatabaseUnavailable(err) {
|
||||
http.Error(w, msgPageUnavailable, http.StatusServiceUnavailable)
|
||||
status, _ := failureStatus(err)
|
||||
pageError(w, status)
|
||||
}
|
||||
|
||||
func pageError(w http.ResponseWriter, status int) {
|
||||
if status == http.StatusServiceUnavailable {
|
||||
http.Error(w, msgPageUnavailable, status)
|
||||
return
|
||||
}
|
||||
http.Error(w, msgPageInternalError, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (s *WebServer) pageZoneExists(ctx context.Context, zone string) (bool, error) {
|
||||
for _, stored := range []string{zone, strings.TrimSuffix(zone, ".")} {
|
||||
ok, err := s.store.ZoneExists(ctx, stored)
|
||||
if err != nil || ok {
|
||||
return ok, err
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s *WebServer) optionalPageData(r *http.Request) pageData {
|
||||
data := s.basePageData()
|
||||
if p, err := s.authenticate(r); err == nil {
|
||||
@@ -297,6 +312,7 @@ func (s *WebServer) handleMethodNotAllowed(allow string) http.HandlerFunc {
|
||||
func (s *WebServer) servePage(route pageRoute) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
defer traceFunc(ctx, s.log, "page."+route.Page)()
|
||||
p, err := s.authenticate(r)
|
||||
if errors.Is(err, errNoSession) {
|
||||
http.Redirect(w, r, loginLocation(r), http.StatusSeeOther)
|
||||
@@ -338,6 +354,15 @@ func (s *WebServer) servePage(route pageRoute) http.HandlerFunc {
|
||||
s.renderPage(w, http.StatusNotFound, "not_found", data)
|
||||
return
|
||||
}
|
||||
exists, err := s.pageZoneExists(ctx, zone)
|
||||
if err != nil {
|
||||
s.pageFailure(w, r, err)
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
s.renderPage(w, http.StatusNotFound, "not_found", data)
|
||||
return
|
||||
}
|
||||
data.Zone = zone
|
||||
}
|
||||
s.renderPage(w, http.StatusOK, route.Page, data)
|
||||
@@ -359,20 +384,32 @@ func (s *WebServer) renderLogin(w http.ResponseWriter, r *http.Request, status i
|
||||
}
|
||||
|
||||
func (s *WebServer) handleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
defer traceFunc(r.Context(), s.log, "page.login.get")()
|
||||
s.renderLogin(w, r, http.StatusOK, r.URL.Query().Get("next"), "")
|
||||
}
|
||||
|
||||
func (s *WebServer) loginAttemptAudit(ctx context.Context, e auditEntry, status int, message string) error {
|
||||
var errLoginStale = errors.New("account changed during login")
|
||||
|
||||
func (s *WebServer) loginAttemptAudit(ctx context.Context, e auditEntry, status int, message string) int {
|
||||
finishAuditEntry(&e, status, message)
|
||||
if err := s.insertAudit(ctx, e); err != nil {
|
||||
s.log.ErrorContext(ctx, "audit insert failed", "operation", e.Operation, "status", status, "error", err)
|
||||
return err
|
||||
if !s.recordAudit(ctx, e) {
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
return nil
|
||||
return status
|
||||
}
|
||||
|
||||
func (s *WebServer) sessionOperationFailure(ctx context.Context, e auditEntry, err error) int {
|
||||
if isAuditWriteError(err) {
|
||||
s.recordInternalAudit(ctx, e)
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
status, msg := failureStatus(err)
|
||||
return s.loginAttemptAudit(ctx, e, status, msg)
|
||||
}
|
||||
|
||||
func (s *WebServer) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
defer traceFunc(ctx, s.log, "page.login.post")()
|
||||
r.Body = http.MaxBytesReader(w, r.Body, loginFormLimit)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
s.log.DebugContext(ctx, "invalid login form", "error", err)
|
||||
@@ -396,7 +433,7 @@ func (s *WebServer) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
case !errors.Is(err, errRowNotFound):
|
||||
s.log.ErrorContext(ctx, "login user lookup failed", "error", err)
|
||||
status, msg := failureStatus(err)
|
||||
_ = s.loginAttemptAudit(ctx, loginAuditEntry(now, opLogin, username, nil, origin), status, msg)
|
||||
status = s.loginAttemptAudit(ctx, loginAuditEntry(now, opLogin, username, nil, origin), status, msg)
|
||||
s.renderLogin(w, r, status, next, msgLoginUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -404,26 +441,28 @@ func (s *WebServer) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
entry := loginAuditEntry(now, opLogin, username, user, origin)
|
||||
if !s.limiter.allow(actor, origin) {
|
||||
s.log.WarnContext(ctx, "login rate limited", "actor", actor, "origin", origin)
|
||||
if err := s.loginAttemptAudit(ctx, entry, http.StatusTooManyRequests, msgTooManyLogins); err != nil {
|
||||
s.renderLogin(w, r, http.StatusInternalServerError, next, msgLoginUnavailable)
|
||||
return
|
||||
}
|
||||
s.renderLogin(w, r, http.StatusTooManyRequests, next, msgTooManyLogins)
|
||||
s.renderLoginResult(w, r, s.loginAttemptAudit(ctx, entry, http.StatusTooManyRequests, msgTooManyLogins), next, msgTooManyLogins)
|
||||
return
|
||||
}
|
||||
if !verifyLogin(s.verifyPassword, user, password) {
|
||||
s.limiter.fail(actor, origin)
|
||||
s.log.WarnContext(ctx, "login failed", "actor", actor, "origin", origin)
|
||||
if err := s.loginAttemptAudit(ctx, entry, http.StatusUnauthorized, msgInvalidLogin); err != nil {
|
||||
s.renderLogin(w, r, http.StatusInternalServerError, next, msgLoginUnavailable)
|
||||
return
|
||||
}
|
||||
s.renderLogin(w, r, http.StatusUnauthorized, next, msgInvalidLogin)
|
||||
s.renderLoginResult(w, r, s.loginAttemptAudit(ctx, entry, http.StatusUnauthorized, msgInvalidLogin), next, msgInvalidLogin)
|
||||
return
|
||||
}
|
||||
token, hash, err := newSessionToken(s.random)
|
||||
if err == nil {
|
||||
err = s.store.InTx(ctx, func(tx webTx) error {
|
||||
current, err := tx.UserByIDForUpdate(ctx, user.ID)
|
||||
if errors.Is(err, errRowNotFound) {
|
||||
return errLoginStale
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current.DeletedAt.Valid || current.PasswordHash != user.PasswordHash {
|
||||
return errLoginStale
|
||||
}
|
||||
if old, ok := requestSessionToken(r); ok {
|
||||
if _, err := tx.DeleteSession(ctx, sessionTokenHash(old)); err != nil {
|
||||
return err
|
||||
@@ -437,15 +476,18 @@ func (s *WebServer) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
e := entry
|
||||
finishAuditEntry(&e, http.StatusSeeOther, "")
|
||||
_, err := tx.InsertAudit(ctx, e)
|
||||
return err
|
||||
return insertAuditRow(ctx, tx, e)
|
||||
})
|
||||
}
|
||||
if errors.Is(err, errLoginStale) {
|
||||
s.limiter.fail(actor, origin)
|
||||
s.log.WarnContext(ctx, "login failed", "actor", actor, "origin", origin, "reason", "account changed during login")
|
||||
s.renderLoginResult(w, r, s.loginAttemptAudit(ctx, entry, http.StatusUnauthorized, msgInvalidLogin), next, msgInvalidLogin)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
s.log.ErrorContext(ctx, "login failed with an internal error", "actor", actor, "error", err)
|
||||
status, msg := failureStatus(err)
|
||||
_ = s.loginAttemptAudit(ctx, entry, status, msg)
|
||||
s.renderLogin(w, r, status, next, msgLoginUnavailable)
|
||||
s.renderLogin(w, r, s.sessionOperationFailure(ctx, entry, err), next, msgLoginUnavailable)
|
||||
return
|
||||
}
|
||||
s.limiter.succeed(actor)
|
||||
@@ -455,8 +497,16 @@ func (s *WebServer) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, next, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *WebServer) renderLoginResult(w http.ResponseWriter, r *http.Request, status int, next, message string) {
|
||||
if status == http.StatusInternalServerError {
|
||||
message = msgLoginUnavailable
|
||||
}
|
||||
s.renderLogin(w, r, status, next, message)
|
||||
}
|
||||
|
||||
func (s *WebServer) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
defer traceFunc(ctx, s.log, "page.logout")()
|
||||
p, err := s.authenticate(r)
|
||||
if errors.Is(err, errNoSession) {
|
||||
http.SetCookie(w, expiredSessionCookie(s.cfg.CookieSecure))
|
||||
@@ -474,13 +524,11 @@ func (s *WebServer) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
e := entry
|
||||
finishAuditEntry(&e, http.StatusSeeOther, "")
|
||||
_, err := tx.InsertAudit(ctx, e)
|
||||
return err
|
||||
return insertAuditRow(ctx, tx, e)
|
||||
})
|
||||
if err != nil {
|
||||
status, msg := failureStatus(err)
|
||||
_ = s.loginAttemptAudit(ctx, entry, status, msg)
|
||||
s.pageFailure(w, r, err)
|
||||
s.log.ErrorContext(ctx, "logout failed", "error", err)
|
||||
pageError(w, s.sessionOperationFailure(ctx, entry, err))
|
||||
return
|
||||
}
|
||||
s.log.InfoContext(ctx, "change committed", "operation", opLogout, "actor", p.User.Username)
|
||||
@@ -495,7 +543,11 @@ func (s *WebServer) registerPageRoutes(r *mux.Router) {
|
||||
"POST /logout": s.handleLogout,
|
||||
}
|
||||
if s.pages != nil {
|
||||
r.PathPrefix("/static/").Handler(s.pages.staticHandler())
|
||||
static := s.pages.staticHandler()
|
||||
r.PathPrefix("/static/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
defer traceFunc(req.Context(), s.log, "page.static")()
|
||||
static.ServeHTTP(w, req)
|
||||
})
|
||||
}
|
||||
byPath := map[string]map[string]http.HandlerFunc{}
|
||||
var paths []string
|
||||
|
||||
Reference in new issue
Block a user