fix: handle frozen globalThis in setGlobalDispatcher#5574
Open
mhayk wants to merge 3 commits into
Open
Conversation
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.
mcollina
requested changes
Jul 20, 2026
mcollina
left a comment
Member
There was a problem hiding this comment.
Thanks for opening a PR! Can you please add a unit test?
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.
Author
|
Thanks for the feedback! I've now added comprehensive unit tests as requested:
The tests are in the new file Tests now passing ✅ |
mcollina
requested changes
Jul 20, 2026
| @@ -0,0 +1,54 @@ | |||
| 'use strict' | |||
|
|
|||
| const { tspl } = require('@matteo.collina/tspl') | |||
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5574 +/- ##
==========================================
- Coverage 93.47% 93.46% -0.01%
==========================================
Files 110 110
Lines 37560 37581 +21
==========================================
+ Hits 35108 35124 +16
- Misses 2452 2457 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Author
|
I've improved the test coverage with more comprehensive test cases:
All tests passing. Tests follow undici conventions using tspl. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
Object.freeze(globalThis)is called before undici globals are accessed for the first time, the lazy initialisation insidesetGlobalDispatcherthrows:This is particularly problematic because the Node.js security best practices guide explicitly recommends
Object.freeze(globalThis)as a defence against monkey-patching (CWE-349).Root Cause
setGlobalDispatcherunconditionally callsObject.defineProperty(globalThis, ...). When globalThis is not extensible, the call throws instead of degrading gracefully.Solution
Wrap the
Object.definePropertycalls insetGlobalDispatcherwith a try/catch. When globalThis is not extensible the dispatcher is stored in a module-level fallback variable.getGlobalDispatcheris updated to returnglobalThis[symbol] ?? fallbackDispatcherso the normal (extensible) path is unchanged.Testing
A test verifies that:
setGlobalDispatchersucceeds even with frozen globalThisgetGlobalDispatcherreturns the correct dispatcher