undici: Can't remove default headers. Can't overwrite `"sec-fetch-mode"`, `"connection"` headers.
- I can’t remove any default header.
- Setting
"connection": "keep-alive"throws an error. - I can’t overwrite default
"sec-fetch-mode"header correctly.
1. I can’t remove the default headers
I can’t remove the default headers: host, connection, accept, accept-encoding, accept-language, sec-fetch-mode, user-agent.
For example, curl allows to remove any header (even host header):
curl http://127.0.0.1:3000 -H 'User-Agent:' -H 'Accept:' -H 'Host:'
And yes, it works.
Using of "" will send "" as the header’s value, using of null will send "null" as the header’s value.
2. "connection": "keep-alive"
Also, if I manually set connection: "keep-alive" I get the error (code: 'UND_ERR_INVALID_ARG').
const resp = await fetch(url, {
headers: {
"connection": "keep-alive"
}
});
It’s important for code compatibility reason — since node-fetch uses connection: close by default.
3. "sec-fetch-mode"
It uses "sec-fetch-mode": "cors" by default.
As I said above, I can’t remove this header (as well as any other default header).
More over, I can’t change it correctly.
For example, using of:
await fetch(url, {
headers: {
"sec-fetch-mode": "navigate"
}
});
Will produce a request with sec-fetch-mode: navigate, cors, while it should be sec-fetch-mode: navigate.
"undici": "4.16.0"
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 49 (32 by maintainers)
Commits related to this issue
- fix: add workaround for nodejs/undici#1305 required to remove sec-fetch-mode header — committed to oplik0/NodeBB by oplik0 3 years ago
- fix: add workaround for nodejs/undici#1305 required to remove sec-fetch-mode header — committed to NodeBB/NodeBB by oplik0 3 years ago
- fix native fetch error in server-test on node 18 (#1341) Summary: Fixing node 18 fetch error due to attempt to set the "Connection" header which is not supported on that version of node (https://gith... — committed to facebook/metro by vzaidman 2 years ago
Enforcing CORS/browser specific rules in a serverside request context is non-sensical. These rules exist in browsers for security reasons to protect the user.
Fine to default to them if the spec requires that, but there definitely needs to be a mechanism to override the default headers. Core constructs should be configurable to enable a wide variety of use cases.
I can simply use http or curl to set whatever headers I want. But why would we handicap the standard lib and require dropping into lower level constructs for this? There is 0 security benefit to having this in fetch for serverside contexts.
Having a node specific mechanism to override default headers does not violate the fetch spec/api or prevent isomorphic fetch code.
I agree that this should be reopened.
I just spent an entire day narrowing in on why my proxy requests were returning 400, but working with node-fetch. Turns out removing the sec-fetch-mode (or changing it to navigate) fixed my issue.
Then what’s the point? The only reason for using this lib is because its now native.
You can get around this whole thing with something like the following:
Such a weird goal to be 100% spec compliant for a browser fetch.
CORS is a spec designed explicitly for browser security to protect normal users.
It has no relevance on the Server. None at all.
No server application will ever desire or get any benefit from adding CORS headers to requests (aside from testing that CORS is configured correctly on a target server)
You are also violating the CORS spec by not providing an Origin. And no Origin exists, because… you aren’t in a browser context!
What is the Origin of a one off node.js script?
Do the people maintaining this feature not understand the purpose of CORS, or what?
CORS handshaking is not even possible or initiated without the Origin header
That this thread has been ongoing for years at this point is truly embarrassing.
Could this be reopened? It was incorrectly closed by #1829, which only addresses
connection.sec-fetch-modestill can’t be unset or changed, as far as I can tell.You know, there are people out there asking Node to do all these things (like CORS support) and some server runtimes like Deno with the concept of an origin are in fact implementing some of this stuff.
So this is not as out of the question as you may assume.
Also, please avoid sarcasm/snark in the tracker. We don’t know you well (yet) and it’s super easy to take things the wrong way in these situations without a lot of prior context (which we don’t have yet!) and it’s important to me that your contribution (feedback) on what node does here doesn’t get overlooked because of that.
That said I tend to agree node’s fetch should allow overriding these things unlike the browser’s. I also absolutely agree with Robert it’s not clear cut. I can ask in a forum of server runtimes I participate in but if you want to move this ahead it’d be great to do what server runtimes that implement fetch do (Deno/cloudflare/shopify etc) as well as what user-land implementations (like node-fetch) do.
Easier said than done. Knowing what parts of the spec to ignore or not is not that easy. To keep things simple with the limited development time we have (I’m doing this work for free) it’s much easier to just follow the spec verbatim, i.e. the aim here is to match the browser as closely as possible and not bike-shed over what is right or wrong; the spec/Chrome is right (with few well discussed exceptions).
fetch is a browser API. If you don’t want/need the browser behavior then don’t use the fetch API. We have much better API’s for node, e.g.
undici.request.We don’t append an origin header because we don’t have an origin, we append a sec-fetch-mode because we have a request mode. If you mean that we don’t implement cors, then I think it might* apply.
MDN’s definition for mode: ‘cors’ is:
The if is doing a lot of carrying here. It’s not saying that we will be respecting CORs, it’s saying that if the request is cross origin then we will. We don’t have an origin by default, so no request can be cross-origin, but the header is still valid in this case.
If someone set an origin header, would we append a
sec-fetch-mode? The spec doesn’t account for environments that don’t implement forbidden headers (someone can set an origin header manually in node). The existence of an origin header doesn’t automatically infer support for CORs either.From my interpretation of the spec, we’re doing things correctly and the issues people are experiencing with servers is the header serving its purpose.
Yeah, I also would like if Node.js have some permission list in
package.jsonlike it is in Web Extensions, Android Applications: My comment in the topic about the recentRIAEvangelist/node-ipccase.While
sec-fetch-*contains “fetch” word, it’s not a part of Fetch API.sec-fetch-*headers were added in browsers much later thanFetch API. The modern browser sendsec-fetch-*headers with any request, not only with XHR made withfetchfunction.Also, why it sends
sec-fetch-mode: cors?What if I do a request to the server from the same process where I host that HTTP server? Is it still “cors”? What even CORS means in a Node.js application? What about other
sec-fetch-*headers? The browsers currently send 3-4 “sec-fetch” headers with every request.I see default adding
sec-fetch-mode: corsheader is meaningless, and even annoying if I want to simulate a fetch request from a browser before 2019.08 (Chrome 76).If any one want to simulate a request from the modern browser (with absolutely the same headers, and the same header’s order), he can (and should) add all required headers by yourself.
About manual removing the other default headers:
Somehow. It can be a property with the array of string (header names) which should not be added by default, for example.
UPD (2022.04.08):
For example, to export a
setDefaultHeadersfunction. It would pretty convenient. Note, it would have the global effect. (As well as usingsetGlobalDispatchercurrently)I think for people Fetch API is about “promises” and “streaming”, it’s about
Request,Response,ReadableStreamandHeadersobjects. But it’s not about which headers a browser adds.Here are headers of
fetchmade withDeno:
Bun:
Node.js:
What is the practical meaning of adding
"sec-fetch-mode: cors"header?As I can see from the posts above, this only has disadvantages.
I don’t know about Cloudflare Workers, but Deno’s
fetchdefault headers are:That’s all. That looks fine. Need additional headers? Add them by yourself.
Also look at the headers order.
While the article about
sec-fetch-*names “Fetch Metadata Request Headers”, but. It’s not about only Fetch API requests. It’s about every request in browsers, made from https origin: navigate (address bar and link clicks), parser (script/image loading), xhr, fetch.https://fetch.spec.whatwg.org/#http-network-or-cache-fetch is also written in mind to be for use in browsers.
Using in Node.js
sec-fetch-*by default without way to disable it for what? It’s meaningless. Node.js applications don’t have origin, so it’s not possible to say that some request was “cors” (cross-origin), or “same-site”. Node.js applications don’t have navigation bar, globallocationobject, it’s not possible to say what some request was “navigate”.I like Fetch API (streaming and convenient), but not the browsers limitations (with the corresponding additional “features”). Separate them.
I need a Node.js tool to do network requests with well know and convenient API. I think, people that asked to add fetch to undici also meant that. A Fetch API compatible function.
BTW,
response.bodyofundiciis “async iterable” (I can use it infor-await-of), while the native one is not. And it’s okay.Mimic browsers requests by adding additional headers (
sec-fetch-*,sec-ch-ua-*,upgrade-insecure-requests,dnt) (and reordering of them*) is more work for a plugin/other lib that will modify the headers object the appropriate way.* Wait, I can’t reorder them. It looks that
undicijust sorts them alphabetically (afterhostandconnectionheaders). Neither Chrome, not Firefox don’t do that. Both browsers use a custom headers order.In particular, it’s the addiction case why I need to set
"connection": "keep-alive"manually. To reorder the headers.It’s in the fetch spec.
Because the spec says so.
We haven’t fully implemented the spec yet so the other headers are missing.
We only target latest version of spec. Old browsers are out of scope.
PR welcome
You a maybe right but that is currently not the priority here. We have chosen to focus on fully implementing the spec. I don’t know enough about Cors and what not the determine what is relevant or not. If you have any specific, constructive and articulated suggestions we are open to discuss them. Even better open a PR.
@adam-arthur I ask that you to self moderate your last comment. While I understand your frustration it does not contribute anything positivie.
@KhafraDev I don’t think so. I think setting it to
corsis incorrect because in https://fetch.spec.whatwg.org/#cors-request it’s defined that aCORS Requestone that includes anOriginheader, and we aren’t (because we do not have one).Which outputs:
I think we should either:
sec-fetch-modeheader only if we do have an origin viasetGlobalOriginand implement the rest of the CORS protocol based on thatI’ve never been in full agreement of this; there was a thread years ago I think by @.mscdex explaining why Node.js should provide primitives -
httpmodule- and not complex functions -fetch-, which better reflected my line of thought. But now thatfetchis added to the core APIs, I agree the best would be to make the function as useful as possible.I also agree with the fact that people requesting
fetchin Node.js were really meaning the API (especially theResponseobject and the ability ofawaiting the request) and not the behaviors inherited from the browser security model.I think the irony here is that the dispatcher API isn’t WHATWG standard. It feels more like a patch to allow tweaking an API thought for frontend environments. Given that
fetchhas ways to add, but not remove headers, the default set of headers should be the absolute minimum required by the standard of an HTTP request, not the minimum required by a browser request; and then people can add required headers if needed.and despite the title of the PR,
sec-fetch-modecan be overwritten with the dispatcher api (there is an example in one of the previous comments)This is a contradiction, yet sums up what people want perfectly.
I wish Node.js’
fetchwere freer (“more capable”) than browser’s, while being compatible with browser’s.This is
globalThis.fetch, which has been shipped into Node.js. I want to fully utilize Node.js’ capability using the elegant dependency-less API,fetch.Nothing fancy is needed; just a few less-restrictive options to customize requests, the spec being the default. So that I don’t need to introduce any unnecessary dependencies (“external packages”) that do the same thing as what Node.js’
fetchdoes.It just changes
connection: keep-alivetoconnection: close.It does not remove any default header.
As I already said, Chrome allows to do it with web extension.
Why it’s not possible to do from a web page context? Because it’s a browser. Web browsers have a lot of limitations.
Browsers do not allow:
"cookies"HTTP headerundiciis not a browser. Don’t overengineer it.ModHeader allows to edit and remove entirely any header (except
"host","connection","cache-control"(on a page reload),"pragma"(on “Disable cache” with DevTools)).So, I assume the next things will be implementing of CORS, CORB limitations, restricting of the access to
"cookie"header since it’s a browser API? [sarcasm]I want to use Fetch API because it’s well know and pretty convenient API.
Since it’s a Node.js library I expect that it will have no limitations/additional headers which browsers applies due to security reason. In particular, no
sec-fetch-*headers.Or just add a way to remove the default headers.
With some option for
setGlobalDispatcher, for example.curl http://example.com/ -H 'User-Agent:' -H 'Accept:' -H 'Host:'no one header, but it works (However, with400 - Bad Requesterror)curl https://example.com/ -H 'User-Agent:' -H 'Accept:'onlyhostheader and it works with no error.So, what about
"sec-fetch-mode"?It’s a pure browser’s thing, like CORS. Why Node.js aimed
undiciuses it by default (and there is no way to disable it)?I have already written it:
And because it’s a pure bug — I set the correct, valid HTTP header (even it used by default), but
undicifails. Also it works without errors in the browser and in any other HTTP client.Because of the bug №3: