Skip to content

[Backport 21.2.X] fix(@angular/build): escape prerender redirect URLs - #34508

Open
SkyZeroZx wants to merge 1 commit into
angular:21.2.xfrom
SkyZeroZx:fix/escape-prerender-redirect-urls-21.2.x
Open

SkyZeroZx wants to merge 1 commit into
angular:21.2.xfrom
SkyZeroZx:fix/escape-prerender-redirect-urls-21.2.x

Conversation

@SkyZeroZx

Copy link
Copy Markdown
Contributor

Backport of #33248

Escape redirect targets before embedding them in the meta refresh tag and the fallback link of
generated static redirect pages.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces HTML escaping for URLs interpolated into static redirect pages to prevent potential injection vulnerabilities. It adds an escapeHtml utility and updates generateRedirectStaticPage to escape characters like ampersands, angle brackets, and quotes. Unit and end-to-end tests are also added to verify the escaping behavior. Feedback suggests adding a nullish coalescing fallback in escapeHtml to prevent returning undefined if the regex is modified in the future without updating the character map.

Comment on lines +42 to +44
function escapeHtml(text: string): string {
return text.replace(/[&<>"']/g, (character) => HTML_ESCAPE_CHARACTER_MAP[character]);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure robust defensive programming and prevent potential runtime issues, consider adding a nullish coalescing fallback (?? character) when accessing HTML_ESCAPE_CHARACTER_MAP. If the regular expression is ever modified in the future to match additional characters without updating the map, or if TypeScript's noUncheckedIndexedAccess is enabled, this prevents returning undefined (which would be stringified as 'undefined' in the resulting HTML).

function escapeHtml(text: string): string {
  return text.replace(/[&<>"']/g, (character) => HTML_ESCAPE_CHARACTER_MAP[character] ?? character);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant