Skip to content

fix(zone.js): harden against frozen intrinsics in toString and console patches - #69505

Closed
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/zone.js_frozen_intrinsics
Closed

arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/zone.js_frozen_intrinsics

Conversation

@arturovt

Copy link
Copy Markdown
Contributor

Loading zone.js under Node's --frozen-intrinsics (or SES lockdown, or any environment that has frozen the relevant built-ins) crashes at import. Two load_patches are responsible.

The toString patch dies trying to overwrite
Function.prototype.toString:

  TypeError: Cannot assign to read only property 'toString' of
  object 'function () { [native code] }'

The console patch dies one step earlier, when it tries to stash the original method under a symbol-named property:

  TypeError: Cannot add property __zone_symbol__dir, object is
  not extensible

Node started freezing the console object a while back (see nodejs/node#46044), so this is expected behavior on the runtime side, not a Node bug — zone.js just isn't checking before it writes.

The fix in both places is to look before leaping. For toString, we inspect the descriptors of Function.prototype.toString and Object.prototype.toString and skip the patch if either one isn't writable or configurable. It has to be all-or-nothing: applying only one of the two would make Promise objects stringify differently depending on which path runs, which is worse than not patching at all.

For console, we first check Object.isExtensible(console), then check each method's descriptor individually. Console methods are independent of each other, so a single locked-down method just gets skipped while the rest are patched normally.

When the patches bail, you lose some behavior:

  • Patched functions show their wrapper source through Function.prototype.toString instead of looking like native code. Anything that grep's for "[native code]" to detect natives will see different output.

  • console calls from inside a non-root zone stay in the calling zone instead of being hoisted to Zone.root. The output still appears; it's just in a different zone context. Most code won't notice, but zone-aware logging setups might.

Neither is great, but both beat crashing at require().

Worth being clear about scope: this isn't "zone.js now works under --frozen-intrinsics." The whole library is built around mutating intrinsics, which is exactly what the flag forbids, so that's a contradiction we can't resolve. The goal here is just that the library shouldn't crash on import. A couple of other load_patches have similar issues (patchMethod especially) and can be hardened the same way in follow-ups.

@pullapprove
pullapprove Bot requested a review from kirjs June 24, 2026 19:24
@angular-robot angular-robot Bot added the area: zones Issues related to zone.js label Jun 24, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jun 24, 2026
@JeanMeche

Copy link
Copy Markdown
Member

I would expect a test to cover this here if we do that change.

Also was this a problem you encountered or was the found by an agent ?

@arturovt

Copy link
Copy Markdown
Contributor Author

@JeanMeche found by me through running our Node servers against security flags.

…e patches

Loading zone.js under Node's --frozen-intrinsics (or SES lockdown,
or any environment that has frozen the relevant built-ins) crashes
at import. Two load_patches are responsible.

The toString patch dies trying to overwrite
Function.prototype.toString:

  TypeError: Cannot assign to read only property 'toString' of
  object 'function () { [native code] }'

The console patch dies one step earlier, when it tries to stash
the original method under a symbol-named property:

  TypeError: Cannot add property __zone_symbol__dir, object is
  not extensible

Node started freezing the console object a while back (see
nodejs/node#46044), so this is expected behavior on the runtime
side, not a Node bug — zone.js just isn't checking before it
writes.

The fix in both places is to look before leaping. For toString,
we inspect the descriptors of Function.prototype.toString and
Object.prototype.toString and skip the patch if either one isn't
writable or configurable. It has to be all-or-nothing: applying
only one of the two would make Promise objects stringify
differently depending on which path runs, which is worse than
not patching at all.

For console, we first check Object.isExtensible(console), then
check each method's descriptor individually. Console methods are
independent of each other, so a single locked-down method just
gets skipped while the rest are patched normally.

When the patches bail, you lose some behavior:

- Patched functions show their wrapper source through
  Function.prototype.toString instead of looking like native
  code. Anything that grep's for "[native code]" to detect
  natives will see different output.

- console calls from inside a non-root zone stay in the calling
  zone instead of being hoisted to Zone.root. The output still
  appears; it's just in a different zone context. Most code
  won't notice, but zone-aware logging setups might.

Neither is great, but both beat crashing at require().

Worth being clear about scope: this isn't "zone.js now works
under --frozen-intrinsics." The whole library is built around
mutating intrinsics, which is exactly what the flag forbids, so
that's a contradiction we can't resolve. The goal here is just
that the library shouldn't crash on import. A couple of other
load_patches have similar issues (patchMethod especially) and
can be hardened the same way in follow-ups.
@arturovt
arturovt force-pushed the fix/zone.js_frozen_intrinsics branch from d157ea7 to 4f99b76 Compare June 25, 2026 17:58
@JeanMeche

Copy link
Copy Markdown
Member

Silently dropping the patch seem a bit dangerous to me, also frozen-intrinsics is still an experimental flag. I'm not sure we should follow on this change.

@alan-agius4

Copy link
Copy Markdown
Contributor

Hi @arturovt, thank you for the PR.

However, we have decided not to accept this change. --frozen-intrinsics has been around since Node.js version 11.12 and this feature is still experimental, and so far we haven't seen significant demand or compelling use cases requesting its support in zone.js.

Since zone.js inherently relies on mutating globals and intrinsics to function, silently skipping patches (such as toString or console) when they are frozen introduces a lot of risk. For instance, not patching console could lead to subtle timing or context issues that are very hard to debug. In these scenarios, having the runtime crash loudly on import is actually preferred over silently leaving the environment partially patched and in an inconsistent state.
Because of this, we believe it's better to keep the current behavior where the application fails early when intrinsics are frozen, rather than silently omitting patches.

Thank you again for your effort!

@alan-agius4 alan-agius4 closed this Jul 8, 2026
@arturovt
arturovt deleted the fix/zone.js_frozen_intrinsics branch July 9, 2026 10:03
@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Aug 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: zones Issues related to zone.js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants