@@ -15,13 +15,13 @@ const getCsps = (html: string) => {
1515 ) . map ( ( m ) => m [ 1 ] ) ; // Only capture group.
1616} ;
1717
18- const ONE_HASH_CSP =
18+ const CSP_SINGLE_HASH_REGEX =
1919 / s c r i p t - s r c ' s t r i c t - d y n a m i c ' ' s h a 2 5 6 - [ ^ ' ] + ' h t t p s : ' u n s a f e - i n l i n e ' ; o b j e c t - s r c ' n o n e ' ; b a s e - u r i ' s e l f ' ; / ;
2020
21- const TWO_HASH_CSP =
21+ const CSP_TWO_HASHES_REGEX =
2222 / s c r i p t - s r c ' s t r i c t - d y n a m i c ' (?: ' s h a 2 5 6 - [ ^ ' ] + ' ) { 2 } h t t p s : ' u n s a f e - i n l i n e ' ; o b j e c t - s r c ' n o n e ' ; b a s e - u r i ' s e l f ' ; / ;
2323
24- const FOUR_HASH_CSP =
24+ const CSP_FOUR_HASHES_REGEX =
2525 / s c r i p t - s r c ' s t r i c t - d y n a m i c ' (?: ' s h a 2 5 6 - [ ^ ' ] + ' ) { 4 } h t t p s : ' u n s a f e - i n l i n e ' ; o b j e c t - s r c ' n o n e ' ; b a s e - u r i ' s e l f ' ; / ;
2626
2727describe ( 'auto-csp' , ( ) => {
@@ -38,8 +38,8 @@ describe('auto-csp', () => {
3838 ` ) ;
3939
4040 const csps = getCsps ( result ) ;
41- expect ( csps . length ) . toBe ( 1 ) ;
42- expect ( csps [ 0 ] ) . toMatch ( ONE_HASH_CSP ) ;
41+ expect ( csps ) . toHaveSize ( 1 ) ;
42+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
4343 expect ( csps [ 0 ] ) . toContain ( hashTextContent ( "console.log('foo');" ) ) ;
4444 } ) ;
4545
@@ -56,8 +56,8 @@ describe('auto-csp', () => {
5656 ` ) ;
5757
5858 const csps = getCsps ( result ) ;
59- expect ( csps . length ) . toBe ( 1 ) ;
60- expect ( csps [ 0 ] ) . toMatch ( ONE_HASH_CSP ) ;
59+ expect ( csps ) . toHaveSize ( 1 ) ;
60+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
6161 expect ( result ) . toContain ( `var scripts = [['./main.js', '', false, false]];` ) ;
6262 } ) ;
6363
@@ -74,8 +74,8 @@ describe('auto-csp', () => {
7474 ` ) ;
7575
7676 const csps = getCsps ( result ) ;
77- expect ( csps . length ) . toBe ( 1 ) ;
78- expect ( csps [ 0 ] ) . toMatch ( ONE_HASH_CSP ) ;
77+ expect ( csps ) . toHaveSize ( 1 ) ;
78+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
7979 // Our loader script appears after the HTML text content.
8080 expect ( result ) . toMatch (
8181 / S o m e t e x t < \/ d i v > \s * < s c r i p t > \s * v a r s c r i p t s = \[ \[ ' .\/ m a i n .j s ' , ' ' , f a l s e , f a l s e \] \] ; / ,
@@ -99,8 +99,8 @@ describe('auto-csp', () => {
9999 ` ) ;
100100
101101 const csps = getCsps ( result ) ;
102- expect ( csps . length ) . toBe ( 1 ) ;
103- expect ( csps [ 0 ] ) . toMatch ( TWO_HASH_CSP ) ;
102+ expect ( csps ) . toHaveSize ( 1 ) ;
103+ expect ( csps [ 0 ] ) . toMatch ( CSP_TWO_HASHES_REGEX ) ;
104104 expect ( result ) . toContain (
105105 // eslint-disable-next-line max-len
106106 `var scripts = [['./main1.js', '', false, false],['./main2.js', '', true, false],['./main3.js', 'module', true, true]];` ,
@@ -127,8 +127,8 @@ describe('auto-csp', () => {
127127 ` ) ;
128128
129129 const csps = getCsps ( result ) ;
130- expect ( csps . length ) . toBe ( 1 ) ;
131- expect ( csps [ 0 ] ) . toMatch ( ONE_HASH_CSP ) ;
130+ expect ( csps ) . toHaveSize ( 1 ) ;
131+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
132132 // & encodes correctly
133133 expect ( result ) . toContain ( `'/foo&bar'` ) ;
134134 // Impossible to escape a string and create invalid loader JS with a '
@@ -158,9 +158,9 @@ describe('auto-csp', () => {
158158 ` ) ;
159159
160160 const csps = getCsps ( result ) ;
161- expect ( csps . length ) . toBe ( 1 ) ;
161+ expect ( csps ) . toHaveSize ( 1 ) ;
162162 // Exactly four hashes for the four scripts that remain (inline, loader, inline, loader).
163- expect ( csps [ 0 ] ) . toMatch ( FOUR_HASH_CSP ) ;
163+ expect ( csps [ 0 ] ) . toMatch ( CSP_FOUR_HASHES_REGEX ) ;
164164 expect ( csps [ 0 ] ) . toContain ( hashTextContent ( "console.log('foo');" ) ) ;
165165 expect ( csps [ 0 ] ) . toContain ( hashTextContent ( "console.log('bar');" ) ) ;
166166 // Loader script for main.js and main2.js appear after 'foo' and before 'bar'.
@@ -190,8 +190,8 @@ describe('auto-csp', () => {
190190 ` ) ;
191191
192192 const csps = getCsps ( result ) ;
193- expect ( csps . length ) . toBe ( 1 ) ;
194- expect ( csps [ 0 ] ) . toMatch ( ONE_HASH_CSP ) ;
193+ expect ( csps ) . toHaveSize ( 1 ) ;
194+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
195195
196196 expect ( result ) . toContain (
197197 // eslint-disable-next-line max-len
@@ -202,4 +202,23 @@ describe('auto-csp', () => {
202202 // Only one loader script is created.
203203 expect ( Array . from ( result . matchAll ( / < s c r i p t > / g) ) . length ) . toEqual ( 1 ) ;
204204 } ) ;
205+
206+ it ( 'should rewrite a single inline script with CRLF' , async ( ) => {
207+ const result = await autoCsp ( `
208+ <html>
209+ <head>
210+ </head>
211+ <body>
212+ <script>\r\nconsole.log('foo');\r\n</script>
213+ <div>Some text </div>
214+ </body>
215+ </html>\r\n
216+ ` ) ;
217+
218+ const csps = getCsps ( result ) ;
219+ expect ( result ) . not . toContain ( `\r\n` ) ;
220+ expect ( csps ) . toHaveSize ( 1 ) ;
221+ expect ( csps [ 0 ] ) . toMatch ( CSP_SINGLE_HASH_REGEX ) ;
222+ expect ( csps [ 0 ] ) . toContain ( hashTextContent ( `\r\nconsole.log('foo');\r\n` ) ) ;
223+ } ) ;
205224} ) ;
0 commit comments