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

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>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-18 17:33:53 +01:00
1 parent e99b42c387
commit 363de086fc
3 files changed
+76 -4

No files matched your search

+22 -1
View File
@@ -112,7 +112,7 @@ async function startIpServer() {
const app = (s as unknown as { app: FastifyInstance }).app;
const login = (ip: string, user: string, pass: string) =>
app.inject({ method: 'GET', url: '/api/status', headers: { authorization: basic(user, pass) }, remoteAddress: ip });
return { login, buckets: s as unknown as FailureBuckets };
return { app, login, buckets: s as unknown as FailureBuckets };
}
describe('multi-user auth', () => {
@@ -270,6 +270,27 @@ describe('multi-user auth', () => {
expect((await login(ipA, 'bob', 'bobpass123')).statusCode).toBe(429);
});
it('requests without credentials are challenged, not counted, even past the per-IP cap', async () => {
const { app, login, buckets } = await startIpServer();
const ip = '203.0.113.40';
const bare = () =>
app.inject({
method: 'GET',
url: '/api/status',
headers: { cookie: 'codeman_session=stale-from-before-restart' },
remoteAddress: ip,
});
// An open tab after a restart: many cookie-only requests, none of them a guess.
for (let i = 0; i < AUTH_IP_FAILURE_MAX + 5; i++) {
const res = await bare();
expect(res.statusCode, `request ${i + 1}`).toBe(401);
expect(res.headers['www-authenticate']).toBe('Basic realm="Codeman"');
}
expect(buckets.authFailures.get(ip)).toBeUndefined();
expect((await login(ip, 'alice', 'alicepass1')).statusCode).toBe(200);
});
it('per-IP pre-gate: a concurrent burst from one IP reaches the verify at most AUTH_IP_FAILURE_MAX times', async () => {
const { login, buckets } = await startIpServer();
const ip = '203.0.113.40';