Repository navigation
Conversation
… 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
There was a problem hiding this comment.
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.
| 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' } })); | ||
| }; |
There was a problem hiding this comment.
Efficiency & Compatibility Improvements in ssrRequestInterceptorFn
There are a few areas in ssrRequestInterceptorFn that can be optimized for better performance, robustness, and compatibility:
- Avoid Double Parsing: Currently,
URL.canParse(request.urlWithParams, href)is called first, followed bynew URL(request.urlWithParams, href). This parses the URL twice for every outgoing HTTP request during SSR. We can optimize this by using a singletry-catchblock withnew URL()to parse only once. - Environment Compatibility:
URL.canParseis a relatively new API. While supported in modern Node.js versions, usingnew URLinside atry-catchblock is 100% backward-compatible and avoids any potential runtime compatibility issues in older or custom environments. - Defensive Injection:
inject(PlatformLocation)can throw an error ifPlatformLocationis not provided (e.g., in certain test environments or minimal custom platforms). Making it optional viainject(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' } }));
};There was a problem hiding this comment.
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.
Requests that an application sends to its own host while it is rendered on the server now carry an
x-angular-ssr-requestheader. 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
HttpClientURL is resolved against the URL of the page being rendered, port included.allowedHostsonly checks the host name, so a request withHost: 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?
HttpClientrequests that the app sends to its own host while it's rendered on the server now carry anx-angular-ssr-requestheader.AngularServerAppanswers those with a 400 (Cache-Control: no-store) instead of rendering them. If something else serves the request first, like an API route inserver.tsor the proxy, nothing changes.Other information
CommonEngineisn't covered. It doesn't go throughAngularServerApp.handle(), andrender()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 exampleexample.com:443), a request withHost: example.com:4000would 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 inutils/validation.ts, which only compares host names, so this repo would need a follow-up to accept the same syntax.