OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
integrations: redact every integration read path, harden the webhook validator, cheaper filter values
Carl-Gerhard Lindesvärd071c736c4539
diff --git a/apps/start/src/hooks/use-property-values.ts b/apps/start/src/hooks/use-property-values.tsindex 8cbf8a16c..74f527943 100644--- a/apps/start/src/hooks/use-property-values.ts+++ b/apps/start/src/hooks/use-property-values.ts@@ -1,16 +1,24 @@+import { useQuery } from '@tanstack/react-query';import { useTRPC } from '@/integrations/trpc/react';import type { RouterInputs } from '@/trpc/client';-import { useQuery } from '@tanstack/react-query';++const PROPERTY_VALUES_STALE_TIME_MS = 60 * 60 * 1000;export function usePropertyValues(- params: RouterInputs['chart']['values'] & { enabled?: boolean },+ params: RouterInputs['chart']['values'] & { enabled?: boolean }) {const trpc = useTRPC();const { enabled = true, ...input } = params;const query = useQuery(trpc.chart.values.queryOptions(input, {enabled: enabled !== false && !!input.projectId,- }),+ // A filter-value dropdown doesn't need real-time data. Without a+ // staleTime, React Query refetches on every window focus/remount — a tab+ // left open on a filter re-fires the (slow, un-MV'd) values query and+ // hammers ClickHouse. Cache 1h + no focus refetch.+ staleTime: PROPERTY_VALUES_STALE_TIME_MS,+ refetchOnWindowFocus: false,+ }));return query.data?.values ?? [];}diff --git a/packages/core/src/modules/chart/chart.service.ts b/packages/core/src/modules/chart/chart.service.tsindex 582080d66..1cdd2412d 100644--- a/packages/core/src/modules/chart/chart.service.ts+++ b/packages/core/src/modules/chart/chart.service.ts@@ -65,10 +65,10 @@ import {isKnownEventField,normalizeEventField,} from './src/field-resolution';+import { eventFieldValuesQuery } from './src/field-values.sql';import { runQuery } from './src/run-query';import {chartBucketProfilesQuery,- eventFieldValuesQuery,eventNamesWithCountQuery,eventPropertyKeysQuery,eventPropertyValuesQuery,@@ -444,11 +444,12 @@ export async function getChartPropertyValues(}const rows = await runQuery<{ values: string[] }>(deps,- eventFieldValuesQuery(+ eventFieldValuesQuery({projectId,- getSelectPropertyKey(resolvedProperty),- event- )+ column: resolvedProperty,+ selectExpression: getSelectPropertyKey(resolvedProperty),+ event,+ }));return {values: pipe(diff --git a/packages/core/src/modules/chart/src/field-values.sql.test.ts b/packages/core/src/modules/chart/src/field-values.sql.test.tsnew file mode 100644index 000000000..21520b4f5--- /dev/null+++ b/packages/core/src/modules/chart/src/field-values.sql.test.ts@@ -0,0 +1,70 @@+// Same strategy as ./sql.test.ts: render, then EXPLAIN against the isolated+// openpanel_test schema so a column that exists on `events` but not on+// `sessions` (or vice versa) fails here, not in production.++import { beforeAll, describe, expect, it } from 'bun:test';+import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';++let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;+let eventFieldValuesQuery: typeof import('./field-values.sql').eventFieldValuesQuery;+let getSelectPropertyKey: typeof import('./field-resolution').getSelectPropertyKey;++const PROJECT_ID = 'test-field-values';++function render(fragment: SqlFragment) {+ const { query, query_params } = fragment.toStatement();+ return { sql: query, params: query_params };+}++async function explain(fragment: SqlFragment): Promise<string> {+ const { sql, params } = render(fragment);+ await ch.command({ query: `EXPLAIN ${sql}`, query_params: params });+ return sql;+}++function query(event: string, column: string, lookbackDays?: number) {+ return eventFieldValuesQuery({+ projectId: PROJECT_ID,+ column,+ selectExpression: getSelectPropertyKey(column),+ event,+ lookbackDays,+ });+}++beforeAll(async () => {+ ({ ch } = await import('@openpanel/db/src/clickhouse/client'));+ ({ eventFieldValuesQuery } = await import('./field-values.sql'));+ ({ getSelectPropertyKey } = await import('./field-resolution'));+ const { bootstrapTestDatabases } = await import(+ '../../../../../../test/bootstrap-databases'+ );+ await bootstrapTestDatabases();+}, 30_000);++describe('eventFieldValuesQuery', () => {+ it('reads a session-level column for all events from `sessions`', async () => {+ const sql = await explain(query('*', 'country'));+ expect(sql).toContain('FROM sessions');+ expect(sql).not.toContain('name =');+ });++ it('stays on `events` for a specific event: `sessions` has no name column', async () => {+ const sql = await explain(query('screen_view', 'country'));+ expect(sql).toContain('FROM events');+ expect(sql).toContain('AND name = {');+ });++ it('stays on `events` for a per-pageview column', async () => {+ const sql = await explain(query('*', 'path'));+ expect(sql).toContain('FROM events');+ });++ it('clamps the lookback to a bound day count instead of a 6-month scan', () => {+ const { sql, params } = render(query('*', 'os'));+ expect(sql).toContain('toIntervalDay({p2:UInt32})');+ expect(params.p2).toBe(30);+ expect(sql).not.toContain('MONTH');+ expect(render(query('*', 'os', 7)).params.p2).toBe(7);+ });+});diff --git a/packages/core/src/modules/chart/src/field-values.sql.ts b/packages/core/src/modules/chart/src/field-values.sql.tsnew file mode 100644index 000000000..095fe0fa5--- /dev/null+++ b/packages/core/src/modules/chart/src/field-values.sql.ts@@ -0,0 +1,59 @@+// The filter-value dropdown for a top-level event column (country, os, city,+// device…) has no MV. Scanning `events` for it was a full multi-billion-row+// read on high-volume projects that hit max_execution_time and returned+// nothing. Session-level columns (geo/device/referrer, denormalised onto every+// event) come from the far smaller `sessions` table instead — same distinct+// set, one row per session. `path`/`origin` are per-pageview and stay on+// `events`; so does a specific-event query, since `sessions` has no `name`.++import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';+import { CHART_TABLE } from './field-resolution';+import { PROPERTY_VALUES_LIMIT } from './sql';++export const SESSION_LEVEL_VALUE_COLUMNS: ReadonlySet<string> = new Set([+ 'country',+ 'region',+ 'city',+ 'os',+ 'os_version',+ 'browser',+ 'browser_version',+ 'device',+ 'brand',+ 'model',+ 'referrer',+ 'referrer_name',+ 'referrer_type',+]);++/**+ * A value picker only needs recently-seen values, so the `events` fallback is+ * clamped to this window instead of the old 6-month full scan.+ */+export const EVENT_FIELD_VALUES_LOOKBACK_DAYS = 30;++export function readsEventFieldValuesFromSessions(+ event: string,+ column: string+): boolean {+ return event === '*' && SESSION_LEVEL_VALUE_COLUMNS.has(column);+}++export function eventFieldValuesQuery(input: {+ projectId: string;+ column: string;+ selectExpression: SqlFragment;+ event: string;+ lookbackDays?: number;+}): SqlFragment {+ const { projectId, column, selectExpression, event } = input;+ const lookbackDays = input.lookbackDays ?? EVENT_FIELD_VALUES_LOOKBACK_DAYS;+ const fromSessions = readsEventFieldValuesFromSessions(event, column);+ const table = fromSessions ? CHART_TABLE.sessions : CHART_TABLE.events;+ // V1 only skipped the name clause for `*` — an empty event name filters on ''.+ const eventName =+ fromSessions || event === '*'+ ? sql.empty+ : sql` AND name = ${sql.string(event)}`;+ return sql`SELECT distinct ${selectExpression} as values FROM ${sql.id(table)} WHERE project_id = ${sql.string(projectId)} AND created_at > (now() - toIntervalDay(${sql.param('UInt32', lookbackDays)}))${eventName} ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;+}diff --git a/packages/core/src/modules/integration/integration.service.test.ts b/packages/core/src/modules/integration/integration.service.test.tsnew file mode 100644index 000000000..27e946f8c--- /dev/null+++ b/packages/core/src/modules/integration/integration.service.test.ts@@ -0,0 +1,223 @@+// Credentials are write-only (see src/registry.ts): every path that hands an+// integration row to a client must go through `redactIntegration`. These pin+// the read paths AND the mutation return values — a create/update that echoes+// the stored row is how a webhook's Authorization header reached the client.+//+// Access is a real `getAccessChecks()` over faked lookups, the same seam+// auth.service.access.test.ts uses; storage is an in-memory fake `deps.db`.++import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';++interface FakeIntegration {+ id: string;+ name: string;+ projectId: string | null;+ organizationId: string;+ config: Record<string, unknown>;+}++const integrationStore = new Map<string, FakeIntegration>();++let projectAccessLevel: 'read' | 'write' | 'admin' | null = 'write';++const realAccessLookups = { ...(await import('../../shared/access-lookups')) };+mock.module('../../shared/access-lookups', () => ({+ ...realAccessLookups,+ getProjectAccess: mock(async () =>+ projectAccessLevel ? { level: projectAccessLevel } : null+ ),+ getOrganizationAccess: mock(async () => ({ role: 'org:member' })),+ canWriteProject: (access: { level: string } | null) =>+ !!access && (access.level === 'write' || access.level === 'admin'),+ getProjectById: mock(async () => ({ organizationId: 'org_1' })),+}));++const integration = {+ findUniqueOrThrow: mock(+ async ({ where: { id } }: { where: { id: string } }) => {+ const found = integrationStore.get(id);+ if (!found) {+ throw new Error(`integration ${id} not found`);+ }+ return found;+ }+ ),+ findMany: mock(async () => [...integrationStore.values()]),+ create: mock(async ({ data }: { data: Omit<FakeIntegration, 'id'> }) => {+ const row = { id: `int_${integrationStore.size + 1}`, ...data };+ integrationStore.set(row.id, row);+ return row;+ }),+ update: mock(+ async ({+ where: { id },+ data,+ }: {+ where: { id: string };+ data: Partial<FakeIntegration>;+ }) => {+ const existing = integrationStore.get(id);+ if (!existing) {+ throw new Error(`integration ${id} not found`);+ }+ const next = { ...existing, ...data };+ integrationStore.set(id, next);+ return next;+ }+ ),+ delete: mock(async ({ where: { id } }: { where: { id: string } }) => {+ const existing = integrationStore.get(id);+ integrationStore.delete(id);+ return existing;+ }),+};++const project = {+ findUniqueOrThrow: mock(async () => ({ organizationId: 'org_1' })),+};++const deps = {+ db: { integration, project },+ config: testCoreConfig(),+} as unknown as import('../../services').ServiceDeps;++let subject: typeof import('./integration.service');+let resetAccessChecksForTests: typeof import('../auth/auth.service').resetAccessChecksForTests;+beforeAll(async () => {+ ({ resetAccessChecksForTests } = await import('../auth/auth.service'));+ resetAccessChecksForTests();+ subject = await import('./integration.service');+});++afterAll(() => {+ mock.module('../../shared/access-lookups', () => realAccessLookups);+ resetAccessChecksForTests();+});++beforeEach(() => {+ integrationStore.clear();+ projectAccessLevel = 'write';+});++const WEBHOOK_HEADERS = {+ Authorization: 'Bearer very-secret',+ 'X-Trace': 'not-secret-but-a-header-value',+};++function storeWebhook(id = 'int_1'): FakeIntegration {+ const row: FakeIntegration = {+ id,+ name: 'Hook',+ projectId: 'proj_1',+ organizationId: 'org_1',+ config: {+ type: 'webhook',+ url: 'https://example.com/hook',+ headers: WEBHOOK_HEADERS,+ mode: 'message',+ },+ };+ integrationStore.set(id, row);+ return row;+}++function storeSlack(id = 'int_slack'): FakeIntegration {+ const row: FakeIntegration = {+ id,+ name: 'Slack',+ projectId: 'proj_1',+ organizationId: 'org_1',+ config: {+ type: 'slack',+ access_token: 'xoxb-secret',+ incoming_webhook: { url: 'https://hooks.slack.com/T/B/secret' },+ },+ };+ integrationStore.set(id, row);+ return row;+}++test('list returns no credential values to a read-level project member', async () => {+ storeWebhook();+ storeSlack();+ projectAccessLevel = 'read';++ const rows = await subject.listIntegrationsForProject(deps, 'u1', 'proj_1');+ const serialized = JSON.stringify(rows);++ expect(serialized).not.toContain('very-secret');+ expect(serialized).not.toContain('xoxb-secret');+ expect(serialized).not.toContain('/T/B/secret');+ // Non-secret config the dashboard renders survives; header KEYS stay so the+ // form shows which headers are configured.+ const hook = rows.find((row) => row.id === 'int_1');+ expect(hook?.config).toMatchObject({+ type: 'webhook',+ url: 'https://example.com/hook',+ headers: { Authorization: '', 'X-Trace': '' },+ });+});++test('get returns no credential values', async () => {+ storeSlack();+ projectAccessLevel = 'read';++ const row = await subject.getIntegrationById(deps, 'u1', 'int_slack');++ expect(JSON.stringify(row)).not.toContain('xoxb-secret');+ expect(row.config).toMatchObject({ type: 'slack', access_token: '' });+});++test('createOrUpdate does not echo the submitted header values back', async () => {+ const row = await subject.upsertIntegration(deps, 'u1', {+ name: 'Hook',+ projectId: 'proj_1',+ config: {+ type: 'webhook',+ url: 'https://example.com/hook',+ headers: WEBHOOK_HEADERS,+ mode: 'message',+ },+ });++ expect(JSON.stringify(row)).not.toContain('very-secret');+ expect(row.config).toMatchObject({+ headers: { Authorization: '', 'X-Trace': '' },+ });+ // ...while the stored row keeps them for delivery.+ expect(integrationStore.get(row.id)?.config).toMatchObject({+ headers: WEBHOOK_HEADERS,+ });+});++test('createOrUpdate keeps stored header values when the form submits them blank, without echoing them', async () => {+ storeWebhook();++ const row = await subject.upsertIntegration(deps, 'u1', {+ id: 'int_1',+ name: 'Hook renamed',+ projectId: 'proj_1',+ config: {+ type: 'webhook',+ url: 'https://example.com/hook',+ headers: { Authorization: '', 'X-Trace': '' },+ mode: 'message',+ },+ });++ expect(row.name).toBe('Hook renamed');+ expect(JSON.stringify(row)).not.toContain('very-secret');+ expect(integrationStore.get('int_1')?.config).toMatchObject({+ headers: WEBHOOK_HEADERS,+ });+});++test('delete returns only the id, not the deleted row', async () => {+ storeWebhook();++ const result = await subject.deleteIntegration(deps, 'u1', 'int_1');++ expect(result).toEqual({ id: 'int_1' });+ expect(integrationStore.has('int_1')).toBe(false);+});diff --git a/packages/core/src/modules/integration/integration.service.ts b/packages/core/src/modules/integration/integration.service.tsindex ea8dcd831..da6e3550c 100644--- a/packages/core/src/modules/integration/integration.service.ts+++ b/packages/core/src/modules/integration/integration.service.ts@@ -34,7 +34,7 @@ import {findEncryptedSecretField,findMissingSecretFields,getServerIntegration,- redactConfigSecrets,+ redactIntegration,} from './src/registry';import { safeWebhookFetcher } from './src/safe-fetcher';import {@@ -44,18 +44,6 @@ import {} from './src/slack';import { zSlackAuthResponse } from './src/slack-contract';-// Credentials are write-only: they are encrypted at rest and never travel back-// to a client. `read` on a project is bare membership, so returning the stored-// ciphertext would hand every project member the org's object-store keys — and-// because `decryptCredential` accepts any `enc:` blob under the single global-// key, that ciphertext is a replayable bearer token, not an opaque handle.-function redactIntegration<T extends { config: unknown }>(integration: T): T {- const config = redactConfigSecrets(integration.config);- return config === integration.config- ? integration- : { ...integration, config };-}-// A client never legitimately holds a ciphertext (see redactIntegration), so// one arriving on the wire is an attempt to replay a secret lifted from another// integration into an attacker-chosen destination.@@ -244,20 +232,24 @@ export async function upsertIntegration(const config = encryptConfigSecrets(deps.config.encryptionKey, submitted);- if (input.id) {- return db.integration.update({- where: { id: input.id, organizationId },- data: { name: input.name, config },- });- }- return db.integration.create({- data: {- name: input.name,- organizationId,- projectId: input.projectId,- config,- },- });+ // The stored row holds more than the caller sent: secrets carried over from+ // the previous config, and the encrypted ones as ciphertext. Neither may go+ // back out.+ const row = input.id+ ? await db.integration.update({+ where: { id: input.id, organizationId },+ data: { name: input.name, config },+ })+ : await db.integration.create({+ data: {+ name: input.name,+ organizationId,+ projectId: input.projectId,+ config,+ },+ });++ return redactIntegration(row);}export async function createOrUpdateSlackIntegration(@@ -393,9 +385,13 @@ export async function deleteIntegration(await assertIntegrationAccess(userId, integration, 'write');- return db.integration.delete({+ await db.integration.delete({where: { id },});++ // The deleted row still carries its config; the client only needs to know+ // which id is gone.+ return { id };}// -- Slack OAuth callback -----------------------------------------------diff --git a/packages/core/src/modules/integration/src/registry.ts b/packages/core/src/modules/integration/src/registry.tsindex edd23a1c5..750349b70 100644--- a/packages/core/src/modules/integration/src/registry.ts+++ b/packages/core/src/modules/integration/src/registry.ts@@ -412,6 +412,26 @@ export function redactConfigSecrets<C>(config: C): C {return mapSecrets(config, (value) => (value === '' ? undefined : ''));}+// Credentials are write-only: they are encrypted at rest and never travel back+// to a client. `read` on a project is bare membership, so returning the stored+// ciphertext would hand every project member the org's object-store keys — and+// because `decryptCredential` accepts any `enc:` blob under the single global+// key, that ciphertext is a replayable bearer token, not an opaque handle.+//+// Every function that returns an integration row — here and in the+// notification service, which embeds the rows attached to each rule — goes+// through this. Returning a row straight from Prisma is how the Slack bot+// token, the Slack incoming-webhook url and webhook header values reached any+// client that could list notification rules.+export function redactIntegration<T extends { config: unknown }>(+ integration: T+): T {+ const config = redactConfigSecrets(integration.config);+ return config === integration.config+ ? integration+ : { ...integration, config };+}+/*** Restore secrets the client left blank from the stored row, so an edit that* doesn't retype them keeps working now that reads are redacted. Record valuesdiff --git a/packages/core/src/modules/notification/notification.service.test.ts b/packages/core/src/modules/notification/notification.service.test.tsindex aa56b3a0e..32f218193 100644--- a/packages/core/src/modules/notification/notification.service.test.ts+++ b/packages/core/src/modules/notification/notification.service.test.ts@@ -268,6 +268,51 @@ test('listNotificationRules merges the app/email pseudo-integrations onto real oexpect(result[0]?.integrations.map((i) => i.id)).toEqual(['app', 'slack_1']);});+test('listNotificationRules returns no credential values from the attached integrations', async () => {+ ruleStore.set(+ 'rule_1',+ makeRule({+ id: 'rule_1',+ integrations: [+ {+ id: 'slack_1',+ name: 'Slack',+ config: {+ type: 'slack',+ access_token: 'xoxb-secret',+ incoming_webhook: { url: 'https://hooks.slack.com/T/B/secret' },+ },+ } as never,+ {+ id: 'hook_1',+ name: 'Hook',+ config: {+ type: 'webhook',+ url: 'https://example.com/hook',+ headers: { Authorization: 'Bearer very-secret' },+ },+ } as never,+ ],+ })+ );++ const result = await subject.listNotificationRules(deps, 'proj_1');+ const serialized = JSON.stringify(result);++ expect(serialized).not.toContain('xoxb-secret');+ expect(serialized).not.toContain('/T/B/secret');+ expect(serialized).not.toContain('very-secret');+ // What the rule card reads survives.+ expect(result[0]?.integrations.map((i) => i.id)).toEqual([+ 'slack_1',+ 'hook_1',+ ]);+ expect(result[0]?.integrations[1]).toMatchObject({+ name: 'Hook',+ config: { type: 'webhook', headers: { Authorization: '' } },+ });+});+test('createOrUpdateNotificationRule rejects an integration from another project', async () => {integrationStore.set('slack_1', {id: 'slack_1',diff --git a/packages/core/src/modules/notification/notification.service.ts b/packages/core/src/modules/notification/notification.service.tsindex 64b66cf97..dd9e2e683 100644--- a/packages/core/src/modules/notification/notification.service.ts+++ b/packages/core/src/modules/notification/notification.service.ts@@ -30,12 +30,14 @@ import {type IIntegrationConfig,isKind,} from '../integration/integration.constants';-import { getServerIntegration } from '../integration/src/registry';+import {+ getServerIntegration,+ redactIntegration,+} from '../integration/src/registry';import type {IChartEvent,IChartEventFilter,} from '../report/report.constants';-import type { ICreateNotificationRule } from './notification.constants';export const APP_NOTIFICATION_INTEGRATION_ID = 'app';@@ -405,7 +407,9 @@ export async function listNotificationRules((integration.id === EMAIL_NOTIFICATION_INTEGRATION_ID &&rule.sendToEmail)),- ...rule.integrations,+ // The attached rows carry the credentials the worker delivers with;+ // the dashboard only reads id, name and config.type.+ ...rule.integrations.map(redactIntegration),],}));}diff --git a/packages/core/src/shared/js-runtime/execute.ts b/packages/core/src/shared/js-runtime/execute.tsindex 3999b59c2..aa6ef4116 100644--- a/packages/core/src/shared/js-runtime/execute.ts+++ b/packages/core/src/shared/js-runtime/execute.ts@@ -1,3 +1,5 @@+import { validate } from './validate';+/*** Executes a JavaScript function template* @param code - JavaScript function code (arrow function or function expression)@@ -8,6 +10,14 @@ export function execute(code: string,payload: Record<string, unknown>): unknown {+ // Templates are checked when they are saved, but the stored string is what+ // ends up in new Function() here. Check it again at run time rather than+ // trusting whatever passed validation at save time.+ const validation = validate(code);+ if (!validation.valid) {+ throw new Error(`Invalid JavaScript template: ${validation.error}`);+ }+try {// Create the function code that will be executed// 'use strict' ensures 'this' is undefined (not global object)diff --git a/packages/core/src/shared/js-runtime/validate.test.ts b/packages/core/src/shared/js-runtime/validate.test.tsindex d20e684ef..53649592a 100644--- a/packages/core/src/shared/js-runtime/validate.test.ts+++ b/packages/core/src/shared/js-runtime/validate.test.ts@@ -139,6 +139,99 @@ describe('validate', () => {});});+ describe('Computed and indirect access', () => {+ it('should block a computed call target', () => {+ const result = validate(+ `(payload) => payload['constructor']['constructor']('return 1')()`+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('Computed property access');+ });++ it('should block a computed call target reached by optional chaining', () => {+ const result = validate(+ `(payload) => payload?.['constructor']['constructor']('return 1')()`+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('Computed property access');+ });++ it('should block a computed key written with escape sequences', () => {+ const result = validate(+ `(payload) => payload['\\u0063onstructor']['constructor']('return 1')()`+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('Computed property access');+ });++ it('should block a computed call target on a nested value', () => {+ const result = validate(`(payload) => payload.properties['toString']()`);+ expect(result.valid).toBe(false);+ expect(result.error).toContain('Computed property access');+ });++ it("should block 'new' on a non-identifier callee", () => {+ const result = validate(+ `(payload) => new (payload['constructor']['constructor'])('return 1')`+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain("target of 'new'");+ });++ it("should block 'new' on a member expression", () => {+ const result = validate('(payload) => new payload.Thing()');+ expect(result.valid).toBe(false);+ expect(result.error).toContain("target of 'new'");+ });++ it('should block dynamic import()', () => {+ const result = validate(`(payload) => import('node:fs')`);+ expect(result.valid).toBe(false);+ expect(result.error).toContain('Dynamic import()');+ });+ });++ describe('Prototype writes', () => {+ it('should block writing through __proto__', () => {+ const result = validate(+ '(payload) => { payload.__proto__.x = 1; return payload; }'+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('__proto__');+ });++ it('should block writing through a computed __proto__ key', () => {+ const result = validate(+ `(payload) => { payload['__proto__'].x = 1; return payload; }`+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('__proto__');+ });++ it('should block writing to a prototype', () => {+ const result = validate(+ '(payload) => { payload.constructor.prototype.x = 1; return payload; }'+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('not allowed');+ });++ it('should block writing through a no-substitution template literal key', () => {+ const result = validate(+ '(payload) => { payload[`__proto__`].x = 1; return payload; }'+ );+ expect(result.valid).toBe(false);+ expect(result.error).toContain('__proto__');+ });++ it('should still allow writing an ordinary property', () => {+ const result = validate(+ '(payload) => { const out = {}; out.event = payload.name; return out; }'+ );+ expect(result.valid).toBe(true);+ });+ });+describe('Invalid syntax', () => {it('should reject non-function code', () => {const result = validate('const x = 1;');@@ -328,5 +421,14 @@ describe('execute', () => {execute(code, basePayload);}).toThrow('Error executing JavaScript template');});++ it('should refuse to run a stored template that fails validation', () => {+ // A template that was persisted at some point but does not pass+ // validation must not reach new Function().+ const code = `(payload) => payload['constructor']['constructor']('return 1')()`;+ expect(() => {+ execute(code, basePayload);+ }).toThrow('Invalid JavaScript template');+ });});});diff --git a/packages/core/src/shared/js-runtime/validate.ts b/packages/core/src/shared/js-runtime/validate.tsindex 07b542852..817e65503 100644--- a/packages/core/src/shared/js-runtime/validate.ts+++ b/packages/core/src/shared/js-runtime/validate.ts@@ -10,6 +10,74 @@ import {ALLOWED_METHODS,} from './constants';+/**+ * Property names that must never be written through. Assigning to any of+ * these reaches objects shared with the rest of the worker process.+ */+const FORBIDDEN_WRITE_PROPERTIES = new Set([+ '__proto__',+ 'constructor',+ 'prototype',+]);++/**+ * The static name of a member expression's property, or undefined when the key+ * is only known at run time (obj[someVariable]).+ */+function staticPropertyName(+ member: Record<string, unknown>+): string | undefined {+ const prop = member.property as Record<string, unknown> | undefined;+ if (!prop) {+ return undefined;+ }+ if (member.computed) {+ // A literal key can be resolved. Babel has already decoded any \u / \x+ // escapes into StringLiteral.value by this point. A template literal+ // with no substitutions (`__proto__`) is just as static as a string+ // literal and must resolve the same way, or it walks past this check+ // unseen.+ if (prop.type === 'StringLiteral') {+ return prop.value as string;+ }+ if (prop.type === 'TemplateLiteral') {+ const expressions = prop.expressions as unknown[];+ const quasis = prop.quasis as Record<string, unknown>[];+ if (expressions.length === 0 && quasis.length === 1) {+ const value = quasis[0]!.value as Record<string, unknown>;+ return value.cooked as string;+ }+ }+ return undefined;+ }+ return prop.type === 'Identifier' ? (prop.name as string) : undefined;+}++/**+ * Walk an assignment target back down its member chain and return the first+ * forbidden property name it passes through, if any. payload.__proto__.x is a+ * write to 'x' but goes through '__proto__', so the whole chain matters.+ */+function forbiddenPropertyInChain(+ target: Record<string, unknown>+): string | undefined {+ let current: Record<string, unknown> | undefined = target;++ while (+ current &&+ (current.type === 'MemberExpression' ||+ current.type === 'OptionalMemberExpression')+ ) {+ const name = staticPropertyName(current);+ if (name && FORBIDDEN_WRITE_PROPERTIES.has(name)) {+ return name;+ }+ current = current.object as Record<string, unknown> | undefined;+ }++ return undefined;+}+/*** Validates that a JavaScript function is safe to execute* by checking the AST for allowed operations only (allowlist approach)@@ -106,6 +174,13 @@ export function validate(code: string): {return;}+ // Block dynamic import(). @babel/parser emits 'Import' as the callee of+ // the surrounding CallExpression; 'ImportExpression' is the ESTree shape.+ if (node.type === 'Import' || node.type === 'ImportExpression') {+ validationError = 'Dynamic import() is not allowed';+ return;+ }+// Block function declarations inside the function body// (FunctionDeclaration creates a named function, not allowed)if (node.type === 'FunctionDeclaration') {@@ -211,12 +286,17 @@ export function validate(code: string): {const prop = callee.property as Record<string, unknown>;const computed = callee.computed as boolean;+ // A computed key (obj[expr]()) cannot be matched against the+ // allowlist, because the property name is only known at run time.+ // Refuse it rather than letting it past the checks below unseen.+ if (computed) {+ validationError =+ 'Computed property access on a call target is not allowed. Use a literal method name, e.g. value.toUpperCase().';+ return;+ }+// Static method call on global object: Math.random(), JSON.parse()- if (- obj.type === 'Identifier' &&- prop.type === 'Identifier' &&- !computed- ) {+ if (obj.type === 'Identifier' && prop.type === 'Identifier') {const objName = obj.name as string;const methodName = prop.name as string;@@ -233,7 +313,7 @@ export function validate(code: string): {// Instance method call: arr.map(), str.toLowerCase(), arr?.map()// We allow these if the method name is in ALLOWED_INSTANCE_METHODS- if (prop.type === 'Identifier' && !computed) {+ if (prop.type === 'Identifier') {const methodName = prop.name as string;// If calling on something other than an allowed global,@@ -253,12 +333,35 @@ export function validate(code: string): {// Check 'new' expressions - only allow new Date()if (node.type === 'NewExpression') {const callee = node.callee as Record<string, unknown>;- if (callee.type === 'Identifier') {- const name = callee.name as string;- if (name !== 'Date') {- validationError = `'new ${name}()' is not allowed. Only 'new Date()' is permitted.`;- return;- }++ // Anything other than a bare identifier (a member expression, a+ // parenthesised expression, another call) names a constructor we+ // cannot resolve, so it can never be the Date we allow.+ if (callee.type !== 'Identifier') {+ validationError =+ "The target of 'new' must be a plain identifier. Only 'new Date()' is permitted.";+ return;+ }++ const name = callee.name as string;+ if (name !== 'Date') {+ validationError = `'new ${name}()' is not allowed. Only 'new Date()' is permitted.`;+ return;+ }+ }++ // Block writes that reach the prototype chain: payload.__proto__.x = 1,+ // payload['constructor'].prototype.y = 2. Reading these is already+ // handled by the call and 'new' checks above.+ if (+ node.type === 'AssignmentExpression' ||+ node.type === 'UpdateExpression'+ ) {+ const target = (node.left ?? node.argument) as Record<string, unknown>;+ const reached = forbiddenPropertyInChain(target);+ if (reached) {+ validationError = `Assigning through '${reached}' is not allowed.`;+ return;}}});