fix(oidc): count only refused back-channel logouts toward the rate limit

A realm-wide sign-out sends one validly signed logout per session from one address, and past 600 a minute the rest got 429 and stayed signed in. Only tokens that are refused count now, so a flood of bad tokens is still capped.
This commit is contained in:
Maurice
2026-11-28 00:33:50 +01:00
parent a6328cef3b
commit 218304179c
5 changed files with 82 additions and 26 deletions
@@ -22,8 +22,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
const logout = vi.fn();
const check = vi.fn();
const exhausted = vi.fn();
const service = { logout } as unknown as OidcBackchannelService;
const rl = { check } as unknown as RateLimitService;
const rl = { check, exhausted } as unknown as RateLimitService;
const controller = () => new OidcBackchannelController(service, rl);
function makeRes() {
@@ -53,6 +54,7 @@ async function refused(call: Promise<unknown>): Promise<{ status: number; body:
beforeEach(() => {
vi.clearAllMocks();
check.mockResolvedValue(true);
exhausted.mockResolvedValue(false);
logout.mockResolvedValue(undefined);
});
@@ -104,23 +106,49 @@ describe('OidcBackchannelController POST /api/auth/oidc/backchannel-logout', ()
});
it('BCL-CTL-005: past the flood limit the answer is 429, before the body is read', async () => {
check.mockResolvedValue(false);
exhausted.mockResolvedValue(true);
const res = makeRes();
expect(await refused(controller().logout(req({ logout_token: 'a.b.c' }), res))).toEqual({
status: 429,
body: { error: 'too_many_requests', error_description: 'Too many requests. Please try again later.' },
});
expect(check).toHaveBeenCalledWith(
expect(exhausted).toHaveBeenCalledWith(
'oidc_backchannel',
'198.51.100.7',
BACKCHANNEL_LOGOUT_RATE_LIMIT,
60_000,
expect.any(Number),
);
expect(check).not.toHaveBeenCalled();
expect(logout).not.toHaveBeenCalled();
expect(res.headers['cache-control']).toBe('no-store');
});
it('BCL-CTL-007: only refused tokens count toward the limit, so a bulk logout of valid tokens is never throttled', async () => {
for (let i = 0; i < BACKCHANNEL_LOGOUT_RATE_LIMIT + 5; i++) {
await controller().logout(req({ logout_token: 'a.b.c' }), makeRes());
}
expect(check).not.toHaveBeenCalled();
expect((await refused(controller().logout(req({}), makeRes()))).status).toBe(400);
logout.mockRejectedValue(new DomainError(400, 'invalid_request'));
expect((await refused(controller().logout(req({ logout_token: 'x.y.z' }), makeRes()))).status).toBe(400);
expect(check).toHaveBeenCalledTimes(2);
expect(check).toHaveBeenLastCalledWith(
'oidc_backchannel',
'198.51.100.7',
BACKCHANNEL_LOGOUT_RATE_LIMIT,
60_000,
expect.any(Number),
);
});
it('BCL-CTL-008: a failure that is not a refused token is passed on without counting', async () => {
logout.mockRejectedValue(new Error('database down'));
await expect(controller().logout(req({ logout_token: 'a.b.c' }), makeRes())).rejects.toThrow('database down');
expect(check).not.toHaveBeenCalled();
});
it('BCL-CTL-006: the route is a public POST that answers 200 under the OIDC prefix', () => {
const handler = OidcBackchannelController.prototype.logout;
expect(Reflect.getMetadata(PATH_METADATA, OidcBackchannelController)).toBe('api/auth/oidc');
@@ -25,6 +25,19 @@ describe('RateLimitService', () => {
expect(await s.check('login', '1.2.3.4', 5, WINDOW, now)).toBe(false);
});
it('exhausted answers whether the key is spent without counting an attempt', async () => {
const s = new RateLimitService();
const now = Date.now();
expect(await s.exhausted('logout', '1.2.3.4', 2, WINDOW, now)).toBe(false);
for (let i = 0; i < 5; i++) await s.exhausted('logout', '1.2.3.4', 2, WINDOW, now);
expect(bucketSize(s, 'logout')).toBe(0);
await s.check('logout', '1.2.3.4', 2, WINDOW, now);
expect(await s.exhausted('logout', '1.2.3.4', 2, WINDOW, now)).toBe(false);
await s.check('logout', '1.2.3.4', 2, WINDOW, now);
expect(await s.exhausted('logout', '1.2.3.4', 2, WINDOW, now)).toBe(true);
expect(await s.exhausted('logout', '1.2.3.4', 2, WINDOW, now + WINDOW)).toBe(false);
});
it('starts a fresh window once the old one elapsed', async () => {
const s = new RateLimitService();
const now = Date.now();