OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
profile, cohort and funnel: allowlist the columns those statements reach
Carl-Gerhard Lindesvärdf3e9ae4e1e02
diff --git a/packages/core/src/modules/chart/src/funnel.sql.ts b/packages/core/src/modules/chart/src/funnel.sql.tsindex 398f373f9..0135d44ee 100644--- a/packages/core/src/modules/chart/src/funnel.sql.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.ts@@ -51,6 +51,9 @@ export type FunnelGroup = (typeof FUNNEL_GROUPS)[number];/** Display label for null/empty breakdown values (e.g. property not set). */export const EMPTY_BREAKDOWN_LABEL = 'Not set';+/** What the profiles join may select: the joinable columns plus the key. */+const FUNNEL_JOIN_COLUMNS = [...JOINABLE_PROFILE_COLUMNS, 'id'];+/** The funnel CTE's events alias — `getSelectPropertyKey` needs the same one. */const EVENTS_ALIAS = 'events';@@ -184,22 +187,20 @@ function breakdownSelects(* instead of every profile's whole properties Map: the join hash was carrying* ~1KB of Map per profile and OOMing at scale.*/-function profileCteColumns(- input: FunnelBaseInput,+/**+ * The profiles columns this join may select, from the filter and breakdown+ * names the caller supplied. Both are user input, so both are matched against+ * the allowlist — V1 split the filter names raw and spliced the result+ * (main #512, GHSA-pc3q-gw7f-p2x2).+ */+export function profileJoinFields(profileFilters: string[],- profileKeys: { keys: string[]; needsFullMap: boolean }-): SqlFragment[] {- const profileBreakdowns = input.breakdowns.filter((breakdown) =>- breakdown.name.startsWith('profile.')- );- if (profileFilters.length === 0 && profileBreakdowns.length === 0) {- return [];- }-+ profileBreakdowns: { name: string }[]+): string[] {const fields = new Set<string>(['id']);for (const filter of profileFilters) {- const fieldName = filter.split('.')[0] as string;- if (fieldName !== 'properties') {+ const fieldName = filter.split('.')[0];+ if (fieldName && JOINABLE_PROFILE_COLUMNS.includes(fieldName)) {fields.add(fieldName);}}@@ -209,12 +210,29 @@ function profileCteColumns(fields.add(fieldName);}}+ return Array.from(fields);+}++function profileCteColumns(+ input: FunnelBaseInput,+ profileFilters: string[],+ profileKeys: { keys: string[]; needsFullMap: boolean }+): SqlFragment[] {+ const profileBreakdowns = input.breakdowns.filter((breakdown) =>+ breakdown.name.startsWith('profile.')+ );+ if (profileFilters.length === 0 && profileBreakdowns.length === 0) {+ return [];+ }++ const fields = new Set(profileJoinFields(profileFilters, profileBreakdowns));- // ADR-013 R3: the breakdown-derived names are vetted above, the- // filter-derived ones are not — V1 spliced both unchecked, `sql.id`- // validates the shape and throws instead of inlining.+ // Both the breakdown- and the filter-derived names are vetted above; the+ // allowlist here is the backstop that makes `sql.id` throw rather than+ // inline if either loop ever lets something else through (main #512,+ // GHSA-pc3q-gw7f-p2x2). V1 spliced both unchecked.const columns: SqlFragment[] = Array.from(fields).map((field) =>- sql.id(field)+ sql.id(field, FUNNEL_JOIN_COLUMNS));const referencesProperties =profileFilters.some((filter) => filter.startsWith('properties')) ||diff --git a/packages/core/src/modules/chart/src/profile-columns.test.ts b/packages/core/src/modules/chart/src/profile-columns.test.tsnew file mode 100644index 000000000..b668f787f--- /dev/null+++ b/packages/core/src/modules/chart/src/profile-columns.test.ts@@ -0,0 +1,42 @@+// Ported from packages/db/src/services/profile-columns.test.ts (main #512).+// The JOIN column list used to be built by splitting raw `profile.*` filter+// names, so a crafted name could close the subquery (GHSA-pc3q-gw7f-p2x2).+// The rewrite binds every identifier through `sql.id`, so injection is+// impossible; what these assert is that the list is also an allowlist, so a+// filter cannot name a column it has no business reading.+import { describe, expect, it } from 'bun:test';+import { JOINABLE_PROFILE_COLUMNS } from '../chart.constants';+import { profileJoinFields } from './funnel.sql';++describe('profile join columns', () => {+ it('keeps the joinable columns a filter names', () => {+ const fields = profileJoinFields(['email', 'first_name'], []);+ expect(fields).toContain('email');+ expect(fields).toContain('first_name');+ });++ it('drops a column that is not joinable', () => {+ const fields = profileJoinFields(['project_id', 'is_external'], []);+ expect(fields).not.toContain('project_id');+ expect(fields).not.toContain('is_external');+ });++ it('drops an injection attempt outright', () => {+ const fields = profileJoinFields(+ ['id) WHERE 1=1 UNION SELECT name FROM events--'],+ []+ );+ expect(fields).toEqual(['id']);+ });++ it('never offers a column outside the allowlist plus the key', () => {+ const allowed = new Set([...JOINABLE_PROFILE_COLUMNS, 'id']);+ const fields = profileJoinFields(+ ['email', 'password', 'properties.plan'],+ [{ name: 'profile.last_seen_at' }, { name: 'profile.avatar' }]+ );+ for (const field of fields) {+ expect(allowed.has(field)).toBe(true);+ }+ });+});diff --git a/packages/core/src/modules/cohort/cohort.service.ts b/packages/core/src/modules/cohort/cohort.service.tsindex 7fe9ca555..4d070e654 100644--- a/packages/core/src/modules/cohort/cohort.service.ts+++ b/packages/core/src/modules/cohort/cohort.service.ts@@ -37,6 +37,7 @@ import type { CoreConfig } from '../../config';import type { ServiceDeps, Services } from '../../services';import { formatClickhouseDate } from '../../shared/ch-dates';import { replicatedTarget } from '../../shared/ch-tables';+import { PROFILE_SELECT_COLUMNS } from '../chart/chart.constants';import type { IServiceProfile } from '../profile/profile.service';import type { IChartEventFilter } from '../report/report.constants';import type {@@ -356,6 +357,14 @@ export function buildEventCriteriaQuery(`;}+// The bare profiles columns a cohort filter may reach. Every name is+// qualified by the time it gets here, and anything outside the list is a+// filter naming a column that is not the caller's to read (main #512,+// GHSA-gvwr-5684-wjqc).+const COHORT_PROFILE_COLUMNS = PROFILE_SELECT_COLUMNS.map(+ (column) => `profiles.${column}`+);+// `profile.<x>` and `profiles.<x>` name the same column; normalizing first// makes the dedup in buildProfileCohortHavingClause see them as one.function normalizeProfileColumn(name: string): string {@@ -375,7 +384,7 @@ function profileColumnAccess(normalizedName: string): SqlFragment {const propKey = normalizedName.replace('profiles.properties.', '');return sql`profiles.properties[${sql.string(propKey)}]`;}- return sql.id(normalizedName);+ return sql.id(normalizedName, COHORT_PROFILE_COLUMNS);}function buildProfileCohortHavingClause(diff --git a/packages/core/src/modules/profile/profile.rpc.ts b/packages/core/src/modules/profile/profile.rpc.tsindex 45725e354..9d2910eaf 100644--- a/packages/core/src/modules/profile/profile.rpc.ts+++ b/packages/core/src/modules/profile/profile.rpc.ts@@ -23,6 +23,7 @@ import {getProfilePropertyNames,getProfileValues,} from './profile.service';+import { PROFILE_VALUE_COLUMNS } from './src/sql';const DEFAULT_LIST_TAKE = 50;@@ -44,6 +45,18 @@ const zProfileWindow = {const zProfileRef = z.object({ profileId: z.string(), projectId: z.string() });+// The picker offers the bare columns plus any `properties.*` path. Validating+// here keeps an unknown column a 400 instead of `sql.id`'s SqlIdentifierError+// surfacing as a 500 (main #512, GHSA-4j6c-j6vc-xq96).+const zProfileValueProperty = z+ .string()+ .refine(+ (property) =>+ property.startsWith('properties.') ||+ (PROFILE_VALUE_COLUMNS as readonly string[]).includes(property),+ { message: 'Unknown profile property' }+ );+export const profileRouter = createTRPCRouter({byId: protectedProcedure.input(zProfileRef).query(async ({ input, ctx }) => {await ctx.services.auth.requireProjectAccess({@@ -165,7 +178,7 @@ export const profileRouter = createTRPCRouter({}),values: protectedProcedure- .input(z.object({ property: z.string(), projectId: z.string() }))+ .input(z.object({ property: zProfileValueProperty, projectId: z.string() })).query(async ({ input, ctx }) => {await ctx.services.auth.requireProjectAccess({userId: ctx.session.userId,