|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +let React; |
| 13 | +let ReactDOMFizzServer; |
| 14 | +let Stream; |
| 15 | + |
| 16 | +function getTestWritable() { |
| 17 | + const writable = new Stream.PassThrough(); |
| 18 | + writable.setEncoding('utf8'); |
| 19 | + const output = {result: '', error: undefined}; |
| 20 | + writable.on('data', chunk => { |
| 21 | + output.result += chunk; |
| 22 | + }); |
| 23 | + writable.on('error', error => { |
| 24 | + output.error = error; |
| 25 | + }); |
| 26 | + const completed = new Promise(resolve => { |
| 27 | + writable.on('finish', () => { |
| 28 | + resolve(); |
| 29 | + }); |
| 30 | + writable.on('error', () => { |
| 31 | + resolve(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + return {writable, completed, output}; |
| 35 | +} |
| 36 | + |
| 37 | +describe('escapeScriptForBrowser', () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.resetModules(); |
| 40 | + React = require('react'); |
| 41 | + ReactDOMFizzServer = require('react-dom/server'); |
| 42 | + Stream = require('stream'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('"<[Ss]cript" strings are replaced with unicode escaped lowercase s or S depending on case', () => { |
| 46 | + const {writable, output} = getTestWritable(); |
| 47 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<div />, { |
| 48 | + bootstrapScriptContent: |
| 49 | + '"prescription pre<scription preScription pre<Scription"', |
| 50 | + }); |
| 51 | + pipe(writable); |
| 52 | + jest.runAllTimers(); |
| 53 | + expect(output.result).toMatch( |
| 54 | + '<div></div><script>"prescription pre<\\u0073cription preScription pre<\\u0053cription"</script>', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('"</[Ss]cript" strings are replaced with encoded lowercase s or S depending on case', () => { |
| 59 | + const {writable, output} = getTestWritable(); |
| 60 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<div />, { |
| 61 | + bootstrapScriptContent: |
| 62 | + '"prescription pre</scription preScription pre</Scription"', |
| 63 | + }); |
| 64 | + pipe(writable); |
| 65 | + jest.runAllTimers(); |
| 66 | + expect(output.result).toMatch( |
| 67 | + '<div></div><script>"prescription pre</\\u0073cription preScription pre</\\u0053cription"</script>', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('"[Ss]cript", "/[Ss]cript", "<[Ss]crip", "</[Ss]crip" strings are not escaped', () => { |
| 72 | + const {writable, output} = getTestWritable(); |
| 73 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<div />, { |
| 74 | + bootstrapScriptContent: |
| 75 | + '"Script script /Script /script <Scrip <scrip </Scrip </scrip"', |
| 76 | + }); |
| 77 | + pipe(writable); |
| 78 | + jest.runAllTimers(); |
| 79 | + expect(output.result).toMatch( |
| 80 | + '<div></div><script>"Script script /Script /script <Scrip <scrip </Scrip </scrip"</script>', |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('matches case insensitively', () => { |
| 85 | + const {writable, output} = getTestWritable(); |
| 86 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<div />, { |
| 87 | + bootstrapScriptContent: '"<sCrIpT <ScripT </scrIPT </SCRIpT"', |
| 88 | + }); |
| 89 | + pipe(writable); |
| 90 | + jest.runAllTimers(); |
| 91 | + expect(output.result).toMatch( |
| 92 | + '<div></div><script>"<\\u0073CrIpT <\\u0053cripT </\\u0073crIPT </\\u0053CRIpT"</script>', |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('does not escape <, >, &, \\u2028, or \\u2029 characters', () => { |
| 97 | + const {writable, output} = getTestWritable(); |
| 98 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<div />, { |
| 99 | + bootstrapScriptContent: '"<, >, &, \u2028, or \u2029"', |
| 100 | + }); |
| 101 | + pipe(writable); |
| 102 | + jest.runAllTimers(); |
| 103 | + expect(output.result).toMatch( |
| 104 | + '<div></div><script>"<, >, &, \u2028, or \u2029"</script>', |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments