Skip to content

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

Backlog

Description

@SkyZeroZx

Description

During SSR, a relative HttpClient URL is resolved against the URL of the request being rendered, port included. The Node adapter builds that URL from Host as it arrived, and allowedHosts checks the host name:

function verifyHostAllowed(
headerName: string,
headerValue: string,
allowedHosts: ReadonlySet<string>,
): void {
const url = `http://${headerValue}`;
if (!URL.canParse(url)) {
throw new Error(`Header "${headerName}" contains an invalid value and cannot be parsed.`);
}
const { hostname, pathname, search, hash, username, password } = new URL(url);
if (pathname !== '/' || search || hash || username || password) {
throw new Error(
`Header "${headerName}" with value "${headerValue}" contains characters that are not allowed.`,
);
}
if (!isHostAllowed(hostname, allowedHosts)) {
throw new Error(`Header "${headerName}" with value "${headerValue}" is not allowed.`);
}
}

So with example.test allowed, a request with Host: example.test:4000, 4000 being the port Node listens on, passes the check, and the page's relative requests go to the SSR server itself instead of the reverse proxy in front of it. The SSR server doesn't serve the API path and the app's ** route matches it, so AngularServerApp.handle() renders it like any other page (it only skips /favicon.ico and the DevTools file):

async handle(request: Request, requestContext?: unknown): Promise<Response | null> {
const url = new URL(request.url);
if (WELL_KNOWN_NON_ANGULAR_URLS.has(url.pathname)) {
return null;
}
this.router ??= await ServerRouter.from(this.manifest, url);
const matchedRoute = this.router.match(url);
if (!matchedRoute) {
// Not a known Angular route.
return null;
}
const { redirectTo, relativeRedirect, status, renderMode, headers } = matchedRoute;
if (redirectTo !== undefined) {
const location = joinUrlParts(
getFirstHeaderValue(request.headers.get('X-Forwarded-Prefix')) ?? '',
buildUrlWithParams(redirectTo, url.pathname, relativeRedirect ? undefined : url.search),
);
// Match Angular router behavior: relative redirects keep the query string.
return createRedirectResponse(
relativeRedirect ? location + url.search : location,
status,
headers,
);
}
if (renderMode === RenderMode.Prerender) {
const response = await this.handleServe(request, matchedRoute);
if (response) {
return response;
}
}
return promiseWithAbort(
this.handleRendering(request, matchedRoute, requestContext),
request.signal,
`Request for: ${request.url}`,
);
}

That render runs the same root component, which sends the same request, which is rendered again. Nothing marks a request as coming from a render and nothing limits the depth, so one request grows a chain of renders until the heap runs out. The API never gets a request from it.

The app is ng new --ssr on 22.3.0. Its root component shows the site's settings, which a service gets from the API:

@Injectable({ providedIn: 'root' })
export class SiteConfig {
  private readonly http = inject(HttpClient);

  get() {
    return this.http.get<{ title: string }>('/api/config');
  }
}

Every route renders on the server (RenderMode.Server on **, as in the SSR guide), unknown paths get a not-found page, allowedHosts is ["example.test"], and server.ts is the generated one. The proxy sends /api/ to the API and the rest to Node.

These proxies pass the client's port on to Node in Host, and localhost in allowedHosts leads back to Node wherever the proxy is. One request each, on 22.3.0 unless the row says otherwise, in a 1 GB container (Node picks a 560 MB heap):

