Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(auth): treat an unreadable key expiry as expired in the key index
Yudhi Armyndharis64e351a473cd
diff --git a/src/modules/auth/active-key-index.spec.ts b/src/modules/auth/active-key-index.spec.tsindex 4c0c7736c..98976a457 100644--- a/src/modules/auth/active-key-index.spec.ts+++ b/src/modules/auth/active-key-index.spec.ts@@ -68,6 +68,13 @@ describe('ActiveKeyIndex', () => {expect(index.recognise({ 'x-api-key': 'fenced' }, '10.1.2.3')).toBe(sha('fenced'));});+ it('treats a stored expiry that reads back as an invalid date as expired, as validateApiKey does', async () => {+ const { index } = indexOver([row('garbled', { expiresAt: new Date('2020-W01-1') })]);+ await index.onApplicationBootstrap();+ index.onModuleDestroy();+ expect(index.recognise({ 'x-api-key': 'garbled' }, '192.0.2.1')).toBeUndefined();+ });+it('keeps the previous view when a refresh fails', async () => {let fail = false;const { index } = indexOver(() => (fail ? Promise.reject(new Error('db down')) : Promise.resolve([row('k1')])));diff --git a/src/modules/auth/active-key-index.ts b/src/modules/auth/active-key-index.tsindex 50b4922d7..2525edc68 100644--- a/src/modules/auth/active-key-index.ts+++ b/src/modules/auth/active-key-index.ts@@ -88,8 +88,9 @@ export class ActiveKeyIndex implements OnApplicationBootstrap, OnModuleDestroy {const key = this.keys.get(keyHash);if (!key) return undefined;// Expiry is checked here rather than in the query: SQLite stores dates as strings, and a key- // that expires between two refreshes must stop counting at once.- if (key.expiresAt && key.expiresAt < new Date()) return undefined;+ // that expires between two refreshes must stop counting at once. An expiry that reads back as an+ // invalid date counts as expired, as it does in validateApiKey.+ if (key.expiresAt && !(key.expiresAt >= new Date())) return undefined;if (key.allowedIps?.length && !key.allowedIps.some(entry => ipMatches(clientIp, entry))) return undefined;return keyHash;}