Skip to content

Commit 38d6287

Browse files
committed
Dedupe warning and add the unsafe URL to the warning message
1 parent 8d9b293 commit 38d6287

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/react-dom/src/__tests__/ReactDOMServerIntegrationUntrustedURL-test.internal.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ function runTests(itRenders, itRejectsRendering, expectToReject) {
2727
});
2828

2929
itRejectsRendering('a javascript protocol href', async render => {
30-
const e = await render(<a href="javascript:notfine">p0wned</a>, 1);
31-
expect(e.href).toBe('javascript:notfine');
30+
// Only the first one warns. The second warning is deduped.
31+
const e = await render(
32+
<div>
33+
<a href="javascript:notfine">p0wned</a>
34+
<a href="javascript:notfineagain">p0wned again</a>
35+
</div>,
36+
1,
37+
);
38+
expect(e.firstChild.href).toBe('javascript:notfine');
39+
expect(e.lastChild.href).toBe('javascript:notfineagain');
3240
});
3341

3442
itRejectsRendering(
@@ -162,7 +170,7 @@ describe('ReactDOMServerIntegration - Untrusted URLs', () => {
162170
expect(fn).toWarnDev(
163171
'Warning: A future version of React will block javascript: URLs as a security precaution. ' +
164172
'Use event handlers instead if you can. If you need to generate unsafe HTML try using ' +
165-
'dangerouslySetInnerHTML instead.\n' +
173+
'dangerouslySetInnerHTML instead. React was passed "javascript:notfine".\n' +
166174
' in a (at **)',
167175
),
168176
);

packages/react-dom/src/shared/sanitizeURL.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ if (__DEV__) {
2929
/* eslint-disable max-len */
3030
const isJavaScriptProtocol = /^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*\:/i;
3131

32+
let didWarn = false;
33+
3234
function sanitizeURL(url: string) {
3335
if (disableJavaScriptURLs) {
3436
invariant(
3537
!isJavaScriptProtocol.test(url),
3638
'React has blocked a javascript: URL as a security precaution.%s',
3739
__DEV__ ? ReactDebugCurrentFrame.getStackAddendum() : '',
3840
);
39-
} else if (__DEV__) {
41+
} else if (__DEV__ && !didWarn && isJavaScriptProtocol.test(url)) {
42+
didWarn = true;
4043
warning(
41-
!isJavaScriptProtocol.test(url),
44+
false,
4245
'A future version of React will block javascript: URLs as a security precaution. ' +
4346
'Use event handlers instead if you can. If you need to generate unsafe HTML try ' +
44-
'using dangerouslySetInnerHTML instead.',
47+
'using dangerouslySetInnerHTML instead. React was passed %s.',
48+
JSON.stringify(url),
4549
);
4650
}
4751
}

0 commit comments

Comments
 (0)