Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): evict a socket whose stored expiry stops parsing

Yudhi Armyndharis5eb81768048b
diff --git a/src/modules/events/events.gateway.authz-sweep.spec.ts b/src/modules/events/events.gateway.authz-sweep.spec.ts
index 93292e5e7..e1735e13b 100644
--- a/src/modules/events/events.gateway.authz-sweep.spec.ts
+++ b/src/modules/events/events.gateway.authz-sweep.spec.ts
@@ -190,6 +190,22 @@ describe('EventsGateway API-key authorization sweep', () => {
expect(later.disconnect).not.toHaveBeenCalled();
});
+ it('evicts a socket when its stored expiry becomes invalid', async () => {
+ const { apiKey, rawKey } = await service.createApiKey({ name: 'invalid expiry' });
+ const sock = await connect(rawKey);
+
+ await repo.query('UPDATE api_keys SET expiresAt = ? WHERE id = ?', ['not-a-date', apiKey.id]);
+ const current = await repo.findOneByOrFail({ id: apiKey.id });
+ expect(current.expiresAt).toBeInstanceOf(Date);
+ expect(Number.isFinite(current.expiresAt!.getTime())).toBe(false);
+ await expect(service.validateApiKey(rawKey, CLIENT_IP)).rejects.toThrow('API key has expired');
+
+ await sweep();
+
+ expect(sock.disconnect).toHaveBeenCalledWith(true);
+ expect(evictionMessage(sock)).toBe('API key has expired');
+ });
+
it('evicts on the snapshot expiry when the api_keys table cannot be read', async () => {
const { rawKey } = await service.createApiKey({
name: 'expiring key',
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index 8e0a4fbc1..f5475407a 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -289,7 +289,8 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
if (!current) return 'deleted';
if (!current.isActive) return 'revoked';
const expiry = apiKeyExpiryTime(current.expiresAt);
- if (expiry !== null && expiry <= now) return 'expired';
+ // A stored expiry that does not parse reads as unset to apiKeyExpiryTime, which AuthService refuses as expired.
+ if (current.expiresAt && (expiry === null || expiry <= now)) return 'expired';
const authorization = apiKeyAuthorizationFingerprint(current);
// Per socket, not per key: sockets under one key connected at different moments, so one can hold
// a stale snapshot while another already carries the new authorization. A socket that subscribed