OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
clients: hash secrets with scrypt again and verify both formats
Carl-Gerhard Lindesvärd0e7d6b0c005f
diff --git a/packages/core/src/http/client-auth.ts b/packages/core/src/http/client-auth.tsindex 04951a2a6..905bac04f 100644--- a/packages/core/src/http/client-auth.ts+++ b/packages/core/src/http/client-auth.ts@@ -19,9 +19,9 @@// below. MCP is NOT here — it authenticates its own `token` form inside// `modules/mcp/src/auth.ts`, exactly as V1's mcp router did.-import { verifyPassword } from '@openpanel/shared/server';import type { DbScope } from '../cacheable-per-deps';import type { HttpCtx } from '../context';+import { verifyClientSecret } from '../shared/client-secret';import { headerValue, type IngestHeaders } from '../shared/headers';/** V1 refuses a client id that is not a UUID before it ever queries@@ -205,7 +205,7 @@ async function authenticateAllowedClient(if (options.allow && !options.allow.includes(client.type as ClientType)) {return refuse(FORBIDDEN_TYPE_MESSAGE[label]);}- if (!(await verifyPassword(clientSecret, client.secret))) {+ if (!(await verifyClientSecret(clientSecret, client.secret))) {return refuse(`${label}: Invalid client secret`);}diff --git a/packages/core/src/modules/client/client.service.ts b/packages/core/src/modules/client/client.service.tsindex e6da117a1..7af3b679d 100644--- a/packages/core/src/modules/client/client.service.ts+++ b/packages/core/src/modules/client/client.service.ts@@ -23,7 +23,7 @@ import crypto from 'node:crypto';import type { Client, Prisma } from '@openpanel/db/src/prisma-client';import { cacheablePerDb, type DbScope } from '../../cacheable-per-deps';import type { ServiceDeps, Services } from '../../services';-import { hashPassword } from '../auth/auth.service';+import { hashClientSecret } from '../../shared/client-secret';export type IServiceClient = Client;export type IServiceClientWithProject = Prisma.ClientGetPayload<{@@ -164,20 +164,13 @@ export function createClientService(}const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;- // FIXME(M15-104, reported not fixed): `hashPassword` here is- // auth.service.ts's argon2 one; every verifier- // (ingest/mcp/http client-auth) checks against `@openpanel/shared/server`'s- // scrypt `verifyPassword`. No secret minted through this path can- // currently authenticate. See docs/review/client.md's "Not covered by- // any rule" #1 — fixing the algorithm is a behaviour change, out of this- // task's scope.const client = await deps.db.client.create({data: {organizationId,projectId: input.projectId || null,name: input.name,type: input.type || 'write',- secret: await hashPassword(secret),+ secret: await hashClientSecret(secret),},});diff --git a/packages/core/src/modules/ingest/src/client-auth.ts b/packages/core/src/modules/ingest/src/client-auth.tsindex 7f289c52e..68f0594cc 100644--- a/packages/core/src/modules/ingest/src/client-auth.ts+++ b/packages/core/src/modules/ingest/src/client-auth.ts@@ -14,9 +14,9 @@// ADR-011 leaves to Carl, not to a port.import { getRedisCache } from '@openpanel/redis';-import { verifyPassword } from '@openpanel/shared/server';import { path } from 'ramda';import type { DbScope } from '../../../cacheable-per-deps';+import { verifyClientSecret as verifyClientSecretHash } from '../../../shared/client-secret';import { headerValue, type IngestHeaders } from '../../../shared/headers';import {getClientByIdCached,@@ -124,7 +124,7 @@ async function verifyClientSecret(return true;}- const isVerified = await verifyPassword(clientSecret, storedSecret);+ const isVerified = await verifyClientSecretHash(clientSecret, storedSecret);if (isVerified) {getRedisCache()diff --git a/packages/core/src/modules/mcp/src/auth.ts b/packages/core/src/modules/mcp/src/auth.tsindex e23581023..7d4bae851 100644--- a/packages/core/src/modules/mcp/src/auth.ts+++ b/packages/core/src/modules/mcp/src/auth.ts@@ -1,7 +1,7 @@import { createHash } from 'node:crypto';import { getCache } from '@openpanel/redis';-import { verifyPassword } from '@openpanel/shared/server';import type { ServiceDeps, Services } from '../../../services';+import { verifyClientSecret } from '../../../shared/client-secret';export interface McpAuthContext {/**@@ -111,7 +111,7 @@ export async function authenticateToken(const isVerified = await getCache(cacheKey,60 * 5,- async () => await verifyPassword(clientSecret, client.secret!),+ async () => await verifyClientSecret(clientSecret, client.secret!),true);diff --git a/packages/core/src/modules/onboarding/onboarding.service.ts b/packages/core/src/modules/onboarding/onboarding.service.tsindex 0aff8e479..9727f6bb3 100644--- a/packages/core/src/modules/onboarding/onboarding.service.ts+++ b/packages/core/src/modules/onboarding/onboarding.service.ts@@ -16,8 +16,8 @@ import { stripTrailingSlash } from '@openpanel/shared';import { format } from 'date-fns';import type { Logger } from '../../logger';import type { ServiceDeps, Services } from '../../services';+import { hashClientSecret } from '../../shared/client-secret';import { getId } from '../../slug-id';-import { hashPassword } from '../auth/auth.service';import {runSequence,type SequenceStep,@@ -170,7 +170,7 @@ export async function createOnboardingProject(organizationId: organization.id,projectId: project.id,type: 'write',- secret: await hashPassword(secret),+ secret: await hashClientSecret(secret),},});diff --git a/packages/core/src/modules/project/project.service.ts b/packages/core/src/modules/project/project.service.tsindex 93309d463..a619deec4 100644--- a/packages/core/src/modules/project/project.service.ts+++ b/packages/core/src/modules/project/project.service.ts@@ -34,8 +34,8 @@ import { TRPCBadRequestError } from '../../rpc/errors';import type { ServiceDeps, Services } from '../../services';import { convertClickhouseDateToJs } from '../../shared/ch-dates';import { TABLE_NAMES } from '../../shared/ch-tables';+import { hashClientSecret } from '../../shared/client-secret';import { getId } from '../../slug-id';-import { hashPassword } from '../auth/auth.service';import { getClientByIdCached } from '../client/client.service';// The `sql` tag is a value import of `@openpanel/db` and stays one: it is a@@ -378,7 +378,7 @@ export function createProjectService(organizationId,name: 'First client',type: 'write',- secret: await hashPassword(secret),+ secret: await hashClientSecret(secret),},},},diff --git a/packages/core/src/shared/client-secret.test.ts b/packages/core/src/shared/client-secret.test.tsnew file mode 100644index 000000000..bb6c8518d--- /dev/null+++ b/packages/core/src/shared/client-secret.test.ts@@ -0,0 +1,23 @@+import { describe, expect, it } from 'bun:test';+import { hash as hashArgon2 } from '@node-rs/argon2';+import { hashClientSecret, verifyClientSecret } from './client-secret';++describe('client secrets', () => {+ it('hashes with scrypt and round-trips', async () => {+ const stored = await hashClientSecret('sec_abc');+ expect(stored).toMatch(/^[0-9a-f]{32}\.[0-9a-f]{64}$/);+ expect(await verifyClientSecret('sec_abc', stored)).toBe(true);+ expect(await verifyClientSecret('sec_nope', stored)).toBe(false);+ });++ it('still verifies argon2 rows minted before the fix', async () => {+ const stored = await hashArgon2('sec_abc');+ expect(await verifyClientSecret('sec_abc', stored)).toBe(true);+ expect(await verifyClientSecret('sec_nope', stored)).toBe(false);+ });++ it('treats a malformed stored hash as a failed verification', async () => {+ expect(await verifyClientSecret('sec_abc', 'garbage')).toBe(false);+ expect(await verifyClientSecret('sec_abc', '')).toBe(false);+ });+});diff --git a/packages/core/src/shared/client-secret.ts b/packages/core/src/shared/client-secret.tsnew file mode 100644index 000000000..9a6260b5f--- /dev/null+++ b/packages/core/src/shared/client-secret.ts@@ -0,0 +1,32 @@+import { verify as verifyArgon2 } from '@node-rs/argon2';+import {+ hashPassword as hashScrypt,+ verifyPassword as verifyScrypt,+} from '@openpanel/shared/server';++const ARGON2_PREFIX = '$argon2';++/** Client secrets are scrypt (`salt.hash`), the format V1 and the seed write. */+export function hashClientSecret(secret: string): Promise<string> {+ return hashScrypt(secret);+}++/**+ * Reads both formats: scrypt, and the argon2 rows the ported client, project+ * and onboarding services minted while they imported the user-password hasher+ * by mistake (M15-104). A malformed stored hash is a failed verification,+ * never a 500 on the ingest path.+ */+export async function verifyClientSecret(+ secret: string,+ storedHash: string+): Promise<boolean> {+ try {+ if (storedHash.startsWith(ARGON2_PREFIX)) {+ return await verifyArgon2(storedHash, secret);+ }+ return await verifyScrypt(secret, storedHash);+ } catch {+ return false;+ }+}