OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
perf(api): remember verified client secrets in process
Carl-Gerhard Lindesvärdf07283cf7427
diff --git a/packages/core/src/modules/ingest/src/client-auth.test.ts b/packages/core/src/modules/ingest/src/client-auth.test.tsindex 1f72a3920..66beae360 100644--- a/packages/core/src/modules/ingest/src/client-auth.test.ts+++ b/packages/core/src/modules/ingest/src/client-auth.test.ts@@ -355,7 +355,7 @@ describe('validateIngestRequest — secret verification (main #481)', () => {deps,headers: {'openpanel-client-id': CLIENT_ID,- 'openpanel-client-secret': 'correct',+ 'openpanel-client-secret': 'verified-by-another-process',},clientIp: '1.2.3.4',body,@@ -367,6 +367,30 @@ describe('validateIngestRequest — secret verification (main #481)', () => {expect(verifyPassword).not.toHaveBeenCalled();});+ it('a verified secret is remembered in process, without Redis or re-hashing', async () => {+ guarded();+ verifyPassword.mockResolvedValue(true);+ const request = {+ deps,+ headers: {+ 'openpanel-client-id': CLIENT_ID,+ 'openpanel-client-secret': 'remembered',+ },+ clientIp: '1.2.3.4',+ body,+ };++ await validateIngestRequest(request);+ redisGet.mockClear();+ verifyPassword.mockClear();+ guarded();+ const result = await validateIngestRequest(request);++ expect(result.secretVerified).toBe(true);+ expect(redisGet).not.toHaveBeenCalled();+ expect(verifyPassword).not.toHaveBeenCalled();+ });+it('does not trust a cached "false" left over from earlier releases', async () => {guarded();redisGet.mockResolvedValue('false');diff --git a/packages/core/src/modules/ingest/src/client-auth.ts b/packages/core/src/modules/ingest/src/client-auth.tsindex a6d31af3a..c741979f2 100644--- a/packages/core/src/modules/ingest/src/client-auth.ts+++ b/packages/core/src/modules/ingest/src/client-auth.ts@@ -5,7 +5,7 @@// 5-minute verify cache whose key holds base64(<plaintext secret>) are all// deliberate.-import { getRedisCache } from '@openpanel/redis';+import { getRedisCache, LRUCache } from '@openpanel/redis';import { path } from 'ramda';import type { DbScope } from '../../../cacheable-per-deps';import { verifyClientSecret as verifyClientSecretHash } from '../../../shared/client-secret';@@ -24,6 +24,14 @@ export type { IngestHeaders } from '../../../shared/headers';const CLIENT_ID_UUID_REGEX =/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;const VERIFY_CACHE_SECONDS = 60 * 5;+const VERIFY_LOCAL_CACHE_TTL_MS = 60 * 1000;+const VERIFY_LOCAL_CACHE_MAX_ENTRIES = 1000;++/** Server SDKs send their secret on every event; this spares Redis that read. */+const verifiedInProcess = new LRUCache<string, true>({+ max: VERIFY_LOCAL_CACHE_MAX_ENTRIES,+ ttl: VERIFY_LOCAL_CACHE_TTL_MS,+});const REDACTED_SECRET_EDGE_LENGTH = 5;const DOMAIN_PROTOCOL = /https?:\/\//;const DOMAIN_TRAILING_SLASH = /\/$/;@@ -110,13 +118,19 @@ async function verifyClientSecret(const cacheKey = `client:auth:${clientId}:${Buffer.from(clientSecret).toString('base64')}`;+ if (verifiedInProcess.has(cacheKey)) {+ return true;+ }+if ((await getRedisCache().get(cacheKey)) === 'true') {+ verifiedInProcess.set(cacheKey, true);return true;}const isVerified = await verifyClientSecretHash(clientSecret, storedSecret);if (isVerified) {+ verifiedInProcess.set(cacheKey, true);getRedisCache().setex(cacheKey, VERIFY_CACHE_SECONDS, 'true').catch(() => {