Skip to content

fix(@angular/ssr): wrap non-Error values thrown while rendering - #34802

Open
SkyZeroZx wants to merge 5 commits into
angular:mainfrom
SkyZeroZx:fix/ssr-non-error-render-rejection
Open

SkyZeroZx wants to merge 5 commits into
angular:mainfrom
SkyZeroZx:fix/ssr-non-error-render-rejection

Conversation

@SkyZeroZx

Copy link
Copy Markdown
Contributor

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?

When an app initializer's HttpClient call fails, the bootstrap rejects with the HttpErrorResponse, which is not an Error. AngularAppEngine.handle() and CommonEngine.render() pass it on as is. The generated server.ts hands it to Express with .catch(next), Express reads its status and headers, res.setHeader() throws, and the request is never answered. The process exits when no process-level handler is installed.

Issue Number: #34801

What is the new behavior?

Non-Error rejections now arrive as an Error with a fixed message and the original value as cause. Error instances, including the AbortError DOMException, are unchanged. A handler that checked err instanceof HttpErrorResponse now finds it on err.cause, and a rejected plain object such as { status: 404 } now ends as a 500.

Angular reports the original value to the ErrorHandler and then rethrows it, and HttpErrorResponse can't extend Error. AngularAppEngine bootstraps the app itself, so renderApplication() isn't on its path. The wrap is in AngularAppEngine.handle() rather than in renderAngular(), so it also covers routes extracted at request time, and the build's prerender errors keep the original message. instanceof Error is deliberate: HttpErrorResponse has a string name and message, so an error-like check would let it through.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

The zoneless case of subscribe() without an error callback throws outside the render promise, so it isn't covered here.

@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 an ensureError utility to ensure that any values thrown or rejected during application rendering and bootstrapping are properly wrapped as Error instances. This utility is integrated into both CommonEngine and AngularAppEngine, and comprehensive unit tests have been added to verify this behavior. The review feedback suggests enhancing ensureError to extract and preserve the original error message or string representation (such as from an HttpErrorResponse) to improve debuggability, and subsequently updating the unit tests to assert on these specific error messages instead of a generic fallback pattern.

Comment thread packages/angular/ssr/src/utils/error.ts Resolved

@alan-agius4 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall looks good, just a few minor things.

Also a couple of items outside the current diff:

  • In AngularServerApp.handleRendering() (https://github.com/angular/angular-cli/blob/main/packages/angular/ssr/src/app.ts#L387), controller.error(error) runs inside the ReadableStream after AngularAppEngine.handle() has resolved, so we should wrap that with ensureError(error) as well. RenderMode.Prerender returns earlier, so prerender error messages stay unchanged.
  • Can you add a brief note in the JSDoc for AngularAppEngine.handle() and AngularNodeAppEngine.handle() mentioning that non-Error rejections are wrapped in an Error with the original value on cause?
Comment thread packages/angular/ssr/src/utils/error.ts Resolved
Comment thread packages/angular/ssr/src/app-engine.ts Resolved
@alan-agius4 alan-agius4 linked an issue Nov 30, 2026 that may be closed by this pull request
1 task
@SkyZeroZx

Copy link
Copy Markdown
Contributor Author

Thanks! handleRendering() now wraps controller.error() with ensureError, and both handle() JSDocs mention the cause. I didn't add a spec for the stream path since I couldn't find a non-Error that reaches content() without stubbing platform-server.

@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Nov 30, 2026

@alan-agius4 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks for your contribution.

@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 30, 2026
@SkyZeroZx

SkyZeroZx commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@alan-agius4 Can this be backported or not?
Since it can lead to a DoS in certain cases that can be used by an attacker

@alan-agius4

Copy link
Copy Markdown
Collaborator

@alan-agius4 Can this be backported or not? Since it can lead to a DoS in certain cases that can be used by an attacker

Yes

A value such as the `HttpErrorResponse` of a failed request in an app
initializer was passed to the server as is, where Express reads its
`status` and `headers`. This could leave the request unanswered or
terminate the server process. `AngularAppEngine` now wraps it in an
`Error`.

Fixes angular#34801
…ommonEngine`

`CommonEngine.render()` now also rejects with an `Error` when the
application throws another value, such as an `HttpErrorResponse`.

Fixes angular#34801
@SkyZeroZx
SkyZeroZx force-pushed the fix/ssr-non-error-render-rejection branch from 734474a to f48986f Compare November 30, 2026 16:07
@angular-robot
angular-robot Bot requested a review from alan-agius4 November 30, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/ssr target: patch This PR is targeted for the next patch release

2 participants