Setup The request Process
nginx with Ubuntu 22.04 or 24.04 proxy_params (Host $http_host, also in PM2's nginx guide) Host: example.test:4000 exits after 14.1 to 15.4 s
nginx with Ubuntu 26.04 proxy_params (Host $host$is_request_port$request_port, also in Debian testing) Host: example.test:4000 exits after 14.7 s
Nginx Proxy Manager, a proxy host as its UI makes it Host: example.test:4000 exits after 14.3 s
Apache with ProxyPreserveHost On, which it needs here (with Off, Angular answers 400) Host: example.test:4000 exits after 15.3 s
Caddy, reverse_proxy with its defaults Host: example.test:4000 exits after 14.2 s
Traefik with its defaults Host: example.test:4000 exits after 12.7 s
HAProxy with its defaults, 22.3.0 or 21.2.31 as generated Host: example.test:4000 exits after 13.4 to 15.1 s
localhost also allowed, nginx with Host $http_host on the same or another machine Host: localhost:4000 exits after 15.6 to 15.8 s
localhost also allowed, Kubernetes with ingress-nginx or Traefik and an Ingress with no host Host: localhost:4000 the pod's container exits after 13.0 to 13.5 s

The proxy_params rows are the distro's own nginx package with a site that only does include proxy_params;. nginx needs some Host line in front of Angular: without one it sends 127.0.0.1:4000 and Angular answers every request with 400. The site's name resolves to the machine that runs Node, as on a VPS with the proxy next to Node. In Kubernetes it points at the load balancer, so there it takes localhost. * in allowedHosts works like localhost with any name.

It isn't tied to HttpClient in a service: httpResource in the root component ends the process after 14.7 s, a resolver on a layout route after 12.5 s.

Proxy headers do the same where Angular trusts them. X-Forwarded-Host: example.test:4000 ends the process after 13.7 to 14.9 s on 20.3.43 and 21.2.31 as generated, and after 14.6 to 15.2 s on 22.3.0 after ng update, even with a Host $host line as in the NGINX reverse proxy guide. 20.3 and 21.2 trust x-forwarded-host and x-forwarded-proto unless trustProxyHeaders is set:

if (trustProxyHeaders === undefined) {
return new Set(['x-forwarded-host', 'x-forwarded-proto']);
}

22 trusts neither, and the v22 migration adds both to every server file that creates AngularNodeAppEngine:

? `{\n ${TODO_COMMENT}\n ` +
`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],\n}`
: `({\n ${TODO_COMMENT}\n ` +
`trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-proto'],\n})`,

With trustProxyHeaders: true, X-Forwarded-Port and, on 22, Forwarded do the same through Debian's proxy_params, which set Host $host.

allowedHosts, added in #32516, compares host names, so a port in Host or X-Forwarded-Host passes. That's known: #33128 proposed checking it and was closed with "Validation is handled by the user when using the trustProxyHeaders configuration". This issue doesn't ask to change that. It's about what follows when the port is the SSR server's own: the render requests itself, again and again. No row of the table depends on a trusted proxy header.

Minimal Reproduction

https://github.com/SkyZeroZx/angular-ssr-self-render-loop-lab

cd docker
docker compose up -d --build site nginx
node ../tools/attack.mjs "Host: example.test:4000"
docker compose ps -a site
visitor   GET /              200, site: from-api, 42 ms
visitor   GET /no/such/page  200, site: from-api, 15 ms
attack    GET / with Host: example.test:4000
visitors  12 answered (median 17 ms, max 89 ms), then 502, site: -, 2258 ms at 15.3 s
attack    502, site: -, 15264 ms after 15.3 s
visitor   GET /              ECONNRESET

docker compose ps -a site shows Exited (134). The README has the commands for the other rows, the other versions and the other places of the request.

Exception or Error

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 0x9434f0  [node]
 2: 0x75f7fa node::OOMErrorHandler(char const*, v8::OOMDetails const&) [node]

Your Environment

@angular/ssr      22.3.0, 21.2.31, 20.3.43
@angular/core     22.3.0, 21.2.26, 20.3.35
@angular/build    22.3.0, 21.2.31, 20.3.43
Express           5.3.0
Node.js           24.22.3 in Docker (node:24-slim)
nginx             1.24.0 from Ubuntu 24.04 (also 1.18.0 and 1.28.3 from Ubuntu, 1.26.3 from Debian 13, 1.30.7 from nginx.org)
Proxies           Nginx Proxy Manager 2.17.1, Apache 2.4.71, Caddy 2.11.9, Traefik 3.8.0, HAProxy 3.4.8
Kubernetes        k3s 1.34.1 with ingress-nginx 1.15.1 or Traefik 3.3.6

Anything else relevant?

The relative URL interceptor already sees every HttpClient request a render sends, so it could mark them, and AngularServerApp.handle() could decline to render a request that carries the mark. That would end the loop whatever puts the SSR server's own address in the URL, without touching how allowedHosts treats ports.

Until then, each of these stops it in the lab: proxy_set_header Host $host; in nginx, not trusting proxy headers that the proxy passes on from the client, and keeping localhost out of allowedHosts.

I think this is a DoS vulnerability according to the SECURITY.md, which says:

DoS attacks which waste server resources significantly beyond the cost of the attacker's request may be security issues.

Here a single request, with a port the client writes in Host, ends the process in about 15 seconds , which cause a OOM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions