From d36d205c45d18cdf8335556dd021e7dfd782cd22 Mon Sep 17 00:00:00 2001 From: Atef Bellaaj Date: Mon, 16 Nov 2026 23:56:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=A4=20fix:=20Strip=20Credentials=20Fro?= =?UTF-8?q?m=20RUM=20Network=20Telemetry=20(#18219)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 + AGENTS.md | 14 ++ client/src/lib/rum/exporter.spec.ts | 102 +++++++++- client/src/lib/rum/exporter.ts | 15 +- client/src/lib/rum/privacy.spec.ts | 213 +++++++++++++++++++++ client/src/lib/rum/privacy.ts | 83 ++++++-- e2e/rum/privacy.spec.ts | 281 +++++++++++++++++++++++++++- 7 files changed, 688 insertions(+), 26 deletions(-) diff --git a/.env.example b/.env.example index a814b4c85b..0c58f326cc 100644 --- a/.env.example +++ b/.env.example @@ -479,6 +479,12 @@ NODE_MAX_OLD_SPACE_SIZE=6144 # URLs, element attributes and assets may still contain sensitive information; # validate with synthetic data before enabling recording for real users. # Console/network capture may collect sensitive browser logs, prompts, responses, or payloads. +# Trace export removes request/response Authorization, Proxy-Authorization, Cookie, Set-Cookie, +# API-key, token and signature header attributes, including custom header names containing these terms. +# Request/response bodies are omitted for auth and credential endpoints, including key settings, +# OAuth connections and credential configuration. Other bodies remain available with advanced capture. +# If advanced capture was enabled, review collector access and retained data for exposed credentials. +# Disable capture, retire exposed tokens/keys, and remove affected telemetry under your retention policy. # RUM_SESSION_REPLAY_ENABLED=false # Deprecated fallback: RUM_DISABLE_REPLAY=false opts in only when the positive flag is unset. # YAML telemetry.rum.enabled / telemetry.rum.sessionReplay.enabled take precedence diff --git a/AGENTS.md b/AGENTS.md index 685b2af282..b446a351a2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -142,6 +142,20 @@ stable code. `packages/data-schemas/src/methods/prompt.ts` currently returns `{ failure; do not extend that pattern. Test absence versus failure, the boundary's non-success response, and that sensitive diagnostics cannot reach a user-visible surface. +## RUM privacy + +When adding, renaming, or changing an endpoint that accepts or returns passwords, tokens, API keys, +or other credentials, review [the RUM privacy rules](client/src/lib/rum/privacy.ts) in the same change. +Check every route and alias, including deployment base paths. Update `CREDENTIAL_ROUTES` when the +endpoint needs request and response bodies removed from telemetry. Check `CREDENTIAL_HEADER` when +introducing a credential header. Keep body capture for ordinary chat and API requests. + +Add regression cases to [the privacy tests](client/src/lib/rum/privacy.spec.ts). Verify that both +request and response secrets are absent and that ordinary payloads remain. For changes to the +telemetry SDK, capture settings, or export path, also check [the exporter](client/src/lib/rum/exporter.ts) +and its [tests](client/src/lib/rum/exporter.spec.ts). Run [the RUM browser tests](e2e/rum/privacy.spec.ts) +to verify the actual SDK output through both proxy and direct export. + ## Frontend theming and styling For frontend work, compose existing `@librechat/client` primitives and variants before adding diff --git a/client/src/lib/rum/exporter.spec.ts b/client/src/lib/rum/exporter.spec.ts index ee3267068a..4bafca4791 100644 --- a/client/src/lib/rum/exporter.spec.ts +++ b/client/src/lib/rum/exporter.spec.ts @@ -15,13 +15,26 @@ it('sanitizes the actual span export boundary, including events, resources and l duration: [1, 0], ended: true, status: { code: 2, message: `failed ${url}` }, - attributes: { 'location.href': url }, - resource: resourceFromAttributes({ 'service.name': 'librechat-web', 'page.url': url }), + attributes: { 'location.href': url, 'http.request.header.authorization': [`Bearer ${secret}`] }, + resource: resourceFromAttributes({ + 'service.name': 'librechat-web', + 'page.url': url, + 'http.response.header.set_cookie': [`session=${secret}`], + }), instrumentationScope: { name: 'test' }, events: [ - { name: 'exception', time: [2, 0], attributes: { 'exception.stacktrace': `at ${url}` } }, + { + name: 'exception', + time: [2, 0], + attributes: { + 'exception.stacktrace': `at ${url}`, + 'http.request.header.x_api_key': secret, + }, + }, + ], + links: [ + { context, attributes: { 'http.url': url, 'http.response.header.x_access_token': [secret] } }, ], - links: [{ context, attributes: { 'http.url': url } }], droppedAttributesCount: 0, droppedEventsCount: 0, droppedLinksCount: 0, @@ -37,8 +50,89 @@ it('sanitizes the actual span export boundary, including events, resources and l new PrivacyTraceExporter(exporter).export([span], jest.fn()); const safe = exportSpans.mock.calls[0][0][0]; expect(JSON.stringify(safe)).not.toContain(secret); + expect(JSON.stringify(safe)).not.toMatch(/authorization|set_cookie|x_api_key|x_access_token/); + expect(span.attributes['http.request.header.authorization']).toEqual([`Bearer ${secret}`]); + expect(span.resource.attributes['http.response.header.set_cookie']).toEqual([ + `session=${secret}`, + ]); + expect(span.events[0].attributes?.['http.request.header.x_api_key']).toBe(secret); + expect(span.links[0].attributes?.['http.response.header.x_access_token']).toEqual([secret]); expect(safe.spanContext()).toEqual(context); expect(safe.attributes['location.href']).toBe('https://app.example/login/2fa'); expect(safe.resource.attributes['service.name']).toBe('librechat-web'); expect(span.attributes['location.href']).toBe(url); }); + +it.each([ + ['/api/auth/refresh', true], + ['/librechat/api/mcp/oauth/tokens/flow-123', true], + ['/api/keys', true], + ['/api/messages', false], +])('applies the body policy to every attribute bag for %s', (path, sensitive) => { + const context = { traceId: 'a'.repeat(32), spanId: 'b'.repeat(16), traceFlags: 1 }; + const bodies = { + 'http.request.body': '{"secret":"synthetic-request-credential"}', + 'http.response.body': ['{"token":"synthetic-response-credential"}'], + }; + const span: ReadableSpan = { + name: 'HTTP POST', + kind: 2, + spanContext: () => context, + startTime: [1, 0], + endTime: [2, 0], + duration: [1, 0], + ended: true, + status: { code: 0 }, + attributes: { + ...bodies, + 'http.url': `https://app.example${path}?ignored=query`, + 'http.method': 'POST', + 'http.status_code': 200, + }, + resource: resourceFromAttributes({ 'service.name': 'librechat-web', ...bodies }), + instrumentationScope: { name: 'test' }, + events: [{ name: 'network', time: [2, 0], attributes: { ...bodies } }], + links: [{ context, attributes: { ...bodies } }], + droppedAttributesCount: 0, + droppedEventsCount: 0, + droppedLinksCount: 0, + }; + const original = JSON.stringify({ ...span, resource: span.resource.attributes }); + const exportSpans = jest.fn< + ReturnType, + Parameters + >(); + const exporter: SpanExporter = { + export: exportSpans, + shutdown: jest.fn().mockResolvedValue(undefined), + }; + new PrivacyTraceExporter(exporter).export([span], jest.fn()); + const safe = exportSpans.mock.calls[0][0][0]; + for (const attributes of [ + safe.attributes, + safe.resource.attributes, + safe.events[0].attributes, + safe.links[0].attributes, + ]) { + if (sensitive) { + expect(attributes).not.toHaveProperty('http.request.body'); + expect(attributes).not.toHaveProperty('http.response.body'); + expect(JSON.stringify(attributes)).not.toContain('synthetic-request-credential'); + expect(JSON.stringify(attributes)).not.toContain('synthetic-response-credential'); + } else { + expect(attributes).toMatchObject(bodies); + } + } + expect(safe.attributes).toMatchObject({ + 'http.url': `https://app.example${path}`, + 'http.method': 'POST', + 'http.status_code': 200, + }); + expect(safe.resource.attributes['service.name']).toBe('librechat-web'); + expect(safe.spanContext()).toEqual(context); + expect(safe.startTime).toEqual(span.startTime); + expect(safe.endTime).toEqual(span.endTime); + expect(safe.duration).toEqual(span.duration); + expect(safe.status).toEqual(span.status); + expect(JSON.stringify({ ...span, resource: span.resource.attributes })).toBe(original); +}); diff --git a/client/src/lib/rum/exporter.ts b/client/src/lib/rum/exporter.ts index f33f339e57..ea1c8c1a38 100644 --- a/client/src/lib/rum/exporter.ts +++ b/client/src/lib/rum/exporter.ts @@ -1,8 +1,9 @@ import { resourceFromAttributes } from '@opentelemetry/resources'; import type { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'; -import { sanitizeSpanAttributes, sanitizeUrlReferences } from './privacy'; +import { sanitizeSpanAttributes, sanitizeUrlReferences, isCredentialRequest } from './privacy'; function sanitizeSpan(span: ReadableSpan): ReadableSpan { + const redactBody = isCredentialRequest(span.attributes); return { name: sanitizeUrlReferences(span.name), kind: span.kind, @@ -16,17 +17,21 @@ function sanitizeSpan(span: ReadableSpan): ReadableSpan { ...span.status, ...(span.status.message ? { message: sanitizeUrlReferences(span.status.message) } : {}), }, - attributes: sanitizeSpanAttributes(span.attributes), - resource: resourceFromAttributes(sanitizeSpanAttributes(span.resource.attributes)), + attributes: sanitizeSpanAttributes(span.attributes, redactBody), + resource: resourceFromAttributes(sanitizeSpanAttributes(span.resource.attributes, redactBody)), instrumentationScope: span.instrumentationScope, events: span.events.map((event) => ({ ...event, name: sanitizeUrlReferences(event.name), - ...(event.attributes ? { attributes: sanitizeSpanAttributes(event.attributes) } : {}), + ...(event.attributes + ? { attributes: sanitizeSpanAttributes(event.attributes, redactBody) } + : {}), })), links: span.links.map((link) => ({ ...link, - ...(link.attributes ? { attributes: sanitizeSpanAttributes(link.attributes) } : {}), + ...(link.attributes + ? { attributes: sanitizeSpanAttributes(link.attributes, redactBody) } + : {}), })), droppedAttributesCount: span.droppedAttributesCount, droppedEventsCount: span.droppedEventsCount, diff --git a/client/src/lib/rum/privacy.spec.ts b/client/src/lib/rum/privacy.spec.ts index 9c581ea345..b6799c20ec 100644 --- a/client/src/lib/rum/privacy.spec.ts +++ b/client/src/lib/rum/privacy.spec.ts @@ -3,6 +3,7 @@ import { sanitizeSpanAttributes, sanitizeReplayEvent, isReplayRouteAllowed, + isCredentialRequest, } from './privacy'; const secret = 'synthetic-login-token-do-not-export'; @@ -204,3 +205,215 @@ it('sanitizes SDK document/referrer/network span attributes and standalone query count: 3, }); }); + +it.each(['request', 'response'])( + 'drops %s credential headers in string and array form', + (direction) => { + const headers = [ + 'authorization', + 'Authorization', + 'proxy-authorization', + 'proxy_authorization', + 'cookie', + 'set-cookie', + 'Set_Cookie', + 'api-key', + 'api_key', + 'apikey', + 'X-API-Key', + 'X_HDX_API_KEY', + 'token', + 'x-access-token', + 'X_Refresh_Token', + 'x-amz-security-token', + 'signature', + 'X-Signature', + 'signature-input', + ]; + for (const header of headers) { + for (const value of [secret, [secret]]) { + const key = `http.${direction}.header.${header}`; + const source = { [key]: value, 'http.status_code': 200 }; + expect(sanitizeSpanAttributes(source)).toEqual({ 'http.status_code': 200 }); + expect(source[key]).toEqual(value); + } + } + }, +); + +it('checks header names before URL redaction and preserves ordinary attributes', () => { + expect( + sanitizeSpanAttributes({ + 'HTTP.REQUEST.HEADER.Authorization': secret, + 'http.response.header.x_token_url': secret, + 'http.request.header.content_type': ['application/json'], + 'http.response.header.x_request_id': ['request-123'], + 'http.response.header.location': [url], + 'http.method': 'POST', + 'gen_ai.usage.input_tokens': 10, + count: 3, + }), + ).toEqual({ + 'http.request.header.content_type': ['application/json'], + 'http.response.header.x_request_id': ['request-123'], + 'http.response.header.location': ['https://librechat.example/c/new'], + 'http.method': 'POST', + 'gen_ai.usage.input_tokens': 10, + count: 3, + }); +}); + +it.each([ + '/api/auth/login', + '/api/auth/register', + '/api/auth/refresh', + '/api/auth/resetPassword', + '/api/auth/2fa/setup/confirm', + '/api/auth/passkey/login/verify', + '/api/auth/graph-token', + '/oauth/openid/callback', + '/oauth/saml/callback', + '/api/user/verify', + '/api/user/verify/resend', + '/api/user/email/change', + '/api/user/email/verify', + '/api/user/delete', + '/api/user/plugins', + '/api/keys', + '/api/api-keys', + '/api/mcp/oauth/tokens/flow-123', + '/api/mcp/server-123/oauth/bind', + '/api/mcp/server-123/oauth/callback', + '/api/mcp/servers', + '/api/mcp/servers/server-123', + '/api/agents/actions/agent-123', + '/api/assistants/v1/actions/assistant-123', + '/api/assistants/v2/actions/assistant-123', + '/api/actions/action-123/oauth/callback', + '/api/pull-requests/oauth/connect', + '/api/admin/login/local', + '/api/admin/oauth/exchange', + '/api/admin/oauth/refresh', + '/api/admin/config/user/user-123/fields', + '/api/admin/config/tenant/tenant-123/changes', + '/api/admin/langfuse/connection', + '/api/admin/langfuse/connection/test', + '/api/admin/skills/sync/credentials/github', + '/api/admin/code-environments/worker-123/pairings', + '/api/code-environments/pairings', + '/api/code-environments/setup/complete', + '/api/channels/workspaces/apps', + '/api/channels/workspaces/connect', + '/api/channels/workspaces/oauth/callback', + '/api/channels/links/preview', + '/api/channels/links/accept', + '/api/agents/agent-123/channels/channel-123', + '/app/api/auth/refresh', + '/app/oauth/openid/callback', + '/proxy/api/app/oauth/openid/callback', + '/oauth/google', + '/oauth/facebook/callback', + '/oauth/github/callback', + '/oauth/discord/callback', + '/oauth/apple/callback', + '/app/%61pi/auth/refresh', + '/app/api%2fauth%2frefresh', + '/API/Auth/Refresh/', + '/api/auth/%ZZ', +])('removes credential request and response bodies for %s', (pathname) => { + const requestUrl = `https://librechat.example${pathname}?retry=true#fragment`; + for (const body of [secret, [secret]]) { + const source = Object.freeze({ + 'http.url': requestUrl, + 'http.method': 'POST', + 'http.status_code': 200, + 'http.request.body': body, + 'http.response.body': body, + 'http.request.header.content_type': ['application/json'], + }); + expect(isCredentialRequest(source)).toBe(true); + expect(sanitizeSpanAttributes(source)).toEqual({ + 'http.url': `https://librechat.example${pathname}`, + 'http.method': 'POST', + 'http.status_code': 200, + 'http.request.header.content_type': ['application/json'], + }); + expect(source['http.request.body']).toEqual(body); + expect(source['http.response.body']).toEqual(body); + } +}); + +it.each([ + '/api/agents/chat', + '/api/messages', + '/api/user', + '/api/user/preferences', + '/api/user/settings/favorites', + '/api/mcp/tools', + '/api/mcp/resources/read', + '/api/mcp/server-123/auth-values', + '/api/mcp/server-123/reinitialize', + '/api/admin/users', + '/api/admin/roles', + '/api/admin/langfuse/prompts', + '/api/admin/skills/sync/run', + '/api/code-environments', + '/api/code-environments/worker-123/status', + '/api/code-environments/setup/attempt-123/approve', + '/api/agents/agent-123/channels/channel-123/workspace-routes', + '/api/channels/workspaces/workspace-123/managers', + '/api/channels/links', + '/api/authors', + '/api/keys-export', + '/api/user/pluginsettings', + '/api/mcp/servers-list', + '/api/agents/actions-report', + '/api/pull-requests/oauth-status', + '/oauth-status', + '/app/api/authors?next=/api/auth/refresh', + '/app/api/messages#/api/auth/refresh', +])('preserves opted-in ordinary request bodies for %s', (pathname) => { + const source = { + 'http.url': `https://librechat.example${pathname}`, + 'location.href': 'https://librechat.example/login', + 'http.request.body': 'ordinary prompt', + 'http.response.body': ['ordinary response'], + 'http.status_code': 200, + }; + expect(isCredentialRequest(source)).toBe(false); + expect(sanitizeSpanAttributes(source)).toMatchObject({ + 'http.request.body': 'ordinary prompt', + 'http.response.body': ['ordinary response'], + 'http.status_code': 200, + }); +}); + +it('propagates credential body removal to attribute bags without a request URL', () => { + const source = Object.freeze({ + 'HTTP.REQUEST.BODY': secret, + 'http.response.body': [secret], + 'http.response.body.size': 123, + 'http.status_code': 200, + }); + expect(sanitizeSpanAttributes(source, true)).toEqual({ + 'http.response.body.size': 123, + 'http.status_code': 200, + }); + expect(source['HTTP.REQUEST.BODY']).toBe(secret); + expect(source['http.response.body']).toEqual([secret]); + expect(sanitizeSpanAttributes(source)).toEqual(source); +}); + +it('uses relative request URLs and ignores page, referrer, missing and invalid URL attributes', () => { + expect(isCredentialRequest({ 'http.url': '/app/api/auth/refresh?retry=true' })).toBe(true); + expect(isCredentialRequest({ 'http.url': 'api/auth/refresh' })).toBe(true); + expect(isCredentialRequest({ 'http.url': 'http://[invalid' })).toBe(false); + expect(isCredentialRequest({ 'http.url': ['/api/auth/refresh'] })).toBe(false); + expect( + isCredentialRequest({ + 'location.href': '/api/auth/refresh', + 'document.referrer': '/api/auth/refresh', + }), + ).toBe(false); + expect(isCredentialRequest({})).toBe(false); +}); diff --git a/client/src/lib/rum/privacy.ts b/client/src/lib/rum/privacy.ts index 530af034b2..1ffc58d036 100644 --- a/client/src/lib/rum/privacy.ts +++ b/client/src/lib/rum/privacy.ts @@ -17,6 +17,48 @@ const AUTH_ROUTE = /\/(?:login|register|forgot-password|reset-password|verify|oa const URL_ATTRIBUTE = /^(?:data-)?(?:href|src|url|uri|action|formaction|poster|cite|background|data|xlink:href)$/i; const URL_SPAN_ATTRIBUTE = /url|uri|href|referr?er|^http\.target$|location/i; +const HEADER_SPAN_ATTRIBUTE = /^http\.(?:request|response)\.header\.(.+)$/i; +const CREDENTIAL_HEADER = /authorization|cookie|apikey|token|signature/i; +const BODY_SPAN_ATTRIBUTE = /^http\.(?:request|response)\.body$/i; +const CREDENTIAL_ROUTES = [ + /\/oauth\/(?:google|facebook|openid|github|discord|apple|saml)(?:\/callback)?\/?$/i, + /\/api\/(?:auth|keys|api-keys)(?:\/|$)/i, + /\/api\/user\/(?:verify|email)(?:\/|$)/i, + /\/api\/user\/(?:plugins|delete)\/?$/i, + /\/api\/admin\/(?:login|oauth|config)(?:\/|$)/i, + /\/api\/admin\/langfuse\/connection(?:\/|$)/i, + /\/api\/admin\/skills\/sync\/credentials(?:\/|$)/i, + /\/api\/admin\/code-environments\/[^/]+\/pairings\/?$/i, + /\/api\/mcp\/(?:oauth|servers)(?:\/|$)/i, + /\/api\/mcp\/[^/]+\/oauth(?:\/|$)/i, + /\/api\/actions\/[^/]+\/oauth(?:\/|$)/i, + /\/api\/(?:agents|assistants\/v[12])\/actions(?:\/|$)/i, + /\/api\/pull-requests\/oauth(?:\/|$)/i, + /\/api\/code-environments\/pairings\/?$/i, + /\/api\/code-environments\/setup\/(?:start|poll|cancel|complete)\/?$/i, + /\/api\/channels\/workspaces\/(?:apps|oauth)(?:\/|$)/i, + /\/api\/channels\/workspaces\/connect\/?$/i, + /\/api\/channels\/links\/(?:preview|accept)\/?$/i, + /\/api\/agents\/[^/]+\/channels\/[^/]+\/?$/i, +]; + +/** Match the request URL, not the current page, so buffered auth calls stay protected. */ +export function isCredentialRequest(attributes: SpanAttributes): boolean { + const requestUrl = attributes['http.url']; + if (typeof requestUrl !== 'string') return false; + try { + let pathname = new URL(requestUrl, 'https://librechat.invalid').pathname; + try { + pathname = decodeURIComponent(pathname); + } catch { + // A malformed parameter must not hide an otherwise recognizable credential route. + } + // A deployment can mount LibreChat below a base path before these route segments. + return CREDENTIAL_ROUTES.some((route) => route.test(pathname)); + } catch { + return false; + } +} export function isReplayRouteAllowed(pathname: string): boolean { try { @@ -149,21 +191,34 @@ export function createReplayPrivacyPlugin(getPathname: () => string): ReplayPlug }; } -/** The SDK calls this hook at export time, including for spans buffered before a route change. */ -export function sanitizeSpanAttributes(attributes: SpanAttributes): SpanAttributes { +/** Remove credential headers and protected bodies before export, including buffered spans. */ +export function sanitizeSpanAttributes( + attributes: SpanAttributes, + redactBody = false, +): SpanAttributes { + const removeBody = redactBody || isCredentialRequest(attributes); return Object.fromEntries( - Object.entries(attributes).map(([key, value]) => { - if (URL_SPAN_ATTRIBUTE.test(key)) { - if (/(?:^|\.)query$|(?:^|\.)fragment$/i.test(key)) return [key, '']; - if (typeof value === 'string') return [key, sanitizeTelemetryUrl(value)]; - if (Array.isArray(value)) { - return [ - key, - value.map((entry) => (typeof entry === 'string' ? sanitizeTelemetryUrl(entry) : entry)), - ]; + Object.entries(attributes) + .filter(([key]) => { + if (removeBody && BODY_SPAN_ATTRIBUTE.test(key)) return false; + const header = HEADER_SPAN_ATTRIBUTE.exec(key)?.[1]; + // The SDK lowercases header names and replaces hyphens with underscores. + return !header || !CREDENTIAL_HEADER.test(header.replace(/[-_]/g, '')); + }) + .map(([key, value]) => { + if (URL_SPAN_ATTRIBUTE.test(key)) { + if (/(?:^|\.)query$|(?:^|\.)fragment$/i.test(key)) return [key, '']; + if (typeof value === 'string') return [key, sanitizeTelemetryUrl(value)]; + if (Array.isArray(value)) { + return [ + key, + value.map((entry) => + typeof entry === 'string' ? sanitizeTelemetryUrl(entry) : entry, + ), + ]; + } } - } - return [key, typeof value === 'string' ? sanitizeUrlReferences(value) : value]; - }), + return [key, typeof value === 'string' ? sanitizeUrlReferences(value) : value]; + }), ); } diff --git a/e2e/rum/privacy.spec.ts b/e2e/rum/privacy.spec.ts index 71eae0d1a6..42f5202d7c 100644 --- a/e2e/rum/privacy.spec.ts +++ b/e2e/rum/privacy.spec.ts @@ -2,6 +2,10 @@ import { build } from 'vite'; import { resolve } from 'node:path'; import { gunzipSync } from 'node:zlib'; import { test, expect } from '@playwright/test'; +import type { + ISpan, + IExportTraceServiceRequest, +} from '@opentelemetry/otlp-transformer/build/src/trace/internal-types'; import type { Page, Route } from '@playwright/test'; import type browser from '../../client/src/lib/rum/browser'; @@ -36,7 +40,7 @@ test.beforeAll(async () => { sdkBundle = chunk.code; }); -async function fixture(page: Page, secret: string) { +async function fixture(page: Page, secret: string, base = '') { const traces: Buffer[] = []; const replays: Buffer[] = []; const capture = async (route: Route) => { @@ -58,13 +62,13 @@ async function fixture(page: Page, secret: string) { }; await page.route('http://collector.test/**', capture); await page.route('http://librechat.test/**', async (route) => { - if (new URL(route.request().url()).pathname.startsWith('/api/rum/')) { + if (new URL(route.request().url()).pathname.startsWith(`${base}/api/rum/`)) { await capture(route); return; } await route.fulfill({ contentType: 'text/html', - body: ` + body: ` Link Relative link @@ -74,7 +78,278 @@ async function fixture(page: Page, secret: string) { return { traces, replays }; } +function exportedSpans(traces: Buffer[]): ISpan[] { + return traces.flatMap((body) => { + const payload: IExportTraceServiceRequest = JSON.parse(body.toString()); + return (payload.resourceSpans ?? []).flatMap((resource) => + resource.scopeSpans.flatMap((scope) => scope.spans ?? []), + ); + }); +} + +function networkSpans(traces: Buffer[], url: string): ISpan[] { + return exportedSpans(traces).filter( + (span) => + span.attributes.some( + (attribute) => attribute.key === 'http.url' && attribute.value.stringValue === url, + ) && span.attributes.some((attribute) => attribute.key === 'http.method'), + ); +} + for (const mode of ['proxy', 'direct'] as const) { + for (const base of ['', '/librechat']) { + test(`${mode}: auth and credential bodies are removed while chat bodies remain at ${base || '/'}`, async ({ + page, + }) => { + const secret = `synthetic-body-secret-${Date.now()}`; + const { traces } = await fixture(page, secret, base); + const scenarios = [ + { + path: '/api/auth/login', + method: 'POST', + requestBody: JSON.stringify({ email: 'user@example.test', password: secret }), + responseBody: JSON.stringify({ token: secret }), + credential: true, + }, + { + path: '/api/auth/refresh', + method: 'POST', + requestBody: JSON.stringify({ refreshToken: secret }), + responseBody: JSON.stringify({ token: secret, user: { id: 'test-user' } }), + credential: true, + }, + { + path: '/api/keys', + method: 'PUT', + requestBody: JSON.stringify({ name: 'provider', value: secret }), + responseBody: JSON.stringify({ saved: true }), + credential: true, + }, + { + path: '/api/mcp/oauth/tokens/test-flow', + method: 'GET', + requestBody: undefined, + responseBody: JSON.stringify({ tokens: { access_token: secret, refresh_token: secret } }), + credential: true, + }, + { + path: '/api/admin/config/user/test-user', + method: 'PUT', + requestBody: JSON.stringify({ + overrides: { endpoints: { custom: [{ apiKey: secret }] } }, + }), + responseBody: JSON.stringify({ + overrides: { endpoints: { custom: [{ apiKey: secret }] } }, + }), + credential: true, + }, + { + path: '/api/agents/chat', + method: 'POST', + requestBody: JSON.stringify({ text: 'normal-chat-request' }), + responseBody: JSON.stringify({ text: 'normal-chat-response' }), + credential: false, + }, + ]; + const requests: { path: string; method: string; body: string | null }[] = []; + await page.route(`http://librechat.test${base}/api/**`, async (route) => { + const request = route.request(); + const path = new URL(request.url()).pathname; + const scenario = scenarios.find((entry) => `${base}${entry.path}` === path); + if (!scenario) { + await route.fallback(); + return; + } + requests.push({ path, method: request.method(), body: request.postData() }); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: scenario.responseBody, + headers: { 'x-request-id': 'safe-request-id' }, + }); + }); + await page.goto(`http://librechat.test${base}/c/new`); + await page.addScriptTag({ content: sdkBundle }); + const responses = await page.evaluate( + async ({ mode, base, scenarios }) => { + window.RumPrivacyHarness.default.init({ + apiKey: 'synthetic-test-key', + service: 'rum-privacy-test', + url: + mode === 'proxy' ? `http://librechat.test${base}/api/rum` : 'http://collector.test', + disableReplay: true, + advancedNetworkCapture: true, + consoleCapture: false, + maskAllInputs: true, + maskAllText: true, + }); + const received: string[] = []; + for (const scenario of scenarios) { + const url = `${base}${scenario.path}`; + const response = await fetch(url, { + method: scenario.method, + headers: { 'Content-Type': 'application/json' }, + body: scenario.requestBody, + }); + received.push(await response.text()); + received.push( + await new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open(scenario.method, url); + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.onload = () => resolve(xhr.responseText); + xhr.onerror = () => reject(new Error('Synthetic credential request failed')); + xhr.send(scenario.requestBody); + }), + ); + } + return received; + }, + { mode, base, scenarios }, + ); + + expect(responses).toEqual( + scenarios.flatMap((scenario) => [scenario.responseBody, scenario.responseBody]), + ); + expect(requests).toHaveLength(scenarios.length * 2); + for (const scenario of scenarios) { + const path = `${base}${scenario.path}`; + expect(requests.filter((request) => request.path === path)).toEqual([ + { path, method: scenario.method, body: scenario.requestBody ?? null }, + { path, method: scenario.method, body: scenario.requestBody ?? null }, + ]); + const url = `http://librechat.test${path}`; + await expect.poll(() => networkSpans(traces, url).length, { timeout: 20_000 }).toBe(2); + const spans = networkSpans(traces, url); + const components = spans.map( + (span) => span.attributes.find(({ key }) => key === 'component')?.value.stringValue, + ); + expect(components).toContain('fetch'); + expect(components.some((component) => component?.includes('xml-http-request'))).toBe(true); + for (const span of spans) { + const attributes = Object.fromEntries( + span.attributes.map(({ key, value }) => [key, value]), + ); + expect(attributes['http.method'].stringValue).toBe(scenario.method); + expect(Number(attributes['http.status_code'].intValue)).toBe(200); + const start = BigInt(String(span.startTimeUnixNano)); + expect(start).toBeGreaterThan(0n); + expect(BigInt(String(span.endTimeUnixNano))).toBeGreaterThanOrEqual(start); + if (scenario.credential) { + expect(Object.keys(attributes)).not.toContain('http.request.body'); + expect(Object.keys(attributes)).not.toContain('http.response.body'); + expect(JSON.stringify(span)).not.toContain(secret); + } else { + expect(attributes['http.request.body'].stringValue).toBe(scenario.requestBody); + // Fetch reads response bodies asynchronously; some SDK spans end before that read completes. + if (attributes.component.stringValue !== 'fetch' || attributes['http.response.body']) { + expect(attributes['http.response.body'].stringValue).toBe(scenario.responseBody); + } + } + } + } + expect(Buffer.concat(traces).toString()).not.toContain(secret); + }); + } + + for (const advancedNetworkCapture of [true, false]) { + test(`${mode}: actual SDK keeps credentials out of fetch and XHR exports with advanced capture ${advancedNetworkCapture ? 'on' : 'off'}`, async ({ + page, + }) => { + const secret = `synthetic-header-secret-${Date.now()}`; + const { traces } = await fixture(page, secret); + const requests: Record[] = []; + await page.route('http://librechat.test/api/header-privacy/**', async (route) => { + requests.push(await route.request().allHeaders()); + await route.fulfill({ + contentType: 'application/json', + body: '{"result":"safe-response-body"}', + headers: { + authorization: `Bearer response-${secret}`, + 'x-api-key': `response-api-${secret}`, + 'x-auth-token': `response-token-${secret}`, + 'x-signature': `response-signature-${secret}`, + 'x-request-id': 'safe-response-id', + }, + }); + }); + await page.goto('http://librechat.test/c/new'); + await page.addScriptTag({ content: sdkBundle }); + await page.evaluate( + async ({ mode, secret, advancedNetworkCapture }) => { + window.RumPrivacyHarness.default.init({ + apiKey: 'synthetic-test-key', + service: 'rum-privacy-test', + url: mode === 'proxy' ? 'http://librechat.test/api/rum' : 'http://collector.test', + disableReplay: true, + advancedNetworkCapture, + consoleCapture: false, + maskAllInputs: true, + maskAllText: true, + }); + const headers = { + Authorization: `Bearer request-${secret}`, + 'X-API-Key': `request-api-${secret}`, + 'X-Auth-Token': `request-token-${secret}`, + 'X-Signature': `request-signature-${secret}`, + 'X-Request-ID': 'safe-request-id', + }; + await fetch('/api/header-privacy/fetch', { + method: 'POST', + headers, + body: 'safe-request-body-fetch', + }); + await new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open('POST', '/api/header-privacy/xhr'); + for (const [name, value] of Object.entries(headers)) xhr.setRequestHeader(name, value); + xhr.onload = () => resolve(); + xhr.onerror = () => reject(new Error('Synthetic XHR request failed')); + xhr.send('safe-request-body-xhr'); + }); + }, + { mode, secret, advancedNetworkCapture }, + ); + + expect(requests).toHaveLength(2); + for (const headers of requests) { + expect(headers.authorization).toBe(`Bearer request-${secret}`); + expect(headers['x-api-key']).toBe(`request-api-${secret}`); + expect(headers['x-auth-token']).toBe(`request-token-${secret}`); + expect(headers['x-signature']).toBe(`request-signature-${secret}`); + } + for (const transport of ['fetch', 'xhr']) { + await expect + .poll( + () => + traces.some((body) => + body.includes(`http://librechat.test/api/header-privacy/${transport}`), + ), + { timeout: 20_000 }, + ) + .toBe(true); + } + const exported = Buffer.concat(traces).toString(); + expect(exported).not.toContain(secret); + for (const direction of ['request', 'response']) { + for (const header of ['authorization', 'x_api_key', 'x_auth_token', 'x_signature']) { + expect(exported).not.toContain(`http.${direction}.header.${header}`); + } + expect(exported.includes(`http.${direction}.header.x_request_id`)).toBe( + advancedNetworkCapture, + ); + expect(exported.includes(`safe-${direction}-id`)).toBe(advancedNetworkCapture); + } + for (const body of [ + 'safe-request-body-fetch', + 'safe-request-body-xhr', + 'safe-response-body', + ]) { + expect(exported.includes(body)).toBe(advancedNetworkCapture); + } + }); + } + test(`${mode}: actual SDK exports scrub metadata, DOM URLs, mutations and auth transitions`, async ({ page, }) => {