Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(throttler): send the remaining window as Retry-After under Redis

Yudhi Armyndharis83f369a0004a
diff --git a/src/common/throttler/redis-throttler.storage.spec.ts b/src/common/throttler/redis-throttler.storage.spec.ts
index 5ab7972fd..5ac1e12bd 100644
--- a/src/common/throttler/redis-throttler.storage.spec.ts
+++ b/src/common/throttler/redis-throttler.storage.spec.ts
@@ -41,12 +41,27 @@ describe('RedisThrottlerStorage', () => {
expect(rec).toEqual({ totalHits: 1, timeToExpire: 1, isBlocked: false, timeToBlockExpire: 0 });
});
- it('over the limit (incr>limit) is blocked with blockDuration in seconds', async () => {
- const redis = makeRedis({ hits: 11, ttlMs: 500 });
+ // The block is the counter itself, so it lifts when the window key expires, not after blockDuration:
+ // advertising blockDuration made a client that honours Retry-After wait up to an hour for nothing.
+ it('over the limit (incr>limit) is blocked until the window expires, in seconds', async () => {
+ const redis = makeRedis({ hits: 1001, ttlMs: 4200 });
+ const rec = await new RedisThrottlerStorage(redis as unknown as Redis).increment(
+ 'k',
+ 3600000,
+ 1000,
+ 3600000,
+ 'long',
+ );
+ expect(rec.isBlocked).toBe(true);
+ expect(rec.totalHits).toBe(1001);
+ expect(rec.timeToBlockExpire).toBe(5); // ceil(4200ms / 1000), not the 3600 s blockDuration
+ });
+
+ it('never advertises a zero Retry-After on a blocked request', async () => {
+ const redis = makeRedis({ hits: 11, ttlMs: 0 });
const rec = await new RedisThrottlerStorage(redis as unknown as Redis).increment('k', 1000, 10, 60000, 'short');
expect(rec.isBlocked).toBe(true);
- expect(rec.totalHits).toBe(11);
- expect(rec.timeToBlockExpire).toBe(60); // 60000ms / 1000
+ expect(rec.timeToBlockExpire).toBe(1);
});
it('fails OPEN on a Redis error (returns a non-blocking record so the limiter never self-DoSes)', async () => {
diff --git a/src/common/throttler/redis-throttler.storage.ts b/src/common/throttler/redis-throttler.storage.ts
index 6776296de..d1a6dbf46 100644
--- a/src/common/throttler/redis-throttler.storage.ts
+++ b/src/common/throttler/redis-throttler.storage.ts
@@ -38,8 +38,10 @@ return {hits, ttl}
*
* The guard sets `Retry-After: timeToBlockExpire` and `RateLimit-Reset: timeToExpire` — both HTTP
* conventions are SECONDS — so the values here are ceil(ms / 1000), matching the default in-memory
- * storage. Fail-OPEN on Redis error: rate limiting is a secondary control, and fail-closed would
- * self-DoS the gateway (every request 500s on the storage call).
+ * storage. Unlike that storage there is no separate block key: the over-limit counter IS the block,
+ * so it lifts when the window expires and Retry-After advertises that, not blockDuration.
+ * Fail-OPEN on Redis error: rate limiting is a secondary control, and fail-closed would self-DoS the
+ * gateway (every request 500s on the storage call).
*
* This class owns the client lifecycle: it is registered as the `ThrottlerStorage` provider by
* @nestjs/throttler (its ThrottlerStorageProvider returns the configured instance), so Nest invokes
@@ -100,7 +102,7 @@ export class RedisThrottlerStorage implements ThrottlerStorage, OnModuleDestroy
totalHits: hits,
timeToExpire: ttlMs > 0 ? Math.ceil(ttlMs / 1000) : 0,
isBlocked,
- timeToBlockExpire: isBlocked ? Math.ceil(blockDuration / 1000) : 0,
+ timeToBlockExpire: isBlocked ? Math.max(1, Math.ceil(ttlMs / 1000)) : 0,
};
} catch (error) {
this.logger.warn('Redis throttler storage failed; failing OPEN (allowing)', {