-
Notifications
You must be signed in to change notification settings - Fork 11.8k
fix(@angular/build): escape prerender redirect URLs #33248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SkyZeroZx
wants to merge
2
commits into
angular:main
Choose a base branch
from
SkyZeroZx:fix-build
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,184
−32
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/angular/build/src/utils/server-rendering/manifest_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { BuildOutputFileType, createOutputFile } from '../../tools/esbuild/bundler-files'; | ||
| import { initializeHash } from '../hash'; | ||
| import { generateAngularServerAppManifest } from './manifest'; | ||
|
|
||
| /** | ||
| * Evaluates a generated manifest, which both asserts that it is syntactically valid JavaScript and | ||
| * gives access to the values it declares. The dynamic imports it contains are never invoked. | ||
| */ | ||
| function evaluateManifest(manifestContent: string): Record<string, unknown> { | ||
| return new Function(manifestContent.replace('export default', 'return'))() as Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| } | ||
|
|
||
| function generateManifest( | ||
| htmlOutputFiles: Record<string, string>, | ||
| baseHref = '/', | ||
| ): ReturnType<typeof generateAngularServerAppManifest> { | ||
| const additionalHtmlOutputFiles = new Map( | ||
| Object.entries(htmlOutputFiles).map(([path, content]) => [ | ||
| path, | ||
| createOutputFile(path, content, BuildOutputFileType.Browser), | ||
| ]), | ||
| ); | ||
|
|
||
| return generateAngularServerAppManifest( | ||
| additionalHtmlOutputFiles, | ||
| [], | ||
| false, | ||
| undefined, | ||
| undefined, | ||
| baseHref, | ||
| new Set(), | ||
| { inputs: {}, outputs: {} }, | ||
| undefined, | ||
| ); | ||
| } | ||
|
|
||
| describe('generateAngularServerAppManifest', () => { | ||
| beforeAll(async () => { | ||
| await initializeHash(); | ||
| }); | ||
|
|
||
| it('serializes asset paths which contain JavaScript string delimiters', () => { | ||
| const assetPath = "catalog/customer's-choice/index.html"; | ||
| const { manifestContent } = generateManifest({ [assetPath]: '<main>Featured</main>' }); | ||
|
|
||
| const assets = evaluateManifest(manifestContent)['assets'] as Record<string, unknown>; | ||
| expect(Object.keys(assets)).toEqual([assetPath]); | ||
| }); | ||
|
|
||
| it('serializes a base href which contains JavaScript string delimiters', () => { | ||
| const { manifestContent } = generateManifest({ 'index.html': '<main></main>' }, "/o'brien/"); | ||
|
|
||
| expect(evaluateManifest(manifestContent)['baseHref']).toBe("/o'brien/"); | ||
| }); | ||
|
|
||
| it('generates chunk names which are usable as a file name and as a module specifier', () => { | ||
| const assetPath = "catalog/customer's#featured?ratio=50%/index.html"; | ||
| const { serverAssetsChunks } = generateManifest({ [assetPath]: '<main>Featured</main>' }); | ||
|
|
||
| expect(serverAssetsChunks).toHaveSize(1); | ||
| expect(serverAssetsChunks[0].path).toMatch(/^assets-chunks\/[a-zA-Z0-9_-]+\.mjs$/); | ||
| }); | ||
|
|
||
| it('generates a dynamic import which resolves back to the emitted chunk', () => { | ||
| // The in-memory ESM loader used while prerendering resolves the specifier as a URL and looks the | ||
| // result up by output file path, so the two have to match exactly. | ||
| const assetPath = "catalog/customer's#featured?ratio=50%/index.html"; | ||
| const { manifestContent, serverAssetsChunks } = generateManifest({ | ||
| [assetPath]: '<main>Featured</main>', | ||
| }); | ||
|
|
||
| const assets = evaluateManifest(manifestContent)['assets'] as Record< | ||
| string, | ||
| { text: () => Promise<string> } | ||
| >; | ||
| const specifier = /import\("(.+?)"\)/.exec(assets[assetPath].text.toString())?.[1]; | ||
|
|
||
| const root = 'file:///virtual/root/'; | ||
| expect(specifier).toBeDefined(); | ||
| expect(new URL(specifier as string, root).href.slice(root.length)).toBe( | ||
| serverAssetsChunks[0].path, | ||
| ); | ||
| }); | ||
|
|
||
| it('generates a distinct chunk for asset paths which map to the same name', () => { | ||
| const { serverAssetsChunks } = generateManifest({ | ||
| 'foo/bar/index.html': '<main>nested</main>', | ||
| 'foo_bar/index.html': '<main>flat</main>', | ||
| }); | ||
|
|
||
| expect(serverAssetsChunks).toHaveSize(2); | ||
| expect(serverAssetsChunks[0].path).not.toBe(serverAssetsChunks[1].path); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/angular/build/src/utils/server-rendering/utils_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { generateRedirectStaticPage } from './utils'; | ||
|
|
||
| describe('generateRedirectStaticPage', () => { | ||
| it('escapes the ampersands of a redirect target', () => { | ||
| const page = generateRedirectStaticPage('https://example.com/docs?from=ssg&next=/ssg'); | ||
|
|
||
| expect(page).toContain( | ||
| '<meta http-equiv="refresh" content="0; url=https://example.com/docs?from=ssg&next=/ssg">', | ||
| ); | ||
| expect(page).toContain( | ||
| '<a href="https://example.com/docs?from=ssg&next=/ssg">' + | ||
| 'https://example.com/docs?from=ssg&next=/ssg</a>', | ||
| ); | ||
| }); | ||
|
|
||
| it('escapes characters which would break out of the attribute or the tag', () => { | ||
| const page = generateRedirectStaticPage(`/"><script>alert('1')</script>`); | ||
|
|
||
| expect(page).not.toContain('<script>'); | ||
| expect(page).toContain( | ||
| '<meta http-equiv="refresh" content="0; url=' + | ||
| '/"><script>alert('1')</script>">', | ||
| ); | ||
| expect(page).toContain('<a href="/"><script>alert('1')</script>">'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon closer inspection (with the help of an agent), this could fail if used on Windows, so I added this helper to avoid these issues.