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:
Maurice
2026-11-28 00:45:24 +01:00
parent 8c5e7373d3
commit ed134fdef3
5 changed files with 41 additions and 38 deletions
+5 -4
View File
@@ -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). */
+5 -4
View File
@@ -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());
}
}
}