From 561c023708bc0cb04613f89da821dc3c55245f01 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Thu, 18 Apr 2024 17:38:43 -0700 Subject: [PATCH] [Fizz] escape '); /** - * This escaping function is designed to work with bootstrapScriptContent and importMap only. - * because we know we are escaping the entire script. We can avoid for instance + * This escaping function is designed to work with with inline scripts where the entire + * contents are escaped. Because we know we are escaping the entire script we can avoid for instance * escaping html comment string sequences that are valid javascript as well because * if there are no sebsequent '); * While untrusted script content should be made safe before using this api it will * ensure that the script cannot be early terminated or never terminated state */ -function escapeBootstrapAndImportMapScriptContent(scriptText: string) { +function escapeEntireInlineScriptContent(scriptText: string) { if (__DEV__) { checkHtmlStringCoercion(scriptText); } @@ -372,9 +372,7 @@ export function createRenderState( if (bootstrapScriptContent !== undefined) { bootstrapChunks.push( inlineScriptWithNonce, - stringToChunk( - escapeBootstrapAndImportMapScriptContent(bootstrapScriptContent), - ), + stringToChunk(escapeEntireInlineScriptContent(bootstrapScriptContent)), endInlineScript, ); } @@ -411,9 +409,7 @@ export function createRenderState( const map = importMap; importMapChunks.push(importMapScriptStart); importMapChunks.push( - stringToChunk( - escapeBootstrapAndImportMapScriptContent(JSON.stringify(map)), - ), + stringToChunk(escapeEntireInlineScriptContent(JSON.stringify(map))), ); importMapChunks.push(importMapScriptEnd); } @@ -3266,7 +3262,7 @@ function pushScriptImpl( pushInnerHTML(target, innerHTML, children); if (typeof children === 'string') { - target.push(stringToChunk(encodeHTMLTextNode(children))); + target.push(stringToChunk(escapeEntireInlineScriptContent(children))); } target.push(endChunkForTag('script')); return null; diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js index c91165a1ea77c..f5abbb3280d9d 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js @@ -4180,79 +4180,100 @@ describe('ReactDOMFizzServer', () => { ]); }); - describe('bootstrapScriptContent and importMap escaping', () => { - it('the "S" in " { - window.__test_outlet = ''; - const stringWithScriptsInIt = - 'prescription pre + window.__test_outlet = 'safe'; + --> + `}, + ); + pipe(writable); }); - pipe(writable); + expect(window.__test_outlet).toBe('safe'); }); - expect(window.__test_outlet_key).toBe(''); - expect(window.__test_outlet_value).toBe(''); }); });