Skip to content

fix(audit): count free refusals instead of writing hash-chained rows - #617

Open
Bccorb wants to merge 1 commit into
mainfrom
fix/unauthenticated-refusal-audit
Open

Bccorb wants to merge 1 commit into
mainfrom
fix/unauthenticated-refusal-audit

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Closes #596.

Problem

Several refusals that any caller can produce for free wrote a hash-chained AuthEvent:

  • the CORS refusal
  • every 404
  • POST /refresh with no token, or with a token that matches no session

Each write took the chain head row lock and stored the raw user agent and, for 404s, originalUrl. A scanner at the general rate limit could grow the trail and queue real sign-ins behind its writes.

Changes

Counted, not recorded. These refusals are counted in process in the new src/services/unauthenticatedRefusals.ts. Each instance logs one summary line at most once a minute, when it sees one: Unauthenticated refusals in the last 60s: unknown_route=... cors_origin_refused=.... The line carries no paths, origins or token material.

  • The per-request 404 log line, which included the caller's path, is gone.
  • Status codes and bodies are unchanged.

Issued tokens are still recorded. A refresh token that matches a session is still a refresh_token_failed row. A revoked session is now recorded against its session and user. It used to be lumped in with unknown tokens and had neither.

  • The rotated-token and reuse paths are untouched.
  • Only tokens that match no session row are now counted rather than recorded.

Caps where rows are still written.

  • The stored audit user_agent is capped at 512 characters, the same limit the trusted client user agent header already had.
  • The route a refused bearer records (bearer_token_failed) is capped at 256 characters, because it falls back to the request path when no route pattern matched.

Docs. docs/security-posture.md, "Refusals at the auth gate", gains "Other free refusals", which lists what is counted and why, and the caps. The CORS section no longer says the refusal writes a request_suspicious row.

Contract impact

No route, schema, token or status code changes.

There is a behavioural change for readers of the trail. request_suspicious rows for unknown routes and refused origins, and refresh_token_failed rows for unknown tokens, stop appearing. The admin dashboard and GET /internal/security/anomalies will show far fewer suspicious events. Their response shapes are unchanged.

Verification

  • npm run typecheck, npm run lint and npm run format:check pass.
  • npm run coverage passes: 196 files, 3447 tests, 99.08% lines, 95.83% branches.
  • npm run generate:api produced no diff.
  • The TEST_DB=postgres suite passes against a scratch Postgres (19 files, 283 tests).

CORS refusals, unknown routes, and /refresh with no token or one that
matches no session each wrote an audit event and took the chain head
lock. They are now counted in process with a per-minute log summary.
Stored user agents and refused bearer routes are capped.

Refs #596
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(audit): stop unauthenticated refusals from writing hash-chained audit rows

1 participant