πΈ Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance analytics API with site-specific query capabilities and PDF export validation
Bill Yang0fa7ec12e012
diff --git a/client/messages/en.json b/client/messages/en.jsonindex f2b8c9eed..9f670893e 100644--- a/client/messages/en.json+++ b/client/messages/en.json@@ -282,7 +282,6 @@"9tjZ7S": "Failed to create user","9uKBh7": "Remote config","9vpJ9O": "Sites ({count})",- "A+G23r": "Share a private link","A/lLjO": "Generating...","A4AHkc": "Min","A5E371": "The page you're looking for doesn't exist or has been moved. Let's get you back to analyzing your data.",@@ -1560,5 +1559,6 @@"zvbn/a": "Use a different email","zxvhnE": "Daily","zyQ3nw": "Add Another Property",- "zz0om1": "IP Exclusions"+ "zz0om1": "IP Exclusions",+ "8GNnVT": "Share or export data"}diff --git a/client/src/api/analytics/endpoints/customQuery.ts b/client/src/api/analytics/endpoints/customQuery.tsindex 8fc0ac7cb..d1f0021d8 100644--- a/client/src/api/analytics/endpoints/customQuery.ts+++ b/client/src/api/analytics/endpoints/customQuery.ts@@ -28,10 +28,10 @@ export type GenerateCustomQueryRequest = {history?: CustomQueryGenerationMessage[];};-export function runCustomQuery(organizationId: string, query: string) {+export function runCustomQuery(organizationId: string, query: string, siteId?: number) {return authedFetch<RunCustomQueryResponse>(`/organizations/${organizationId}/analytics/query`, undefined, {method: "POST",- data: { query },+ data: { query, siteId },});}diff --git a/client/src/api/analytics/hooks/useCustomQuery.ts b/client/src/api/analytics/hooks/useCustomQuery.tsindex 51de5c0f0..9657b59d2 100644--- a/client/src/api/analytics/hooks/useCustomQuery.ts+++ b/client/src/api/analytics/hooks/useCustomQuery.ts@@ -3,8 +3,8 @@ import { CustomQueryGenerationMessage, generateCustomQuery, runCustomQuery } froexport function useRunCustomQuery() {return useMutation({- mutationFn: ({ organizationId, query }: { organizationId: string; query: string }) =>- runCustomQuery(organizationId, query),+ mutationFn: ({ organizationId, query, siteId }: { organizationId: string; query: string; siteId?: number }) =>+ runCustomQuery(organizationId, query, siteId),});}diff --git a/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts b/client/src/app/[site]/components/SubHeader/Export/exportPdf.tsindex e1af5576c..08d6c754c 100644--- a/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts+++ b/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts@@ -1,4 +1,5 @@import { Filter } from "@rybbit/shared";+import { DateTime } from "luxon";import { exportPdfReport } from "../../../../../api/analytics/endpoints";import { getStartAndEndDate } from "../../../../../api/utils";import { Time } from "../../../../../components/DateSelector/types";@@ -10,12 +11,40 @@ interface ExportPdfParams {timeZone: string;}-export async function exportPdf({ site, time, filters, timeZone }: ExportPdfParams): Promise<void> {+// The PDF report is a whole-day summary and its server contract requires a+// concrete YYYY-MM-DD range. Map every time mode (including past-minutes,+// all-time, and exact datetime ranges) to whole days so the export never sends+// empty dates, which would otherwise produce an all-time report or a 400.+export function getPdfDateRange(time: Time, timeZone: string): { startDate: string; endDate: string } {+ const today = DateTime.now().setZone(timeZone);++ if (time.mode === "past-minutes") {+ return {+ startDate: today.minus({ minutes: time.pastMinutesStart }).toISODate() ?? "",+ endDate: today.toISODate() ?? "",+ };+ }+ if (time.mode === "all-time") {+ // No stored site-creation date on the client; use a wide lower bound.+ return { startDate: "2020-01-01", endDate: today.toISODate() ?? "" };+ }+ if (time.mode === "range" && time.startTime && time.endTime) {+ return { startDate: time.startDate, endDate: time.endDate };+ }+const { startDate, endDate } = getStartAndEndDate(time);+ return {+ startDate: startDate ?? today.toISODate() ?? "",+ endDate: endDate ?? today.toISODate() ?? "",+ };+}++export async function exportPdf({ site, time, filters, timeZone }: ExportPdfParams): Promise<void> {+ const { startDate, endDate } = getPdfDateRange(time, timeZone);await exportPdfReport(site, {- startDate: startDate ?? "",- endDate: endDate ?? "",+ startDate,+ endDate,timeZone,filters,});diff --git a/client/src/app/[site]/dashboards/examples.ts b/client/src/app/[site]/dashboards/examples.tsindex eb17fd0e9..c3ec48c12 100644--- a/client/src/app/[site]/dashboards/examples.ts+++ b/client/src/app/[site]/dashboards/examples.ts@@ -159,7 +159,7 @@ ORDER BY day`,category: "Traffic",vizType: "area",mapping: { xColumn: "time", yColumns: ["pageviews"] },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,countIf(type = 'pageview') AS pageviewsFROM scoped_eventsGROUP BY time@@ -172,7 +172,7 @@ ORDER BY time`,category: "Traffic",vizType: "line",mapping: { xColumn: "time", yColumns: ["sessions", "users"] },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,countDistinct(session_id) AS sessions,countDistinct(user_id) AS usersFROM scoped_events@@ -186,7 +186,7 @@ ORDER BY time`,category: "Traffic",vizType: "line",mapping: { xColumn: "time", yColumns: ["bounce_rate"] },- sql: `SELECT toStartOfInterval(session_start, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(session_start, {{tz}}), INTERVAL {{bucket}})) AS time,round(100 * countIf(pages = 1) / count(), 1) AS bounce_rateFROM (SELECT session_id,@@ -460,7 +460,7 @@ LIMIT 20`,beyondPrebuilt: true,vizType: "line",mapping: { xColumn: "time", yColumns: ["avg_seconds"] },- sql: `SELECT toStartOfInterval(session_start, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(session_start, {{tz}}), INTERVAL {{bucket}})) AS time,round(avg(duration_seconds)) AS avg_secondsFROM (SELECT session_id,@@ -508,9 +508,9 @@ WHERE has_pricing = 1`,WHERE user_id != ''GROUP BY user_id)-SELECT toStartOfInterval(e.timestamp, INTERVAL {{bucket}}) AS time,+SELECT toDateTime(toStartOfInterval(toTimeZone(e.timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,if(- toStartOfInterval(e.timestamp, INTERVAL {{bucket}}) = toStartOfInterval(f.first_seen_at, INTERVAL {{bucket}}),+ toStartOfInterval(toTimeZone(e.timestamp, {{tz}}), INTERVAL {{bucket}}) = toStartOfInterval(toTimeZone(f.first_seen_at, {{tz}}), INTERVAL {{bucket}}),'New in range','Returning in range') AS visitor_type,@@ -629,7 +629,7 @@ LIMIT 50`,beyondPrebuilt: true,vizType: "line",mapping: { xColumn: "time", yColumns: ["conversion_rate_pct"] },- sql: `SELECT toStartOfInterval(session_start, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(session_start, {{tz}}), INTERVAL {{bucket}})) AS time,round(100 * countIf(converted = 1) / nullIf(count(), 0), 1) AS conversion_rate_pctFROM (SELECT session_id,@@ -700,7 +700,7 @@ LIMIT 50`,category: "Events",vizType: "line",mapping: { xColumn: "time", yColumns: ["events"], seriesColumn: "event_name" },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,event_name,count() AS eventsFROM scoped_events@@ -809,7 +809,7 @@ LIMIT 50`,category: "Events",vizType: "line",mapping: { xColumn: "time", yColumns: ["errors"] },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,count() AS errorsFROM scoped_eventsWHERE type = 'error'@@ -840,7 +840,7 @@ LIMIT 50`,beyondPrebuilt: true,vizType: "line",mapping: { xColumn: "time", yColumns: ["errors_per_1k_pageviews"] },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,round(1000 * countIf(type = 'error') / nullIf(countIf(type = 'pageview'), 0), 1) AS errors_per_1k_pageviewsFROM scoped_eventsGROUP BY time@@ -963,7 +963,7 @@ LIMIT 50`,beyondPrebuilt: true,vizType: "line",mapping: { xColumn: "time", yColumns: ["lcp_p75", "inp_p75"] },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,round(quantile(0.75)(lcp)) AS lcp_p75,round(quantile(0.75)(inp)) AS inp_p75FROM scoped_events@@ -1158,7 +1158,7 @@ LIMIT 50`,beyondPrebuilt: true,vizType: "line",mapping: { xColumn: "time", yColumns: ["sessions"], seriesColumn: "channel" },- sql: `SELECT toStartOfInterval(timestamp, INTERVAL {{bucket}}) AS time,+ sql: `SELECT toDateTime(toStartOfInterval(toTimeZone(timestamp, {{tz}}), INTERVAL {{bucket}})) AS time,channel,countDistinct(session_id) AS sessionsFROM scoped_eventsdiff --git a/client/src/app/[site]/dashboards/utils.ts b/client/src/app/[site]/dashboards/utils.tsindex f72966c3c..cd775d8d9 100644--- a/client/src/app/[site]/dashboards/utils.ts+++ b/client/src/app/[site]/dashboards/utils.ts@@ -154,11 +154,17 @@ export function buildWideData(rows: CustomQueryRow[], mapping: DashboardCardMappconst TICK_TARGET = 8;-/** Parse a ClickHouse date/datetime string ("yyyy-MM-dd[ HH:mm:ss]"). */+/**+ * Parse a ClickHouse date/datetime string ("yyyy-MM-dd[ HH:mm:ss]"). Time-series+ * cards bucket in the viewer's timezone (toTimeZone(..., {{tz}})), so the bucket+ * start is a local wall-clock value β interpret it in that same timezone, exactly+ * as the main analytics charts do (Chart.tsx: fromSQL(time, { zone: timezone })).+ */export function parseChartDate(value: unknown): DateTime | null {if (typeof value !== "string" || value === "") return null;- let dt = DateTime.fromSQL(value, { zone: "utc" });- if (!dt.isValid) dt = DateTime.fromISO(value, { zone: "utc" });+ const zone = getTimezone();+ let dt = DateTime.fromSQL(value, { zone });+ if (!dt.isValid) dt = DateTime.fromISO(value, { zone });return dt.isValid ? dt : null;}diff --git a/client/src/app/[site]/main/components/sections/SearchConsole.tsx b/client/src/app/[site]/main/components/sections/SearchConsole.tsxindex d34ddce43..a4e51c162 100644--- a/client/src/app/[site]/main/components/sections/SearchConsole.tsx+++ b/client/src/app/[site]/main/components/sections/SearchConsole.tsx@@ -3,6 +3,7 @@ import round from "lodash/round";import { Expand, Info, SquareArrowOutUpRight } from "lucide-react";import { useExtracted } from "next-intl";import { useState } from "react";+import { useCurrentSite } from "../../../../../api/admin/hooks/useSites";import { GSCDimension } from "../../../../../api/gsc/endpoints";import { useConnectGSC } from "../../../../../api/gsc/hooks/useConnectGSC";import { useGetGSCConnection } from "../../../../../api/gsc/hooks/useGetGSCConnection";@@ -21,17 +22,29 @@ type Tab = "queries" | "pages" | "countries" | "devices";function ConnectPrompt() {const { mutate: connect, isPending } = useConnectGSC();+ const { site } = useCurrentSite();const t = useExtracted();+ // Connecting completes an OAuth flow that requires site admin access on the+ // server. Members would be stranded on a 403 after Google consent, so show+ // them an explanation instead of a button they can't complete.+ const canConnect = site?.isOwner ?? false;+return (<div className="flex flex-col items-center justify-center mt-12 gap-4"><div className="text-sm text-neutral-600 dark:text-neutral-400 text-center max-w-sm">{t("Connect your Google Search Console account to view search performance data including top keywords and pages.")}</div>- <Button onClick={() => connect()} disabled={isPending}>- <SiGoogle />- {isPending ? t("Connecting...") : t("Connect Google Search Console")}- </Button>+ {canConnect ? (+ <Button onClick={() => connect()} disabled={isPending}>+ <SiGoogle />+ {isPending ? t("Connecting...") : t("Connect Google Search Console")}+ </Button>+ ) : (+ <div className="text-sm text-neutral-500 text-center max-w-sm">+ {t("Ask a site admin to connect Google Search Console.")}+ </div>+ )}</div>);}diff --git a/client/src/app/[site]/query/page.tsx b/client/src/app/[site]/query/page.tsxindex 0a1d1a2eb..1418d7c11 100644--- a/client/src/app/[site]/query/page.tsx+++ b/client/src/app/[site]/query/page.tsx@@ -164,7 +164,11 @@ export default function QueryPage() {updateTab(tab.id, { resultError: null });try {- const result = await runMutation.mutateAsync({ organizationId, query: tab.query });+ const result = await runMutation.mutateAsync({+ organizationId,+ query: tab.query,+ siteId: Number.isFinite(siteId) ? siteId : undefined,+ });updateTab(tab.id, { rows: result.data, hasRun: true, resultError: null });} catch (error) {updateTab(tab.id, {diff --git a/server/src/api/analytics/generatePdfReport.ts b/server/src/api/analytics/generatePdfReport.tsindex dcc5bc591..ef5bff6ea 100644--- a/server/src/api/analytics/generatePdfReport.ts+++ b/server/src/api/analytics/generatePdfReport.ts@@ -1,12 +1,21 @@+import { eq } from "drizzle-orm";import { FastifyReply, FastifyRequest } from "fastify";+import { DateTime } from "luxon";import { z } from "zod";+import { db } from "../../db/postgres/postgres.js";+import { sites } from "../../db/postgres/schema.js";+import { IS_CLOUD } from "../../lib/const.js";import { pdfReportService } from "../../services/pdfReports/pdfReportService.js";-import { DateTime } from "luxon";+import { getSubscriptionInner } from "../stripe/getSubscription.js";+import { getFilterStatement } from "./utils/getFilterStatement.js";+import { validateFilters } from "./utils/query-validation.js";++const dateRegex = /^\d{4}-\d{2}-\d{2}$/;const QuerystringSchema = z.object({- start_date: z.string(),- end_date: z.string(),- time_zone: z.string(),+ start_date: z.string().regex(dateRegex, "start_date must be a valid YYYY-MM-DD date"),+ end_date: z.string().regex(dateRegex, "end_date must be a valid YYYY-MM-DD date"),+ time_zone: z.string().min(1, "time_zone is required"),filters: z.string().optional(),});@@ -17,29 +26,71 @@ interface GeneratePdfReportRequest {Querystring: z.infer<typeof QuerystringSchema>;}-export async function generatePdfReport(- request: FastifyRequest<GeneratePdfReportRequest>,- reply: FastifyReply-) {- try {- const { siteId } = request.params;- const queryResult = QuerystringSchema.safeParse(request.query);+// PDF export is a paid feature. Mirror the client-side gate (planName !== "free"+// and not the entry AppSumo tiers) so the endpoint can't be called directly to+// bypass it. Self-hosted deployments have no billing and are always allowed.+const PLAN_GATED_APPSUMO_TIERS = ["appsumo-1", "appsumo-2"];++async function isPdfExportAllowed(organizationId: string | null): Promise<boolean> {+ if (!IS_CLOUD) {+ return true;+ }+ if (!organizationId) {+ return false;+ }+ const subscription = await getSubscriptionInner(organizationId);+ const planName = subscription?.planName ?? "free";+ return planName !== "free" && !PLAN_GATED_APPSUMO_TIERS.includes(planName);+}++export async function generatePdfReport(request: FastifyRequest<GeneratePdfReportRequest>, reply: FastifyReply) {+ const queryResult = QuerystringSchema.safeParse(request.query);++ if (!queryResult.success) {+ return reply.status(400).send({+ error: "Validation error",+ details: queryResult.error.flatten(),+ });+ }++ const { start_date, end_date, time_zone, filters } = queryResult.data;+ const siteId = Number(request.params.siteId);- if (!queryResult.success) {+ // Validate filters up front so malformed JSON, unknown parameters, or invalid+ // regex patterns return 400 instead of surfacing as a 500 from deep in the+ // report service. getFilterStatement runs the same validation the service does.+ let parsedFilters: ReturnType<typeof validateFilters> | undefined;+ if (filters) {+ try {+ parsedFilters = validateFilters(filters);+ getFilterStatement(filters, siteId);+ } catch (error) {return reply.status(400).send({- error: "Validation error",- details: queryResult.error.flatten(),+ error: error instanceof Error ? error.message : "Invalid filters",});}+ }++ try {+ const site = await db.query.sites.findFirst({+ where: eq(sites.siteId, siteId),+ columns: { organizationId: true },+ });++ if (!site) {+ return reply.status(404).send({ error: `Site not found: ${siteId}` });+ }- const { start_date, end_date, time_zone, filters } = queryResult.data;+ if (!(await isPdfExportAllowed(site.organizationId))) {+ return reply.status(403).send({ error: "PDF export requires a paid plan" });+ }const pdfBuffer = await pdfReportService.generatePdfReport({- siteId: Number(siteId),+ siteId,startDate: start_date,endDate: end_date,timeZone: time_zone,- filters: filters ? JSON.parse(filters) : undefined,+ filters: parsedFilters,});const formattedStart = DateTime.fromISO(start_date).toFormat("yyyy-MM-dd");diff --git a/server/src/api/analytics/runCustomQuery.ts b/server/src/api/analytics/runCustomQuery.tsindex ab01f04ee..510c6352e 100644--- a/server/src/api/analytics/runCustomQuery.ts+++ b/server/src/api/analytics/runCustomQuery.ts@@ -9,6 +9,9 @@ const MAX_RESULT_ROWS = 1000;const requestBodySchema = z.object({query: z.string().trim().min(1).max(MAX_CUSTOM_QUERY_LENGTH),+ // When provided, scope scoped_events to this single site (the per-site Query+ // page). Omitted β query spans every site the caller can access in the org.+ siteId: z.number().int().positive().optional(),});export async function runCustomQuery(@@ -31,14 +34,22 @@ export async function runCustomQuery(}const userSites = await getSitesUserHasAccessTo(request);- const siteIds = userSites+ const accessibleSiteIds = userSites.filter(site => site.organizationId === request.params.organizationId).map(site => site.siteId);- if (siteIds.length === 0) {+ if (accessibleSiteIds.length === 0) {return reply.status(403).send({ error: "No access to organization or no sites found" });}+ let siteIds = accessibleSiteIds;+ if (body.data.siteId !== undefined) {+ if (!accessibleSiteIds.includes(body.data.siteId)) {+ return reply.status(403).send({ error: "No access to the requested site" });+ }+ siteIds = [body.data.siteId];+ }+const query = `WITH scoped_events AS (SELECT *diff --git a/server/src/api/analytics/runDashboardCardQuery.ts b/server/src/api/analytics/runDashboardCardQuery.tsindex d46322fb8..a88fd7a2e 100644--- a/server/src/api/analytics/runDashboardCardQuery.ts+++ b/server/src/api/analytics/runDashboardCardQuery.ts@@ -1,4 +1,5 @@import { FastifyReply, FastifyRequest } from "fastify";+import SqlString from "sqlstring";import { z } from "zod";import { clickhouse } from "../../db/clickhouse/clickhouse.js";import { MAX_CUSTOM_QUERY_LENGTH, normalizeCustomQuery, validateScopedQuery } from "./utils/customQueryValidation.js";@@ -8,6 +9,7 @@ const MAX_EXECUTION_TIME_SECONDS = 10;const MAX_RESULT_ROWS = 1000;const BUCKET_TOKEN = /\{\{\s*bucket\s*\}\}/gi;+const TZ_TOKEN = /\{\{\s*tz\s*\}\}/gi;const requestBodySchema = z.object({query: z.string().trim().min(1).max(MAX_CUSTOM_QUERY_LENGTH),@@ -42,11 +44,17 @@ export async function runDashboardCardQuery(return reply.status(400).send({ error: body.error.errors[0]?.message ?? "Invalid request body" });}- // Substitute {{bucket}} BEFORE validation so the validator never sees the- // template token. The value comes from an allowlisted enum mapped to a- // constant interval string, so this is injection-safe.+ // Substitute {{bucket}} and {{tz}} BEFORE validation so the validator never+ // sees the template tokens. {{bucket}} comes from an allowlisted enum mapped+ // to a constant interval string; {{tz}} is SqlString-escaped to a quoted+ // literal β both injection-safe. {{tz}} lets time-series cards bucket in the+ // viewer's timezone (e.g. toStartOfInterval(toTimeZone(timestamp, {{tz}}), ...))+ // so day buckets align to local calendar days, matching the standard charts.const bucketInterval = bucketIntervalMap[body.data.bucket ?? "hour"];- const substitutedQuery = body.data.query.replace(BUCKET_TOKEN, bucketInterval);+ const timeZoneLiteral = SqlString.escape(body.data.timeZone || "UTC");+ const substitutedQuery = body.data.query+ .replace(BUCKET_TOKEN, bucketInterval)+ .replace(TZ_TOKEN, timeZoneLiteral);const validationError = validateScopedQuery(substitutedQuery);if (validationError) {diff --git a/server/src/api/analytics/utils/customQueryValidation.test.ts b/server/src/api/analytics/utils/customQueryValidation.test.tsindex 2db692aa7..1f850716d 100644--- a/server/src/api/analytics/utils/customQueryValidation.test.ts+++ b/server/src/api/analytics/utils/customQueryValidation.test.ts@@ -77,6 +77,28 @@ describe("validateScopedQuery β table reference scoping", () => {).toBeNull();});+ it("does not treat IN function(...) value expressions as table references", () => {+ expect(validateScopedQuery("SELECT count() FROM scoped_events WHERE country IN tuple('US','GB')")).toBeNull();+ expect(validateScopedQuery("SELECT count() FROM scoped_events WHERE country IN ('US','GB')")).toBeNull();+ });++ it("allows ARRAY JOIN over an array expression without flagging it as a table", () => {+ expect(+ validateScopedQuery("SELECT k, count() FROM scoped_events ARRAY JOIN mapKeys(url_parameters) AS k GROUP BY k")+ ).toBeNull();+ expect(+ validateScopedQuery("SELECT k FROM scoped_events LEFT ARRAY JOIN mapKeys(url_parameters) AS k")+ ).toBeNull();+ });++ it("still blocks a real JOIN to an unauthorized table after an ARRAY JOIN", () => {+ expect(+ validateScopedQuery(+ "SELECT k FROM scoped_events ARRAY JOIN mapKeys(url_parameters) AS k JOIN events e 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);diff --git a/server/src/api/analytics/utils/customQueryValidation.ts b/server/src/api/analytics/utils/customQueryValidation.tsindex afe486687..665a68715 100644--- a/server/src/api/analytics/utils/customQueryValidation.ts+++ b/server/src/api/analytics/utils/customQueryValidation.ts@@ -282,13 +282,21 @@ function collectTableReferences(query: string): string[] {}};- const keywordPattern = /\b(FROM|JOIN)\b/gi;+ // Match ARRAY JOIN ahead of a bare JOIN so its inner JOIN isn't rescanned.+ const keywordPattern = /\b(ARRAY\s+JOIN|FROM|JOIN)\b/gi;let match: RegExpExecArray | null;while ((match = keywordPattern.exec(query)) !== null) {const afterKeyword = match.index + match[0].length;+ const keyword = match[1].toLowerCase();++ // ARRAY JOIN takes an array expression (a column or function like+ // mapKeys(...)), not a table reference β skip it.+ if (keyword.includes("array")) {+ continue;+ }// A JOIN introduces exactly one table reference.- if (match[1].toLowerCase() === "join") {+ if (keyword === "join") {readReference(afterKeyword);continue;}@@ -336,7 +344,13 @@ function collectInTableReferences(query: string): string[] {while ((match = inPattern.exec(query)) !== null) {const start = skipWhitespace(query, match.index + match[0].length);if (start < query.length && isIdentifierStart(query[start])) {- references.push(readIdentifier(query, start)[0]);+ const [identifier, end] = readIdentifier(query, start);+ // `expr IN function(...)` (e.g. tuple('US','GB')) is a value expression, not+ // the `expr IN table` shorthand β a following "(" marks it as a function call.+ if (query[skipWhitespace(query, end)] === "(") {+ continue;+ }+ references.push(identifier);}}diff --git a/server/src/api/gsc/connect.ts b/server/src/api/gsc/connect.tsindex 65eed4bca..3b7f7a613 100644--- a/server/src/api/gsc/connect.ts+++ b/server/src/api/gsc/connect.ts@@ -1,6 +1,6 @@import { FastifyReply, FastifyRequest } from "fastify";import { ConnectGSCRequest } from "./types.js";-import { getSessionFromReq, getUserHasAccessToSite } from "../../lib/auth-utils.js";+import { getSessionFromReq, getUserHasAdminAccessToSite } from "../../lib/auth-utils.js";import { logger } from "../../lib/logger/logger.js";import { signGSCState } from "./utils.js";@@ -17,8 +17,10 @@ export async function connectGSC(req: FastifyRequest<ConnectGSCRequest>, res: Fareturn res.status(400).send({ error: "Invalid site ID" });}- // Check if user has access to this site- const hasAccess = await getUserHasAccessToSite(req, numericSiteId);+ // Require admin access to match the callback, which writes OAuth tokens and+ // demands admin. A member who could start the flow would be stranded on a+ // 403 after Google consent.+ const hasAccess = await getUserHasAdminAccessToSite(req, numericSiteId);if (!hasAccess) {return res.status(403).send({ error: "Access denied" });}diff --git a/server/src/api/gsc/disconnect.ts b/server/src/api/gsc/disconnect.tsindex e7a6bf40f..7ad800675 100644--- a/server/src/api/gsc/disconnect.ts+++ b/server/src/api/gsc/disconnect.ts@@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";import { DisconnectGSCRequest } from "./types.js";import { gscConnections } from "../../db/postgres/schema.js";import { eq } from "drizzle-orm";-import { getUserHasAccessToSite } from "../../lib/auth-utils.js";+import { getUserHasAdminAccessToSite } from "../../lib/auth-utils.js";import { db } from "../../db/postgres/postgres.js";import { logger } from "../../lib/logger/logger.js";@@ -19,8 +19,9 @@ export async function disconnectGSC(req: FastifyRequest<DisconnectGSCRequest>, rreturn res.status(400).send({ error: "Invalid site ID" });}- // Check if user has access to this site- const hasAccess = await getUserHasAccessToSite(req, numericSiteId);+ // Managing (removing) a GSC connection is an admin action, consistent with+ // connect/select-property.+ const hasAccess = await getUserHasAdminAccessToSite(req, numericSiteId);if (!hasAccess) {return res.status(403).send({ error: "Access denied" });}diff --git a/server/src/api/gsc/selectProperty.ts b/server/src/api/gsc/selectProperty.tsindex 47beb5a1d..acb582972 100644--- a/server/src/api/gsc/selectProperty.ts+++ b/server/src/api/gsc/selectProperty.ts@@ -1,9 +1,10 @@import { FastifyReply, FastifyRequest } from "fastify";import { gscConnections } from "../../db/postgres/schema.js";import { eq } from "drizzle-orm";-import { getUserHasAccessToSite } from "../../lib/auth-utils.js";+import { getUserHasAdminAccessToSite } from "../../lib/auth-utils.js";import { db } from "../../db/postgres/postgres.js";import { logger } from "../../lib/logger/logger.js";+import { getGSCProperties, refreshGSCToken } from "./utils.js";interface SelectPropertyRequest {Params: {@@ -31,12 +32,25 @@ export async function selectGSCProperty(req: FastifyRequest<SelectPropertyRequesreturn res.status(400).send({ error: "Property URL is required" });}- // Check if user has access to this site- const hasAccess = await getUserHasAccessToSite(req, numericSiteId);+ // Managing the connection is an admin action, consistent with connect/disconnect.+ const hasAccess = await getUserHasAdminAccessToSite(req, numericSiteId);if (!hasAccess) {return res.status(403).send({ error: "Access denied" });}+ // The property must be one the connected Google account actually owns.+ // Accepting an arbitrary string would silently break every later data fetch+ // (Google returns 403 for unowned properties) until the site is reconnected.+ const accessToken = await refreshGSCToken(numericSiteId);+ if (!accessToken) {+ return res.status(404).send({ error: "GSC connection not found" });+ }++ const availableProperties = await getGSCProperties(accessToken);+ if (!availableProperties.includes(propertyUrl)) {+ return res.status(400).send({ error: "Selected property is not available for the connected account" });+ }+// Update the connection with the selected propertyconst result = await db.update(gscConnections)diff --git a/server/src/index.ts b/server/src/index.tsindex dd7d23e42..346ab857f 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -350,7 +350,7 @@ async function analyticsRoutes(fastify: FastifyInstance) {fastify.get("/sites/:siteId/bots/overview", publicSite, getBotOverview);fastify.get("/sites/:siteId/bots/time-series", publicSite, getBotTimeSeries);fastify.get("/sites/:siteId/bots/by-dimension", publicSite, getBotDimension);- fastify.get("/sites/:siteId/export/pdf", publicSite, generatePdfReport);+ fastify.get("/sites/:siteId/export/pdf", authSite, generatePdfReport);}async function sessionReplayRoutes(fastify: FastifyInstance) {@@ -423,11 +423,11 @@ async function userRoutes(fastify: FastifyInstance) {async function gscRoutes(fastify: FastifyInstance) {// GOOGLE SEARCH CONSOLE- fastify.get("/sites/:siteId/gsc/connect", authSite, connectGSC);+ fastify.get("/sites/:siteId/gsc/connect", adminSite, connectGSC);fastify.get("/gsc/callback", gscCallback); // Public - OAuth callbackfastify.get("/sites/:siteId/gsc/status", publicSite, getGSCStatus);- fastify.delete("/sites/:siteId/gsc/disconnect", authSite, disconnectGSC);- fastify.post("/sites/:siteId/gsc/select-property", authSite, selectGSCProperty);+ fastify.delete("/sites/:siteId/gsc/disconnect", adminSite, disconnectGSC);+ fastify.post("/sites/:siteId/gsc/select-property", adminSite, selectGSCProperty);fastify.get("/sites/:siteId/gsc/data", publicSite, getGSCData);}diff --git a/server/src/services/pdfReports/pdfReportService.ts b/server/src/services/pdfReports/pdfReportService.tsindex a7f58fc61..47edcf768 100644--- a/server/src/services/pdfReports/pdfReportService.ts+++ b/server/src/services/pdfReports/pdfReportService.ts@@ -42,10 +42,13 @@ class PdfReportService {throw new Error(`Site not found: ${siteId}`);}- // Calculate previous period (same duration before startDate)+ // Calculate previous period (same duration before startDate). The current+ // window is inclusive of both boundary dates (getTimeStatement makes the end+ // exclusive at end_date + 1 day), so an N-day range spans floor(diff)+1 days.+ // The previous window must match that length, else every growth % is biased.const start = DateTime.fromISO(startDate, { zone: timeZone });const end = DateTime.fromISO(endDate, { zone: timeZone });- const durationDays = Math.ceil(end.diff(start, "days").days) || 1;+ const durationDays = Math.floor(end.diff(start, "days").days) + 1;const previousEnd = start.minus({ days: 1 });const previousStart = previousEnd.minus({ days: durationDays - 1 });