镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[v6.x] fix: handle frozen globalThis in setGlobalDispatcher (#5574) - #6018

Merged
metcoder95 merged 1 commit into
nodejs:v6.xfrom
mrityunjay-pandey:backport/v6.x-frozen-global
Oct 16, 2026
Merged

metcoder95 merged 1 commit into
nodejs:v6.xfrom
mrityunjay-pandey:backport/v6.x-frozen-global

Conversation

@mrityunjay-pandey

Copy link
Copy Markdown
Contributor

This relates to...

Backport of #5574 to v6.x.

Rationale

On v6.x, setGlobalDispatcher calls Object.defineProperty(globalThis, …) without a guard, so after Object.freeze(globalThis), loading undici or setting a dispatcher throws a TypeError. The upstream test test/global-frozen.js fails 3 of 3 on v6.x.

Changes

Features

N/A

Bug Fixes

  • setGlobalDispatcher / getGlobalDispatcher work when globalThis is frozen.

Breaking Changes and Deprecations

N/A

Status

Tested locally on Linux with Node 24: npm run test:unit (1045 tests, 1040 pass, 5 skipped, 0 fail) and npm run lint.

* fix: handle frozen globalThis in setGlobalDispatcher

When Object.freeze(globalThis) is called before undici globals are accessed,
setGlobalDispatcher would throw TypeError because it cannot extend globalThis.

This fix wraps the Object.defineProperty calls in try/catch. When globalThis
is not extensible (frozen), the dispatcher is stored in a module-level
fallback variable instead. getGlobalDispatcher is updated to return the
fallback dispatcher when the globalThis property is not available.

This allows undici to work correctly even when globalThis has been frozen,
which is recommended by Node.js security best practices (CWE-349).

Fixes issue where Object.freeze(globalThis) breaks undici access.

* test: add unit test for frozen globalThis in setGlobalDispatcher

Add comprehensive test coverage for the frozen globalThis fix. Tests verify:
1. setGlobalDispatcher does not throw when globalThis is frozen
2. getGlobalDispatcher continues to return a valid dispatcher
3. The fallback mechanism works correctly when globalThis is not extensible

This addresses the review feedback from mcollina requesting tests.

* test: improve frozen globalThis test coverage

Add comprehensive test cases to ensure all code paths in the frozen globalThis
fix are exercised. Tests verify:
1. setGlobalDispatcher does not throw when globalThis is frozen
2. getGlobalDispatcher returns a valid dispatcher
3. Fallback dispatcher persists across multiple calls

This addresses review feedback requesting tests.

(cherry picked from commit a0922b0)
Signed-off-by: Mrityunjay Pandey <mrityunjayp1409@gmail.com>

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@metcoder95
metcoder95 merged commit e409b25 into nodejs:v6.x Oct 16, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants