Skip to content

fix(auth): stop counting credential-less requests as failed logins - #838

Open
kkalisz wants to merge 4 commits into
Ark0N:masterfrom
kkalisz:fix/auth-429-lockout
Open

kkalisz wants to merge 4 commits into
Ark0N:masterfrom
kkalisz:fix/auth-429-lockout

Conversation

@kkalisz

@kkalisz kkalisz commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

After a server restart, a browser that had Codeman open gets stuck on Too Many Requests — try again later and reloading doesn't help.

  1. Cookie sessions live in memory, so a restart invalidates every cookie.
  2. The open tab's background requests (polling, reconnects) carry only the dead cookie and no Authorization header. Each was counted as a failed login against the client IP.
  3. Ten of them fill the single-user bucket (AUTH_FAILURE_MAX) within seconds.
  4. From then on every request gets a 429 with no WWW-Authenticate challenge, so the browser never shows its login dialog again. The user can't type the (still accepted) correct password until the 15-minute window expires.

Behind a tunnel all traffic arrives as 127.0.0.1, so one stale tab locks out every device. Multi-user mode has the same shape: credential-less requests filled the per-IP pre-gate (AUTH_IP_FAILURE_MAX), which refuses even a correct password once full.

Fix

A request with no Authorization header is not a guess and runs no password check. It now gets a plain 401 + WWW-Authenticate challenge and is not counted, in both modes and even while the IP is rate-limited. That keeps the browser's login dialog reachable.

Unchanged: wrong credentials and malformed Authorization headers are counted and limited exactly as before (same buckets, same caps, same 429 + Retry-After). The hook-secret bucket is untouched.

Security: nothing is gained by sending header-less requests. They cost no scrypt and reveal nothing beyond the existing 401.

Tests

  • auth-security.test.ts: cookie-only requests (3× the cap) stay 401 with a challenge and leave the wrong-password bucket empty. During a lockout a header-less request still gets the 401 challenge, while wrong credentials still get 429. A malformed header (Bearer x) is still counted: 10 get 401, the next 429.
  • multiuser-auth.test.ts: cookie-only requests past AUTH_IP_FAILURE_MAX stay 401, the per-IP bucket stays empty, and the correct password logs in. With the per-IP bucket full, a header-less request still gets the 401 challenge and a malformed one gets 429. Malformed headers still fill the per-IP cap.
  • The four fix tests fail on master without the fix; the two malformed-header tests pass on both, since they pin unchanged behaviour. The six auth suites pass with it (163 tests). Typecheck, lint, format:check, check:frontend-syntax and check:browser-excludes are clean.
  • By hand, on a real server with its own data dir and tmux socket, restarted while a cookie was kept: on master the 10th stale-cookie request got 429 with no challenge. With this PR, 30 of them got 401 + WWW-Authenticate: Basic realm="Codeman", the correct password logged in, 10 (single-user) or 50 (multi-user) × Bearer x still led to 429, and a header-less request during that lockout got the 401 challenge.

Docs

docs/security-architecture.md lists the no-header step in the auth pipeline; the rate-limit rows in CLAUDE.md and docs/architecture-invariants.md, the README and the Security / Remote-Access wiki pages say a request without credentials is never counted. Patch changeset included.

🤖 Generated with Claude Code

After a server restart every cookie session is gone (they live in memory).
An open tab's background requests then carry only the dead cookie and no
Authorization header. Each one was counted as a failed login: ten filled
the single-user per-IP bucket within seconds, and the 429 that followed
carries no WWW-Authenticate challenge, so the browser never showed its
login dialog again and reloading could not recover for 15 minutes. Behind
a tunnel all clients share 127.0.0.1, so every device was locked out.
Multi-user mode had the same shape against its per-IP pre-gate, which
refuses even a correct password once full.

A request with no Authorization header is not a guess and runs no
password check, so it is now challenged with a plain 401 and not counted,
in both modes and even while the IP is limited. Wrong and malformed
credentials are counted and limited exactly as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kkalisz
kkalisz marked this pull request as ready for review November 20, 2026 07:49
@Ark0N

Ark0N commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you for tracking this down. This PR stops counting requests that carry no Authorization header as failed logins. A tab left open across a restart now keeps getting a 401 challenge, and with it the browser's login dialog, instead of locking every tunnel user out for 15 minutes.

I traced both hooks and the change is right. In single-user mode the correct password already beat the rate limit, so the challenge was the only thing a lockout took away. In multi-user mode the early return comes before the in-flight join and both pre-verify gates, so a header-less request runs no scrypt and takes no slot. The three new tests fail on master without the fix, the full npm test gate passes, and typecheck, lint and format are clean. I also checked the other side of the line you drew: malformed headers such as Bearer x still fill the bucket and hit 429 in both modes.

Two small things are left, and I will add both on top when I merge, so you don't need to push anything:

  1. docs/security-architecture.md:141-143 walks through the auth pipeline step by step and still says a credential-less request is counted and gets 429 during a lockout. I will add a step there for the header-less challenge, plus a line in the Rate limit rows of CLAUDE.md and docs/architecture-invariants.md, so a later change can't bring the counting back without anyone noticing.
  2. No test covers the unchanged half yet. The existing malformed-header test in test/auth-security.test.ts:110 asserts a single 401. I will add one test per mode that sends the cap's worth of Authorization: Bearer x requests and expects the next one to be 429.

In the same pass I'll make two cosmetic changes: a blank comment line before the new paragraph at src/web/middleware/auth.ts:722, and challengeBasicAuth() at the two spots that still inline it (auth.ts:514 and :641).

Thanks again for the fix.

…llenge

Review on Ark0N#838. The unchanged half had no test: send the cap's worth of
`Authorization: Bearer x` and expect the next request to be 429, in
single-user and multi-user mode. Both fail if the header-less skip is
widened to malformed headers.

docs/security-architecture.md gets a pipeline step for the header-less
challenge (not counted, even during a lockout), and the Rate limit rows in
CLAUDE.md and docs/architecture-invariants.md say so, so a later change
cannot bring the counting back unnoticed. auth.ts uses challengeBasicAuth()
at the two spots that still inlined it, with a blank comment line before
the new paragraph.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ark0N

Ark0N commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you for the follow-up commit. This PR stops counting requests with no Authorization header as failed logins, so a tab left open across a restart keeps getting the 401 challenge instead of locking everyone behind the tunnel out for 15 minutes.

The second commit covers everything from my earlier comment: the new pipeline step in docs/security-architecture.md, the Rate limit rows in CLAUDE.md and docs/architecture-invariants.md, one Authorization: Bearer x test per mode that expects 429 at the cap, and challengeBasicAuth() at every challenge site. As a check I widened the skip to malformed headers, and both new malformed-header tests failed, so they hold the line you drew. The full gate and CI are green.

You don't need to push anything else. This is ready to merge as it stands.

kkalisz and others added 2 commits November 21, 2026 00:08
The pipeline list said it "runs in this order" but put the no-header
challenge before the HTTP Basic check, while single-user auth.ts checks
the correct password first. Swap steps 3 and 4. Note in the README and
the Security / Remote-Access wiki pages that a request with no
credentials is never counted, and add the patch changeset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lockout

The single-user suite pinned this; the multi-user one only showed that
header-less requests are not counted. Fill the per-IP bucket with
malformed headers, then expect 401 + challenge for no header and 429
for another malformed one. Fails on master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kkalisz

kkalisz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Pushed three commits that cover the review items:

  • ad08cb9: both malformed-header paths reuse one challengeBasicAuth() helper, and new tests pin that a malformed Authorization header (Bearer x) is still counted: 401 up to the cap, then 429, in single-user (10) and multi-user (50) mode. The rate-limit rows in CLAUDE.md and docs/architecture-invariants.md say a request with no header is never counted and a malformed one is.
  • cc2b7d9: in the auth pipeline list in docs/security-architecture.md, the no-header step now comes after the HTTP Basic check, where single-user auth.ts runs it. It also notes that multi-user mode runs it before the pre-verify gates. The rate-limit section, the README and the Security / Remote-Access wiki pages say a request with no credentials is never counted. Adds a patch changeset.
  • 5ff0187: a multi-user test for a request with no header while the IP is locked out. The per-IP bucket is filled with malformed headers; a request with no header still gets 401 + WWW-Authenticate, and another malformed one gets 429. It fails on master.

Checks on 5ff0187:

  • Typecheck, lint, format:check, check:frontend-syntax and check:browser-excludes pass, and the six auth suites pass (163 tests).
  • I hand-tested on a real server with its own data dir and tmux socket, restarted while a cookie was kept, in both modes.
    • 30 stale-cookie requests got 401 with the challenge, and the correct password logged in.
    • The cap's worth of Bearer x led to 429, and a request with no header during that lockout still got the 401 challenge.
    • On master, the 10th stale-cookie request got 429 with no challenge.

🤖 Generated with Claude Code

@Ark0N

Ark0N commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you for the two follow-up commits. This PR stops counting requests with no Authorization header as failed logins, so a tab left open across a restart keeps getting the 401 login challenge instead of locking everyone behind the tunnel out for 15 minutes.

I checked the latest head (5ff01877). The no-header step is now in the right place in the docs/security-architecture.md pipeline, the README and wiki lines match the code, and the changeset is there. The new multi-user test pins the challenge during a per-IP lockout. With the master middleware swapped in, exactly the four fix tests fail, and the full npm test gate and CI are green on this head.

You don't need to push anything else. This is ready to merge as it stands. Thanks again for tracking down the lockout.

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.

2 participants