AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): browser and Next.js Sentry events mask URLs the way backend events do (#15787)
Reinier van der Leera689834cd3cf
diff --git a/autogpt_platform/frontend/instrumentation-client.ts b/autogpt_platform/frontend/instrumentation-client.tsindex eaa92ffa27..321c5ffe90 100644--- a/autogpt_platform/frontend/instrumentation-client.ts+++ b/autogpt_platform/frontend/instrumentation-client.ts@@ -14,7 +14,8 @@ import {stripUserPII,} from "@/lib/sentry-filters";import { getSentryTunnelURL } from "@/lib/sentry-tunnel";-import { setupSessionReplay } from "@/lib/session-replay";+import { redactEventURLs, redactSpanURLs } from "@/lib/sentry-url-redaction";+import { holdReplayBackFor, setupSessionReplay } from "@/lib/session-replay";import { environment } from "@/services/environment";import * as Sentry from "@sentry/nextjs";@@ -46,20 +47,21 @@ Sentry.init({// A failed API request the request logger reports, titled by its route,// would otherwise arrive again from the HttpClient integration (BUILDER-95D).// Some browsers inject a script that logs every cross-origin iframe it may- // not read (BUILDER-943).+ // not read (BUILDER-943). URLs are masked last: the request logger's match+ // needs the URL as it was requested.beforeSend(event) {stripSpanPII(stripUserPII(event));if (isFromBot(navigator.userAgent)) return null;if (isLoggedRequestFailure(event)) return null;if (isInjectedIframeProbe(event)) return null;- return isNextRSCNavigationFallback(event) ? null : event;+ return isNextRSCNavigationFallback(event) ? null : redactEventURLs(event);},beforeSendTransaction(event) {return isFromBot(navigator.userAgent)? null- : stripSpanPII(stripUserPII(event));+ : stripSpanPII(stripUserPII(redactEventURLs(event)));},- beforeSendSpan: stripSpanDataPII,+ beforeSendSpan: (span) => redactSpanURLs(stripSpanDataPII(span)),beforeSendLog: stripLogUserPII,// Add optional integrations for additional features@@ -114,4 +116,9 @@ Sentry.init({enableLogs: true,});-export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;+export function onRouterTransitionStart(+ ...args: Parameters<typeof Sentry.captureRouterTransitionStart>+) {+ Sentry.captureRouterTransitionStart(...args);+ holdReplayBackFor(args[0]);+}diff --git a/autogpt_platform/frontend/sentry.edge.config.ts b/autogpt_platform/frontend/sentry.edge.config.tsindex 6762f63dde..fd308728d1 100644--- a/autogpt_platform/frontend/sentry.edge.config.ts+++ b/autogpt_platform/frontend/sentry.edge.config.ts@@ -11,6 +11,7 @@ import {stripUserPII,} from "@/lib/sentry-filters";import { isSentryTunnelTransaction } from "@/lib/sentry-tunnel";+import { redactEventURLs, redactSpanURLs } from "@/lib/sentry-url-redaction";import { environment } from "@/services/environment";import * as Sentry from "@sentry/nextjs";@@ -35,14 +36,18 @@ Sentry.init({// No email or IP address reaches Sentry; the user id is enough.sendDefaultPii: false,beforeSend(event) {- return stripSpanPII(stripUserPII(stripRequestCredentials(event)));+ return stripSpanPII(+ stripUserPII(redactEventURLs(stripRequestCredentials(event))),+ );},beforeSendTransaction(event) {return isSentryTunnelTransaction(event)? null- : stripSpanPII(stripUserPII(stripRequestCredentials(event)));+ : stripSpanPII(+ stripUserPII(redactEventURLs(stripRequestCredentials(event))),+ );},- beforeSendSpan: stripSpanDataPII,+ beforeSendSpan: (span) => redactSpanURLs(stripSpanDataPII(span)),beforeSendLog: stripLogUserPII,// Setting this option to true will print useful information to the console while you're setting up Sentry.diff --git a/autogpt_platform/frontend/sentry.server.config.ts b/autogpt_platform/frontend/sentry.server.config.tsindex b26dfec254..32fccde3c1 100644--- a/autogpt_platform/frontend/sentry.server.config.ts+++ b/autogpt_platform/frontend/sentry.server.config.ts@@ -10,6 +10,7 @@ import {stripUserPII,} from "@/lib/sentry-filters";import { isSentryTunnelTransaction } from "@/lib/sentry-tunnel";+import { redactEventURLs, redactSpanURLs } from "@/lib/sentry-url-redaction";import { environment } from "@/services/environment";import * as Sentry from "@sentry/nextjs";// import { NodeProfilingIntegration } from "@sentry/profiling-node";@@ -35,14 +36,18 @@ Sentry.init({// No email or IP address reaches Sentry; the user id is enough.sendDefaultPii: false,beforeSend(event) {- return stripSpanPII(stripUserPII(stripRequestCredentials(event)));+ return stripSpanPII(+ stripUserPII(redactEventURLs(stripRequestCredentials(event))),+ );},beforeSendTransaction(event) {return isSentryTunnelTransaction(event)? null- : stripSpanPII(stripUserPII(stripRequestCredentials(event)));+ : stripSpanPII(+ stripUserPII(redactEventURLs(stripRequestCredentials(event))),+ );},- beforeSendSpan: stripSpanDataPII,+ beforeSendSpan: (span) => redactSpanURLs(stripSpanDataPII(span)),beforeSendLog: stripLogUserPII,// Setting this option to true will print useful information to the console while you're setting up Sentry.diff --git a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.tsindex 5d75a7202c..5e25246db8 100644--- a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts+++ b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts@@ -1,12 +1,16 @@import { afterEach, describe, expect, it, vi } from "vitest";-const { initMock, reportingObserverIntegrationMock } = vi.hoisted(() => ({- initMock: vi.fn(),- reportingObserverIntegrationMock: vi.fn((options?: { types?: string[] }) => ({- name: "ReportingObserver",- options,- })),-}));+const { initMock, reportingObserverIntegrationMock, holdReplayBackForMock } =+ vi.hoisted(() => ({+ initMock: vi.fn(),+ holdReplayBackForMock: vi.fn(),+ reportingObserverIntegrationMock: vi.fn(+ (options?: { types?: string[] }) => ({+ name: "ReportingObserver",+ options,+ }),+ ),+ }));vi.mock("@sentry/nextjs", () => {function stubIntegration(name: string) {@@ -26,11 +30,31 @@ vi.mock("@sentry/nextjs", () => {};});+vi.mock("@/lib/session-replay", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@/lib/session-replay")>()),+ holdReplayBackFor: holdReplayBackForMock,+}));+afterEach(() => {vi.unstubAllEnvs();});describe("Sentry client instrumentation", () => {+ it("holds session replay back as Next starts each navigation", async () => {+ const Sentry = await import("@sentry/nextjs");+ const { onRouterTransitionStart } = await import(+ "../../instrumentation-client"+ );++ onRouterTransitionStart("/link/t-1", "push");++ expect(Sentry.captureRouterTransitionStart).toHaveBeenCalledWith(+ "/link/t-1",+ "push",+ );+ expect(holdReplayBackForMock).toHaveBeenCalledWith("/link/t-1");+ });+it("does not forward browser deprecation reports to Sentry", async () => {await import("../../instrumentation-client");diff --git a/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts b/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.tsindex 0b047a3b0f..418fef39ea 100644--- a/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts+++ b/autogpt_platform/frontend/src/__tests__/sentry-request-failures.test.ts@@ -1,3 +1,4 @@+import { getV2ListLibraryAgents } from "@/app/api/__generated__/endpoints/library/library";import { listCopilotFollowupSchedules } from "@/app/api/__generated__/endpoints/schedules/schedules";import { resetClientRequestFailureLog } from "@/app/api/mutators/request-failure-log";import { server } from "@/mocks/mock-server";@@ -86,6 +87,19 @@ describe("a failed API request in Sentry", () => {]);});+ it("is reported once when its URL carries a query", async () => {+ server.use(+ http.get("*/api/proxy/api/library/agents", () =>+ HttpResponse.json({ detail: "boom" }, { status: 500 }),+ ),+ );++ await expect(getV2ListLibraryAgents({ page: 2 })).rejects.toThrow("boom");+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ });+it("is still reported once when the request bypassed the generated client", async () => {server.use(http.get("*/api/proxy/api/workspace/files/:id/download", () =>diff --git a/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts b/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.tsindex d40881de08..2a0e4cda64 100644--- a/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts+++ b/autogpt_platform/frontend/src/__tests__/sentry-runtime-configs.test.ts@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const { initMock } = vi.hoisted(() => ({ initMock: vi.fn() }));const BODY_MARKER = "marker-4f1c";+const URL_MARKER = "marker-u-7d3b";vi.mock("@sentry/nextjs", async (importOriginal) => {const actual = await importOriginal<typeof import("@sentry/nextjs")>();@@ -141,6 +142,182 @@ describe.each([expect(beforeSendTransaction(withCredentials()).request).toEqual(stripped);});+ it("masks the credentials in the URLs of errors, transactions and spans", async () => {+ await load();++ const { beforeSend, beforeSendTransaction, beforeSendSpan } =+ initMock.mock.calls[0][0];+ function onATokenPage() {+ return {+ type: "transaction",+ transaction: "GET /reset-password",+ request: {+ url: `https://platform.agpt.co/reset-password?token=${URL_MARKER}`,+ query_string: `token=${URL_MARKER}`,+ headers: {+ referer: `https://platform.agpt.co/link/${URL_MARKER}`,+ "user-agent": "ua",+ },+ },+ contexts: {+ trace: {+ trace_id: "a".repeat(32),+ span_id: "b".repeat(16),+ data: {+ "http.target": `/reset-password?token=${URL_MARKER}`,+ "http.route": "/reset-password",+ },+ },+ },+ spans: [+ {+ trace_id: "a".repeat(32),+ span_id: "c".repeat(16),+ start_timestamp: 1,+ description: `GET https://backend.agpt.co/api/platform-linking/tokens/${URL_MARKER}/info`,+ data: {+ "http.url": `https://backend.agpt.co/api/platform-linking/tokens/${URL_MARKER}/info`,+ "server.address": "backend.agpt.co",+ },+ },+ ],+ };+ }++ for (const event of [+ beforeSend(onATokenPage(), {}),+ beforeSendTransaction(onATokenPage()),+ ]) {+ expect(event.transaction).toBe("GET /reset-password");+ expect(event.request).toEqual({+ url: "https://platform.agpt.co/reset-password?token=[REDACTED]",+ query_string: "[REDACTED]",+ headers: {+ referer: "https://platform.agpt.co/link/[REDACTED]",+ "user-agent": "ua",+ },+ });+ expect(event.contexts.trace.data).toEqual({+ "http.target": "/reset-password?token=[REDACTED]",+ "http.route": "/reset-password",+ });+ expect(event.spans[0].description).toBe(+ "GET https://backend.agpt.co/api/platform-linking/tokens/[REDACTED]/info",+ );+ expect(event.spans[0].data).toEqual({+ "http.url":+ "https://backend.agpt.co/api/platform-linking/tokens/[REDACTED]/info",+ "server.address": "backend.agpt.co",+ });+ }++ const span = beforeSendSpan({+ trace_id: "a".repeat(32),+ span_id: "c".repeat(16),+ start_timestamp: 1,+ description: `GET /link/${URL_MARKER}`,+ data: {+ "http.target": `/link/${URL_MARKER}`,+ "http.route": "/link/[token]",+ },+ });+ expect(span.description).toBe("GET /link/[REDACTED]");+ expect(span.data).toEqual({+ "http.target": "/link/[REDACTED]",+ "http.route": "/link/[token]",+ });+ });++ it("masks the page a request's headers name, as headers and as span attributes", async () => {+ await load();++ const { beforeSend, beforeSendTransaction, beforeSendSpan } =+ initMock.mock.calls[0][0];+ const routerState = encodeURIComponent(+ JSON.stringify(["", { children: ["link", ["token", URL_MARKER, "d"]] }]),+ );+ function duringANavigation() {+ return {+ type: "transaction",+ transaction: "GET /library",+ request: {+ url: "https://platform.agpt.co/library",+ headers: {+ "next-url": `/link/${URL_MARKER}`,+ "Next-Router-State-Tree": routerState,+ "user-agent": "ua",+ },+ },+ contexts: {+ trace: {+ trace_id: "a".repeat(32),+ span_id: "b".repeat(16),+ data: {+ "http.request.header.referer": `https://platform.agpt.co/reset-password?token=${URL_MARKER}`,+ "http.request.header.next_url": `/link/${URL_MARKER}`,+ "http.request.header.next_router_state_tree": routerState,+ "http.request.header.user_agent": "ua",+ "http.route": "/library",+ },+ },+ },+ };+ }++ for (const event of [+ beforeSend(duringANavigation(), {}),+ beforeSendTransaction(duringANavigation()),+ ]) {+ expect(event.request.headers).toEqual({+ "next-url": "/link/[REDACTED]",+ "Next-Router-State-Tree": "[REDACTED]",+ "user-agent": "ua",+ });+ expect(event.contexts.trace.data).toEqual({+ "http.request.header.referer":+ "https://platform.agpt.co/reset-password?token=[REDACTED]",+ "http.request.header.next_url": "/link/[REDACTED]",+ "http.request.header.next_router_state_tree": "[REDACTED]",+ "http.request.header.user_agent": "ua",+ "http.route": "/library",+ });+ }++ const span = beforeSendSpan({+ trace_id: "a".repeat(32),+ span_id: "c".repeat(16),+ start_timestamp: 1,+ data: { "http.request.header.referer": `/link/${URL_MARKER}` },+ });+ expect(span.data).toEqual({+ "http.request.header.referer": "/link/[REDACTED]",+ });+ });++ it("masks a span's query and fragment whether or not they keep their ? or #", async () => {+ await load();++ const { beforeSendSpan } = initMock.mock.calls[0][0];+ const span = beforeSendSpan({+ trace_id: "a".repeat(32),+ span_id: "c".repeat(16),+ start_timestamp: 1,+ data: {+ "url.query": `token=${URL_MARKER}&page=2`,+ "url.fragment": `access_token=${URL_MARKER}`,+ "http.query": `?token=${URL_MARKER}`,+ "http.fragment": "#section",+ },+ });++ expect(span.data).toEqual({+ "url.query": "token=[REDACTED]&page=[REDACTED]",+ "url.fragment": "access_token=[REDACTED]",+ "http.query": "?token=[REDACTED]",+ "http.fragment": "#section",+ });+ });+it("sends no user email or IP address", async () => {await load();diff --git a/autogpt_platform/frontend/src/__tests__/sentry-url-redaction.test.ts b/autogpt_platform/frontend/src/__tests__/sentry-url-redaction.test.tsnew file mode 100644index 0000000000..84efabb9ff--- /dev/null+++ b/autogpt_platform/frontend/src/__tests__/sentry-url-redaction.test.ts@@ -0,0 +1,323 @@+import * as Sentry from "@sentry/nextjs";+import {+ afterAll,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from "vitest";++const { replayIntegration } = vi.hoisted(() => ({+ replayIntegration: vi.fn((options: object) => ({ name: "Replay", options })),+}));++// The client bundle runs the browser SDK; Vitest would load the Node one. The+// replay itself never records outside a browser, so only its options are kept.+vi.mock("@sentry/nextjs", async () => {+ const sdk = await import(+ /* @vite-ignore */ await sentryPackageEntry("browser")+ );+ return {+ ...sdk,+ replayIntegration,+ captureRouterTransitionStart: () => undefined,+ };+});++// With monitoring consent stored, the client installs session replay at init.+vi.mock("@/services/consent/consent", () => ({+ hasConsentFor: () => true,+ subscribeToConsent: () => () => undefined,+}));++const R = "[REDACTED]";+const RESET_MARKER = "marker-r-4f1c";+const LINK_MARKER = "marker-l-9a2e";+const RESET_PAGE = `/reset-password?token=${RESET_MARKER}`;+const LINK_PAGE = `/link/${LINK_MARKER}?platform=discord`;+const LINK_INFO_CALL = `/api/proxy/api/platform-linking/tokens/${LINK_MARKER}/info`;++const sent: Sentry.Event[] = [];+const sentSpans: { data?: Record<string, unknown> }[] = [];++beforeAll(async () => {+ vi.spyOn(console, "info").mockImplementation(() => undefined);+ vi.stubEnv("NEXT_PUBLIC_SENTRY_DSN", "https://[email protected]/0");+ await import("../../instrumentation-client");+ vi.spyOn(Sentry.getClient()!.getTransport()!, "send").mockImplementation(+ async ([, items]) => {+ for (const [header, payload] of items) {+ if (header.type === "event" || header.type === "transaction") {+ sent.push(payload as Sentry.Event);+ }+ if (header.type === "span") sentSpans.push(payload as never);+ }+ return {};+ },+ );+});++beforeEach(() => {+ sent.length = 0;+ sentSpans.length = 0;+ Sentry.getIsolationScope().clearBreadcrumbs();+ Sentry.getCurrentScope().setTransactionName(undefined);+ vi.spyOn(document, "referrer", "get").mockReturnValue("");+});++afterAll(() => {+ vi.unstubAllEnvs();+ vi.restoreAllMocks();+});++describe("a browser error report", () => {+ it("masks the token in the page's address, its referrer and the navigation to it", async () => {+ vi.spyOn(document, "referrer", "get").mockReturnValue(+ `${location.origin}${RESET_PAGE}`,+ );+ history.replaceState({}, "", RESET_PAGE);+ history.pushState({}, "", LINK_PAGE);+ Sentry.getCurrentScope().setTransactionName(`/link/${LINK_MARKER}`);+ Sentry.addBreadcrumb({+ category: "fetch",+ type: "http",+ data: { method: "GET", url: LINK_INFO_CALL, status_code: 200 },+ });++ Sentry.captureException(new Error("boom"));+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ const [event] = sent;+ expect(event.request?.url).toBe(+ `${location.origin}/link/${R}?platform=${R}`,+ );+ expect(event.request?.headers?.Referer).toBe(+ `${location.origin}/reset-password?token=${R}`,+ );+ expect(event.transaction).toBe(`/link/${R}`);+ const crumbs = event.breadcrumbs?.map((crumb) => crumb.data);+ expect(crumbs).toContainEqual({+ from: `/reset-password?token=${R}`,+ to: `/link/${R}?platform=${R}`,+ });+ expect(crumbs).toContainEqual({+ method: "GET",+ url: `/api/proxy/api/platform-linking/tokens/${R}/info`,+ status_code: 200,+ });+ expect(JSON.stringify(event)).not.toContain(RESET_MARKER);+ expect(JSON.stringify(event)).not.toContain(LINK_MARKER);+ });++ it("keeps an ordinary page's address as it is", async () => {+ history.replaceState({}, "", "/library/agents/a-1");+ history.pushState({}, "", "/marketplace");++ Sentry.captureException(new Error("boom"));+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ const [event] = sent;+ expect(event.request?.url).toBe(`${location.origin}/marketplace`);+ expect(event.breadcrumbs?.map((crumb) => crumb.data)).toContainEqual({+ from: "/library/agents/a-1",+ to: "/marketplace",+ });+ });+});++describe("a browser transaction", () => {+ it("masks the token in its name, its page and its spans", async () => {+ history.replaceState({}, "", LINK_PAGE);++ Sentry.startSpan(+ {+ name: `/link/${LINK_MARKER}`,+ forceTransaction: true,+ attributes: { "sentry.source": "url" },+ },+ () => {+ Sentry.startSpan(+ {+ name: `GET ${LINK_INFO_CALL}`,+ op: "http.client",+ attributes: {+ url: LINK_INFO_CALL,+ "http.query": `?token=${RESET_MARKER}`,+ "http.method": "GET",+ },+ },+ () => {},+ );+ },+ );+ await Sentry.flush(2000);++ expect(sent).toHaveLength(1);+ const [transaction] = sent;+ expect(transaction.transaction).toBe(`/link/${R}`);+ expect(transaction.request?.url).toBe(+ `${location.origin}/link/${R}?platform=${R}`,+ );+ const [span] = transaction.spans ?? [];+ expect(span.description).toBe(+ `GET /api/proxy/api/platform-linking/tokens/${R}/info`,+ );+ expect(span.data).toMatchObject({+ url: `/api/proxy/api/platform-linking/tokens/${R}/info`,+ "http.query": `?token=${R}`,+ "http.method": "GET",+ });+ expect(JSON.stringify(transaction)).not.toContain(RESET_MARKER);+ expect(JSON.stringify(transaction)).not.toContain(LINK_MARKER);+ });++ it("sends web vitals with the page they were measured on masked", async () => {+ Sentry.startInactiveSpan({+ name: "interaction",+ experimental: { standalone: true },+ attributes: { transaction: `/link/${LINK_MARKER}` },+ }).end();+ await Sentry.flush(2000);++ expect(sentSpans).toHaveLength(1);+ expect(sentSpans[0].data?.transaction).toBe(`/link/${R}`);+ });+});++describe("a session replay", () => {+ it("masks the token in the replay event's pages and address", async () => {+ history.replaceState({}, "", RESET_PAGE);+ const client = Sentry.getClient()!;+ const event: Sentry.Event & { urls: string[] } = {+ type: "replay_event",+ urls: [`${location.origin}${RESET_PAGE}`, `${location.origin}/library`],+ };+ const hint = { event_id: "a".repeat(32) };++ // As the replay integration prepares its events (prepareReplayEvent).+ client.emit("preprocessEvent", event, hint);+ const { prepareEvent } = await import(+ /* @vite-ignore */ await sentryPackageEntry("core")+ );+ const prepared = await prepareEvent(+ client.getOptions(),+ event,+ hint,+ Sentry.getCurrentScope(),+ client,+ Sentry.getIsolationScope(),+ );++ expect(prepared.urls).toEqual([+ `${location.origin}/reset-password?token=${R}`,+ `${location.origin}/library`,+ ]);+ expect(prepared.request.url).toBe(+ `${location.origin}/reset-password?token=${R}`,+ );+ expect(JSON.stringify(prepared)).not.toContain(RESET_MARKER);+ });++ it("masks the token in the navigations, calls and breadcrumbs it records", () => {+ const [[{ beforeAddRecordingEvent }]] = replayIntegration.mock.calls as [+ [{ beforeAddRecordingEvent: (event: object) => object }],+ ];+ // As the replay records them (createPerformanceSpans, addBreadcrumbEvent).+ const recorded = [+ performanceSpan("navigation.navigate", `${location.origin}${RESET_PAGE}`),+ performanceSpan("navigation.push", LINK_PAGE, { previous: RESET_PAGE }),+ performanceSpan("resource.fetch", `${location.origin}${LINK_INFO_CALL}`),+ breadcrumb({+ category: "navigation",+ data: { from: RESET_PAGE, to: LINK_PAGE },+ }),+ breadcrumb({+ category: "ui.slowClickDetected",+ data: { url: `${location.origin}${LINK_PAGE}`, clickCount: 1 },+ }),+ ].map(beforeAddRecordingEvent);++ expect(recorded.map(recordedURLs)).toEqual([+ [`${location.origin}/reset-password?token=${R}`],+ [`/link/${R}?platform=${R}`, `/reset-password?token=${R}`],+ [`${location.origin}/api/proxy/api/platform-linking/tokens/${R}/info`],+ [`/reset-password?token=${R}`, `/link/${R}?platform=${R}`],+ [`${location.origin}/link/${R}?platform=${R}`],+ ]);+ });++ it("keeps the ordinary pages it records as they are", () => {+ const [[{ beforeAddRecordingEvent }]] = replayIntegration.mock.calls as [+ [{ beforeAddRecordingEvent: (event: object) => object }],+ ];++ const recorded = [+ performanceSpan("navigation.push", "/marketplace", {+ previous: "/library",+ }),+ breadcrumb({+ category: "navigation",+ data: { from: "/library", to: "/marketplace" },+ }),+ ].map(beforeAddRecordingEvent);++ expect(recorded.map(recordedURLs)).toEqual([+ ["/marketplace", "/library"],+ ["/library", "/marketplace"],+ ]);+ });+});++function performanceSpan(op: string, description: string, data?: object) {+ return {+ type: 5,+ timestamp: 1,+ data: {+ tag: "performanceSpan",+ payload: { op, description, startTimestamp: 1, endTimestamp: 1, data },+ },+ };+}++function breadcrumb(crumb: { category: string; data: object }) {+ return {+ type: 5,+ timestamp: 1,+ data: {+ tag: "breadcrumb",+ payload: { timestamp: 1, type: "default", ...crumb },+ },+ };+}++function recordedURLs(event: object) {+ const { payload } = (event as ReturnType<typeof performanceSpan>).data;+ const data = (payload.data ?? {}) as Record<string, unknown>;+ const urls = [+ payload.description,+ data.previous,+ data.from,+ data.to,+ data.url,+ ];+ return urls.filter((url) => typeof url === "string");+}++// The browser SDK's build, and the core it imports, resolved as pnpm laid them out.+async function sentryPackageEntry(name: "browser" | "core") {+ const { createRequire } = await import("node:module");+ const nextjs = createRequire(import.meta.url).resolve(+ "@sentry/nextjs/package.json",+ );+ const react = createRequire(nextjs).resolve("@sentry/react/package.json");+ const browser = createRequire(react).resolve("@sentry/browser/package.json");+ if (name === "browser") {+ return browser.replace("package.json", "build/npm/esm/prod/index.js");+ }+ const core = createRequire(browser).resolve("@sentry/core/package.json");+ return core.replace("package.json", "build/esm/index.js");+}diff --git a/autogpt_platform/frontend/src/app/providers.tsx b/autogpt_platform/frontend/src/app/providers.tsxindex a269b0e7be..57a30cadbd 100644--- a/autogpt_platform/frontend/src/app/providers.tsx+++ b/autogpt_platform/frontend/src/app/providers.tsx@@ -1,6 +1,7 @@"use client";import { TooltipProvider } from "@/components/atoms/Tooltip/BaseTooltip";+import { ReplayRouteGuard } from "@/components/monitor/ReplayRouteGuard";import { SentryUserTracker } from "@/components/monitor/SentryUserTracker";import { BackendAPIProvider } from "@/lib/autogpt-server-api/context";import { getQueryClient } from "@/lib/react-query/queryClient";@@ -27,10 +28,11 @@ export function Providers({ children, ...props }: ThemeProviderProps) {<NuqsAdapter><PostHogProvider><BackendAPIProvider>- {/* All four read useSearchParams (directly or via useAuth), which+ {/* All of these read useSearchParams (directly or via useAuth), whichbails out of static rendering unless it sits under Suspense. */}<Suspense fallback={null}><SentryUserTracker />+ <ReplayRouteGuard /><PostHogUserTracker /><AttributionReporter /><AdsConversionTracker />diff --git a/autogpt_platform/frontend/src/components/monitor/ReplayRouteGuard.tsx b/autogpt_platform/frontend/src/components/monitor/ReplayRouteGuard.tsxnew file mode 100644index 0000000000..84abdeae42--- /dev/null+++ b/autogpt_platform/frontend/src/components/monitor/ReplayRouteGuard.tsx@@ -0,0 +1,20 @@+"use client";++import { syncReplayWithPage } from "@/lib/session-replay";+import { usePathname, useSearchParams } from "next/navigation";+import { useEffect } from "react";++/**+ * Brings session replay back once a navigation lands on a page whose address+ * carries no credential.+ */+export function ReplayRouteGuard() {+ const pathname = usePathname();+ const searchParams = useSearchParams();++ useEffect(() => {+ syncReplayWithPage(window.location.href);+ }, [pathname, searchParams]);++ return null;+}diff --git a/autogpt_platform/frontend/src/components/monitor/__tests__/ReplayRouteGuard.test.tsx b/autogpt_platform/frontend/src/components/monitor/__tests__/ReplayRouteGuard.test.tsxnew file mode 100644index 0000000000..1a1116d5e0--- /dev/null+++ b/autogpt_platform/frontend/src/components/monitor/__tests__/ReplayRouteGuard.test.tsx@@ -0,0 +1,34 @@+import { syncReplayWithPage } from "@/lib/session-replay";+import { render } from "@testing-library/react";+import { afterEach, expect, it, vi } from "vitest";+import { ReplayRouteGuard } from "../ReplayRouteGuard";++const navigation = vi.hoisted(() => ({ pathname: "/link/t-1" }));++vi.mock("next/navigation", () => ({+ usePathname: () => navigation.pathname,+ useSearchParams: () => new URLSearchParams(),+}));++vi.mock("@/lib/session-replay", () => ({ syncReplayWithPage: vi.fn() }));++afterEach(() => {+ history.replaceState({}, "", "/");+});++it("checks session replay against each page a navigation lands on", () => {+ history.replaceState({}, "", "/link/t-1");+ const { rerender } = render(<ReplayRouteGuard />);+ expect(syncReplayWithPage).toHaveBeenLastCalledWith(+ `${location.origin}/link/t-1`,+ );++ history.replaceState({}, "", "/library");+ navigation.pathname = "/library";+ rerender(<ReplayRouteGuard />);++ expect(syncReplayWithPage).toHaveBeenLastCalledWith(+ `${location.origin}/library`,+ );+ expect(syncReplayWithPage).toHaveBeenCalledTimes(2);+});diff --git a/autogpt_platform/frontend/src/lib/__tests__/sentry-url-redaction.test.ts b/autogpt_platform/frontend/src/lib/__tests__/sentry-url-redaction.test.tsnew file mode 100644index 0000000000..3470061ae0--- /dev/null+++ b/autogpt_platform/frontend/src/lib/__tests__/sentry-url-redaction.test.ts@@ -0,0 +1,238 @@+import { readdirSync, readFileSync } from "node:fs";+import { getLoginRedirect } from "@/app/(no-navbar)/link/[token]/helpers";+import { join } from "node:path";+import { describe, expect, it } from "vitest";+import {+ holdsCredential,+ REDACTED,+ redactURL,+ redactURLsInText,+} from "../sentry-url-redaction";++describe("redactURL", () => {+ // backend/util/url_redaction_test.py's cases, which both sides must agree on.+ it.each([+ ["/ws?token=a.b.c", `/ws?token=${REDACTED}`],+ ["/x?token=a&page=2&&flag", `/x?token=${REDACTED}&page=${REDACTED}&&flag`],+ ["/x?token=a=b;c=d", `/x?token=${REDACTED}`],+ ["/x?a=1?token=b", `/x?a=${REDACTED}`],+ ["/x?=bare", `/x?=${REDACTED}`],+ ["/ws?a=1#&token=b", `/ws?a=${REDACTED}&token=${REDACTED}`],+ [+ "http://evil?/api/public/shared/chats/s-1/messages",+ `http://evil?/api/public/shared/chats/${REDACTED}/messages`,+ ],+ [+ "http://x/api/invitations/api/public/shared/chats/s-1/messages",+ `http://x/api/invitations/${REDACTED}/public/shared/chats/${REDACTED}/messages`,+ ],+ [+ "/ws?/api/integrations/a=x/y&/webhooks/t",+ `/ws?/api/integrations/a=${REDACTED}&/webhooks/${REDACTED}`,+ ],+ ["/ws?q=it's&token=a", `/ws?q=${REDACTED}&token=${REDACTED}`],+ ['/ws?q="&token=a', `/ws?q=${REDACTED}&token=${REDACTED}`],+ ["/ws?q=a b&token=c", `/ws?q=${REDACTED}&token=${REDACTED}`],+ [+ "https://h:1/api/email/unsubscribe?token=dXNlcjpzaWc%3D",+ `https://h:1/api/email/unsubscribe?token=${REDACTED}`,+ ],+ [+ "/api/integrations/github/webhooks/w-1/ingress",+ `/api/integrations/github/webhooks/${REDACTED}/ingress`,+ ],+ [+ "/api/integrations/webhooks/w-1/ping",+ `/api/integrations/webhooks/${REDACTED}/ping`,+ ],+ [+ "/api/platform-linking/tokens/t-1/confirm",+ `/api/platform-linking/tokens/${REDACTED}/confirm`,+ ],+ [+ "/api/platform-linking/user-tokens/t-1/confirm",+ `/api/platform-linking/user-tokens/${REDACTED}/confirm`,+ ],+ ["/api/public/shared/s-1", `/api/public/shared/${REDACTED}`],+ [+ "/api/public/shared/s-1/files/f-1/download",+ `/api/public/shared/${REDACTED}/files/f-1/download`,+ ],+ [+ "/api/public/shared/chats/s-1/messages?limit=5",+ `/api/public/shared/chats/${REDACTED}/messages?limit=${REDACTED}`,+ ],+ ["/api/invitations/i-1/accept", `/api/invitations/${REDACTED}/accept`],+ ["/api/invitations/i-1", `/api/invitations/${REDACTED}`],+ ["/api/invitations/pending-1/x", `/api/invitations/${REDACTED}/x`],+ [+ "/backend/api/invitations/i-1/accept",+ `/backend/api/invitations/${REDACTED}/accept`,+ ],+ [`/ws?token=${REDACTED}`, `/ws?token=${REDACTED}`],+ ])("masks %s as the backend does", (url, expected) => {+ expect(redactURL(url)).toBe(expected);+ });++ it.each([+ [+ "https://platform.agpt.co/reset-password?token=r-1",+ `https://platform.agpt.co/reset-password?token=${REDACTED}`,+ ],+ [+ "https://platform.agpt.co/link/t-1?platform=discord",+ `https://platform.agpt.co/link/${REDACTED}?platform=${REDACTED}`,+ ],+ // Next fetches a page's server components from its own address.+ ["/link/t-1?_rsc=1ab2c", `/link/${REDACTED}?_rsc=${REDACTED}`],+ ["/share/s-1", `/share/${REDACTED}`],+ ["/share/chat/s-1", `/share/chat/${REDACTED}`],+ [+ "/api/proxy/api/platform-linking/tokens/t-1/info",+ `/api/proxy/api/platform-linking/tokens/${REDACTED}/info`,+ ],+ [+ "/auth/callback#access_token=a&type=recovery",+ `/auth/callback#access_token=${REDACTED}&type=${REDACTED}`,+ ],+ ])("masks the browser's %s", (url, expected) => {+ expect(redactURL(url)).toBe(expected);+ });++ it.each([+ "https://platform.agpt.co/library/agents/a-1",+ "/marketplace#featured",+ "/api/orgs/o-1/invitations/i-1",+ "/api/invitations/pending",+ "/api/integrations/github/webhooks",+ "/api/library/agents/a-1",+ "/api/chat/sessions/s-1/share/state",+ "/link/slack",+ "/link/slack/installed",+ // Route names, which is how Next names a page's transactions.+ "/link/[token]",+ "/share/[token]",+ "/share/chat/[token]",+ ])("keeps %s", (url) => {+ expect(redactURL(url)).toBe(url);+ });+});++describe("redactURLsInText", () => {+ it.each([+ [+ '1.2.3.4:5 - "GET /api/invitations/i-1/decline?x=1 HTTP/1.1" 401',+ `1.2.3.4:5 - "GET /api/invitations/${REDACTED}/decline?x=${REDACTED} HTTP/1.1" 401`,+ ],+ [+ '"GET /ws?q=it\'s&token=a HTTP/1.1"',+ `"GET /ws?q=${REDACTED}&token=${REDACTED} HTTP/1.1"`,+ ],+ [+ "acting as user u for requesting GET https://h/api/public/shared/s-1?x=1",+ `acting as user u for requesting GET https://h/api/public/shared/${REDACTED}?x=${REDACTED}`,+ ],+ [+ '"GET /ws?a=1#&token=t HTTP/1.1"',+ `"GET /ws?a=${REDACTED}&token=${REDACTED} HTTP/1.1"`,+ ],+ ["GET /link/t-1", `GET /link/${REDACTED}`],+ [+ "GET https://platform.agpt.co/share/chat/s-1",+ `GET https://platform.agpt.co/share/chat/${REDACTED}`,+ ],+ ])("masks %s", (text, expected) => {+ expect(redactURLsInText(text)).toBe(expected);+ });++ it.each(["Did it work? Yes.", "GET /library", "GET /link/[token]"])(+ "keeps %s",+ (text) => {+ expect(redactURLsInText(text)).toBe(text);+ },+ );+});++describe("holdsCredential", () => {+ const APP = join(__dirname, "../../app");++ // The app's pages that receive a credential in their query. A page or key+ // renamed without updating the credential keys fails here.+ it.each([+ ["(platform)/reset-password/page.tsx", "token"],+ ["(platform)/auth/authorize/page.tsx", "state"],+ ["(platform)/auth/integrations/setup-wizard/page.tsx", "state"],+ ])("holds for %s's ?%s=", (page, key) => {+ expect(readFileSync(join(APP, page), "utf8")).toContain(+ `searchParams.get("${key}")`,+ );+ expect(holdsCredential(`${routeOf(page)}?${key}=c-1`)).toBe(true);+ });++ it("holds for every page whose path is a token", () => {+ const pages = readdirSync(APP, { recursive: true, encoding: "utf8" })+ .map((path) => path.split("\\").join("/"))+ .filter((path) => /\[token\]\/page\.tsx$/.test(path));++ expect(pages).toHaveLength(3);+ for (const page of pages) {+ const url = routeOf(page).replace("[token]", "t-1");+ expect(holdsCredential(url), url).toBe(true);+ }+ });++ // The sign-in and sign-up links a token page sends a visitor out on, which+ // carry it back in their redirect target.+ it("holds for the sign-in and sign-up links that lead back to a token page", () => {+ const signUpLink = "/signup?next=/link/${token}";+ expect(+ readFileSync(+ join(+ APP,+ "(no-navbar)/link/[token]/components/NotAuthenticatedView.tsx",+ ),+ "utf8",+ ),+ ).toContain(signUpLink);++ for (const url of [+ getLoginRedirect("t-1"),+ signUpLink.replace("${token}", "t-1"),+ // The sign-in page passes its target on to sign-up, encoded.+ `/signup?next=${encodeURIComponent("/link/t-1")}`,+ ]) {+ expect(holdsCredential(url), url).toBe(true);+ expect(redactURL(url), url).not.toContain("t-1");+ }+ });++ it.each([+ "https://platform.agpt.co/link/t-1?platform=discord",+ "/auth/callback#access_token=a&type=recovery",+ "/login?next=%2Freset-password%3Ftoken%3Dr-1",+ "/auth/callback?callbackURL=%2Fshare%2Fchat%2Fs-1",+ "/x?CODE=1",+ ])("holds for %s", (url) => {+ expect(holdsCredential(url)).toBe(true);+ });++ it.each([+ "/copilot?sessionId=s-1",+ "/team?expertId=e-1&turn=2",+ "/library?tab=runs&page=2",+ "/marketplace#featured",+ "/link/slack",+ "/link/[token]",+ "/",+ "/login?next=%2Flibrary",+ "/signup?next=/marketplace/skills/s-1",+ ])("does not hold for %s", (url) => {+ expect(holdsCredential(url)).toBe(false);+ });+});++// A page file's route: route groups and the file name are not in the URL.+function routeOf(page: string) {+ const segments = page.split("/").slice(0, -1);+ return "/" + segments.filter((segment) => !segment.startsWith("(")).join("/");+}diff --git a/autogpt_platform/frontend/src/lib/__tests__/session-replay.test.ts b/autogpt_platform/frontend/src/lib/__tests__/session-replay.test.tsindex 4d24a99374..82c6c4b000 100644--- a/autogpt_platform/frontend/src/lib/__tests__/session-replay.test.ts+++ b/autogpt_platform/frontend/src/lib/__tests__/session-replay.test.ts@@ -6,10 +6,12 @@ import {installCookiebot,removeCookiebot,} from "@/tests/integrations/cookiebot";+import { redactRecordingEventURLs } from "../sentry-url-redaction";import { setupSessionReplay } from "../session-replay";vi.mock("@sentry/nextjs", () => ({addIntegration: vi.fn(),+ getReplay: vi.fn(),replayIntegration: vi.fn(() => ({ name: "Replay" })),replayCanvasIntegration: vi.fn(() => ({ name: "ReplayCanvas" })),}));@@ -38,7 +40,10 @@ describe("setupSessionReplay", () => {it("installs replay at init when monitoring consent is already stored", () => {installCookiebot({ statistics: true });- expect(integrationNames(setupSessionReplay())).toEqual(["Replay"]);+ expect(integrationNames(setupSessionReplay())).toEqual([+ "Replay",+ "ReplayEventURLRedaction",+ ]);});it("records nothing before consent, then adds replay once it is granted", () => {@@ -51,10 +56,13 @@ describe("setupSessionReplay", () => {expect(Sentry.addIntegration).not.toHaveBeenCalled();answerCookiebot({ statistics: true });- expect(addedIntegrationNames()).toEqual(["Replay"]);+ expect(addedIntegrationNames()).toEqual([+ "Replay",+ "ReplayEventURLRedaction",+ ]);answerCookiebot({ statistics: true, marketing: true });- expect(Sentry.addIntegration).toHaveBeenCalledTimes(1);+ expect(Sentry.addIntegration).toHaveBeenCalledTimes(2);});it("masks all text, inputs and media unless an element opts in", () => {@@ -66,6 +74,7 @@ describe("setupSessionReplay", () => {maskAllInputs: true,blockAllMedia: true,unmask: [".sentry-unmask, [data-sentry-unmask]"],+ beforeAddRecordingEvent: redactRecordingEventURLs,});});@@ -86,3 +95,191 @@ describe("setupSessionReplay", () => {expect(Sentry.addIntegration).not.toHaveBeenCalled();});});++describe("session replay on a page whose address carries a credential", () => {+ // The replay holds state across navigations, so each test loads it afresh.+ async function load() {+ vi.resetModules();+ return {+ Sentry: vi.mocked(await import("@sentry/nextjs")),+ ...(await import("../session-replay")),+ };+ }++ // A resume waits for the stop before it, so it lands on a later tick.+ async function settled() {+ await new Promise((resolve) => setTimeout(resolve, 0));+ }++ function recordingReplay(mode: "session" | "buffer") {+ return {+ getReplayId: vi.fn(() => "r-1"),+ getRecordingMode: vi.fn(() => mode),+ stop: vi.fn(async () => undefined),+ start: vi.fn(),+ startBuffering: vi.fn(),+ };+ }++ beforeEach(() => {+ vi.clearAllMocks();+ configureCookiebot();+ installCookiebot({ statistics: true });+ });++ afterEach(() => {+ history.replaceState({}, "", "/");+ removeCookiebot();+ vi.unstubAllEnvs();+ });++ it.each(["/reset-password?token=r-1", "/link/t-1"])(+ "starts no replay on %s",+ async (page) => {+ history.replaceState({}, "", page);+ const { Sentry, setupSessionReplay } = await load();++ expect(integrationNames(setupSessionReplay())).toEqual([+ "ReplayEventURLRedaction",+ ]);+ expect(Sentry.replayIntegration).not.toHaveBeenCalled();+ },+ );++ it("starts one on an ordinary page, query and all", async () => {+ history.replaceState({}, "", "/copilot?sessionId=s-1");+ const { setupSessionReplay } = await load();++ expect(integrationNames(setupSessionReplay())).toEqual([+ "Replay",+ "ReplayEventURLRedaction",+ ]);+ });++ it("starts the replay once the visitor leaves the page", async () => {+ history.replaceState({}, "", "/link/t-1");+ const { Sentry, setupSessionReplay, syncReplayWithPage } = await load();+ setupSessionReplay();++ syncReplayWithPage(`${location.origin}/link/t-1`);+ await settled();+ expect(Sentry.addIntegration).not.toHaveBeenCalled();++ history.replaceState({}, "", "/library");+ syncReplayWithPage(location.href);+ await settled();+ expect(addedIntegrationNames()).toEqual(["Replay"]);+ });++ it("starts nothing when consent is granted there, until the visitor leaves", async () => {+ installCookiebot();+ history.replaceState({}, "", "/reset-password?token=r-1");+ const { setupSessionReplay, syncReplayWithPage } = await load();++ expect(setupSessionReplay()).toEqual([]);+ answerCookiebot({ statistics: true });+ expect(addedIntegrationNames()).not.toContain("Replay");++ history.replaceState({}, "", "/library");+ syncReplayWithPage(location.href);+ await settled();+ expect(+ addedIntegrationNames().filter((name) => name === "Replay"),+ ).toHaveLength(1);+ });++ it.each([+ ["session", "start"],+ ["buffer", "startBuffering"],+ ] as const)(+ "stops a %s recording on the way in and resumes it on the way out",+ async (mode, resume) => {+ const { Sentry, holdReplayBackFor, syncReplayWithPage } = await load();+ const replay = recordingReplay(mode);+ Sentry.getReplay.mockReturnValue(replay as never);++ // As Next calls it when the navigation starts, then once it has landed.+ holdReplayBackFor("/link/t-1");+ syncReplayWithPage(`${location.origin}/link/t-1`);+ await settled();+ expect(replay.stop).toHaveBeenCalledTimes(1);+ expect(replay.start).not.toHaveBeenCalled();+ expect(replay.startBuffering).not.toHaveBeenCalled();++ holdReplayBackFor("/library");+ syncReplayWithPage(`${location.origin}/library`);+ await settled();+ expect(replay[resume]).toHaveBeenCalledTimes(1);+ expect(replay.stop).toHaveBeenCalledTimes(1);+ },+ );++ it("leaves a replay alone between ordinary pages", async () => {+ const { Sentry, holdReplayBackFor, syncReplayWithPage } = await load();+ const replay = recordingReplay("session");+ Sentry.getReplay.mockReturnValue(replay as never);++ holdReplayBackFor("/marketplace?page=2");+ syncReplayWithPage(`${location.origin}/marketplace?page=2`);+ await settled();++ expect(replay.stop).not.toHaveBeenCalled();+ expect(replay.start).not.toHaveBeenCalled();+ });++ it("does not resume once consent is withdrawn", async () => {+ const { Sentry, holdReplayBackFor, syncReplayWithPage } = await load();+ const replay = recordingReplay("session");+ Sentry.getReplay.mockReturnValue(replay as never);++ holdReplayBackFor("/link/t-1");+ answerCookiebot({ statistics: false });+ syncReplayWithPage(`${location.origin}/library`);+ await settled();++ expect(replay.stop).toHaveBeenCalledTimes(1);+ expect(replay.start).not.toHaveBeenCalled();+ });++ it("does not start a replay that was not recording", async () => {+ const { Sentry, holdReplayBackFor, syncReplayWithPage } = await load();+ const replay = { ...recordingReplay("buffer"), getReplayId: vi.fn() };+ Sentry.getReplay.mockReturnValue(replay as never);++ holdReplayBackFor("/link/t-1");+ syncReplayWithPage(`${location.origin}/library`);+ await settled();++ expect(replay.stop).not.toHaveBeenCalled();+ expect(replay.startBuffering).not.toHaveBeenCalled();+ });++ it("restarts only once a slow stop has settled, on the page it lands on", async () => {+ const { Sentry, holdReplayBackFor, syncReplayWithPage } = await load();+ const replay = recordingReplay("session");+ let finishStop = () => {};+ replay.stop.mockReturnValue(+ new Promise<undefined>((resolve) => {+ finishStop = () => resolve(undefined);+ }),+ );+ Sentry.getReplay.mockReturnValue(replay as never);++ holdReplayBackFor("/link/t-1");+ history.replaceState({}, "", "/library");+ syncReplayWithPage(location.href);+ await settled();+ expect(replay.start).not.toHaveBeenCalled();++ // The visitor is back on a credential page by the time the stop settles.+ history.replaceState({}, "", "/link/t-2");+ finishStop();+ await settled();+ expect(replay.start).not.toHaveBeenCalled();++ history.replaceState({}, "", "/library");+ syncReplayWithPage(location.href);+ await settled();+ expect(replay.start).toHaveBeenCalledTimes(1);+ });+});diff --git a/autogpt_platform/frontend/src/lib/sentry-url-redaction.ts b/autogpt_platform/frontend/src/lib/sentry-url-redaction.tsnew file mode 100644index 0000000000..0c9501a5f5--- /dev/null+++ b/autogpt_platform/frontend/src/lib/sentry-url-redaction.ts@@ -0,0 +1,225 @@+import type {+ Breadcrumb,+ Event,+ init,+ replayIntegration,+} from "@sentry/nextjs";++// The backend's marker and rule (backend/util/url_redaction.py): every query+// value is masked and its key kept, and so is every credential path segment.+export const REDACTED = "[REDACTED]";++/**+ * Mask the credentials in a URL. A fragment is treated like a query, since in+ * the browser it can hold an `access_token=` too.+ */+export function redactURL(url: string): string {+ const start = url.search(/[?#]/);+ if (start < 0) return redactPath(url);+ return redactPath(url.slice(0, start) + redactQuery(url.slice(start)));+}++/** Mask the credentials in every URL inside a span description or message. */+export function redactURLsInText(text: string): string {+ return redactPath(text.replace(QUERY_IN_TEXT, redactQuery));+}++/**+ * True when a page's address carries a credential: in a path segment the+ * redactor masks, under a credential key in its query or fragment, or in the+ * address a redirect key points on to.+ */+export function holdsCredential(url: string): boolean {+ const start = url.search(/[?#]/);+ const path = start < 0 ? url : url.slice(0, start);+ if (redactPath(path) !== path) return true;+ if (start < 0) return false;+ return url+ .slice(start + 1)+ .split(/[?#&]/)+ .some((pair) => {+ const [key, ...value] = pair.split("=");+ const name = key.toLowerCase();+ if (CREDENTIAL_QUERY_KEYS.has(name)) return true;+ return (+ REDIRECT_QUERY_KEYS.has(name) &&+ holdsCredential(decode(value.join("=")))+ );+ });+}++type SentryOptions = NonNullable<Parameters<typeof init>[0]>;+type SentrySpan = Parameters<NonNullable<SentryOptions["beforeSendSpan"]>>[0];++/**+ * An event carries its page as `request.url`, the page before it as Referer,+ * earlier pages and calls in breadcrumbs and spans, and a replay's in `urls`.+ */+export function redactEventURLs<T extends Event & { urls?: string[] }>(+ event: T,+): T {+ redactRequestURLs(event.request);+ // An error is named after the page's raw path when its route isn't known.+ if (event.transaction) event.transaction = redactURL(event.transaction);+ if (event.urls) event.urls = event.urls.map(redactURL);+ event.breadcrumbs?.forEach(redactBreadcrumbURLs);+ const trace = event.contexts?.trace;+ if (trace?.data) redactAttributeURLs(trace.data);+ event.spans?.forEach(redactSpanURLs);+ return event;+}++export function redactSpanURLs<T extends SentrySpan>(span: T): T {+ if (span.description) span.description = redactURLsInText(span.description);+ if (span.data) redactAttributeURLs(span.data);+ return span;+}++// Navigation breadcrumbs record `from` and `to`; fetch, xhr and the replay's+// own click and hydration breadcrumbs record `url`.+const BREADCRUMB_URL_FIELDS = ["url", "from", "to"];++export function redactBreadcrumbURLs(breadcrumb: Breadcrumb): Breadcrumb {+ const { data } = breadcrumb;+ if (!data) return breadcrumb;+ for (const field of BREADCRUMB_URL_FIELDS) {+ if (typeof data[field] === "string") data[field] = redactURL(data[field]);+ }+ return breadcrumb;+}++type ReplayOptions = NonNullable<Parameters<typeof replayIntegration>[0]>;+type RecordingEvent = Parameters<+ NonNullable<ReplayOptions["beforeAddRecordingEvent"]>+>[0];++/**+ * A replay records breadcrumbs, page navigations and resource and network+ * timings as events of its own, each named by its URL.+ */+export function redactRecordingEventURLs(event: RecordingEvent) {+ if (event.data.tag === "breadcrumb") redactBreadcrumbURLs(event.data.payload);+ if (event.data.tag === "performanceSpan") {+ const span = event.data.payload;+ span.description = redactURL(span.description);+ const { data } = span;+ if (data && "previous" in data && typeof data.previous === "string") {+ data.previous = redactURL(data.previous);+ }+ }+ return event;+}++// Where fetch, xhr and Next's server spans, the semantic conventions and web+// vitals put a URL.+const URL_ATTRIBUTES = new Set([+ "url",+ "url.full",+ "url.path",+ "url.query",+ "url.fragment",+ "http.url",+ "http.target",+ "http.query",+ "http.fragment",+ "lcp.url",+ "transaction",+]);++function redactAttributeURLs(attributes: Record<string, unknown>) {+ for (const [name, value] of Object.entries(attributes)) {+ if (typeof value !== "string") continue;+ if (name.startsWith(HEADER_ATTRIBUTE_PREFIX)) {+ attributes[name] = redactHeader(name, value);+ } else if (QUERY_ATTRIBUTES.has(name)) {+ // The semantic conventions give a query or fragment without its ? or #.+ attributes[name] = redactQuery(value);+ } else if (URL_ATTRIBUTES.has(name)) {+ attributes[name] = redactURL(value);+ }+ }+}++function redactRequestURLs(request: Event["request"]) {+ if (!request) return;+ if (request.url) request.url = redactURL(request.url);+ // Server events also carry the parsed query, which can't be masked by key.+ if (request.query_string) request.query_string = REDACTED;+ const { headers } = request;+ if (!headers) return;+ for (const [name, value] of Object.entries(headers)) {+ headers[name] = redactHeader(name, value);+ }+}++// Any request header is read as a URL, since the Referer and Next's next-url+// name the page. Next's router state tree names it in a form no rule can parse.+function redactHeader(name: string, value: string) {+ return ROUTER_STATE_HEADER.test(name) ? REDACTED : redactURL(value);+}++// The backend's credential routes, matched anywhere (behind /api/proxy too) and+// zero-width so stacked prefixes all match; /api/invitations/pending is kept.+const API_CREDENTIAL_SEGMENT =+ /(?=(\/api\/(?:integrations\/(?:[^/?\s]+\/)?webhooks|platform-linking\/(?:user-)?tokens|public\/shared(?:\/chats)?|invitations(?!\/pending(?:[/?\s"']|$)))\/)([^/?\s]+))/g;+// Pages whose own path is the token, at the start of a URL or after a space.+// /link/slack is static, and a route name like /link/[token] holds no token.+const PAGE_CREDENTIAL_SEGMENT =+ /(^|\s)((?:[a-z][a-z\d+.-]*:)?\/\/[^/?#\s]*)?(\/(?:link\/(?!slack(?:[/?#\s]|$))|share\/(?!chat\/\[)(?:chat\/)?))(?!\[)[^/?\s]+/gi;+// Query keys that carry a credential into a page; a new page that receives one+// in its query adds its key here. Masking needs no list: it masks every value.+const CREDENTIAL_QUERY_KEYS = new Set([+ "token",+ "code",+ "state",+ "access_token",+ "refresh_token",+ "id_token",+]);+// Query keys whose value is the address a page sends the visitor on to.+const REDIRECT_QUERY_KEYS = new Set([+ "next",+ "callbackurl",+ "redirect_uri",+ "returnto",+]);+const QUERY_ATTRIBUTES = new Set([+ "url.query",+ "url.fragment",+ "http.query",+ "http.fragment",+]);+const HEADER_ATTRIBUTE_PREFIX = "http.request.header.";+const ROUTER_STATE_HEADER = /next[-_]router[-_]state[-_]tree$/i;+const QUERY_IN_TEXT = /\?\S+/g;+const QUERY_VALUE = /=[^&]*/g;++function redactQuery(query: string) {+ return query.replace(QUERY_VALUE, `=${REDACTED}`);+}++function decode(value: string) {+ const encoded = value.replace(/\+/g, " ");+ try {+ return decodeURIComponent(encoded);+ } catch {+ return encoded;+ }+}++// Built from the spans, as the backend does, so overlapping matches merge.+function redactPath(text: string) {+ let redacted = "";+ let done = 0;+ for (const match of text.matchAll(API_CREDENTIAL_SEGMENT)) {+ const start = match.index + match[1].length;+ const end = start + match[2].length;+ if (end <= done) continue;+ redacted += text.slice(done, Math.max(start, done)) + REDACTED;+ done = end;+ }+ return (redacted + text.slice(done)).replace(+ PAGE_CREDENTIAL_SEGMENT,+ `$1$2$3${REDACTED}`,+ );+}diff --git a/autogpt_platform/frontend/src/lib/session-replay.ts b/autogpt_platform/frontend/src/lib/session-replay.tsindex 5dd53efeca..2116a0001b 100644--- a/autogpt_platform/frontend/src/lib/session-replay.ts+++ b/autogpt_platform/frontend/src/lib/session-replay.ts@@ -1,19 +1,17 @@+import {+ holdsCredential,+ redactEventURLs,+ redactRecordingEventURLs,+} from "@/lib/sentry-url-redaction";import { hasConsentFor, subscribeToConsent } from "@/services/consent/consent";import * as Sentry from "@sentry/nextjs";-function createReplayIntegrations() {- // Everything on screen is masked unless an element opts in with- // `sentry-unmask`, which only copy that is the same for every user should.- // No canvas recording: its pixels are sent as drawn, with no masking.- return [- Sentry.replayIntegration({- maskAllText: true,- maskAllInputs: true,- blockAllMedia: true,- unmask: [".sentry-unmask, [data-sentry-unmask]"],- }),- ];-}+// How the replay was recording when it was held back for a page whose address+// carries a credential, or "start" when it had not started yet.+let heldBack: "start" | "session" | "buffer" | null = null;+// The stop that held it back, which a restart waits for: it clears the+// recording's state when it settles.+let stopping: Promise<void> = Promise.resolve();/*** Session replay records the screen, so it only exists once the visitor has@@ -33,3 +31,71 @@ export function setupSessionReplay() {});return [];}++/**+ * Stops the replay before a page whose address carries a credential renders.+ * Next calls it as each client navigation starts.+ */+export function holdReplayBackFor(href: string) {+ if (heldBack || !holdsCredential(href)) return;+ const replay = Sentry.getReplay();+ const mode = replay?.getRecordingMode();+ if (!replay?.getReplayId() || !mode) return;+ heldBack = mode;+ stopping = replay.stop().catch(() => undefined);+}++/**+ * Called once a navigation has landed: holds the replay back on a page whose+ * address carries a credential, and otherwise resumes it as it was.+ */+export function syncReplayWithPage(href: string) {+ if (holdsCredential(href)) {+ holdReplayBackFor(href);+ return;+ }+ if (heldBack) void stopping.then(resumeReplay);+}++// Runs once the stop has settled, against the address and consent as they+// are by then.+function resumeReplay() {+ const mode = heldBack;+ if (!mode || holdsCredential(window.location.href)) return;+ heldBack = null;+ if (!hasConsentFor("monitoring")) return;+ if (mode === "start") Sentry.addIntegration(createReplayIntegration());+ else if (mode === "session") Sentry.getReplay()?.start();+ else Sentry.getReplay()?.startBuffering();+}++function createReplayIntegrations() {+ if (!holdsCredential(window.location.href)) {+ return [createReplayIntegration(), replayEventURLRedaction()];+ }+ heldBack = "start";+ return [replayEventURLRedaction()];+}++function createReplayIntegration() {+ // Everything on screen is masked unless an element opts in with+ // `sentry-unmask`, which only copy that is the same for every user should.+ // No canvas recording: its pixels are sent as drawn, with no masking.+ return Sentry.replayIntegration({+ maskAllText: true,+ maskAllInputs: true,+ blockAllMedia: true,+ unmask: [".sentry-unmask, [data-sentry-unmask]"],+ beforeAddRecordingEvent: redactRecordingEventURLs,+ });+}++// Replay events skip beforeSend, so their URLs are masked here instead.+function replayEventURLRedaction() {+ return {+ name: "ReplayEventURLRedaction",+ processEvent(event: Sentry.Event) {+ return event.type === "replay_event" ? redactEventURLs(event) : event;+ },+ };+}