fix(fetch): only send Sec-Fetch-Mode to potentially trustworthy URLs - #5738
Merged
Conversation
appendFetchMetadata skipped step 1 of "append the Fetch metadata headers for a request" (https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-append-the-fetch-metadata-headers-for-a-request): 1. If r's url is not a potentially trustworthy URL, return. and left the corresponding "Assert: r's url is a potentially trustworthy URL" step of the Sec-Fetch-Mode algorithm as a TODO, so Sec-Fetch-Mode was sent to every destination, including plain-http, non-loopback hosts such as http://192.168.0.12/. Browsers do not send any Sec-Fetch-* header there. Add the early return using the existing isURLPotentiallyTrustworthy() helper on the request's current URL, so the check is applied per redirect hop like the referrer-policy code and like Chromium/Firefox. https/wss, localhost, *.localhost, 127.0.0.0/8 and [::1] destinations are unchanged. The three WPT cases in fetch/metadata/generated/fetch.sub.html ("sec-fetch-mode - Not sent to non-trustworthy ... destination, no init") now pass; test/web-platform-tests/expectation.json is updated accordingly. Refs: nodejs#1305 (comment) Signed-off-by: jongsik <kjsik11@gmail.com>
tsctx
reviewed
Sep 3, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5738 +/- ##
==========================================
+ Coverage 93.47% 93.50% +0.03%
==========================================
Files 110 110
Lines 38908 39077 +169
==========================================
+ Hits 36368 36538 +170
+ Misses 2540 2539 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
tsctx
approved these changes
Sep 4, 2026
Merged
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...
Refs #1305, in particular #1305 (comment).
Spec: https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-append-the-fetch-metadata-headers-for-a-request
Rationale
appendFetchMetadataimplements the Sec-Fetch-Mode sub-algorithm but skips step 1 of the outer algorithm, which the code marks asTODO:So
fetch('http://192.168.0.12/')sendssec-fetch-mode: cors, while browsers send noSec-Fetch-*header to plain-http, non-loopback destinations. Reproduces onmainand on the undici bundled with current Node releases.Repro (Node built-ins only)
Prints
{ 'sec-fetch-mode': 'cors' }; with this PR the header is absent.https://,http://localhostandhttp://127.0.0.1keep sending it.Changes
lib/web/fetch/util.js: early return using the existingisURLPotentiallyTrustworthy()helper on the request's current URL, so the gate applies per redirect hop (matches browsers and WPTfetch/metadata/redirect/*). TheSec-Fetch-Dest/Site/UserTODOs are untouched and will inherit the gate.test/fetch/fetch-metadata.js(new): header absent for plain-http non-loopback origins, present forhttps://and loopback, dropped/added acrosshttps -> http/http -> httpsredirects. 4 of the 7 cases fail onmain.Features
N/A
Bug Fixes
Sec-Fetch-Modeis no longer sent to URLs that are not potentially trustworthy (plainhttp:///ws://to non-loopback hosts), per Fetch Metadata step 1.Breaking Changes and Deprecations
N/A
Test results (Node 24.20.0)
npm run lintclean;test/fetch/fetch-metadata.js7/7 (4/7 fail onmain)test/fetch/*.js483/483test/mock-agent.js111/111,test/proxy-agent.js38/38,test/eventsource/eventsource-connect.js11/11 — every existing assertion on this header targetslocalhostThis fix was prepared with AI assistance; I reproduced the bug locally and reviewed every line.
Status