🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance custom query validation to block quoted identifiers
Bill Yang45ecd14e0a45
diff --git a/server/src/api/analytics/utils/customQueryValidation.test.ts b/server/src/api/analytics/utils/customQueryValidation.test.tsindex b2248cd6f..28edf3efd 100644--- a/server/src/api/analytics/utils/customQueryValidation.test.ts+++ b/server/src/api/analytics/utils/customQueryValidation.test.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";import { validateScopedQuery } from "./customQueryValidation.js";const SCOPED_ONLY_ERROR = "Queries can only read from scoped_events";+const QUOTED_IDENTIFIER_ERROR = "Quoted identifiers are not allowed in custom analytics queries";describe("validateScopedQuery — table reference scoping", () => {it("allows reading from scoped_events", () => {@@ -10,14 +11,10 @@ describe("validateScopedQuery — table reference scoping", () => {it("allows CTEs and self-joins on scoped_events", () => {expect(- validateScopedQuery(- "WITH t AS (SELECT user_id, count() c FROM scoped_events GROUP BY user_id) SELECT * FROM t"- )+ validateScopedQuery("WITH t AS (SELECT user_id, count() c FROM scoped_events GROUP BY user_id) SELECT * FROM t")).toBeNull();expect(- validateScopedQuery(- "SELECT a.user_id FROM scoped_events a JOIN scoped_events b ON a.user_id = b.user_id"- )+ validateScopedQuery("SELECT a.user_id FROM scoped_events a JOIN scoped_events b ON a.user_id = b.user_id")).toBeNull();});@@ -43,24 +40,28 @@ describe("validateScopedQuery — table reference scoping", () => {});it("blocks comma-join without surrounding whitespace", () => {- expect(- validateScopedQuery("SELECT * FROM scoped_events,sessions_mv_target LIMIT 1")- ).toBe(SCOPED_ONLY_ERROR);+ expect(validateScopedQuery("SELECT * FROM scoped_events,sessions_mv_target LIMIT 1")).toBe(SCOPED_ONLY_ERROR);});it("blocks an unauthorized table inside a subquery comma-join", () => {- expect(- validateScopedQuery(- "SELECT * FROM (SELECT * FROM scoped_events, sessions_mv_target) x LIMIT 1"- )- ).toBe(SCOPED_ONLY_ERROR);+ expect(validateScopedQuery("SELECT * FROM (SELECT * FROM scoped_events, sessions_mv_target) x LIMIT 1")).toBe(+ SCOPED_ONLY_ERROR+ );});it("still blocks plain FROM/JOIN to unauthorized tables", () => {expect(validateScopedQuery("SELECT * FROM events")).toBe(SCOPED_ONLY_ERROR);- expect(- validateScopedQuery("SELECT * FROM scoped_events JOIN events ON 1=1")- ).toBe(SCOPED_ONLY_ERROR);+ expect(validateScopedQuery("SELECT * FROM scoped_events JOIN events ON 1=1")).toBe(SCOPED_ONLY_ERROR);+ });++ it("blocks quoted identifiers that can hide unauthorized table references", () => {+ expect(validateScopedQuery('SELECT * FROM "events" scoped_events WHERE 1=1')).toBe(QUOTED_IDENTIFIER_ERROR);+ expect(validateScopedQuery("SELECT * FROM `events` scoped_events WHERE 1=1")).toBe(QUOTED_IDENTIFIER_ERROR);+ expect(validateScopedQuery('SELECT * FROM "scoped_events" WHERE 1=1')).toBe(QUOTED_IDENTIFIER_ERROR);+ });++ it("allows double quotes and backticks inside string literals", () => {+ expect(validateScopedQuery("SELECT 'quoted \"text\" and `ticks`' AS label FROM scoped_events")).toBeNull();});it("requires the query to reference scoped_events at all", () => {diff --git a/server/src/api/analytics/utils/customQueryValidation.ts b/server/src/api/analytics/utils/customQueryValidation.tsindex b708f2cf6..4bac0fcf9 100644--- a/server/src/api/analytics/utils/customQueryValidation.ts+++ b/server/src/api/analytics/utils/customQueryValidation.ts@@ -77,6 +77,54 @@ const blockedFunctions = ["viewIfPermitted",] as const;+const quotedIdentifierError = "Quoted identifiers are not allowed in custom analytics queries";++function hasQuotedIdentifierSyntax(query: string) {+ let index = 0;+ let state: "normal" | "single" | "line-comment" | "block-comment" = "normal";++ while (index < query.length) {+ const char = query[index];+ const next = query[index + 1];++ if (state === "normal") {+ if (char === '"' || char === "`") {+ return true;+ }+ if (char === "'") {+ state = "single";+ } else if (char === "-" && next === "-") {+ state = "line-comment";+ index++;+ } else if (char === "/" && next === "*") {+ state = "block-comment";+ index++;+ }+ } else if (state === "single") {+ if (char === "\\" && next !== undefined) {+ index++;+ } else if (char === "'" && next === "'") {+ index++;+ } else if (char === "'") {+ state = "normal";+ }+ } else if (state === "line-comment") {+ if (char === "\n") {+ state = "normal";+ }+ } else if (state === "block-comment") {+ if (char === "*" && next === "/") {+ state = "normal";+ index++;+ }+ }++ index++;+ }++ return false;+}+function stripSqlLiteralsAndComments(query: string) {let result = "";let index = 0;@@ -90,7 +138,7 @@ function stripSqlLiteralsAndComments(query: string) {if (char === "'") {state = "single";result += " ";- } else if (char === "\"") {+ } else if (char === '"') {state = "double";result += " ";} else if (char === "`") {@@ -124,7 +172,7 @@ function stripSqlLiteralsAndComments(query: string) {if (char === "\\" && next !== undefined) {result += " ";index++;- } else if (char === "\"") {+ } else if (char === '"') {state = "normal";result += " ";} else {@@ -282,6 +330,14 @@ function collectTableReferences(query: string): string[] {export function validateScopedQuery(query: string): string | null {const normalizedQuery = normalizeCustomQuery(query);++ // ClickHouse treats double quotes and backticks as identifier quoting, not+ // string literals. Allowing them lets a quoted table name disappear before the+ // validator scans FROM/JOIN targets.+ if (hasQuotedIdentifierSyntax(normalizedQuery)) {+ return quotedIdentifierError;+ }+const queryWithoutLiterals = stripSqlLiteralsAndComments(normalizedQuery);const compactQuery = queryWithoutLiterals.trim();const cteNames = getCteNames(compactQuery);