Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

client: stop returning the secret hash from every read path

Carl-Gerhard Lindesvärde37d15cc2a2c
diff --git a/apps/start/src/components/onboarding/connect-web.tsx b/apps/start/src/components/onboarding/connect-web.tsx
index c67c40262..4f6d4233d 100644
--- a/apps/start/src/components/onboarding/connect-web.tsx
+++ b/apps/start/src/components/onboarding/connect-web.tsx
@@ -1,4 +1,4 @@
-import type { IServiceClient } from '@openpanel/core';
+import type { IPublicClient } from '@openpanel/core';
import { frameworks } from '@openpanel/sdk-info';
import { CopyIcon, PlugIcon } from 'lucide-react';
import { Button } from '../ui/button';
@@ -8,7 +8,7 @@ import { pushModal } from '@/modals';
import { clipboard } from '@/utils/clipboard';
interface Props {
- client: IServiceClient | null;
+ client: IPublicClient | null;
}
const ConnectWeb = ({ client }: Props) => {
diff --git a/apps/start/src/modals/Instructions.tsx b/apps/start/src/modals/Instructions.tsx
index 941460dcf..0df3ae69a 100644
--- a/apps/start/src/modals/Instructions.tsx
+++ b/apps/start/src/modals/Instructions.tsx
@@ -1,4 +1,4 @@
-import type { IServiceClient } from '@openpanel/core';
+import type { IPublicClient } from '@openpanel/core';
import type { frameworks } from '@openpanel/sdk-info';
import { ExternalLinkIcon, XIcon } from 'lucide-react';
import { popModal } from '.';
@@ -11,7 +11,7 @@ import {
} from '@/components/ui/sheet';
type Props = {
- client: IServiceClient | null;
+ client: IPublicClient | null;
framework: (typeof frameworks)[number];
};
diff --git a/apps/start/src/modals/edit-client.tsx b/apps/start/src/modals/edit-client.tsx
index 923f3ee30..2460baff6 100644
--- a/apps/start/src/modals/edit-client.tsx
+++ b/apps/start/src/modals/edit-client.tsx
@@ -1,5 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod';
-import type { IServiceClient } from '@openpanel/core';
+import type { IPublicClient } from '@openpanel/core';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
@@ -11,7 +11,7 @@ import { InputWithLabel } from '@/components/forms/input-with-label';
import { Button } from '@/components/ui/button';
import { handleError, useTRPC } from '@/integrations/trpc/react';
-type EditClientProps = IServiceClient;
+type EditClientProps = IPublicClient;
const validator = z.object({
id: z.string().min(1),
diff --git a/apps/start/src/routes/_app.$organizationId.$projectId.settings._tabs.tracking.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.settings._tabs.tracking.tsx
index e51e46b31..7e3f33ade 100644
--- a/apps/start/src/routes/_app.$organizationId.$projectId.settings._tabs.tracking.tsx
+++ b/apps/start/src/routes/_app.$organizationId.$projectId.settings._tabs.tracking.tsx
@@ -1,4 +1,4 @@
-import type { IServiceClient } from '@openpanel/core';
+import type { IPublicClient } from '@openpanel/core';
import { frameworks } from '@openpanel/sdk-info';
import { useQuery } from '@tanstack/react-query';
import { createFileRoute } from '@tanstack/react-router';
@@ -27,11 +27,11 @@ function Component() {
}
interface Props {
- clients: IServiceClient[];
+ clients: IPublicClient[];
}
const ConnectWeb = ({ clients }: Props) => {
- const [client, setClient] = useState<IServiceClient | null>(null);
+ const [client, setClient] = useState<IPublicClient | null>(null);
useEffect(() => {
if (!client && clients && clients.length > 0) {
setClient(clients[0]);
diff --git a/apps/start/src/routes/_steps.onboarding.$projectId.connect.tsx b/apps/start/src/routes/_steps.onboarding.$projectId.connect.tsx
index 66371782f..bbb9724af 100644
--- a/apps/start/src/routes/_steps.onboarding.$projectId.connect.tsx
+++ b/apps/start/src/routes/_steps.onboarding.$projectId.connect.tsx
@@ -123,7 +123,7 @@ function Component() {
</div>
</div>
<div className="-mx-4 h-px bg-muted" />
- <ConnectWeb client={{ ...client, secret }} />
+ <ConnectWeb client={client} />
</div>
</div>
<ButtonContainer className="mt-0 flex-shrink-0 border-t bg-background p-4">
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index fccddad7f..218794ef8 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -354,6 +354,7 @@ export { buildFilterWhere } from './modules/chart/src/table-filter-where';
// re-export shim.
export type {
CreatedClient,
+ IPublicClient,
IServiceClient,
IServiceClientWithProject,
} from './modules/client/client.service';
diff --git a/packages/core/src/modules/client/client.service.test.ts b/packages/core/src/modules/client/client.service.test.ts
index 912621607..2925ce3f4 100644
--- a/packages/core/src/modules/client/client.service.test.ts
+++ b/packages/core/src/modules/client/client.service.test.ts
@@ -249,3 +249,57 @@ test('deleteClientForOrganization deletes only when the client belongs to the or
expect(result).toBe(true);
expect(clientStore.has('client_a')).toBe(false);
});
+
+// The stored secret is a hash, and the published docs promise it is never
+// retrievable after creation (docs/api/manage/clients.mdx). The fake Prisma
+// above ignores `omit`, so these assert on what the service ASKED FOR rather
+// than on the rows it got back.
+const OMIT_SECRET = { secret: true };
+
+test('every client read path asks Prisma to omit the secret', async () => {
+ clientStore.set(
+ 'client_1',
+ makeClient({ id: 'client_1', projectId: 'proj_1' })
+ );
+
+ client.findMany.mockClear();
+ await subject.getClientsByProjectId('proj_1');
+ await subject.getClientsByOrganizationId('org_1');
+ await subject.listClientsForOrganization('org_1');
+ expect(client.findMany).toHaveBeenCalledTimes(3);
+ for (const call of client.findMany.mock.calls) {
+ expect(call[0]).toMatchObject({ omit: OMIT_SECRET });
+ }
+
+ client.findFirst.mockClear();
+ await subject.getClientForOrganization('client_1', 'org_1');
+ expect(client.findFirst).toHaveBeenCalledTimes(1);
+ expect(client.findFirst.mock.calls[0]?.[0]).toMatchObject({
+ omit: OMIT_SECRET,
+ });
+});
+
+test('updateClientForOrganization omits the secret from the row it returns', async () => {
+ clientStore.set('client_1', makeClient({ id: 'client_1' }));
+
+ client.update.mockClear();
+ await subject.updateClientForOrganization('client_1', 'org_1', {
+ name: 'renamed',
+ });
+
+ expect(client.update).toHaveBeenCalledTimes(1);
+ expect(client.update.mock.calls[0]?.[0]).toMatchObject({
+ omit: OMIT_SECRET,
+ });
+});
+
+test('getClientById keeps the secret — client authentication verifies against it', async () => {
+ clientStore.set('client_1', makeClient({ id: 'client_1' }));
+
+ client.findUnique.mockClear();
+ const found = await subject.getClientById('client_1');
+
+ expect(client.findUnique).toHaveBeenCalledTimes(1);
+ expect(client.findUnique.mock.calls[0]?.[0]).not.toHaveProperty('omit');
+ expect(found?.secret).toBe('hashed');
+});
diff --git a/packages/core/src/modules/client/client.service.ts b/packages/core/src/modules/client/client.service.ts
index 7af3b679d..d938a4bce 100644
--- a/packages/core/src/modules/client/client.service.ts
+++ b/packages/core/src/modules/client/client.service.ts
@@ -26,6 +26,17 @@ import type { ServiceDeps, Services } from '../../services';
import { hashClientSecret } from '../../shared/client-secret';
export type IServiceClient = Client;
+
+/**
+ * What a read path may return. The stored `secret` is a hash and the docs
+ * promise it is never retrievable after creation
+ * (apps/public/content/docs/api/manage/clients.mdx), so every path but the
+ * one authentication uses drops it.
+ */
+export type IPublicClient = Omit<Client, 'secret'>;
+
+/** Handed to Prisma by every read path that serves a response. */
+const OMIT_SECRET = { secret: true } as const;
export type IServiceClientWithProject = Prisma.ClientGetPayload<{
include: {
project: true;
@@ -49,6 +60,7 @@ export async function getClientsByOrganizationId(
where: {
organizationId,
},
+ omit: OMIT_SECRET,
include: {
project: true,
},
@@ -67,6 +79,7 @@ export async function getClientsByProjectId(
where: {
projectId,
},
+ omit: OMIT_SECRET,
});
}
@@ -118,6 +131,7 @@ export async function listClientsForOrganization(
organizationId,
...(projectId ? { projectId } : {}),
},
+ omit: OMIT_SECRET,
orderBy: {
createdAt: 'desc',
},
@@ -134,6 +148,7 @@ export async function getClientForOrganization(
id,
organizationId,
},
+ omit: OMIT_SECRET,
});
}
@@ -183,9 +198,10 @@ export function createClientService(
id: string,
organizationId: string,
input: { name?: string }
- ): Promise<IServiceClient | null> {
+ ): Promise<IPublicClient | null> {
const existing = await deps.db.client.findFirst({
where: { id, organizationId },
+ select: { id: true },
});
if (!existing) {
@@ -200,6 +216,7 @@ export function createClientService(
const client = await deps.db.client.update({
where: { id },
data: updateData,
+ omit: OMIT_SECRET,
});
await getClientByIdCached.clear(deps, client.id);
diff --git a/packages/core/src/modules/project/project.service.ts b/packages/core/src/modules/project/project.service.ts
index a619deec4..5b463048c 100644
--- a/packages/core/src/modules/project/project.service.ts
+++ b/packages/core/src/modules/project/project.service.ts
@@ -56,7 +56,7 @@ const NON_TRACKING_EVENT_NAMES = ['session_start', 'session_end'];
export type IServiceProject = Project;
export type IServiceProjectWithClients = Prisma.ProjectGetPayload<{
include: {
- clients: true;
+ clients: { omit: { secret: true } };
};
}>;
@@ -95,7 +95,8 @@ export async function getProjectWithClients(deps: ServiceDeps, id: string) {
id,
},
include: {
- clients: true,
+ // The stored secret is a hash and is never retrievable after creation.
+ clients: { omit: { secret: true } },
},
});