Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): treat an unparseable key expiry as expired in the sweep
Yudhi Armyndharisad2fed689ae9
diff --git a/src/modules/auth/api-key-authorization.spec.ts b/src/modules/auth/api-key-authorization.spec.tsnew file mode 100644index 000000000..dabacfec6--- /dev/null+++ b/src/modules/auth/api-key-authorization.spec.ts@@ -0,0 +1,29 @@+import { apiKeyAuthorizationFingerprint, apiKeyExpiryTime } from './api-key-authorization';+import { ApiKeyRole } from './entities/api-key.entity';++describe('apiKeyExpiryTime', () => {+ it.each([null, undefined])('reads %s as no expiry', value => {+ expect(apiKeyExpiryTime(value)).toBeNull();+ });++ it.each([new Date(123_456), '1970-01-01T00:02:03.456Z'])('reads the instant of %s', value => {+ expect(apiKeyExpiryTime(value)).toBe(123_456);+ });++ // REST refuses a key whose stored expiry is an Invalid Date (validateApiKey), so the socket sweep that+ // reads the same value must not mistake it for a key that never expires.+ it.each([new Date('not-a-date'), 'not-a-date'])('reads the unparseable expiry %s as already expired', value => {+ expect(apiKeyExpiryTime(value)).toBe(0);+ });+});++describe('apiKeyAuthorizationFingerprint', () => {+ const unscoped = { role: ApiKeyRole.OPERATOR, allowedIps: null, allowedSessions: null, allowedChats: null };++ it('tells an unparseable expiry from no expiry', () => {+ const open = apiKeyAuthorizationFingerprint({ ...unscoped, expiresAt: null });+ const broken = apiKeyAuthorizationFingerprint({ ...unscoped, expiresAt: new Date('not-a-date') });+ expect(open).toBe('["operator",null,null,null,null]');+ expect(broken).toBe('["operator",null,null,null,0]');+ });+});diff --git a/src/modules/auth/api-key-authorization.ts b/src/modules/auth/api-key-authorization.tsindex 9a840189a..d41770c1e 100644--- a/src/modules/auth/api-key-authorization.ts+++ b/src/modules/auth/api-key-authorization.ts@@ -19,11 +19,15 @@ export function normalizeScopeList(list: string[] | null | undefined): string[]return cleaned.length > 0 ? cleaned : null;}-/** An `expiresAt` in milliseconds, or null when it is unset or unparseable (a Date from the driver, a string from a snapshot). */+/**+ * An `expiresAt` in milliseconds (a Date from the driver, a string from a key a socket carries), or null when it is unset.+ * An unparseable value is 0, the epoch: validateApiKey refuses such a key as expired, so it must not read as+ * one that never expires.+ */export function apiKeyExpiryTime(value: Date | string | null | undefined): number | null {if (!value) return null;const time = value instanceof Date ? value.getTime() : new Date(value).getTime();- return Number.isFinite(time) ? time : null;+ return Number.isFinite(time) ? time : 0;}/** The columns that decide what a key may do. Everything else on the row is descriptive or advisory. */