Skip to content

fix(@angular/ssr): reject recursive SSR requests before rendering - #34794

Draft
alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:fix/ssr-prevent-recursive-requests
Draft

alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:fix/ssr-prevent-recursive-requests

Conversation

@alan-agius4

Copy link
Copy Markdown
Collaborator

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?

Issue Number: Closes angular/angular#72223

When an application sends an HttpClient request during SSR that lands back on the Angular SSR server (for example, matching a ** wildcard route or redirect), @angular/ssr attempts to extract routes or render the page again, which can deadlock on cold-start route extraction or recurse without bound until the Node.js process runs out of memory.

What is the new behavior?

Consumes ɵisSsrRequest and ɵSSR_REQUEST_HEADER from @angular/platform-server (introduced in angular/angular#72226) to reject incoming recursive SSR requests early:

  • Cold-start route extraction deadlock guard: In AngularServerApp.handle(), reject requests carrying x-angular-ssr-request when !this.router && !this.manifest.routes before calling ServerRouter.from() so self-requests during route extraction cannot deadlock on the in-flight extraction promise.
  • Static & locale redirects: Reject matching static redirects (redirectTo) in AngularServerApp.handle() and locale redirects in AngularAppEngine.redirectBasedOnAcceptLanguage() when ɵisSsrRequest(request) is true.
  • Server-side rendering: Reject server-rendered routes (and on-demand rendering of prerendered routes when allowStaticRouteRender is enabled) in AngularServerApp.handleRendering() before bootstrapping, while still returning null for non-Angular routes and serving static prerendered/CSR responses without rendering.
  • Caching & observability: Return 404 Not Found with Cache-Control: no-store and Content-Type: text/plain (avoiding CDNs like CloudFront caching 400 responses despite no-store, without triggering 5xx outlier ejection) and emit a dev-only console.warn containing the request URL and header name.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Consume `ɵisSsrRequest` and `ɵSSR_REQUEST_HEADER` from `@angular/platform-server` (introduced in angular/angular#72226) to reject incoming requests that were sent by an Angular application while rendering on the server:

- Guard cold-start `ServerRouter.from()` extraction (`!this.router && !this.manifest.routes`) in `AngularServerApp.handle()` to avoid deadlocking on the in-flight route extraction promise.
- Reject static redirects in `AngularServerApp.handle()`, locale redirects in `AngularAppEngine.redirectBasedOnAcceptLanguage()`, and server-side rendering in `AngularServerApp.handleRendering()` (including on-demand rendering of prerendered routes when `allowStaticRouteRender` is enabled), while continuing to return `null` for non-Angular routes and serving static prerendered/CSR responses without rendering.
- Return `404 Not Found` with `Cache-Control: no-store` (avoiding CDNs like CloudFront caching `400` responses despite `no-store` without triggering 5xx outlier ejection) and log a dev-only warning so loops are observable during development.

Closes angular/angular#72223
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.

Angular SSR can render itself until the server runs out of memory when Host carries the server's port

1 participant