Skip to content

Commit fece237

Browse files
oiokiclaude
andcommitted
feat(browser): Load the report dialog script through a Trusted Types policy
showReportDialog assigned a plain string to script.src, which pages that enforce Trusted Types reject. Mint the URL through a sentry-report-dialog policy created on first use, so those pages only need to allowlist the name. The policy only accepts the error-page embed endpoint. Without Trusted Types, or if the policy can't be created, the raw URL is used as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T35on2nNWQAfBWfSA9eBp1
1 parent 2a58c97 commit fece237

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎packages/browser/src/report-dialog.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,54 @@ import { debug, getClient, getCurrentScope, getReportDialogEndpoint, lastEventId
33
import { DEBUG_BUILD } from './debug-build';
44
import { WINDOW } from './helpers';
55

6+
const REPORT_DIALOG_POLICY_NAME = 'sentry-report-dialog';
7+
8+
interface ScriptURLPolicy {
9+
createScriptURL(input: string): unknown;
10+
}
11+
12+
interface TrustedTypesWindow {
13+
trustedTypes?: {
14+
createPolicy(name: string, rules: { createScriptURL(input: string): string }): ScriptURLPolicy;
15+
};
16+
}
17+
18+
// `undefined` until the first attempt, `null` if Trusted Types is unavailable or the
19+
// policy could not be created.
20+
let reportDialogPolicy: ScriptURLPolicy | null | undefined;
21+
22+
function getReportDialogPolicy(): ScriptURLPolicy | null {
23+
if (reportDialogPolicy !== undefined) {
24+
return reportDialogPolicy;
25+
}
26+
27+
reportDialogPolicy = null;
28+
const trustedTypes = (WINDOW as TrustedTypesWindow).trustedTypes;
29+
if (!trustedTypes) {
30+
return reportDialogPolicy;
31+
}
32+
33+
try {
34+
reportDialogPolicy = trustedTypes.createPolicy(REPORT_DIALOG_POLICY_NAME, {
35+
createScriptURL: (input: string) => {
36+
const url = new URL(input);
37+
if (!/^https?:$/.test(url.protocol) || !url.pathname.endsWith('/api/embed/error-page/')) {
38+
throw new TypeError(`Refusing to load ${input} as the report dialog script`);
39+
}
40+
return input;
41+
},
42+
});
43+
} catch (error) {
44+
DEBUG_BUILD &&
45+
debug.warn(
46+
`[showReportDialog] Could not create the "${REPORT_DIALOG_POLICY_NAME}" Trusted Types policy. Add it to your \`trusted-types\` CSP directive.`,
47+
error,
48+
);
49+
}
50+
51+
return reportDialogPolicy;
52+
}
53+
654
/**
755
* Present the user with a report dialog.
856
*
@@ -39,7 +87,9 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
3987
const script = WINDOW.document.createElement('script');
4088
script.async = true;
4189
script.crossOrigin = 'anonymous';
42-
script.src = getReportDialogEndpoint(dsn, mergedOptions);
90+
const endpoint = getReportDialogEndpoint(dsn, mergedOptions);
91+
const policy = getReportDialogPolicy();
92+
script.src = (policy ? policy.createScriptURL(endpoint) : endpoint) as string;
4393

4494
const { onLoad, onClose } = mergedOptions;
4595

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6+
7+
const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291';
8+
9+
type Rules = { createScriptURL(input: string): string };
10+
11+
function fakeTrustedTypes() {
12+
return {
13+
createPolicy: vi.fn((_name: string, rules: Rules) => ({
14+
createScriptURL: (input: string) => rules.createScriptURL(input),
15+
})),
16+
};
17+
}
18+
19+
async function loadSdk() {
20+
vi.resetModules();
21+
const sdk = await import('../src');
22+
sdk.init({ dsn });
23+
return sdk;
24+
}
25+
26+
function reportDialogScript(): HTMLScriptElement | null {
27+
return document.head.querySelector('script[src*="/api/embed/error-page/"]');
28+
}
29+
30+
describe('showReportDialog with Trusted Types', () => {
31+
beforeEach(() => {
32+
document.head.innerHTML = '';
33+
});
34+
35+
afterEach(() => {
36+
delete (window as { trustedTypes?: unknown }).trustedTypes;
37+
});
38+
39+
it('loads the dialog script through the sentry-report-dialog policy', async () => {
40+
const trustedTypes = fakeTrustedTypes();
41+
(window as { trustedTypes?: unknown }).trustedTypes = trustedTypes;
42+
43+
const { showReportDialog } = await loadSdk();
44+
showReportDialog({ eventId: 'abc' });
45+
46+
expect(trustedTypes.createPolicy).toHaveBeenCalledWith('sentry-report-dialog', expect.anything());
47+
expect(reportDialogScript()?.src).toContain('https://sentry.io/api/embed/error-page/');
48+
});
49+
50+
it('creates the policy only once', async () => {
51+
const trustedTypes = fakeTrustedTypes();
52+
(window as { trustedTypes?: unknown }).trustedTypes = trustedTypes;
53+
54+
const { showReportDialog } = await loadSdk();
55+
showReportDialog({ eventId: 'abc' });
56+
showReportDialog({ eventId: 'def' });
57+
58+
expect(trustedTypes.createPolicy).toHaveBeenCalledTimes(1);
59+
});
60+
61+
it.each(['https://example.com/evil.js', 'javascript:alert(1)'])(
62+
'refuses to mint %s as the dialog script URL',
63+
async url => {
64+
const trustedTypes = fakeTrustedTypes();
65+
(window as { trustedTypes?: unknown }).trustedTypes = trustedTypes;
66+
67+
const { showReportDialog } = await loadSdk();
68+
showReportDialog({ eventId: 'abc' });
69+
70+
const rules = trustedTypes.createPolicy.mock.calls[0]![1];
71+
expect(() => rules.createScriptURL(url)).toThrow(TypeError);
72+
},
73+
);
74+
75+
it('falls back to the raw URL when the policy cannot be created', async () => {
76+
(window as { trustedTypes?: unknown }).trustedTypes = {
77+
createPolicy: vi.fn(() => {
78+
throw new Error('Policy "sentry-report-dialog" disallowed');
79+
}),
80+
};
81+
82+
const { showReportDialog } = await loadSdk();
83+
84+
expect(() => showReportDialog({ eventId: 'abc' })).not.toThrow();
85+
expect(reportDialogScript()?.src).toContain('https://sentry.io/api/embed/error-page/');
86+
});
87+
88+
it('uses the raw URL when Trusted Types is unavailable', async () => {
89+
const { showReportDialog } = await loadSdk();
90+
showReportDialog({ eventId: 'abc' });
91+
92+
expect(reportDialogScript()?.src).toContain('https://sentry.io/api/embed/error-page/');
93+
});
94+
});

0 commit comments

Comments
 (0)