Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Harden site scoping for custom SQL, site-id params and user deletion (#1308)

Goldflag1a8f1ff9ca8b
diff --git a/server/src/api/analytics/customQueryExecution.test.ts b/server/src/api/analytics/customQueryExecution.test.ts
index 1ce9a5cbe..4b9486230 100644
--- a/server/src/api/analytics/customQueryExecution.test.ts
+++ b/server/src/api/analytics/customQueryExecution.test.ts
@@ -2,8 +2,16 @@ import { ClickHouseError } from "@clickhouse/client";
import Fastify, { FastifyInstance } from "fastify";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-const mocks = vi.hoisted(() => ({ query: vi.fn(), getOrganizationSitesForCaller: vi.fn() }));
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+ getOrganizationSitesForCaller: vi.fn(),
+ isQueryUserIsolated: vi.fn(async () => true),
+}));
vi.mock("../../db/clickhouse/clickhouse.js", () => ({ clickhouseQuery: { query: mocks.query } }));
+vi.mock("../../db/clickhouse/queryUser.js", async importOriginal => ({
+ ...(await importOriginal<typeof import("../../db/clickhouse/queryUser.js")>()),
+ isQueryUserIsolated: mocks.isQueryUserIsolated,
+}));
vi.mock("../../lib/auth-utils.js", () => ({ getOrganizationSitesForCaller: mocks.getOrganizationSitesForCaller }));
import { runCustomQuery } from "./runCustomQuery.js";
@@ -14,6 +22,7 @@ let app: FastifyInstance;
beforeEach(async () => {
vi.resetAllMocks();
mocks.getOrganizationSitesForCaller.mockResolvedValue([{ siteId: 1, organizationId: "org-1" }]);
+ mocks.isQueryUserIsolated.mockResolvedValue(true);
app = Fastify();
app.post<{ Params: { organizationId: string }; Body: unknown }>(
"/organizations/:organizationId/query",
@@ -67,6 +76,24 @@ describe.each(["/organizations/org-1/query", "/sites/1/run-card"])("custom SQL r
expect(response.json()).toEqual({ error: "Unknown expression identifier 'missing_column'" });
});
+ it("sends the caller's site ids for the rybbit_query row policy", async () => {
+ mocks.query.mockResolvedValue({ query_id: "test-query", json: async () => [] });
+ await request();
+ expect(mocks.query).toHaveBeenCalledWith(
+ expect.objectContaining({
+ http_headers: { "X-Rybbit-Site-Ids": "[1]" },
+ query_params: expect.objectContaining({ siteIds: [1] }),
+ })
+ );
+ });
+
+ it("refuses to run while the query user's row policy is unverified", async () => {
+ mocks.isQueryUserIsolated.mockResolvedValue(false);
+ const response = await request();
+ expect(response.statusCode).toBe(503);
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+
it("reports the 60-second execution limit for successful queries", async () => {
mocks.query.mockResolvedValue({ query_id: "test-query", json: async () => [{ count: 1 }] });
const response = await request();
diff --git a/server/src/api/analytics/runCustomQuery.ts b/server/src/api/analytics/runCustomQuery.ts
index 0bcb2e2f7..8c90fad08 100644
--- a/server/src/api/analytics/runCustomQuery.ts
+++ b/server/src/api/analytics/runCustomQuery.ts
@@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";
import { clickhouseQuery } from "../../db/clickhouse/clickhouse.js";
import { QUERY_USER_LIMITS } from "../../db/clickhouse/queryLimits.js";
+import { isQueryUserIsolated, QUERY_USER_UNAVAILABLE_ERROR, siteScopeHeaders } from "../../db/clickhouse/queryUser.js";
import { getOrganizationSitesForCaller } from "../../lib/auth-utils.js";
import {
MAX_CUSTOM_QUERY_LENGTH,
@@ -55,6 +56,10 @@ export async function runCustomQuery(
siteIds = [body.data.siteId];
}
+ if (!(await isQueryUserIsolated())) {
+ return reply.status(503).send({ error: QUERY_USER_UNAVAILABLE_ERROR });
+ }
+
const query = `
WITH scoped_events AS (
SELECT *
@@ -76,6 +81,9 @@ export async function runCustomQuery(
siteIds,
limit: QUERY_USER_LIMITS.maxResultRows,
},
+ // The rybbit_query row policy only admits rows from these sites, so the
+ // scoped_events CTE isn't the only thing keeping the query in tenant.
+ http_headers: siteScopeHeaders(siteIds),
// Execution limits (readonly, max_execution_time, max_memory_usage,
// max_result_rows, …) come from the rybbit_query settings profile and are
// pinned there with constraints; sending them here would be rejected.
diff --git a/server/src/api/analytics/runDashboardCardQuery.ts b/server/src/api/analytics/runDashboardCardQuery.ts
index b457b91f0..d938ecac4 100644
--- a/server/src/api/analytics/runDashboardCardQuery.ts
+++ b/server/src/api/analytics/runDashboardCardQuery.ts
@@ -3,6 +3,7 @@ import SqlString from "sqlstring";
import { z } from "zod";
import { clickhouseQuery } from "../../db/clickhouse/clickhouse.js";
import { QUERY_USER_LIMITS } from "../../db/clickhouse/queryLimits.js";
+import { isQueryUserIsolated, QUERY_USER_UNAVAILABLE_ERROR, siteScopeHeaders } from "../../db/clickhouse/queryUser.js";
import {
MAX_CUSTOM_QUERY_LENGTH,
normalizeCustomQuery,
@@ -85,6 +86,10 @@ export async function runDashboardCardQuery(
return reply.status(400).send({ error: validationError });
}
+ if (!(await isQueryUserIsolated())) {
+ return reply.status(503).send({ error: QUERY_USER_UNAVAILABLE_ERROR });
+ }
+
// Auto-scope the timestamp to the global time range. getTimeStatement returns
// an "AND timestamp >= ... AND timestamp < ..." fragment (or "" for all-time),
// with all values Zod-sanitized and SqlString-escaped internally.
@@ -112,6 +117,9 @@ export async function runDashboardCardQuery(
siteIds: [siteId],
limit: QUERY_USER_LIMITS.maxResultRows,
},
+ // The rybbit_query row policy only admits rows from these sites, so the
+ // scoped_events CTE isn't the only thing keeping the query in tenant.
+ http_headers: siteScopeHeaders([siteId]),
// Execution limits (readonly, max_execution_time, max_memory_usage,
// max_result_rows, …) come from the rybbit_query settings profile and are
// pinned there with constraints; sending them here would be rejected.
diff --git a/server/src/api/analytics/users/deleteUser.test.ts b/server/src/api/analytics/users/deleteUser.test.ts
new file mode 100644
index 000000000..019ef5644
--- /dev/null
+++ b/server/src/api/analytics/users/deleteUser.test.ts
@@ -0,0 +1,60 @@
+import Fastify, { type FastifyInstance } from "fastify";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ command: vi.fn(async () => ({})),
+ aliases: [] as { anonymousId: string }[],
+}));
+
+vi.mock("../../../db/clickhouse/clickhouse.js", () => ({ clickhouse: { command: mocks.command, query: vi.fn() } }));
+vi.mock("../../../db/postgres/postgres.js", () => ({
+ db: {
+ select: () => ({ from: () => ({ where: async () => mocks.aliases }) }),
+ delete: () => ({ where: async () => undefined }),
+ },
+}));
+vi.mock("../../../services/storage/r2StorageService.js", () => ({ r2Storage: { isEnabled: () => false } }));
+
+import { deleteUser } from "./deleteUser.js";
+
+let app: FastifyInstance;
+
+beforeEach(async () => {
+ vi.clearAllMocks();
+ mocks.aliases = [];
+ app = Fastify();
+ app.delete("/sites/:siteId/users/:userId", deleteUser);
+ await app.ready();
+});
+
+afterEach(async () => {
+ await app.close();
+});
+
+describe("deleteUser", () => {
+ it("refuses an empty user id without touching ClickHouse", async () => {
+ const response = await app.inject({ method: "DELETE", url: "/sites/1/users/" });
+ expect(response.statusCode).toBe(400);
+ expect(mocks.command).not.toHaveBeenCalled();
+ });
+
+ it("still erases a whitespace identity, which identify() accepts", async () => {
+ const response = await app.inject({ method: "DELETE", url: "/sites/1/users/%20%20" });
+ expect(response.statusCode).toBe(200);
+ expect(mocks.command).toHaveBeenCalledTimes(3);
+ });
+
+ it("deletes only the user's rows and never matches the empty id", async () => {
+ mocks.aliases = [{ anonymousId: "device-1" }, { anonymousId: "" }];
+ const response = await app.inject({ method: "DELETE", url: "/sites/1/users/user-42" });
+
+ expect(response.statusCode).toBe(200);
+ expect(mocks.command).toHaveBeenCalledTimes(3);
+ for (const [call] of mocks.command.mock.calls as unknown as [
+ { query: string; query_params: Record<string, unknown> },
+ ][]) {
+ expect(call.query).toContain("{userId:String} != ''");
+ expect(call.query_params).toEqual({ siteId: 1, userId: "user-42", deviceIds: ["user-42", "device-1"] });
+ }
+ });
+});
diff --git a/server/src/api/analytics/users/deleteUser.ts b/server/src/api/analytics/users/deleteUser.ts
index ca435b03c..5e017b03c 100644
--- a/server/src/api/analytics/users/deleteUser.ts
+++ b/server/src/api/analytics/users/deleteUser.ts
@@ -26,15 +26,24 @@ export async function deleteUser(req: FastifyRequest<DeleteUserRequest>, res: Fa
const { userId } = req.params;
const siteId = Number(req.params.siteId);
+ // Anonymous rows carry identified_user_id = '', so an empty id would match
+ // every one of them and erase the site's anonymous history. Any other id,
+ // whitespace included, is a real identity identify() may have stored.
+ if (!userId) {
+ return res.status(400).send({ error: "userId is required" });
+ }
+
try {
// Devices linked to this user via identify() calls
const aliases = await db
.select({ anonymousId: userAliases.anonymousId })
.from(userAliases)
.where(and(eq(userAliases.siteId, siteId), eq(userAliases.userId, userId)));
- const deviceIds = [userId, ...aliases.map(a => a.anonymousId)];
+ const deviceIds = [userId, ...aliases.map(a => a.anonymousId)].filter(id => id !== "");
+ // The `!= ''` guard backstops the check above inside the DELETE itself.
const userCondition = `site_id = {siteId:UInt16}
+ AND {userId:String} != ''
AND (
identified_user_id = {userId:String}
OR (user_id IN ({deviceIds:Array(String)}) AND identified_user_id = '')
diff --git a/server/src/api/analytics/utils/customQueryValidation.internals.test.ts b/server/src/api/analytics/utils/customQueryValidation.internals.test.ts
index b700134b7..39eb7a9cd 100644
--- a/server/src/api/analytics/utils/customQueryValidation.internals.test.ts
+++ b/server/src/api/analytics/utils/customQueryValidation.internals.test.ts
@@ -74,12 +74,9 @@ describe("stripSqlLiteralsAndComments", () => {
expect(stripSqlLiteralsAndComments("SELECT 1 /* FROM secret")).toBe("SELECT 1 ");
});
- it("does not treat nested /* as re-opening β€” first */ ends the comment", () => {
- // The comment ends at the first */; the trailing "*/" becomes live SQL.
- const out = stripSqlLiteralsAndComments("SELECT 1 /* a /* b */ x */ FROM t");
- expect(out).toHaveLength("SELECT 1 /* a /* b */ x */ FROM t".length);
- // The "x */" after the first close is NOT stripped.
- expect(out).toContain("x */ FROM t");
+ it("nests block comments like ClickHouse β€” the matching */ ends the comment", () => {
+ const input = "SELECT 1 /* a /* b */ x */ FROM t";
+ expect(stripSqlLiteralsAndComments(input)).toBe("SELECT 1 FROM t");
});
});
@@ -224,6 +221,14 @@ describe("collectInTableReferences", () => {
// hasUnsupportedSyntax
// =============================================================================
+describe("collectTableReferences β€” qualified names", () => {
+ it("reads a database-qualified name as one reference even with whitespace around the dot", () => {
+ expect(collectTableReferences("SELECT 1 FROM db . tbl")).toEqual(["db.tbl"]);
+ expect(collectTableReferences("SELECT 1 FROM db .tbl JOIN db. tbl2 ON 1")).toEqual(["db.tbl", "db."]);
+ expect(collectInTableReferences("SELECT 1 WHERE x IN db . tbl")).toEqual(["db.tbl"]);
+ });
+});
+
describe("hasUnsupportedSyntax", () => {
it("detects a double-quoted identifier", () => {
expect(hasUnsupportedSyntax('SELECT "x" FROM t')).toBe(true);
@@ -252,6 +257,26 @@ describe("hasUnsupportedSyntax", () => {
it("returns false for a clean query with no quoting", () => {
expect(hasUnsupportedSyntax("SELECT count(*) FROM scoped_events")).toBe(false);
});
+
+ it("detects a /* opened inside a block comment (ClickHouse nests them)", () => {
+ expect(hasUnsupportedSyntax("SELECT 1 /* a /* b */ c */ FROM t")).toBe(true);
+ expect(hasUnsupportedSyntax("SELECT 1 /* a //* b */ FROM t")).toBe(true);
+ });
+
+ it("does NOT flag sequential or self-closing block comments", () => {
+ expect(hasUnsupportedSyntax("SELECT 1 /* a */ /* b */ /**/ FROM t")).toBe(false);
+ expect(hasUnsupportedSyntax("SELECT 1 /* a */* 2 FROM t")).toBe(false);
+ });
+
+ it("detects non-ASCII characters outside literals and comments", () => {
+ expect(hasUnsupportedSyntax("SELECT \u2018x\u2019 FROM t")).toBe(true);
+ expect(hasUnsupportedSyntax("SELECT \u201cx\u201d FROM t")).toBe(true);
+ expect(hasUnsupportedSyntax("SELECT 1 FROM\u00a0t")).toBe(true);
+ });
+
+ it("does NOT flag non-ASCII text inside a string literal or comment", () => {
+ expect(hasUnsupportedSyntax("SELECT '/\u00fcber \u2018x\u2019' -- \u00e9\nFROM t /* \u201c */")).toBe(false);
+ });
});
// =============================================================================
diff --git a/server/src/api/analytics/utils/customQueryValidation.test.ts b/server/src/api/analytics/utils/customQueryValidation.test.ts
index d7836a0b3..523c9be0c 100644
--- a/server/src/api/analytics/utils/customQueryValidation.test.ts
+++ b/server/src/api/analytics/utils/customQueryValidation.test.ts
@@ -3,7 +3,7 @@ import { describe, expect, it } from "vitest";
import { sanitizeClickhouseError, validateScopedQuery } from "./customQueryValidation.js";
const SCOPED_ONLY_ERROR = "Queries can only read from scoped_events";
-const QUOTED_IDENTIFIER_ERROR = "Quoted identifiers, # and // comments, and $$ strings are not allowed in custom analytics queries";
+const QUOTED_IDENTIFIER_ERROR = "Quoted identifiers, nested /* comments, # and // comments, $$ strings, and non-ASCII characters outside string literals are not allowed in custom analytics queries";
describe("validateScopedQuery β€” table reference scoping", () => {
it("allows reading from scoped_events", () => {
@@ -118,7 +118,7 @@ describe("validateScopedQuery β€” table reference scoping", () => {
// Lexer-mismatch bypasses: syntax ClickHouse understands but the literal
// stripper doesn't. Each of these read another site's rows before the fix.
describe("validateScopedQuery lexer-mismatch bypasses", () => {
- const UNSUPPORTED = "Quoted identifiers, # and // comments, and $$ strings are not allowed in custom analytics queries";
+ const UNSUPPORTED = "Quoted identifiers, nested /* comments, # and // comments, $$ strings, and non-ASCII characters outside string literals are not allowed in custom analytics queries";
it("rejects # line comments used to hide a UNION", () => {
expect(
@@ -148,6 +148,42 @@ describe("validateScopedQuery lexer-mismatch bypasses", () => {
);
});
+ it("rejects nested block comments", () => {
+ expect(
+ validateScopedQuery(
+ "SELECT site_id, pathname FROM scoped_events /* /* */ ' */ UNION ALL SELECT site_id, pathname FROM events -- '"
+ )
+ ).toBe(UNSUPPORTED);
+ });
+
+ it("rejects Unicode quotes and whitespace outside literals", () => {
+ expect(validateScopedQuery("SELECT * FROM scoped_events WHERE pathname = \u2018x\u2019")).toBe(UNSUPPORTED);
+ expect(validateScopedQuery("SELECT \u201cpathname\u201d FROM scoped_events")).toBe(UNSUPPORTED);
+ expect(validateScopedQuery("SELECT pathname FROM\u180escoped_events")).toBe(UNSUPPORTED);
+ });
+
+ it("still allows non-ASCII text inside literals and comments", () => {
+ expect(validateScopedQuery("SELECT count() FROM scoped_events WHERE pathname = '/\u00fcber' -- caf\u00e9")).toBeNull();
+ expect(validateScopedQuery("SELECT count() /* a */ /* b */ FROM scoped_events")).toBeNull();
+ });
+
+ it("checks the whole qualified table name, not a first part that matches a CTE", () => {
+ expect(validateScopedQuery("WITH db AS (SELECT 1) SELECT * FROM db . tbl, scoped_events")).toBe(
+ "Queries can only read from scoped_events"
+ );
+ });
+
+ it("never treats events as a CTE name, even when a WINDOW name looks like one", () => {
+ expect(validateScopedQuery("WITH events AS (SELECT * FROM scoped_events) SELECT * FROM events")).toBe(
+ "events is reserved and cannot be used as a CTE name"
+ );
+ expect(
+ validateScopedQuery(
+ "SELECT count() OVER w FROM scoped_events WINDOW w AS (ORDER BY site_id), events AS (ORDER BY site_id)"
+ )
+ ).toBe("events is reserved and cannot be used as a CTE name");
+ });
+
it("still allows # and $ inside single-quoted literals", () => {
expect(validateScopedQuery("SELECT count() FROM scoped_events WHERE pathname LIKE '%#top%' OR pathname = '$x$'")).toBeNull();
});
diff --git a/server/src/api/analytics/utils/customQueryValidation.ts b/server/src/api/analytics/utils/customQueryValidation.ts
index d451bc6c0..2b5337f8e 100644
--- a/server/src/api/analytics/utils/customQueryValidation.ts
+++ b/server/src/api/analytics/utils/customQueryValidation.ts
@@ -134,16 +134,17 @@ const blockedFunctionPrefixes = [
] as const;
const unsupportedSyntaxError =
- "Quoted identifiers, # and // comments, and $$ strings are not allowed in custom analytics queries";
+ "Quoted identifiers, nested /* comments, # and // comments, $$ strings, and non-ASCII characters outside string literals are not allowed in custom analytics queries";
// Rejects syntax the literal stripper below can't model. ClickHouse treats
-// double quotes and backticks as identifier quoting, `#` / `#!` / `//` as line
-// comments, and `$tag$…$tag$` as string literals. If any of these reached the
-// stripper it would disagree with ClickHouse about where a literal starts and
-// ends, and text it classified as "inside a string" would execute as SQL β€”
-// e.g. `WHERE 1=1 # '\nUNION ALL SELECT * FROM events -- '`. None of them are
-// needed for analytics SQL, so they're rejected outright outside a
-// single-quoted literal.
+// double quotes, backticks and β€œβ€¦β€ as identifier quoting, `#` / `#!` / `//` as
+// line comments, `$tag$…$tag$` and β€˜β€¦β€™ as string literals, nests `/* */`
+// comments, and skips Unicode whitespace the JS regexes below don't all match.
+// If any of these reached the stripper it would disagree with ClickHouse about
+// where a literal or comment starts and ends, and text it classified as
+// "inside a string" would execute as SQL. None of them are needed for
+// analytics SQL, so they're rejected outright outside a single-quoted literal
+// (non-ASCII text stays allowed inside literals and comments).
export function hasUnsupportedSyntax(query: string) {
let index = 0;
let state: "normal" | "single" | "line-comment" | "block-comment" = "normal";
@@ -153,7 +154,14 @@ export function hasUnsupportedSyntax(query: string) {
const next = query[index + 1];
if (state === "normal") {
- if (char === '"' || char === "`" || char === "#" || char === "$" || (char === "/" && next === "/")) {
+ if (
+ char === '"' ||
+ char === "`" ||
+ char === "#" ||
+ char === "$" ||
+ (char === "/" && next === "/") ||
+ char.charCodeAt(0) > 0x7f
+ ) {
return true;
}
if (char === "'") {
@@ -178,6 +186,11 @@ export function hasUnsupportedSyntax(query: string) {
state = "normal";
}
} else if (state === "block-comment") {
+ // ClickHouse nests block comments, so `/* /* */` is still open after the
+ // first `*/`. Mirror its lexer order: an opener is checked before a closer.
+ if (char === "/" && next === "*") {
+ return true;
+ }
if (char === "*" && next === "/") {
state = "normal";
index++;
@@ -194,6 +207,7 @@ export function stripSqlLiteralsAndComments(query: string) {
let result = "";
let index = 0;
let state: "normal" | "single" | "double" | "backtick" | "line-comment" | "block-comment" = "normal";
+ let commentDepth = 0;
while (index < query.length) {
const char = query[index];
@@ -215,6 +229,7 @@ export function stripSqlLiteralsAndComments(query: string) {
index++;
} else if (char === "/" && next === "*") {
state = "block-comment";
+ commentDepth = 1;
result += " ";
index++;
} else {
@@ -256,8 +271,16 @@ export function stripSqlLiteralsAndComments(query: string) {
result += " ";
}
} else if (state === "block-comment") {
- if (char === "*" && next === "/") {
- state = "normal";
+ // Block comments nest in ClickHouse; the comment ends at the matching */.
+ if (char === "/" && next === "*") {
+ commentDepth++;
+ result += " ";
+ index++;
+ } else if (char === "*" && next === "/") {
+ commentDepth--;
+ if (commentDepth === 0) {
+ state = "normal";
+ }
result += " ";
index++;
} else {
@@ -315,12 +338,27 @@ function isIdentifierChar(char: string) {
return /[A-Za-z0-9_.]/.test(char);
}
+// ClickHouse joins `db . table` (whitespace, or a stripped comment, around the
+// dot) into one compound identifier. Read it the same way so the qualified
+// name is what gets checked, not just its first part β€” which could otherwise
+// match a declared CTE.
function readIdentifier(query: string, start: number): [string, number] {
+ let identifier = "";
let end = start;
- while (end < query.length && isIdentifierChar(query[end])) {
- end++;
+ for (;;) {
+ const partStart = end;
+ while (end < query.length && isIdentifierChar(query[end])) {
+ end++;
+ }
+ identifier += query.slice(partStart, end);
+ const dot = skipWhitespace(query, end);
+ const next = skipWhitespace(query, dot + 1);
+ if (query[dot] !== "." || next >= query.length || !isIdentifierStart(query[next])) {
+ return [identifier, end];
+ }
+ identifier += ".";
+ end = next;
}
- return [query.slice(start, end), end];
}
function skipWhitespace(query: string, index: number): number {
@@ -480,6 +518,14 @@ export function validateScopedQuery(query: string): string | null {
return "scoped_events is reserved and cannot be redefined";
}
+ // getCteNames also matches `, name AS (` outside a WITH clause (e.g. a second
+ // named WINDOW), so a name it reports may not be a CTE ClickHouse knows about.
+ // That's harmless for made-up names, but `events` is the real table: treating
+ // it as a CTE would let `FROM events` through, so it can never be one.
+ if (cteNames.has("events")) {
+ return "events is reserved and cannot be used as a CTE name";
+ }
+
// Every data-source reference must be scoped_events or a declared CTE. collectTableReferences
// walks the full FROM list, so comma-separated targets (`FROM scoped_events, other`) are
// validated too β€” a FROM/JOIN-keyword-only scan captured only the first table and let the
diff --git a/server/src/db/clickhouse/client.ts b/server/src/db/clickhouse/client.ts
index 00ad1faf1..21f80889d 100644
--- a/server/src/db/clickhouse/client.ts
+++ b/server/src/db/clickhouse/client.ts
@@ -19,10 +19,11 @@ export const clickhouse = createClient({
// Least-privilege connection for user-authored SQL (custom query page and
// dashboard cards). queryUser.ts provisions `rybbit_query` at startup with
-// SELECT on the events table only, no table-function grants (url/s3/file/remote…),
-// readonly=2, and its own
-// memory/time/concurrency limits. The SQL validator is defense-in-depth on top
-// of this β€” a validator bypass must still land inside these grants.
+// SELECT on the events table only, a row policy limiting it to the sites in
+// each request's siteScopeHeaders(), no table-function grants
+// (url/s3/file/remote…), readonly=2, and its own memory/time/concurrency
+// limits. The SQL validator is defense-in-depth on top of this β€” a validator
+// bypass must still land inside these grants and the caller's sites.
export const CLICKHOUSE_QUERY_USER = process.env.CLICKHOUSE_QUERY_USER || "rybbit_query";
export const clickhouseQuery = createClient({
diff --git a/server/src/db/clickhouse/queryUser.test.ts b/server/src/db/clickhouse/queryUser.test.ts
index fa3bef301..ef0403f0b 100644
--- a/server/src/db/clickhouse/queryUser.test.ts
+++ b/server/src/db/clickhouse/queryUser.test.ts
@@ -1,13 +1,29 @@
-import { describe, expect, it } from "vitest";
-import { buildQueryUserStatements } from "./queryUser.js";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ exec: vi.fn(async (_args: { query: string }) => ({})),
+ query: vi.fn(),
+ queryUserQuery: vi.fn(async () => ({ json: async () => [] })),
+}));
+
+vi.mock("./client.js", () => ({
+ CLICKHOUSE_QUERY_USER: "rybbit_query",
+ clickhouse: { exec: mocks.exec, query: mocks.query },
+ clickhouseQuery: { query: mocks.queryUserQuery },
+}));
+
+import { buildQueryUserStatements, provisionQueryUser, siteScopeHeaders } from "./queryUser.js";
+
+const rows = (data: unknown[]) => ({ json: async () => data });
+const executed = () => mocks.exec.mock.calls.map(([args]) => args.query);
describe("buildQueryUserStatements", () => {
it("escapes the password and pins every limit as READONLY", () => {
- const statements = buildQueryUserStatements("analytics", "rybbit_query", "a&b<c'\"\\d");
- expect(statements.find(s => s.startsWith("CREATE USER"))).toBe(
+ const { setup } = buildQueryUserStatements("analytics", "rybbit_query", "a&b<c'\"\\d");
+ expect(setup.find(s => s.startsWith("CREATE USER"))).toBe(
"CREATE USER IF NOT EXISTS rybbit_query IDENTIFIED WITH sha256_password BY 'a&b<c\\'\\\"\\\\d'"
);
- const profile = statements.find(s => s.startsWith("ALTER SETTINGS PROFILE"))!;
+ const profile = setup.find(s => s.startsWith("ALTER SETTINGS PROFILE"))!;
for (const setting of [
"readonly = 2",
"max_execution_time = 60",
@@ -16,15 +32,103 @@ describe("buildQueryUserStatements", () => {
"max_result_rows = 1000",
"result_overflow_mode = 'break'",
"max_concurrent_queries_for_user = 8",
+ "allow_get_client_http_header = 1",
]) {
expect(profile).toContain(`${setting} READONLY`);
}
- expect(statements[statements.length - 2]).toBe("REVOKE ALL ON *.* FROM rybbit_query");
- expect(statements[statements.length - 1]).toBe("GRANT SELECT ON analytics.events TO rybbit_query");
+ });
+
+ it("grants SELECT on events only after the site-scope policy, and adds no other policy", () => {
+ const { access } = buildQueryUserStatements("analytics", "rybbit_query", "x");
+ expect(access).toEqual([
+ "REVOKE ALL ON *.* FROM rybbit_query",
+ "CREATE ROW POLICY OR REPLACE rybbit_query_site_scope ON analytics.events FOR SELECT USING has(JSONExtract(getClientHTTPHeader('X-Rybbit-Site-Ids'), 'Array(UInt16)'), site_id) TO rybbit_query",
+ "GRANT SELECT ON analytics.events TO rybbit_query",
+ ]);
});
it("rejects non-identifier database or user names", () => {
expect(() => buildQueryUserStatements("analytics; DROP", "rybbit_query", "x")).toThrow();
expect(() => buildQueryUserStatements("analytics", "bad name", "x")).toThrow();
});
+
+ it("encodes the site scope header as a JSON array the row policy can parse", () => {
+ expect(siteScopeHeaders([1, 42])).toEqual({ "X-Rybbit-Site-Ids": "[1,42]" });
+ });
+});
+
+describe("provisionQueryUser", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ process.env.CLICKHOUSE_DB = "analytics";
+ });
+
+ it("installs the row policy and grant when unnamed users keep reading rows", async () => {
+ mocks.query.mockResolvedValueOnce(rows([])).mockResolvedValueOnce(rows([{ c: "1" }]));
+ await provisionQueryUser();
+ const queries = executed();
+ expect(queries.some(q => q.startsWith("CREATE ROW POLICY OR REPLACE rybbit_query_site_scope"))).toBe(true);
+ expect(queries[queries.length - 1]).toBe("GRANT SELECT ON analytics.events TO rybbit_query");
+ // The probe table and its policy are always cleaned up.
+ expect(queries.filter(q => q.startsWith("DROP TABLE IF EXISTS analytics.rybbit_row_policy_probe_"))).toHaveLength(
+ 1
+ );
+ });
+
+ it("revokes every role granted to the query user, since role policies would widen its scope", async () => {
+ mocks.query
+ .mockResolvedValueOnce(rows([{ role: "analyst" }, { role: "odd`name" }]))
+ .mockResolvedValueOnce(rows([{ c: "1" }]));
+ await provisionQueryUser();
+ expect(executed()).toEqual(
+ expect.arrayContaining(["REVOKE `analyst` FROM rybbit_query", "REVOKE `odd\\`name` FROM rybbit_query"])
+ );
+ });
+
+ it("leaves the user without access when a row policy would hide events from the main user", async () => {
+ mocks.query.mockResolvedValueOnce(rows([])).mockResolvedValueOnce(rows([{ c: "0" }]));
+ await provisionQueryUser();
+ const queries = executed();
+ expect(queries.some(q => q.includes("ON analytics.events FOR SELECT"))).toBe(false);
+ expect(queries.some(q => q.startsWith("GRANT"))).toBe(false);
+ expect(queries).toContain("REVOKE ALL ON *.* FROM rybbit_query");
+ expect(queries).toContain("DROP ROW POLICY IF EXISTS rybbit_query_site_scope ON analytics.events");
+ });
+
+ it("never grants when a setup statement fails", async () => {
+ mocks.exec.mockRejectedValueOnce(new Error("no access_management"));
+ await provisionQueryUser();
+ expect(executed().some(q => q.startsWith("GRANT"))).toBe(false);
+ });
+});
+
+describe("isQueryUserIsolated", () => {
+ beforeEach(() => {
+ vi.resetModules();
+ vi.clearAllMocks();
+ });
+
+ // A factory, so a rejected result isn't created (and flagged unhandled)
+ // before the module under test attaches its handlers.
+ const check = async (result: () => Promise<unknown>) => {
+ mocks.query.mockImplementationOnce(result);
+ const { isQueryUserIsolated } = await import("./queryUser.js");
+ return isQueryUserIsolated();
+ };
+
+ it("is true only with the scope policy and nothing that widens it", async () => {
+ expect(await check(async () => rows([{ scoped: "1", widening: "0", roles: "0" }]))).toBe(true);
+ });
+
+ it.each([
+ ["the scope policy is missing", { scoped: "0", widening: "0", roles: "0" }],
+ ["another permissive policy reaches the user", { scoped: "1", widening: "1", roles: "0" }],
+ ["the user holds a role", { scoped: "1", widening: "0", roles: "1" }],
+ ])("is false when %s", async (_label, row) => {
+ expect(await check(async () => rows([row]))).toBe(false);
+ });
+
+ it("fails closed when the check errors", async () => {
+ expect(await check(() => Promise.reject(new Error("down")))).toBe(false);
+ });
});
diff --git a/server/src/db/clickhouse/queryUser.ts b/server/src/db/clickhouse/queryUser.ts
index 4ffde1009..b21c85b99 100644
--- a/server/src/db/clickhouse/queryUser.ts
+++ b/server/src/db/clickhouse/queryUser.ts
@@ -1,3 +1,4 @@
+import { randomBytes } from "node:crypto";
import SqlString from "sqlstring";
import { CLICKHOUSE_QUERY_USER, clickhouse, clickhouseQuery } from "./client.js";
import { clickhouseInitLogger as logger } from "./initUtils.js";
@@ -13,12 +14,40 @@ export { QUERY_USER_LIMITS } from "./queryLimits.js";
// CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1); otherwise the steps fail, the error
// is logged, and custom queries fail closed until the user is created by hand.
//
-// The SQL validator is defense-in-depth on top of this user: a validator bypass
-// must still land inside SELECT-on-events, with no table functions
+// Tenant isolation is enforced here, not only by the SQL validator: a row
+// policy limits every read of events by this user to the site ids the backend
+// sends in the SITE_SCOPE_HEADER HTTP header. SQL can't set HTTP headers and
+// allow_get_client_http_header is pinned READONLY, so a validator bypass still
+// lands inside the caller's own sites, with no table functions
// (url/s3/file/remote…), no system tables, and the pinned resource limits.
+// A request without the header reads no rows. Custom SQL is refused until
+// isQueryUserIsolated() confirms the policy is in place.
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
-export function buildQueryUserStatements(database: string, user: string, password: string): string[] {
+export const SITE_SCOPE_HEADER = "X-Rybbit-Site-Ids";
+
+// Per-request headers for clickhouseQuery: the site ids the row policy admits.
+export function siteScopeHeaders(siteIds: number[]): Record<string, string> {
+ return { [SITE_SCOPE_HEADER]: JSON.stringify(siteIds) };
+}
+
+function quoteIdentifier(name: string) {
+ return "`" + name.replace(/\\/g, "\\\\").replace(/`/g, "\\`") + "`";
+}
+
+export function siteScopePolicyName(user: string) {
+ return `${user}_site_scope`;
+}
+
+export interface QueryUserStatements {
+ // Profile and user; safe to run before anything else is known.
+ setup: string[];
+ // Grants exactly SELECT on events behind the site-scope row policy. The policy
+ // is created before the grant, so a failure part-way leaves no access.
+ access: string[];
+}
+
+export function buildQueryUserStatements(database: string, user: string, password: string): QueryUserStatements {
if (!IDENTIFIER.test(database) || !IDENTIFIER.test(user)) {
throw new Error("ClickHouse database and query user names must be plain identifiers");
}
@@ -38,20 +67,46 @@ export function buildQueryUserStatements(database: string, user: string, passwor
`max_concurrent_queries_for_user = ${l.maxConcurrentQueriesForUser} READONLY`,
`max_bytes_before_external_group_by = ${l.maxBytesBeforeExternalBytes} READONLY`,
`max_bytes_before_external_sort = ${l.maxBytesBeforeExternalBytes} READONLY`,
+ // The row policy reads the site scope from the request's HTTP header.
+ "allow_get_client_http_header = 1 READONLY",
"enable_json_type = 1",
"log_queries = 0",
].join(", ");
- return [
- `CREATE SETTINGS PROFILE IF NOT EXISTS ${profile}`,
- `ALTER SETTINGS PROFILE ${profile} SETTINGS ${profileSettings}`,
- `CREATE USER IF NOT EXISTS ${user} IDENTIFIED WITH sha256_password BY ${secret}`,
- // Re-assert the password and profile so an env change or a drifted user is corrected.
- `ALTER USER ${user} IDENTIFIED WITH sha256_password BY ${secret} SETTINGS PROFILE ${SqlString.escape(profile)}`,
- // Reset grants to exactly SELECT on the events table.
- `REVOKE ALL ON *.* FROM ${user}`,
- `GRANT SELECT ON ${database}.events TO ${user}`,
- ];
+ return {
+ setup: [
+ `CREATE SETTINGS PROFILE IF NOT EXISTS ${profile}`,
+ `ALTER SETTINGS PROFILE ${profile} SETTINGS ${profileSettings}`,
+ `CREATE USER IF NOT EXISTS ${user} IDENTIFIED WITH sha256_password BY ${secret}`,
+ // Re-assert the password and profile so an env change or a drifted user is corrected.
+ `ALTER USER ${user} IDENTIFIED WITH sha256_password BY ${secret} SETTINGS PROFILE ${SqlString.escape(profile)}`,
+ ],
+ access: [
+ `REVOKE ALL ON *.* FROM ${user}`,
+ `CREATE ROW POLICY OR REPLACE ${siteScopePolicyName(user)} ON ${database}.events FOR SELECT USING has(JSONExtract(getClientHTTPHeader('${SITE_SCOPE_HEADER}'), 'Array(UInt16)'), site_id) TO ${user}`,
+ `GRANT SELECT ON ${database}.events TO ${user}`,
+ ],
+ };
+}
+
+// With users_without_row_policies_can_read_rows=false (the default is true), a
+// row policy on events would hide every row from users it doesn't name β€” the
+// main connection included, blanking every dashboard. Check on a throwaway
+// table rather than on events, so the probe itself can't blank anything.
+async function rowPoliciesHideUnnamedUsers(database: string, user: string): Promise<boolean> {
+ const table = `${database}.rybbit_row_policy_probe_${randomBytes(4).toString("hex")}`;
+ const policy = `rybbit_row_policy_probe ON ${table}`;
+ try {
+ await clickhouse.exec({ query: `CREATE TABLE ${table} (x UInt8) ENGINE = Memory` });
+ await clickhouse.exec({ query: `INSERT INTO ${table} VALUES (1)` });
+ await clickhouse.exec({ query: `CREATE ROW POLICY ${policy} USING 0 TO ${user}` });
+ const result = await clickhouse.query({ query: `SELECT count() AS c FROM ${table}`, format: "JSONEachRow" });
+ const [row] = await result.json<{ c: string | number }>();
+ return Number(row?.c) === 0;
+ } finally {
+ await clickhouse.exec({ query: `DROP ROW POLICY IF EXISTS ${policy}` }).catch(() => undefined);
+ await clickhouse.exec({ query: `DROP TABLE IF EXISTS ${table}` }).catch(() => undefined);
+ }
}
export async function provisionQueryUser() {
@@ -60,14 +115,39 @@ export async function provisionQueryUser() {
const user = CLICKHOUSE_QUERY_USER;
try {
- for (const query of buildQueryUserStatements(database, user, password)) {
+ const { setup, access } = buildQueryUserStatements(database, user, password);
+ for (const query of setup) {
+ await clickhouse.exec({ query });
+ }
+ // Roles carry their own row policies, which would OR with the site scope;
+ // REVOKE ALL doesn't touch role grants, so drop them explicitly.
+ const roles = await clickhouse
+ .query({
+ query: "SELECT granted_role_name AS role FROM system.role_grants WHERE user_name = {user:String}",
+ query_params: { user },
+ format: "JSONEachRow",
+ })
+ .then(result => result.json<{ role: string }>());
+ for (const { role } of roles) {
+ await clickhouse.exec({ query: `REVOKE ${quoteIdentifier(role)} FROM ${user}` });
+ }
+ if (await rowPoliciesHideUnnamedUsers(database, user)) {
+ await clickhouse.exec({ query: `REVOKE ALL ON *.* FROM ${user}` });
+ await clickhouse.exec({ query: `DROP ROW POLICY IF EXISTS ${siteScopePolicyName(user)} ON ${database}.events` });
+ logger.error(
+ { user },
+ "ClickHouse hides rows from users without a row policy (access_control_improvements.users_without_row_policies_can_read_rows=false). Custom SQL queries and dashboards are disabled; set it to true and restart."
+ );
+ return;
+ }
+ for (const query of access) {
await clickhouse.exec({ query });
}
logger.info({ user, database }, "ClickHouse query user provisioned");
} catch (error) {
logger.error(
{ err: error, user },
- `Could not provision ClickHouse user "${user}" (the main ClickHouse user needs access_management). Custom SQL queries and dashboards will fail until it exists with SELECT on ${database}.events β€” see docs: self-hosting-advanced β†’ Custom SQL queries`
+ `Could not provision ClickHouse user "${user}" (the main ClickHouse user needs access_management). Custom SQL queries and dashboards are disabled until it exists with SELECT on ${database}.events behind the ${siteScopePolicyName(user)} row policy`
);
}
@@ -81,3 +161,48 @@ export async function provisionQueryUser() {
);
}
}
+
+export const QUERY_USER_UNAVAILABLE_ERROR =
+ "Custom SQL is unavailable: the ClickHouse query user is not fully provisioned. Check the server logs.";
+
+const ISOLATION_CHECK_TTL_MS = 60_000;
+let isolationCheck: { at: number; result: Promise<boolean> } | null = null;
+
+// Whether events reads by the query user are confined to the site-scope row
+// policy: the policy exists, still keys on the scope header, and no other
+// permissive policy on events reaches the user (permissive policies are ORed,
+// so one would widen the scope), and the user holds no role, whose policies
+// would apply too. Checked per process from system.row_policies,
+// so it holds under CLUSTER_WORKERS (provisioning runs only on the primary),
+// after a failed upgrade that left an older user's grant in place, and for a
+// hand-provisioned user. Fails closed on any error.
+export function isQueryUserIsolated(): Promise<boolean> {
+ if (isolationCheck && Date.now() - isolationCheck.at < ISOLATION_CHECK_TTL_MS) {
+ return isolationCheck.result;
+ }
+ const database = process.env.CLICKHOUSE_DB || "default";
+ const user = CLICKHOUSE_QUERY_USER;
+ const result = clickhouse
+ .query({
+ query: `
+ SELECT
+ countIf(short_name = {policy:String} AND NOT is_restrictive AND has(apply_to_list, {user:String})
+ AND NOT apply_to_all AND position(ifNull(select_filter, ''), {header:String}) > 0) AS scoped,
+ countIf(short_name != {policy:String} AND NOT is_restrictive
+ AND (has(apply_to_list, {user:String}) OR (apply_to_all AND NOT has(apply_to_except, {user:String})))) AS widening,
+ (SELECT count() FROM system.role_grants WHERE user_name = {user:String}) AS roles
+ FROM system.row_policies
+ WHERE database = {database:String} AND table = 'events'
+ `,
+ query_params: { policy: siteScopePolicyName(user), user, header: SITE_SCOPE_HEADER, database },
+ format: "JSONEachRow",
+ })
+ .then(response => response.json<{ scoped: string | number; widening: string | number; roles: string | number }>())
+ .then(([row]) => Number(row?.scoped) === 1 && Number(row?.widening) === 0 && Number(row?.roles) === 0)
+ .catch(error => {
+ logger.error({ err: error, user }, "Could not verify the ClickHouse query user's row policy");
+ return false;
+ });
+ isolationCheck = { at: Date.now(), result };
+ return result;
+}
diff --git a/server/src/lib/auth-middleware.test.ts b/server/src/lib/auth-middleware.test.ts
index 2f360bfec..a50be6c5c 100644
--- a/server/src/lib/auth-middleware.test.ts
+++ b/server/src/lib/auth-middleware.test.ts
@@ -344,3 +344,56 @@ describe("auth middleware rate limit reporting", () => {
expect(response.headers["ratelimit-limit"]).toBeUndefined();
});
});
+
+describe("resolveSiteId", () => {
+ let app: FastifyInstance;
+
+ beforeEach(async () => {
+ vi.clearAllMocks();
+ app = Fastify();
+ app.get("/sites/:siteId", { preHandler: [resolveSiteId] as any }, async request => ({
+ siteId: (request.params as { siteId: string }).siteId,
+ }));
+ await app.ready();
+ });
+
+ afterEach(async () => {
+ await app.close();
+ });
+
+ // The access check reads the param with Number(), handlers with parseInt();
+ // any spelling they'd read differently must never reach either.
+ it.each(["1e1", "0x5", "5.0", "+5", "-5", "5a", "%205", "Infinity"])("rejects the non-decimal id %s", async id => {
+ const response = await app.inject({ method: "GET", url: `/sites/${id}` });
+ expect(response.statusCode).toBe(404);
+ });
+
+ it("canonicalizes a decimal id so every reader sees the same number", async () => {
+ expect((await app.inject({ method: "GET", url: "/sites/0042" })).json()).toEqual({ siteId: "42" });
+ expect((await app.inject({ method: "GET", url: "/sites/7" })).json()).toEqual({ siteId: "7" });
+ });
+
+ it("resolves a tracking id to its numeric site id", async () => {
+ const { siteConfig } = await import("./siteConfig.js");
+ vi.mocked(siteConfig.resolveSiteId).mockResolvedValueOnce(123);
+ expect((await app.inject({ method: "GET", url: "/sites/abcdef123456" })).json()).toEqual({ siteId: "123" });
+ });
+
+ it("resolves an all-digit tracking id before treating it as a numeric id", async () => {
+ const { siteConfig } = await import("./siteConfig.js");
+ vi.mocked(siteConfig.resolveSiteId).mockResolvedValueOnce(9);
+ expect((await app.inject({ method: "GET", url: "/sites/123456789012" })).json()).toEqual({ siteId: "9" });
+ });
+
+ it("canonicalizes a long zero-padded id that is not a tracking id", async () => {
+ expect((await app.inject({ method: "GET", url: "/sites/000042" })).json()).toEqual({ siteId: "42" });
+ });
+
+ it("rejects an id beyond the safe integer range", async () => {
+ expect((await app.inject({ method: "GET", url: "/sites/99999999999999999999" })).statusCode).toBe(404);
+ });
+
+ it("rejects an unknown non-numeric tracking id", async () => {
+ expect((await app.inject({ method: "GET", url: "/sites/abcdef123456" })).statusCode).toBe(404);
+ });
+});
diff --git a/server/src/lib/auth-middleware.ts b/server/src/lib/auth-middleware.ts
index 24c6e7695..cf028b0ca 100644
--- a/server/src/lib/auth-middleware.ts
+++ b/server/src/lib/auth-middleware.ts
@@ -160,26 +160,38 @@ const attachApiKeyUser = (request: FastifyRequest, reply: FastifyReply, apiKeyRe
/**
* Resolves string site IDs to numeric IDs and updates request params.
* Should be first in preHandler chain for routes with site params.
+ *
+ * Always leaves `params.siteId` as a canonical decimal integer (or rejects the
+ * request). The access check reads the param with Number() while handlers
+ * parse it themselves (often parseInt), and the two disagree on non-decimal
+ * spellings β€” the check would approve one site while the handler queried
+ * another.
*/
export const resolveSiteId: AuthMiddleware = async (request, reply) => {
const params = request.params as Record<string, string>;
const siteId = getSiteIdFromParams(request);
- if (!siteId || String(siteId).length <= 4) {
+ if (siteId === undefined) {
return;
}
- const numericId = await siteConfig.resolveSiteId(siteId);
- if (numericId !== null) {
- params.siteId = String(numericId);
- return;
+ // Tracking ids are longer than any numeric id in use and may be all digits,
+ // so try them first.
+ if (siteId.length > 4) {
+ const numericId = await siteConfig.resolveSiteId(siteId);
+ if (numericId !== null) {
+ params.siteId = String(numericId);
+ return;
+ }
}
- // A digit-only identifier is already in the shape the handlers expect; leave
- // it alone and let the access check below produce the 404/403, as before.
- if (!/^\d+$/.test(siteId)) {
+ // Otherwise only a plain decimal id is accepted; the access check below
+ // produces the 404/403 for one that doesn't exist or isn't the caller's.
+ const numericId = Number(siteId);
+ if (!/^\d+$/.test(siteId) || !Number.isSafeInteger(numericId)) {
return reply.status(404).send({ error: "Site not found" });
}
+ params.siteId = String(numericId);
};
/**