mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
fix(oidc): count every back-channel logout at the door and give the attempt back when it was valid
Counting only the refused ones after the work let a burst of forged tokens run past the limit while the provider's keys were still being fetched.
This commit is contained in:
@@ -19,11 +19,12 @@ export class RateLimitService {
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the key already used up `max` in this window, without counting
|
||||
* the request; for a route that counts only the attempts it refuses.
|
||||
* Give back an attempt `check` counted, for a route that only holds the
|
||||
* requests it refuses against the limit. Counting at the door and giving back
|
||||
* afterwards keeps concurrent requests capped while they are still running.
|
||||
*/
|
||||
exhausted(bucket: string, key: string, max: number, windowMs: number, now: number): Promise<boolean> {
|
||||
return this.store.exhausted(bucket, key, max, windowMs, now);
|
||||
release(bucket: string, key: string, windowMs: number, now: number): Promise<void> {
|
||||
return this.store.release(bucket, key, windowMs, now);
|
||||
}
|
||||
|
||||
/** Test helper: clear a bucket (mirrors the legacy exported maps used for resets). */
|
||||
|
||||
@@ -15,8 +15,8 @@ export abstract class RateLimitStore {
|
||||
* answers 429), true otherwise.
|
||||
*/
|
||||
abstract hit(bucket: string, key: string, max: number, windowMs: number, now: number): Promise<boolean>;
|
||||
/** Whether `key` has already used `max` attempts in the current window, without counting one. */
|
||||
abstract exhausted(bucket: string, key: string, max: number, windowMs: number, now: number): Promise<boolean>;
|
||||
/** Give back one attempt `key` counted in the current window, for a request that turned out fine. */
|
||||
abstract release(bucket: string, key: string, windowMs: number, now: number): Promise<void>;
|
||||
/** Clear one bucket, or every bucket. */
|
||||
abstract reset(bucket?: string): Promise<void>;
|
||||
}
|
||||
@@ -47,9 +47,10 @@ export class InMemoryRateLimitStore extends RateLimitStore {
|
||||
return Promise.resolve(this.count(bucket, key, max, windowMs, now));
|
||||
}
|
||||
|
||||
exhausted(bucket: string, key: string, max: number, windowMs: number, now: number): Promise<boolean> {
|
||||
release(bucket: string, key: string, windowMs: number, now: number): Promise<void> {
|
||||
const record = this.buckets.get(bucket)?.get(key);
|
||||
return Promise.resolve(!!record && record.count >= max && now - record.first < windowMs);
|
||||
if (record && record.count > 0 && now - record.first < windowMs) record.count--;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
reset(bucket?: string): Promise<void> {
|
||||
|
||||
@@ -59,11 +59,12 @@ export class OidcBackchannelController {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
res.set('Pragma', 'no-cache');
|
||||
const key = req.ip || 'unknown';
|
||||
if (await this.rl.exhausted('oidc_backchannel', key, BACKCHANNEL_LOGOUT_RATE_LIMIT, RATE_WINDOW_MS, Date.now())) {
|
||||
if (!(await this.rl.check('oidc_backchannel', key, BACKCHANNEL_LOGOUT_RATE_LIMIT, RATE_WINDOW_MS, Date.now()))) {
|
||||
throw new DomainError(429, 'too_many_requests', {
|
||||
details: { error_description: 'Too many requests. Please try again later.' },
|
||||
});
|
||||
}
|
||||
let refused = false;
|
||||
try {
|
||||
const body = oidcBackchannelLogoutRequestSchema.safeParse(req.body);
|
||||
if (!body.success) {
|
||||
@@ -75,10 +76,10 @@ export class OidcBackchannelController {
|
||||
await this.backchannel.logout(body.data.logout_token, getClientIp(req) ?? null);
|
||||
return {};
|
||||
} catch (err) {
|
||||
if (err instanceof DomainError && err.getStatus() === 400) {
|
||||
await this.rl.check('oidc_backchannel', key, BACKCHANNEL_LOGOUT_RATE_LIMIT, RATE_WINDOW_MS, Date.now());
|
||||
}
|
||||
refused = err instanceof DomainError && err.getStatus() === 400;
|
||||
throw err;
|
||||
} finally {
|
||||
if (!refused) await this.rl.release('oidc_backchannel', key, RATE_WINDOW_MS, Date.now());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const logout = vi.fn();
|
||||
const check = vi.fn();
|
||||
const exhausted = vi.fn();
|
||||
const release = vi.fn();
|
||||
const service = { logout } as unknown as OidcBackchannelService;
|
||||
const rl = { check, exhausted } as unknown as RateLimitService;
|
||||
const rl = { check, release } as unknown as RateLimitService;
|
||||
const controller = () => new OidcBackchannelController(service, rl);
|
||||
|
||||
function makeRes() {
|
||||
@@ -54,7 +54,7 @@ async function refused(call: Promise<unknown>): Promise<{ status: number; body:
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
check.mockResolvedValue(true);
|
||||
exhausted.mockResolvedValue(false);
|
||||
release.mockResolvedValue(undefined);
|
||||
logout.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
@@ -106,47 +106,46 @@ 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 () => {
|
||||
exhausted.mockResolvedValue(true);
|
||||
check.mockResolvedValue(false);
|
||||
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(exhausted).toHaveBeenCalledWith(
|
||||
expect(check).toHaveBeenCalledWith(
|
||||
'oidc_backchannel',
|
||||
'198.51.100.7',
|
||||
BACKCHANNEL_LOGOUT_RATE_LIMIT,
|
||||
60_000,
|
||||
expect.any(Number),
|
||||
);
|
||||
expect(check).not.toHaveBeenCalled();
|
||||
expect(release).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++) {
|
||||
it('BCL-CTL-007: every request is counted at the door, and a valid logout gives its attempt back', async () => {
|
||||
const n = BACKCHANNEL_LOGOUT_RATE_LIMIT + 5;
|
||||
for (let i = 0; i < n; i++) {
|
||||
await controller().logout(req({ logout_token: 'a.b.c' }), makeRes());
|
||||
}
|
||||
expect(check).not.toHaveBeenCalled();
|
||||
expect(check).toHaveBeenCalledTimes(n);
|
||||
expect(release).toHaveBeenCalledTimes(n);
|
||||
expect(release).toHaveBeenLastCalledWith('oidc_backchannel', '198.51.100.7', 60_000, expect.any(Number));
|
||||
|
||||
check.mockClear();
|
||||
release.mockClear();
|
||||
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),
|
||||
);
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
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();
|
||||
expect(release).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('BCL-CTL-006: the route is a public POST that answers 200 under the OIDC prefix', () => {
|
||||
|
||||
@@ -25,17 +25,18 @@ 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 () => {
|
||||
it('release gives back a counted attempt, so only the kept ones reach the limit', 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);
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(await s.check('logout', '1.2.3.4', 2, WINDOW, now)).toBe(true);
|
||||
await s.release('logout', '1.2.3.4', WINDOW, now);
|
||||
}
|
||||
expect(await s.check('logout', '1.2.3.4', 2, WINDOW, now)).toBe(true);
|
||||
expect(await s.check('logout', '1.2.3.4', 2, WINDOW, now)).toBe(true);
|
||||
expect(await s.check('logout', '1.2.3.4', 2, WINDOW, now)).toBe(false);
|
||||
await s.release('nobody', '9.9.9.9', WINDOW, now);
|
||||
expect(bucketSize(s, 'nobody')).toBe(0);
|
||||
});
|
||||
|
||||
it('starts a fresh window once the old one elapsed', async () => {
|
||||
|
||||
Reference in New Issue
Block a user