Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Keep customer content out of PostHog analytics and replays (#941)
Ben Senescu28aed627d03c
diff --git a/src/client/lib/posthog-sanitize.test.ts b/src/client/lib/posthog-sanitize.test.tsindex cfb4bb36..0a30bbd2 100644--- a/src/client/lib/posthog-sanitize.test.ts+++ b/src/client/lib/posthog-sanitize.test.ts@@ -1,37 +1,78 @@import { describe, expect, it } from "vitest";import {- sanitizePostHogProperties,+ sanitizePostHogEvent,sanitizePostHogUrl,} from "@/client/lib/posthog-sanitize";-const consentUrl =- "https://app.example.test/oauth-consent?state=state-secret&future_parameter=future-secret";-describe("sanitizePostHogUrl", () => {- it("removes the complete query from OAuth consent URLs, including on event and session properties", () => {- expect(sanitizePostHogUrl(consentUrl)).toBe(- "https://app.example.test/oauth-consent",+ it("drops content query params and keeps allowlisted ones", () => {+ expect(+ sanitizePostHogUrl(+ "https://app.example.test/p/abc/keywords?q=running+shoes&utm_source=newsletter#row",+ ),+ ).toBe("https://app.example.test/p/abc/keywords?utm_source=newsletter");+ expect(sanitizePostHogUrl("/p/abc/domain?domain=acme.test&tab=pages")).toBe(+ "/p/abc/domain?tab=pages",);+ });+});++describe("sanitizePostHogEvent", () => {+ it("sanitizes event and person URLs and removes autocaptured link targets", () => {+ const url = "https://app.example.test/oauth-consent?state=secret";expect(- sanitizePostHogProperties({- $current_url: consentUrl,- $session_entry_url: consentUrl,- $referrer: consentUrl,- event_detail: "kept",+ sanitizePostHogEvent({+ properties: {+ $current_url: url,+ $external_click_url: "https://customer.test/page",+ $elements_chain:+ 'a:attr__href="https://customer.test/page"nth-child="1"',+ },+ $set_once: { $initial_current_url: url },}),).toEqual({- $current_url: "https://app.example.test/oauth-consent",- $session_entry_url: "https://app.example.test/oauth-consent",- $referrer: "https://app.example.test/oauth-consent",- event_detail: "kept",+ properties: {+ $current_url: "https://app.example.test/oauth-consent",+ $elements_chain: 'a:nth-child="1"',+ },+ $set_once: {+ $initial_current_url: "https://app.example.test/oauth-consent",+ },});});- it("preserves ordinary query data while removing email", () => {- expect(- sanitizePostHogUrl(- "https://app.example.test/onboarding?step=2&email=user%40example.test",- ),- ).toBe("https://app.example.test/onboarding?step=2");+ it("sanitizes URLs nested in web vitals and used as heatmap keys", () => {+ const { properties } = sanitizePostHogEvent({+ properties: {+ $web_vitals_LCP_event: {+ $current_url: "https://app.example.test/p/abc?q=running+shoes",+ },+ $heatmap_data: {+ "https://app.example.test/p/abc?domain=acme.test&tab=x": [{ x: 1 }],+ "https://app.example.test/p/abc?domain=other.test&tab=x": [{ x: 2 }],+ },+ },+ });+ expect(properties).toEqual({+ $web_vitals_LCP_event: { $current_url: "https://app.example.test/p/abc" },+ $heatmap_data: {+ "https://app.example.test/p/abc?tab=x": [{ x: 1 }, { x: 2 }],+ },+ });+ });++ it("drops exception messages but keeps bare error codes", () => {+ const { properties } = sanitizePostHogEvent({+ properties: {+ $exception_list: [+ { type: "SyntaxError", value: `"running shoes" is not valid JSON` },+ { type: "Error", value: "PAYMENT_REQUIRED" },+ ],+ },+ });+ expect(properties.$exception_list).toEqual([+ { type: "SyntaxError", value: "" },+ { type: "Error", value: "PAYMENT_REQUIRED" },+ ]);});});diff --git a/src/client/lib/posthog-sanitize.ts b/src/client/lib/posthog-sanitize.tsindex 572756fd..67afb99c 100644--- a/src/client/lib/posthog-sanitize.ts+++ b/src/client/lib/posthog-sanitize.ts@@ -1,44 +1,114 @@-const OAUTH_CONSENT_PATH = "/oauth-consent";--export const POSTHOG_PERSONAL_DATA_QUERY_PARAMETERS = [- "email",- "response_type",- "client_id",- "redirect_uri",- "scope",- "state",- "code_challenge",- "code_challenge_method",- "resource",-] as const;+import { isErrorCode } from "@/shared/error-codes";+// Query params that never carry customer content. Every other param is+// dropped: app URLs carry keywords, domains, competitor lists, AI prompts, GSC+// filters, emails and full OAuth requests in their query strings, none of which+// may reach analytics. Route paths only hold opaque ids, so they are kept.+const ALLOWED_QUERY_PARAMETERS = new Set([+ "utm_source",+ "utm_medium",+ "utm_campaign",+ "utm_content",+ "utm_term",+ "tab",+ "view",+]);++// Autocapture records link targets even with attribute masking on, and an+// app link can point at a customer's page (a GSC row, a backlink source).+const HREF_ATTRIBUTE = /(?:attr__)?href="(?:[^"\\]|\\.)*"/g;++type PostHogEvent = {+ properties: Record<string, unknown>;+ $set?: Record<string, unknown>;+ $set_once?: Record<string, unknown>;+};++/** Strips every non-allowlisted query param and the hash from an absolute+ * http(s) URL or a root-relative path. Other strings pass through unchanged. */export function sanitizePostHogUrl(value: string): string {+ const isPath = value.startsWith("/") && !value.startsWith("//");+ let url: URL;try {- const url = new URL(value);-- // The consent route carries the complete OAuth authorization request. Keep- // only the route identity in analytics so current URLs, session-entry URLs,- // referrers, and replay metadata cannot expose present or future params.- if (url.pathname === OAUTH_CONSENT_PATH) {- url.search = "";- return url.toString();- }-- // Preserve the existing email redaction for URLs outside the consent flow.- url.searchParams.delete("email");- return url.toString();+ url = new URL(value, isPath ? "https://path.invalid" : undefined);} catch {return value;}+ if (url.protocol !== "http:" && url.protocol !== "https:") return value;++ url.search = new URLSearchParams(+ Array.from(url.searchParams).filter(([key]) =>+ ALLOWED_QUERY_PARAMETERS.has(key),+ ),+ ).toString();+ url.hash = "";+ return isPath ? url.pathname + url.search : url.toString();+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null && !Array.isArray(value);+}++// Nested values too: each web vitals metric carries its own copy of the page+// URL.+function sanitizeNested(value: unknown): unknown {+ if (typeof value === "string") return sanitizePostHogUrl(value);+ if (Array.isArray(value)) {+ return value.map((item: unknown) => sanitizeNested(item));+ }+ if (isRecord(value)) {+ for (const [key, nested] of Object.entries(value)) {+ value[key] = sanitizeNested(nested);+ }+ }+ return value;}-export function sanitizePostHogProperties(- properties: Record<string, unknown>,-): Record<string, unknown> {- for (const [key, value] of Object.entries(properties)) {- if (typeof value !== "string") continue;- properties[key] = sanitizePostHogUrl(value);+/** Removes customer content from an outgoing event: URL query strings in+ * event and person properties, link targets on autocaptured elements, and+ * exception messages. */+export function sanitizePostHogEvent<T extends PostHogEvent>(event: T): T {+ const { properties } = event;+ sanitizeNested(properties);+ if (event.$set) sanitizeNested(event.$set);+ if (event.$set_once) sanitizeNested(event.$set_once);++ // Heatmaps key their click positions by page URL.+ if (isRecord(properties.$heatmap_data)) {+ const byUrl: Record<string, unknown[]> = {};+ for (const [url, points] of Object.entries(properties.$heatmap_data)) {+ const merged = (byUrl[sanitizePostHogUrl(url)] ??= []);+ if (Array.isArray(points)) {+ points.forEach((point: unknown) => merged.push(point));+ }+ }+ properties.$heatmap_data = byUrl;}- return properties;+ delete properties.$external_click_url;+ if (typeof properties.$elements_chain === "string") {+ properties.$elements_chain = properties.$elements_chain.replace(+ HREF_ATTRIBUTE,+ "",+ );+ }+ if (Array.isArray(properties.$elements)) {+ properties.$elements.forEach((element: unknown) => {+ if (typeof element === "object" && element !== null) {+ Reflect.deleteProperty(element, "attr__href");+ }+ });+ }+ // Error tracking keeps each exception's type and stack frames. A message+ // can quote page content (a JSON snippet, a URL); a bare error code can't.+ if (Array.isArray(properties.$exception_list)) {+ properties.$exception_list.forEach((entry: unknown) => {+ if (typeof entry !== "object" || entry === null) return;+ const value: unknown = Reflect.get(entry, "value");+ if (typeof value !== "string" || !isErrorCode(value)) {+ Reflect.set(entry, "value", "");+ }+ });+ }+ return event;}diff --git a/src/client/lib/posthog.ts b/src/client/lib/posthog.tsindex c6cab591..e22e6763 100644--- a/src/client/lib/posthog.ts+++ b/src/client/lib/posthog.ts@@ -1,7 +1,6 @@import { isHostedClientAuthMode } from "@/lib/auth-mode";import {- POSTHOG_PERSONAL_DATA_QUERY_PARAMETERS,- sanitizePostHogProperties,+ sanitizePostHogEvent,sanitizePostHogUrl,} from "@/client/lib/posthog-sanitize";@@ -84,33 +83,51 @@ function getBrowserPostHogClient(): Promise<BrowserPostHogClient | null> {defaults: "2026-01-30",capture_exceptions: true,before_send(event) {+ if (!event) return null;if (- event?.event === "$exception" &&+ event.event === "$exception" &&isIgnorableException(event.properties)) {return null;}- return event;+ return sanitizePostHogEvent(event);},capture_pageview: "history_change",+ // Autocaptured clicks carry no on-screen text or element attributes.+ mask_all_text: true,+ mask_all_element_attributes: true,mask_personal_data_properties: true,- custom_personal_data_properties: [- ...POSTHOG_PERSONAL_DATA_QUERY_PARAMETERS,- ],respect_dnt: true,+ // Console output can quote page content and full error messages,+ // and the project setting would otherwise record it in replays.+ enable_recording_console_log: false,+ // Recordings mask all text and inputs: app screens show customer+ // content (keywords, domains, GSC/GA data, reports, chat messages).session_recording: {maskAllInputs: true,- maskTextSelector: "[data-ph-mask], .ph-mask",+ maskTextSelector: "*",+ // Text masking doesn't cover attributes, and the recorder has no+ // attribute masker. These elements carry content in attributes+ // (keyword links, customer URLs, favicons, prompt and URL titles+ // and labels), so they are recorded as blank boxes.+ blockSelector:+ 'a[href^="http"], a[href*="?"], img, [title], [aria-label]',+ recordHeaders: false,+ recordBody: false,+ // Runs on page URLs and network entries. Project settings can turn+ // payload capture on remotely, so headers and bodies are dropped+ // here too.maskCapturedNetworkRequestFn(request) {return {...request,name: sanitizePostHogUrl(request.name),+ requestHeaders: undefined,+ requestBody: undefined,+ responseHeaders: undefined,+ responseBody: undefined,};},},- sanitize_properties(properties) {- return sanitizePostHogProperties(properties);- },});browserPostHogInitialized = true;}diff --git a/src/middleware/errorHandling.ts b/src/middleware/errorHandling.tsindex 83cf2e4e..0934aa71 100644--- a/src/middleware/errorHandling.ts+++ b/src/middleware/errorHandling.ts@@ -69,12 +69,13 @@ export const errorHandlingMiddleware = createMiddleware({const request = getRequest();const url = new URL(request.url);- console.error("server.function error:", error);+ // Details can quote customer content (a DataForSEO response body), so+ // they stay in this log line and never reach error tracking.+ console.error("server.function error:", error, appError?.details ?? "");const properties = {errorCode: appError?.code ?? "INTERNAL_ERROR",method: request.method,path: url.pathname,- ...appError?.details,};waitUntil(resolveErrorDistinctId(request.headers).then((distinctId) =>diff --git a/src/routes/s/$token/raw.ts b/src/routes/s/$token/raw.tsindex 496ee76f..b8a8581d 100644--- a/src/routes/s/$token/raw.ts+++ b/src/routes/s/$token/raw.ts@@ -102,8 +102,10 @@ async function handleSharedReportRequest(report_id: report.id,project_id: report.projectId,skill: report.skill,- // Kept so link-preview bots can be excluded from the count.- user_agent: request.headers.get("User-Agent"),+ // Browsers framing the report send Sec-Fetch-Dest; link-preview+ // bots generally don't, so this excludes them from the count+ // without recording the viewer's user agent.+ from_browser: dest === "iframe",// Readers are anonymous and stay that way: no person profile is// created for a share pseudonym.$process_person_profile: false,diff --git a/src/server/features/sam/SamChatAgent.ts b/src/server/features/sam/SamChatAgent.tsindex 34010797..d2faca34 100644--- a/src/server/features/sam/SamChatAgent.ts+++ b/src/server/features/sam/SamChatAgent.ts@@ -455,7 +455,7 @@ export class SamChatAgent extends Think {this.ctx.waitUntil(this.telemetry.report(result.status,- { request_id: result.requestId, error_message: result.error },+ { request_id: result.requestId },this.messages,{ billing: this.billing },),diff --git a/src/server/features/sam/samTurnTelemetry.ts b/src/server/features/sam/samTurnTelemetry.tsindex 85c9016a..975450d4 100644--- a/src/server/features/sam/samTurnTelemetry.ts+++ b/src/server/features/sam/samTurnTelemetry.ts@@ -265,7 +265,7 @@ export class SamTelemetry {...extra,credits: turn.credits,};- const error = extra.error_message;+ const error = extra.error_name;const trace =turn.steps > 0? turn.traceProperties(@@ -291,7 +291,6 @@ export class SamTelemetry {billing?: Promise<void>,): void {const errorName = error instanceof Error ? error.name : typeof error;- const errorMessage = error instanceof Error ? error.message : String(error);this.waitUntil(this.report("error",@@ -299,8 +298,8 @@ export class SamTelemetry {request_id: ctx?.requestId,error_stage: ctx?.stage,error_class: ctx?.classification,+ // No message: model and tool errors can quote the user's content.error_name: errorName,- error_message: errorMessage.slice(0, 500),},messages,{ synthesize: true, billing },diff --git a/src/server/lib/posthog.test.ts b/src/server/lib/posthog.test.tsnew file mode 100644index 00000000..faca548b--- /dev/null+++ b/src/server/lib/posthog.test.ts@@ -0,0 +1,36 @@+import { expect, it, vi } from "vitest";+import { AppError } from "@/server/lib/errors";+import { captureServerError } from "@/server/lib/posthog";++const captureExceptionImmediate = vi.hoisted(() =>+ vi.fn<(error: Error) => Promise<void>>(),+);+vi.mock("cloudflare:workers", () => ({+ env: { POSTHOG_PUBLIC_KEY: "phc_test", POSTHOG_HOST: "https://ph.test" },+}));+vi.mock("@/server/lib/runtime-env", () => ({+ isHostedServerAuthMode: async () => true,+}));+vi.mock("posthog-node", () => ({+ PostHog: class {+ captureExceptionImmediate = captureExceptionImmediate;+ shutdown = async () => {};+ },+}));++it("reports errors without their messages, which can quote customer content", async () => {+ const error = new AppError(+ "INTERNAL_ERROR",+ "Failed query: insert into keywords\nparams: coffee at home,acme.test",+ );+ error.cause = new Error("acme.test refused the connection");++ await captureServerError(error);++ const sent = captureExceptionImmediate.mock.calls[0]?.[0];+ expect(sent?.stack).toMatch(/^AppError: INTERNAL_ERROR\n\s+at /);+ expect(sent?.stack).not.toMatch(/coffee|acme/);+ const cause = sent?.cause instanceof Error ? sent.cause : undefined;+ expect(cause?.message).toBe("");+ expect(cause?.stack).not.toContain("acme");+});diff --git a/src/server/lib/posthog.ts b/src/server/lib/posthog.tsindex c73141a3..267d4569 100644--- a/src/server/lib/posthog.ts+++ b/src/server/lib/posthog.ts@@ -1,5 +1,6 @@import { env } from "cloudflare:workers";import { PostHog } from "posthog-node";+import { asAppError } from "@/server/lib/errors";import { isHostedServerAuthMode } from "@/server/lib/runtime-env";/** Returns a one-shot PostHog client, or null if the key is missing. Caller must shut down after use.@@ -16,6 +17,31 @@ function getServerPostHogClient(): PostHog | null {});}+/**+ * The error as error tracking receives it: its type, AppError code and stack+ * frames, never its message. Messages can quote customer content (DataForSEO+ * echoes request params, failed queries print their values, model errors+ * include output), so call sites log the full error to Cloudflare instead.+ */+function withoutMessage(error: unknown, depth = 0): Error {+ const redacted = new Error(asAppError(error)?.code ?? "");+ if (!(error instanceof Error)) {+ redacted.stack = `Error: ${redacted.message}`;+ return redacted;+ }+ redacted.name = error.name;+ // Only real frames: a multi-line message would otherwise be parsed into+ // frames, content included.+ const frames = (error.stack ?? "")+ .split("\n")+ .filter((line) => /^\s+at /.test(line));+ redacted.stack = [`${error.name}: ${redacted.message}`, ...frames].join("\n");+ if (error.cause !== undefined && depth < 4) {+ redacted.cause = withoutMessage(error.cause, depth + 1);+ }+ return redacted;+}+/*** `distinctId` must be the better-auth user id, the same identity* identifyAnalyticsUser and captureServerEvent send, so exceptions land on the@@ -35,7 +61,7 @@ export async function captureServerError(if (!client) return;try {- await client.captureExceptionImmediate(error, distinctId, {+ await client.captureExceptionImmediate(withoutMessage(error), distinctId, {source: "server",...properties,});diff --git a/src/server/mcp/tools/create-rank-tracker.ts b/src/server/mcp/tools/create-rank-tracker.tsindex cc8052da..e8a7f31c 100644--- a/src/server/mcp/tools/create-rank-tracker.ts+++ b/src/server/mcp/tools/create-rank-tracker.ts@@ -111,7 +111,6 @@ export const createRankTrackerTool = {organizationId: context.auth.organizationId,properties: {project_id: args.projectId,- domain: config.domain,devices: config.devices,schedule: config.scheduleInterval,source: "mcp",diff --git a/src/server/mcp/tools/rank-tracking-management-tools.test.ts b/src/server/mcp/tools/rank-tracking-management-tools.test.tsindex 08a98564..358f8e17 100644--- a/src/server/mcp/tools/rank-tracking-management-tools.test.ts+++ b/src/server/mcp/tools/rank-tracking-management-tools.test.ts@@ -88,7 +88,6 @@ describe("rank tracking management MCP tools", () => {organizationId: "org_123",properties: {project_id: projectId,- domain: "openseo.so",devices: "mobile",schedule: "manual",source: "mcp",diff --git a/src/server/workflows/RankCheckWorkflow.ts b/src/server/workflows/RankCheckWorkflow.tsindex af4532b5..9facaeef 100644--- a/src/server/workflows/RankCheckWorkflow.ts+++ b/src/server/workflows/RankCheckWorkflow.ts@@ -280,7 +280,9 @@ async function markRankCheckRunFailed(input: {properties: {project_id: input.projectId,status: "failed",- error: errorMessage,+ // A code, not the message: provider errors can quote keywords or domains.+ error_code:+ input.error instanceof AppError ? input.error.code : "INTERNAL_ERROR",},});}diff --git a/src/serverFunctions/gsc.ts b/src/serverFunctions/gsc.tsindex 21cbb090..0d195e25 100644--- a/src/serverFunctions/gsc.ts+++ b/src/serverFunctions/gsc.ts@@ -112,7 +112,7 @@ export const setGscSite = createServerFn({ method: "POST" })distinctId: context.userId,event: "gsc:property_select",organizationId: context.organizationId,- properties: { project_id: context.projectId, site_url: data.siteUrl },+ properties: { project_id: context.projectId },}),);return {diff --git a/src/serverFunctions/rank-tracking.ts b/src/serverFunctions/rank-tracking.tsindex d68fc77d..2cdb48fe 100644--- a/src/serverFunctions/rank-tracking.ts+++ b/src/serverFunctions/rank-tracking.ts@@ -104,7 +104,6 @@ export const createRankTrackingConfig = createServerFn({ method: "POST" })organizationId: context.organizationId,properties: {project_id: context.projectId,- domain: data.domain,devices: data.devices ?? "both",schedule: data.scheduleInterval ?? "weekly",},