Skip to content

fix(@angular/ssr): add excludedPathPrefixes option - #34809

Closed
SkyZeroZx wants to merge 2 commits into
angular:mainfrom
SkyZeroZx:fix/ssr-excluded-path-prefixes
Closed

SkyZeroZx wants to merge 2 commits into
angular:mainfrom
SkyZeroZx:fix/ssr-excluded-path-prefixes

Conversation

@SkyZeroZx

Copy link
Copy Markdown
Contributor

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

A catch-all server route (**) renders any path that nothing else answered. When a page requests one of its own API paths during SSR and that request reaches the SSR server instead of the API, Angular renders it as a page, that render sends the same request again, and so on until the server runs out of memory.

Issue Number: angular/angular#72223

What is the new behavior?

AngularAppEngine and AngularNodeAppEngine accept excludedPathPrefixes. A request whose path starts with one of them is never rendered: handle() resolves to null and the next handler answers it.

const angularApp = new AngularNodeAppEngine({ excludedPathPrefixes: ['/api'] });

The check runs after the host validation and before the app is selected. Paths are compared decoded, and also without the base href and the locale, so /en/api/users matches /api but /apiary and /docs/api don't.

It's opt-in. It doesn't try to detect the loop, it only takes away the paths that feed it, so no header is read and nothing a client sends changes the result.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

With the app from the issue and ['/api'], the request from the issue costs one render, also with withXhr(), native fetch, node:cluster and an extra proxy hop. Without the option the server runs out of memory in about 15 s.

Not covered: a document without a <base> element that requests the API with a path-relative URL (api/config). That resolves against the path of the page, which a prefix can't match. It's noted in the option's JSDoc.

This doesn't replace angular/angular#72203. It's meant as an alternative for apps that can't list ports.

Requests whose path starts with one of these prefixes are left to the
next handler instead of being rendered by a catch-all route, which could
repeat a request the application sends to its own host without bound.

@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 the excludedPathPrefixes option to AngularAppEngine, allowing developers to specify path prefixes (such as API routes) that the engine should ignore and pass to the next handler. The implementation includes logic to normalize, decode, and match these prefixes against request paths, taking localized applications and base hrefs into account, along with comprehensive unit tests. The review feedback focuses on optimizing the request-handling hot path by precomputing the normalized base path and locale prefixes during initialization, and adding a fast-path check in decodePath to avoid unnecessary string operations when no percent-encoded characters are present.

Comment thread packages/angular/ssr/src/app-engine.ts Resolved
Comment thread packages/angular/ssr/src/app-engine.ts Resolved
Comment thread packages/angular/ssr/src/app-engine.ts Resolved
@alan-agius4

Copy link
Copy Markdown
Collaborator

Thanks for the thorough investigation in angular/angular#72223 and for putting this PR together.

We are going to close this as we do not want to add excludedPathPrefixes to AngularAppEngineOptions. Here is the reasoning behind this decision:

  1. Wrong architectural layer: AngularAppEngineOptions is intentionally scoped to ingress security settings (allowedHosts and trustProxyHeaders, which also map to NG_* environment variables and angular.json configuration). Adding URL path filtering here introduces a separate path matching layer alongside server.ts middleware and ServerRouter, completely disconnected from build time route extraction, prerendering, and client navigation. Once exposed as public API, it also creates ongoing pressure to expand into exact matches, globs, regexes, HTTP method filters, and env var parity.

  2. Redundant with server.ts: Since configuring excludedPathPrefixes already requires editing server.ts where AngularAppEngine or AngularNodeAppEngine is instantiated, handling or rejecting backend prefixes directly in the server middleware before angularApp.handle(req) is simpler and more flexible. That keeps ownership of non Angular routes in the host server (Express, Fastify, Hono, etc.) with full control over matching rules, status codes, and response payloads.

  3. baseHref, locale, and URL normalization pitfalls: Matching configured prefixes against both the raw pathname and the stripped basePath and locale path causes unexpected collisions. For example, an app with baseHref: '/docs/' or localized routes like /en that excludes /api for a root backend will also block legitimate Angular pages at /docs/api or /en/api with no way to opt out. Having a separate path matcher in app-engine.ts also diverges from ServerRouter and RouteTree normalization (for instance, duplicate slashes like //api/config or matrix parameters like /api;x=1/config bypass the prefix check while still matching a catch all ** route).

  4. Incomplete mitigation for the root cause: Because this is an opt in path denylist, apps remain unprotected by default, any unlisted prefix (such as /graphql, /auth, or missing static assets) or path relative fetch without <base> can still loop, and it does not prevent port retargeting SSRF against other services on the same host.

As you noted in angular/angular#72223, supporting ports in allowedHosts (angular/angular#72203 and the follow up to #34793) is the right fix for the root cause at ingress. For apps that cannot pin ports, standard server.ts middleware, reverse proxy Host header normalization, or routing SSR API calls via an interceptor with HTTP_TRANSFER_CACHE_ORIGIN_MAP are better options than adding a path exclusion list to AngularAppEngineOptions.

Thanks again for all the research and testing on this!

@alan-agius4 alan-agius4 closed this Dec 1, 2026
@SkyZeroZx
SkyZeroZx deleted the fix/ssr-excluded-path-prefixes branch December 1, 2026 16:08
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.

2 participants