fix: avoid 401 failures for stream-backed request bodies#4941
Open
fix: avoid 401 failures for stream-backed request bodies#4941
Conversation
Member
Author
|
Somehow I garbled the file, will fix |
a9eab22 to
30c8822
Compare
- Properly implement isTraversableNavigable() to return false in Node.js - Return 401 response directly for stream-backed bodies instead of throwing network error - Aligns with Fetch spec discussion in whatwg/fetch#1132 - Add regression tests for PUT with ReadableStream and POST with JSON body
30c8822 to
7c1b59a
Compare
Member
Author
|
@KhafraDev this should be correct for us |
KhafraDev
approved these changes
Mar 29, 2026
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.
This relates to...
Summary
This PR fixes a bug where
fetch()throwsTypeError: fetch failedfor POST/PUT requests with ReadableStream bodies when the server responds with a 401 status code.Root Cause
The issue occurred in the 401 HTTP authentication handling code in
httpNetworkOrCacheFetch(). When:isTraversableNavigable()returnedtrue(a TODO stub)The code would enter the 401 retry block and check
request.body.source == null, which is true for ReadableStreams, causing it to throw a network error before thereturn responsefix from #4769 could be reached.The Fix
1. Updated
isTraversableNavigable()(lib/web/fetch/util.js)Changed from always returning
trueto properly checking if we have an actual navigable object:This ensures that in Node.js (which has no Window/navigable), the 401 retry block is never entered, and the response is returned directly.
2. Changed network error to return response (lib/web/fetch/index.js)
For the edge case where
isTraversableNavigable()returnstrue(in browser environments with actual navigables), changed the body source check to return the response instead of a network error:This is more spec-compliant as it aligns with the Fetch spec discussion in whatwg/fetch#1132, which allows implementations flexibility when credentials can't be obtained.
Spec Compliance
The current undici behavior was not fully spec-compliant. The Fetch specification:
This fix brings undici closer to spec compliance by:
isTraversableNavigable()to distinguish between browser and Node.js contextsChanges
Features
N/A
Bug Fixes
fetch failed: expected non-null body sourceisTraversableNavigable()to avoid entering 401 retry block in Node.jsBreaking Changes
N/A
Testing
Added test cases for:
All tests pass successfully.