Skip to content

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

fix(security): allowlist profile columns in profiles JOINs and profile.values

Carl-Gerhard Lindesvärda2aa053cb23e
diff --git a/packages/db/src/services/cohort.service.ts b/packages/db/src/services/cohort.service.ts
index 32fe26837..db4ccf7b6 100644
--- a/packages/db/src/services/cohort.service.ts
+++ b/packages/db/src/services/cohort.service.ts
@@ -18,7 +18,7 @@ import {
getReplicatedTableName,
} from '../clickhouse/client';
import { db } from '../prisma-client';
-import { buildFilterWhere } from './filter-where.service';
+import { buildFilterWhere, PROFILE_TABLE_COLUMNS } from './filter-where.service';
import {
getProfiles,
profileSearchSql,
@@ -303,17 +303,6 @@ export function buildEventCriteriaQuery(
`;
}
-/** Columns of the profiles table a cohort filter may reference directly. */
-const PROFILE_FILTER_COLUMNS = new Set([
- 'id',
- 'first_name',
- 'last_name',
- 'email',
- 'avatar',
- 'created_at',
- 'last_seen_at',
-]);
-
// SQL for a profile filter's column: either a properties Map lookup or a
// plain column, qualified with the table name. The column name is an
// identifier and cannot be escaped like a value, so it must come from the
@@ -327,7 +316,7 @@ export function profileColumnAccess(name: string): string {
return `profiles.properties[${sqlstring.escape(propKey)}]`;
}
const column = normalizedName.replace(/^profiles\./, '');
- if (!PROFILE_FILTER_COLUMNS.has(column)) {
+ if (!PROFILE_TABLE_COLUMNS.has(column)) {
throw new Error(`Unknown profile filter column: ${name}`);
}
return `profiles.${column}`;
diff --git a/packages/db/src/services/event.service.ts b/packages/db/src/services/event.service.ts
index 1e7301290..242b7e6dc 100644
--- a/packages/db/src/services/event.service.ts
+++ b/packages/db/src/services/event.service.ts
@@ -1,7 +1,7 @@
import { DateTime, toDots } from '@openpanel/common';
import { cacheable } from '@openpanel/redis';
import type { IChartEventFilter } from '@openpanel/validation';
-import { assocPath, last, mergeDeepRight, path, uniq } from 'ramda';
+import { assocPath, last, mergeDeepRight, path } from 'ramda';
import sqlstring from 'sqlstring';
import { v4 as uuid } from 'uuid';
import { botBuffer, eventBuffer } from '../buffers';
@@ -18,7 +18,7 @@ import { db } from '../prisma-client';
import { createSqlBuilder, type SqlBuilderObject } from '../sql-builder';
import { resolveMaxLookbackDays } from './lookback';
import { getEventFiltersWhereClause } from './chart.service';
-import { buildFilterWhere } from './filter-where.service';
+import { buildFilterWhere, profileJoinColumns } from './filter-where.service';
import type { IServiceProfile, IServiceUpsertProfile } from './profile.service';
import {
getProfileById,
@@ -696,7 +696,7 @@ export async function getEventList(options: GetEventListOptions) {
.map((f) => f.name.replace('profile.', ''));
if (profileFilters.length > 0) {
- sb.joins.profiles = `LEFT ANY JOIN (SELECT id, ${uniq(profileFilters.map((f) => f.split('.')[0])).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
+ sb.joins.profiles = `LEFT ANY JOIN (SELECT ${profileJoinColumns(profileFilters).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
}
// Join groups table if any filter uses group fields
@@ -787,7 +787,7 @@ export async function getEventsCount({
.map((f) => f.name.replace('profile.', ''));
if (profileFilters.length > 0) {
- sb.joins.profiles = `LEFT ANY JOIN (SELECT id, ${uniq(profileFilters.map((f) => f.split('.')[0])).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
+ sb.joins.profiles = `LEFT ANY JOIN (SELECT ${profileJoinColumns(profileFilters).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
}
// Join groups table if any filter uses group fields
@@ -926,7 +926,7 @@ class EventService {
.where('project_id', '=', projectId)
.when(profileFilters.length > 0, (q) => {
q.leftJoin(
- `(SELECT id, ${uniq(profileFilters.map((f) => f.split('.')[0])).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile`,
+ `(SELECT ${profileJoinColumns(profileFilters).join(', ')} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile`,
'profile.id = e.profile_id'
);
})
diff --git a/packages/db/src/services/filter-where.service.ts b/packages/db/src/services/filter-where.service.ts
index 5920667ea..4ed59bf4c 100644
--- a/packages/db/src/services/filter-where.service.ts
+++ b/packages/db/src/services/filter-where.service.ts
@@ -169,7 +169,7 @@ function compileScalarClause(
* `getProfilePropertySelect` in chart.service.ts, which lists the same set for
* the SELECT side.
*/
-const PROFILE_COLUMNS = new Set([
+export const PROFILE_TABLE_COLUMNS = new Set([
'id',
'first_name',
'last_name',
@@ -179,6 +179,32 @@ const PROFILE_COLUMNS = new Set([
'last_seen_at',
]);
+/** Whether `name` (with or without the `profile.` prefix) is a real profiles column. */
+export function isProfileColumn(name: string): boolean {
+ return PROFILE_TABLE_COLUMNS.has(name.replace(/^profile\./, ''));
+}
+
+/**
+ * The columns a profiles JOIN subquery must SELECT so that the given
+ * `profile.*` filter/breakdown names resolve. `profile.properties.<key>`
+ * needs the `properties` Map; a bare field needs that column. Column names
+ * are identifiers and cannot be escaped, so anything not on the allowlist is
+ * dropped here instead of reaching the SQL text (GHSA-pc3q-gw7f-p2x2).
+ * Always includes `id`, which the JOIN condition uses.
+ */
+export function profileJoinColumns(names: string[]): string[] {
+ const columns = new Set<string>(['id']);
+ for (const name of names) {
+ const withoutPrefix = name.replace(/^profile\./, '');
+ if (withoutPrefix.startsWith('properties.') || withoutPrefix === 'properties') {
+ columns.add('properties');
+ } else if (PROFILE_TABLE_COLUMNS.has(withoutPrefix)) {
+ columns.add(withoutPrefix);
+ }
+ }
+ return [...columns];
+}
+
/**
* Translate `profile.<field>` into the SQL accessor used when querying the
* profiles table directly. Bare fields (email, first_name, …) map to columns;
@@ -191,7 +217,7 @@ function profileColumnSql(name: string): string | null {
const key = withoutPrefix.replace(/^properties\./, '');
return `properties[${sqlstring.escape(key)}]`;
}
- if (PROFILE_COLUMNS.has(withoutPrefix)) {
+ if (PROFILE_TABLE_COLUMNS.has(withoutPrefix)) {
return withoutPrefix;
}
return null;
diff --git a/packages/db/src/services/funnel.service.ts b/packages/db/src/services/funnel.service.ts
index 381ca6a6e..5e507ea1c 100644
--- a/packages/db/src/services/funnel.service.ts
+++ b/packages/db/src/services/funnel.service.ts
@@ -24,6 +24,7 @@ import {
rewriteProfilePropertyRefs,
} from './chart.service';
import { mergeGlobalFilters, onlyReportEvents } from './reports.service';
+import { profileJoinColumns } from './filter-where.service';
/** Display label for null/empty breakdown values (e.g. property not set). */
export const EMPTY_BREAKDOWN_LABEL = 'Not set';
@@ -383,13 +384,12 @@ export class FunnelService {
if (anyFilterOnProfile || profileBreakdowns.length > 0) {
// Scalar columns (email etc.) are selected as-is; the properties Map is
// narrowed to the referenced keys via profilePropertiesCteSelect.
- const profileFields = new Set<string>(['id']);
- for (const f of profileFilters) {
- const fieldName = f.split('.')[0]!;
- if (fieldName !== 'properties') {
- profileFields.add(fieldName);
- }
- }
+ // Column names are identifiers and cannot be escaped: only allowlisted
+ // profile columns may be selected. The properties Map is added below
+ // via profilePropertiesCteSelect instead.
+ const profileFields = new Set<string>(
+ profileJoinColumns(profileFilters).filter((c) => c !== 'properties'),
+ );
for (const b of profileBreakdowns) {
const fieldName = b.name.replace('profile.', '').split('.')[0];
if (
diff --git a/packages/db/src/services/profile-columns.test.ts b/packages/db/src/services/profile-columns.test.ts
new file mode 100644
index 000000000..5ac881a71
--- /dev/null
+++ b/packages/db/src/services/profile-columns.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from 'vitest';
+import {
+ isProfileColumn,
+ profileJoinColumns,
+} from './filter-where.service';
+
+describe('profileJoinColumns (GHSA-pc3q-gw7f-p2x2)', () => {
+ it('maps filter names to allowlisted columns and always includes id', () => {
+ expect(
+ profileJoinColumns([
+ 'profile.email',
+ 'profile.properties.plan',
+ 'first_name',
+ 'properties.tier',
+ ]),
+ ).toEqual(['id', 'email', 'properties', 'first_name']);
+ });
+
+ it('drops anything that is not a real column', () => {
+ expect(
+ profileJoinColumns([
+ "profile.email, project_id FROM profiles) AS p USING (project_id) -- ",
+ 'profile.password',
+ 'profile.email;',
+ ]),
+ ).toEqual(['id']);
+ });
+});
+
+describe('isProfileColumn (GHSA-4j6c-j6vc-xq96)', () => {
+ it('accepts real columns with or without the prefix', () => {
+ expect(isProfileColumn('email')).toBe(true);
+ expect(isProfileColumn('profile.last_seen_at')).toBe(true);
+ });
+
+ it('rejects expressions', () => {
+ expect(isProfileColumn("email as values FROM profiles WHERE project_id = 'x' -- ")).toBe(false);
+ expect(isProfileColumn('properties')).toBe(false);
+ expect(isProfileColumn('')).toBe(false);
+ });
+});
diff --git a/packages/trpc/src/routers/chart.ts b/packages/trpc/src/routers/chart.ts
index 0c0288570..b607f8f99 100644
--- a/packages/trpc/src/routers/chart.ts
+++ b/packages/trpc/src/routers/chart.ts
@@ -1,7 +1,7 @@
import {
AggregateChartEngine,
- ChartEngine,
ch,
+ ChartEngine,
chQuery,
clix,
conversionService,
@@ -21,13 +21,14 @@ import {
getRetentionCohort,
getSelectPropertyKey,
getSettingsForProject,
- type IServiceProfile,
isKnownEventField,
mergeGlobalFilters,
normalizeEventField,
onlyReportEvents,
+ profileJoinColumns,
sankeyService,
TABLE_NAMES,
+ type IServiceProfile,
validateShareAccess,
} from '@openpanel/db';
import {
@@ -824,11 +825,10 @@ export const chartRouter = createTRPCRouter({
];
if (profileFields.length > 0) {
- // Extract top-level field names and select only what's needed
- const fieldsToSelect = uniq(
- profileFields.map((f) => f.split('.')[0])
- ).join(', ');
- sb.joins.profiles = `LEFT ANY JOIN (SELECT id, ${fieldsToSelect} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
+ // Only allowlisted columns: the names are user input and identifiers
+ // cannot be escaped.
+ const fieldsToSelect = profileJoinColumns(profileFields).join(', ');
+ sb.joins.profiles = `LEFT ANY JOIN (SELECT ${fieldsToSelect} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;
}
// Check for group filters/breakdowns and add ARRAY JOIN if needed
diff --git a/packages/trpc/src/routers/profile.ts b/packages/trpc/src/routers/profile.ts
index 3c1441b91..426b48bd9 100644
--- a/packages/trpc/src/routers/profile.ts
+++ b/packages/trpc/src/routers/profile.ts
@@ -3,7 +3,6 @@ import sqlstring from 'sqlstring';
import { z } from 'zod';
import {
- TABLE_NAMES,
chQuery,
createSqlBuilder,
getProfileById,
@@ -11,9 +10,12 @@ import {
getProfileListCount,
getProfileMetrics,
getProfiles,
+ isProfileColumn,
+ TABLE_NAMES,
} from '@openpanel/db';
import { zChartEventFilter } from '@openpanel/validation';
+import { TRPCBadRequestError } from '../errors';
import { createTRPCRouter, protectedProcedure } from '../trpc';
export const profileRouter = createTRPCRouter({
@@ -159,7 +161,12 @@ export const profileRouter = createTRPCRouter({
property.replace(/^properties\./, '').replace('.*.', '.%.'),
)}))) as values`;
} else {
- sb.select.values = `${property} as values`;
+ // The property is an identifier and cannot be escaped; only real
+ // profile columns are accepted (GHSA-4j6c-j6vc-xq96).
+ if (!isProfileColumn(property)) {
+ throw new TRPCBadRequestError(`Unknown profile property: ${property}`);
+ }
+ sb.select.values = `${property.replace(/^profile\./, '')} as values`;
}
const profiles = await chQuery<{ values: string[] }>(getSql());