Skip to content

fix(@angular/ssr): do not render requests sent while rendering on the… - #34790

Open
SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-requests-sent-while-rendering
Open

SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-requests-sent-while-rendering

Conversation

@SkyZeroZx

@SkyZeroZx SkyZeroZx commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Requests that an application sends to its own host while it is rendered on the server now carry an x-angular-ssr-request header. The server answers them with a 400 response instead of rendering them, which could repeat the same request without bound.

Fixes #34789

What is the current behavior?

During SSR, a relative HttpClient URL is resolved against the URL of the page being rendered, port included. allowedHosts only checks the host name, so a request with Host: example.test:4000 (the port Node listens on) sends the page's API calls back to the SSR server. When the ** route matches the API path, AngularServerApp.handle() renders it, that render sends the same call again, and so on until the server runs out of memory.

What is the new behavior?

HttpClient requests that the app sends to its own host while it's rendered on the server now carry an x-angular-ssr-request header. AngularServerApp answers those with a 400 (Cache-Control: no-store) instead of rendering them. If something else serves the request first, like an API route in server.ts or the proxy, nothing changes.

Other information

CommonEngine isn't covered. It doesn't go through AngularServerApp.handle(), and render() only receives the URL, not the request headers, so it can't see the header. I have a possible fix for it in a separate branch, see the comment below.

angular/angular#72203 would complement this. With a port in allowedHosts (for example example.com:443), a request with Host: example.com:4000 would be rejected before rendering. That closes the case from the issue whatever the engine is, and however the app sends its requests. However, it's opt-in: entries without a port still allow any port. Also, both engines here check the host with their own matcher in utils/validation.ts, which only compares host names, so this repo would need a follow-up to accept the same syntax.

… server

Requests that an application sends to its own host while it is
rendered on the server now carry an `x-angular-ssr-request` header.
The server answers them with a 400 response instead of rendering them,
which could repeat the same request without bound.

Fixes angular#34789

@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 mechanism to prevent infinite request loops during server-side rendering (SSR) by intercepting HTTP requests sent by the application to its own host. It adds an HTTP interceptor (ssrRequestInterceptorFn) that appends a custom header (x-angular-ssr-request) to such requests, and configures the server application to return a 400 Bad Request response when this header is detected. Feedback on the implementation suggests optimizing the interceptor by avoiding double URL parsing, improving backward compatibility by replacing URL.canParse with a try-catch block around new URL, and defensively injecting PlatformLocation to prevent runtime errors in environments where it might not be provided.

Comment on lines +30 to +46
export const ssrRequestInterceptorFn: HttpInterceptorFn = (request, next) => {
const { href, hostname } = inject(PlatformLocation);
if (!URL.canParse(href)) {
// The URL of the page is unknown, so requests sent to its host cannot be recognized.
return next(request);
}

// URLs that cannot be parsed are marked too: some HTTP clients still send a request for them.
if (
URL.canParse(request.urlWithParams, href) &&
new URL(request.urlWithParams, href).hostname !== hostname
) {
return next(request);
}

return next(request.clone({ setHeaders: { [SSR_REQUEST_HEADER]: '1' } }));
};

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

Efficiency & Compatibility Improvements in ssrRequestInterceptorFn

There are a few areas in ssrRequestInterceptorFn that can be optimized for better performance, robustness, and compatibility:

  1. Avoid Double Parsing: Currently, URL.canParse(request.urlWithParams, href) is called first, followed by new URL(request.urlWithParams, href). This parses the URL twice for every outgoing HTTP request during SSR. We can optimize this by using a single try-catch block with new URL() to parse only once.
  2. Environment Compatibility: URL.canParse is a relatively new API. While supported in modern Node.js versions, using new URL inside a try-catch block is 100% backward-compatible and avoids any potential runtime compatibility issues in older or custom environments.
  3. Defensive Injection: inject(PlatformLocation) can throw an error if PlatformLocation is not provided (e.g., in certain test environments or minimal custom platforms). Making it optional via inject(PlatformLocation, { optional: true }) makes the interceptor much more robust.
export const ssrRequestInterceptorFn: HttpInterceptorFn = (request, next) => {
  const platformLocation = inject(PlatformLocation, { optional: true });
  if (!platformLocation) {
    return next(request);
  }

  const { href, hostname } = platformLocation;
  try {
    new URL(href);
  } catch {
    // The URL of the page is unknown, so requests sent to its host cannot be recognized.
    return next(request);
  }

  try {
    const url = new URL(request.urlWithParams, href);
    if (url.hostname !== hostname) {
      return next(request);
    }
  } catch {
    // URLs that cannot be parsed are marked too: some HTTP clients still send a request for them.
  }

  return next(request.clone({ setHeaders: { [SSR_REQUEST_HEADER]: '1' } }));
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

URL.canParse is already used in this package (utils/validation.ts runs it on every request) and is available in every supported Node version. PlatformLocation is providedIn: 'platform' with a factory, and platform-server always provides ServerPlatformLocation, so inject() can't throw here.

The extra parse happens once per outgoing HttpClient request during SSR, so I'd rather keep it consistent with the rest of the package.

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