Skip to content

fix(@angular/ssr): prevent open redirect via protocol-relative pathname - #34208

Closed
SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/open-redirect
Closed

SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/open-redirect

Conversation

@SkyZeroZx

Copy link
Copy Markdown
Contributor

The Location header for a server-side navigation was built by joining the PlatformLocation pathname with the search and hash. A pathname may start with //, which as a Location value is a protocol-relative URL to another origin. The router serializes /.;/(//example.com) to /.//example.com, dot-segment removal leaves //example.com, and a single unauthenticated GET returned 302 Location: //example.com. Any route with a parameter in the first segment and a wildcard child is affected, with no redirect logic in the application.

Collapse the leading slashes of the pathname before using it as a location, and normalize the same way in createRedirectResponse(), which also prefixes a slash so the value cannot resolve anywhere but the current origin.

The collapse consumes tab, line feed and carriage return too: the WHATWG URL parser removes them before parsing, so a client reads /\t/example.com as //example.com. Tab is a legal header value character and reaches the wire, and prefixing a slash would otherwise turn a leading tab, which the header layer trims, into an inner one that it does not.

Fixes angular/angular#71076

The `Location` header for a server-side navigation was built by joining the
`PlatformLocation` pathname with the search and hash. A pathname may start with
`//`, which as a `Location` value is a protocol-relative URL to another origin.
The router serializes `/.;/(//example.com)` to `/.//example.com`, dot-segment
removal leaves `//example.com`, and a single unauthenticated `GET` returned
`302 Location: //example.com`. Any route with a parameter in the first segment
and a wildcard child is affected, with no redirect logic in the application.

Collapse the leading slashes of the pathname before using it as a location, and
normalize the same way in `createRedirectResponse()`, which also prefixes a
slash so the value cannot resolve anywhere but the current origin.

The collapse consumes tab, line feed and carriage return too: the WHATWG URL
parser removes them before parsing, so a client reads `/\t/example.com` as
`//example.com`. Tab is a legal header value character and reaches the wire, and
prefixing a slash would otherwise turn a leading tab, which the header layer
trims, into an inner one that it does not.

Fixes #34207

@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 a new utility function 'collapseLeadingSlashes' to collapse multiple leading slashes, backslashes, and whitespace characters (like tabs or line feeds) into a single forward slash. This utility is used in Angular SSR's rendering and redirect utilities to prevent open redirect vulnerabilities caused by protocol-relative URLs (e.g., '//example.com'). It also adds comprehensive tests to verify that redirect locations are safely normalized to absolute paths on the current origin. I have no feedback to provide on this pull request.

@alan-agius4

alan-agius4 commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for investigating this and opening the PR!

We’ve reviewed the issue and the proposed changes, and while we want to ensure protocol-relative open redirects cannot occur, we cannot accept this PR in its current form:

  1. Breaking external redirects:
    Normalizing location via addLeadingSlash(collapseLeadingSlashes(location)) forces all redirects to be treated as paths on the current origin. Redirect helpers and Location headers must continue to support external targets.

  2. Root cause belongs in @angular/platform-server:
    For issue Angular SSR pathname normalization can turn a same-origin navigation into an open redirect angular#71076 (/.;/(//evil.test)), the Angular application never intended to issue a redirect. The redirect was triggered because the Router serialized the navigation to /.//evil.test, and platform-server's resolveUrl failed to reject it before storing pathname = '//evil.test' in ServerPlatformLocation.
    resolveUrl in @angular/platform-server already defines RuntimeErrorCode.PROTOCOL_RELATIVE_URL_NOT_ALLOWED, but its urlStr.startsWith('//') check runs before WHATWG dot-segment normalization. Enforcing !allowProtocolRelative on resolved.pathname.startsWith('//') in platform-server resolves this at the platform boundary for all consumers.

  3. X-Forwarded-Prefix and the dev-server:
    In production, reverse-proxy headers like X-Forwarded-Prefix are governed by AngularAppEngine, where they are only read if explicitly configured in trustedProxyHeaders and are strictly validated using VALID_PREFIX_REGEX (which requires a single leading / and alphanumeric characters, rejecting // and control characters). The dev-server is strictly intended for local development rather than exposure to untrusted public networks, so direct calls to AngularServerApp without reverse-proxy validation do not represent a production attack vector and shouldn't be used to justify rewriting redirect URLs.

Because of this, the underlying issue is being addressed upstream in @angular/platform-server.

@SkyZeroZx
SkyZeroZx deleted the fix/open-redirect branch September 30, 2026 12:51
@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Oct 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Angular SSR pathname normalization can turn a same-origin navigation into an open redirect

2 participants