Skip to content

Commit

Permalink
Weird corner-case URLs that can escape/break our loader script.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshim committed Oct 17, 2024
1 parent 0226a13 commit e7e43d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/angular/build/src/utils/index-file/auto-csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function createLoaderScript(srcList: SrcScriptTag[], enableTrustedTypes = false)
const srcListFormatted = srcList
.map(
(s) =>
`['${encodeURI(s.src)}', ${s.type ? "'" + encodeURI(s.type) + "'" : undefined}, ${s.async ? 'true' : 'false'}, ${s.defer ? 'true' : 'false'}]`,
`['${encodeURI(s.src).replaceAll("'", "\\'")}', ${s.type ? "'" + encodeURI(s.type) + "'" : undefined}, ${s.async ? 'true' : 'false'}, ${s.defer ? 'true' : 'false'}]`,

Check failure on line 283 in packages/angular/build/src/utils/index-file/auto-csp.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 174. Maximum allowed is 140
)
.join();
return enableTrustedTypes

Check failure on line 286 in packages/angular/build/src/utils/index-file/auto-csp.ts

View workflow job for this annotation

GitHub Actions / lint

Expected blank line before this statement
Expand Down
29 changes: 29 additions & 0 deletions packages/angular/build/src/utils/index-file/auto-csp_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ describe('auto-csp', () => {
expect(Array.from(result.matchAll(/\<script\>/g)).length).toEqual(1);
});

it('should rewrite source scripts with weird URLs', async () => {
const result = await autoCsp(`
<html>
<head>
</head>
<body>
<script src="/foo&amp;bar"></script>
<script src="/one'two\\'three\\\\'four\\\\\\'five"></script>
<script src="/one&two&amp;three&amp;amp;four"></script>
<script src="./</script>"></script>
<div>Some text </div>
</body>
</html>
`);

const csps = getCsps(result);
expect(csps.length).toBe(1);
expect(csps[0]).toMatch(ONE_HASH_CSP);
// &amp; encodes correctly
expect(result).toContain(`'/foo&bar'`);
// Impossible to escape a string and create invalid loader JS with a '
// (Quotes and backslashes work)
expect(result).toContain(`'/one\\'two%5C\\'three%5C%5C\\'four%5C%5C%5C\\'five'`);
// HTML entities work
expect(result).toContain(`'/one&two&three&amp;four'`);
// Cannot escape JS context to HTML
expect(result).toContain(`'./%3C/script%3E'`);
});

it('should rewrite all script tags', async () => {
const result = await autoCsp(`
<html>
Expand Down

0 comments on commit e7e43d6

Please sign in to comment.