Skip to content

@angular/ssr when passes a non-Error to Express, can crashing the server  #34801

In review

Description

@SkyZeroZx

Command

other

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When an app initializer makes an HttpClient call during SSR and the call gets a 4xx or 5xx, the bootstrap rejects with the HttpErrorResponse, which isn't an Error. Both server templates pass it to Express:

app.use((req, res, next) => {
angularApp
.handle(req)
.then((response) =>
response ? writeResponseToNodeResponse(response, res) : next(),
)
.catch(next);
});

commonEngine
.render({
<% if (isStandalone) { %>bootstrap<% } else { %>bootstrap: AppServerModule<% } %>,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
})
.then((html) => res.send(html))
.catch((err) => next(err));

finalhandler uses err.status and sets every own field of err.headers as a response header (index.js#L90-L100, #L262-L263). lazyInit is undefined (headers with the XHR backend), so res.setHeader() throws ERR_HTTP_INVALID_HEADER_VALUE. It runs from onFinished (#L291), outside the promise chain, so the exception is uncaught and the request never gets an answer.

@angular/ssr registers a listener for that, but not when zone.js is loaded:

* @remarks
* This function is a no-op if zone.js is available.
* For Zone-based apps, similar functionality is provided by Zone.js itself. See the Zone.js implementation here:
* https://github.com/angular/angular/blob/4a8d0b79001ec09bcd6f2d6b15117aa6aac1932c/packages/zone.js/lib/node/node.ts#L94%7C
*
* @internal
*/
export function attachNodeGlobalErrorHandlers(): void {
if (typeof Zone !== 'undefined') {
return;
}

zone.js handles unhandled rejections, not a throw from a callback. Without outputMode, the builder puts import './polyfills.server.mjs'; first in server.mjs, so zone.js is loaded before new CommonEngine() and the listener is never attached:

const isOldBehaviour = !outputMode;
if (ssrEntryPoint && isOldBehaviour) {
// Old behavior: 'server.ts' was bundled together with the SSR (Server-Side Rendering) code.
// This approach combined server-side logic and rendering into a single bundle.
entryPoints['server'] = ssrEntryPoint;
}
const buildOptions: BuildOptions = {
...getEsBuildServerCommonOptions(options),
target,
banner: {
js: `import './polyfills.server.mjs';`,
},

The initializer is the one from the provideAppInitializer usage notes, with a local URL. Same app and same failing call in every row (the API answers 503); only the server changes:

Server Also in the process The failing request 5 requests in flight Process
CommonEngine, no outputMode, zone.js first nothing cut cut exit 1
ng new --ssr with zone.js @sentry/node, defaults cut cut exit 1
ng new --ssr as generated a handler that logs and exits cut cut exit 1
Fastify, the server file of the CLI's e2e test nothing 200, with the HttpErrorResponse as JSON 200 alive
ng new --ssr as generated (zoneless) nothing never answered 200 alive

The first row is the server ng add @angular/ssr writes for projects on the webpack builders, and what an application-builder project has without outputMode. 199 of the 200 statuses from 400 to 599 do it; 407 doesn't, because fetch reports it as a network error. With the API down instead there is no status, and the same server answers 500.

In the Sentry row, zone.js alone survives and so does Sentry alone. Once zone.js is loaded, process.listeners('uncaughtException') returns [] while listenerCount() is 1, and Sentry exits when that list is empty (onuncaughtexception.ts#L82-L93).

The Fastify row doesn't exit, but the visitor gets the URL of the server-side call and the API's error body:

{"headers":{"headers":{},"normalizedNames":{},"lazyUpdate":null},"status":503,"statusText":"Service Unavailable","url":"http://127.0.0.1:4401/api/user","ok":false,"redirected":false,"responseType":"basic","name":"HttpErrorResponse","message":"Http failure response for http://127.0.0.1:4401/api/user: 503 Service Unavailable","error":{"error":"status 503"}}

On Express, it takes a failure the bootstrap waits for, so an app initializer. The same call from a component or a resolver is logged and the server still answers on the servers with zone.js, CommonEngine included.

A zoneless app, which is what ng new creates today, has a second way down with no initializer: a component that calls http.get(...).subscribe(...) without an error callback. RxJS rethrows the error from a timer (reportUnhandledError.ts#L13-L24), Angular's listener logs it, and a handler that logs and exits ends the process. That happened with the API answering 503 and with two 5 KB cookies getting a 400 from the API's nginx.

An API that answers 503 while it redeploys is enough, and so is the 502 its proxy sends in the meantime.

Not #34754: nothing is aborted. The client keeps the socket open, and the process exits 76 to 202 ms after the request.

Minimal Reproduction

https://github.com/SkyZeroZx/angular-ssr-failed-initializer-crash

npm ci
npm run poc:build
npm run api &
NG_ALLOWED_HOSTS=localhost node dist/legacy/server/server.mjs &
curl -sS -o /dev/null -w "%{http_code}\n" http://localhost:4000/products/42
curl -sS "http://127.0.0.1:4401/__control?status=503"
curl -sS http://localhost:4000/products/42
curl -sS http://localhost:4000/products/42
200
{"status":503,"delay":0}
curl: (56) Recv failure: Connection was reset
curl: (7) Failed to connect to localhost:4000 after 0 ms: Could not connect to server

Exception or Error

ERROR or {
  headers: e {
    headers: Map(6) {
      'connection' => [Array],
      'content-length' => [Array],
      'content-type' => [Array],
      'date' => [Array],
      'etag' => [Array],
      'keep-alive' => [Array]
    },
    normalizedNames: Map(6) {
      'connection' => 'connection',
      'content-length' => 'content-length',
      'content-type' => 'content-type',
      'date' => 'date',
      'etag' => 'etag',
      'keep-alive' => 'keep-alive'
    },
    lazyInit: undefined,
    lazyUpdate: null
  },
  status: 503,
  statusText: 'Service Unavailable',
  url: 'http://127.0.0.1:4401/api/user',
  ok: false,
  type: undefined,
  redirected: false,
  responseType: 'basic',
  name: 'HttpErrorResponse',
  message: 'Http failure response for http://127.0.0.1:4401/api/user: 503 Service Unavailable',
  error: { error: 'status 503' }
}
file:///<root>/dist/legacy/server/polyfills.server.mjs:3
[17,896 characters of minified source]
TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "lazyInit"
    at ServerResponse.setHeader (node:_http_outgoing:703:3)
    at a (file:///<root>/dist/legacy/server/server.mjs:65:1260)
    at AsyncResource.runInAsyncScope (node:async_hooks:214:14)
    at e (file:///<root>/dist/legacy/server/server.mjs:50:424824)
    at a (file:///<root>/dist/legacy/server/server.mjs:50:424380)
    at u (file:///<root>/dist/legacy/server/server.mjs:50:423565)
    at IncomingMessage.<anonymous> (file:///<root>/dist/legacy/server/server.mjs:50:423847)
    at l.invokeTask (file:///<root>/dist/legacy/server/polyfills.server.mjs:3:8703)
    at Cn.runTask (file:///<root>/dist/legacy/server/polyfills.server.mjs:3:3324)
    at invokeTask (file:///<root>/dist/legacy/server/polyfills.server.mjs:3:9931) {
  code: 'ERR_HTTP_INVALID_HEADER_VALUE'
}
Node.js v22.22.0

Your Environment

@angular/ssr      22.3.0
@angular/core     22.3.0
@angular/build    22.3.0
Express           5.3.0 
rxjs              7.8.2
zone.js           0.16.3
Fastify           5.12.8
@sentry/node      11.16.0
Node.js           22.22.0 on Linux (WSL2), 24.16.0 on Windows 11
nginx             1.29.8 in Docker, for the cookie and supervisor runs

Anything else relevant?

I think this may be a security issue. Since security policy says :

"DoS attacks which waste server resources significantly beyond the cost of the attacker's request".

Here one request ends the process on the servers in the first three rows, and the trigger is the documented use of provideAppInitializer.

The API doesn't need to be down. Any visitor input that reaches the initializer's call and gets a 4xx or 5xx is enough:

Activity

  1. added this to the needsTriage milestone on Nov 30, 2026
  2. changed the title [-]@angular/ssr passes a non-Error HttpErrorResponse to Express, crashing the server when an app initializer's HttpClient call fails[/-] on Nov 30, 2026
  3. changed the title [-]@angular/ssr when passes a non-Error to Express, can crashing the server when an app initializer's HttpClient call fails[/-] on Nov 30, 2026
  4. added 2 commits that reference this issue on Nov 30, 2026
    90d2740
    8e21a25
  5. added 2 commits that reference this issue on Nov 30, 2026
    7dbe2a3
    d51a698
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