You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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').
Will produce a request with sec-fetch-mode: navigate, cors, while it should be sec-fetch-mode: navigate.
"undici": "4.16.0"
Reacted by issuefiler, Travis Fischer, Justin Dalrymple, Anton Begehr, ghj7211, Saad Nadeem, Adam, Danilo Fuchs, Olivier Tassinari, Danny Navarro and 2 more
Either add a way to remove the default headers, for example, by setting its value to null (not "null"). It's the best choose especially if it will be a part of Node.js. (UPD: However it will not be compatible with native fetch, so no.)
Or at least remove sec-fetch-mode from the default headers (and maybe accept-language too). (For me it would be enough. Why you have even added it to default?)
Can you check what Chrome does? I think most of the behavior you are seeing is regulated by the spec.
What does Chrome have to do with it? Chrome, as a browser, also follows CORS limitations, for example.
But even Chrome:
Does not throws errors if there is "connection": "keep-alive" in headers of fetch.
Correctly set any custom sec-fetch-mode header value (with Web Extension API).
HTTP specification does not say that these headers are mandatory.
But undiciforces to use them without any way not to do.
So, removing sec-fetch-mode from default headers is most simple and the correct thing.
If someone want to emulate a browser's behaviour he will add both sec-fetch-mode and sec-fetch-dest, sec-fetch-site: none, sec-fetch-user headers. As well as sec-ch-* headers.
Why a tool that going to be a part of Node.js have too much own vision of which headers are mandatory to use?
Either add a way to remove the default headers, for example, by setting its value to null (not "null"). It's the best > choose especially if it will be a part of Node.js. (UPD: However it will not be compatible with native fetch, so no.)
Or at least remove sec-fetch-mode from the default headers
What does Chrome have to do with it? Chrome, as a browser, also follows CORS limitations, for example.
We try to match the behaviour of Chrome. Basically we use it as our reference implementation.
Does not throws errors if there is "connection": "keep-alive" in headers of fetch.
That is something we might want to revisit. It just don't make much sense to provide that header. Why do you do that? Depending on the settings of the undici client it might or might not be correct and might need to be stripped anyway if keep alive is disabled on the connection.
Correctly set any custom sec-fetch-mode header value (with Web Extension API).
I'm not sure why you think you can't do that today?
HTTP specification does not say that these headers are mandatory.
But undiciforces to use them without any way not to do.
I don't quite agree with this. You can disable the keep-alive header by disabling keep-alive and the other headers are mandated by the spec, as for as I know at least.
It's a pure browser's thing, like CORS.
Why Node.js aimed undici uses it by default (and there is no way to disable it)?
Why do you do that?
I have already written it:
It's important for code compatibility reason — since node-fetch uses connection: close by default.
And because it's a pure bug — I set the correct, valid HTTP header (even it used by default), but undici fails. Also it works without errors in the browser and in any other HTTP client.
I'm not sure why you think you can't do that today?
Regarding the "sec-fetch-mode": "navigate"... as far as I can tell the spec doesn't allow it and neither does Chrome. We could bypass the spec to make more sense in a node environment but I'm not familiar enough with these things to have confidence in making a good decision. Maybe @annevk can provide some guidance?
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).
undici is not a browser. Don't overengineer it.
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.
Reacted by Jordan Harband, Szymon Marczak, Owen Buckley, Chi Ma and Carlos Fuentes
ModHeader allows to edit and remove entirely any header (except "host", "connection", "cache-control" (on a page reload), "pragma" (on "Disable cache" with DevTools)).
fetch is a browser API.
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, with 400 - Bad Request error)
curl https://example.com/ -H 'User-Agent:' -H 'Accept:' only host header and it works with no error.
Didn't want to mean this was a bad solution (though it was faster for me to rely on the good ol' http module). My comment around the irony was for Matteo's X message about being API compliant 😅
import{createServer}from'http';import{once}from'events';constserver=createServer((req,res)=>{console.log(req.method,req.headers)res.end('Hello, world!');})server.listen(3000)awaitonce(server,'listening');console.log('Server is listening on port 3000');constres=awaitfetch('http://localhost:3000',{method: 'POST',body: JSON.stringify({hello: 'world'})})console.log(awaitres.text());server.close()
Which outputs:
Server is listening on port 3000
POST {
host: 'localhost:3000',
connection: 'keep-alive',
'content-type': 'text/plain;charset=UTF-8',
accept: '*/*',
'accept-language': '*',
'sec-fetch-mode': 'cors',
'user-agent': 'node',
'accept-encoding': 'gzip, deflate',
'content-length': '17'
}
Hello, world!
I think we should either:
add the sec-fetch-mode header only if we do have an origin via setGlobalOrigin and implement the rest of the CORS protocol based on that
completely remove the header, because we don't have an origin, and it will just confuse APIs that we are calling.
sec-fetch-mode is set to a request's mode. The mode is cors, by default, unless set in RequestInit.mode. If you set mode manually, fetch('...', { mode: 'same-origin' }) for example, you should see the value change with it.
and implement the rest of the CORS protocol based on that
this would be a fun project, but no one would use it
it will just confuse APIs that we are calling
based on the definition of the header, it's not confusing APIs -- they are rejecting requests made from fetch purposefully. In these cases, as others have mentioned, node:http/s, undici.requests, and others work fine.
http-network-cache-or-fetch step 8.13 tells us to append the Fetch metadata headers for a request, so for whatever reason we aren't appending all of the headers we are supposed to. I assume people wouldn't be happy if we ended up doing that, though.
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.
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.
We don't have an origin by default, so no request can be cross-origin
another more likely interpretation is that since there's no origin, no request can be same origin (since it's the "same origin policy", not the "cross origin policy").
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.
Reacted by Alt, Saad Nadeem, iDeBugger Freeman, BlackGlory, llamaz, Kendell and 华
Activity
ronag commentedon Mar 28, 2022
Can you check what Chrome does? I think most of the behavior you are seeing is regulated by the spec.
AlttiRi commentedon Mar 28, 2022
2 and 3 are obviously bugs.
For 1, for me:
for example, by setting its value to null (not "null"). It's the best choose especially if it will be a part of Node.js.(UPD: However it will not be compatible with nativefetch, so no.)sec-fetch-modefrom the default headers (and maybeaccept-languagetoo).(For me it would be enough. Why you have even added it to default?)
What does Chrome have to do with it? Chrome, as a browser, also follows CORS limitations, for example.
But even Chrome:
"connection": "keep-alive"inheadersoffetch.sec-fetch-modeheader value (with Web Extension API).HTTP specification does not say that these headers are mandatory.
But
undiciforces to use them without any way not to do.So, removing
sec-fetch-modefrom default headers is most simple and the correct thing.If someone want to emulate a browser's behaviour he will add both
sec-fetch-modeandsec-fetch-dest,sec-fetch-site: none,sec-fetch-userheaders. As well assec-ch-*headers.Why a tool that going to be a part of Node.js have too much own vision of which headers are mandatory to use?
ronag commentedon Mar 28, 2022
https://fetch.spec.whatwg.org/#http-network-or-cache-fetch
Step 13
https://w3c.github.io/webappsec-fetch-metadata/#sec-fetch-mode-header
https://fetch.spec.whatwg.org/#fetching
Step 13:
We try to match the behaviour of Chrome. Basically we use it as our reference implementation.
That is something we might want to revisit. It just don't make much sense to provide that header. Why do you do that? Depending on the settings of the undici client it might or might not be correct and might need to be stripped anyway if keep alive is disabled on the connection.
I'm not sure why you think you can't do that today?
I don't quite agree with this. You can disable the keep-alive header by disabling keep-alive and the other headers are mandated by the spec, as for as I know at least.
ronag commentedon Mar 28, 2022
Sorry I accidentally edited your response instead of quoting it. Sorry. Tried to restore it.
AlttiRi commentedon Mar 28, 2022
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:
AlttiRi commentedon Mar 28, 2022
Wait.
Could you show me the example?
ronag commentedon Mar 28, 2022
ronag commentedon Mar 28, 2022
Regarding the
"sec-fetch-mode": "navigate"... as far as I can tell the spec doesn't allow it and neither does Chrome. We could bypass the spec to make more sense in a node environment but I'm not familiar enough with these things to have confidence in making a good decision. Maybe @annevk can provide some guidance?ronag commentedon Mar 28, 2022
Actually we were doing it slight wrong, we should set, not append the header. 5b9acf2
ronag commentedon Mar 28, 2022
One way to do what you want would be:
But again... the spec explicitly disallows it. But maybe it would make sense to bypass that? I don't know.
AlttiRi commentedon Mar 28, 2022
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.ronag commentedon Mar 28, 2022
We need to set one of the headers. I'm not sure what you want here? #1307 PR welcome.
How? I'm curious. Can you show an example. I'm not familiar with web extension.
EDIT: Ah, yea doing it with web extensions is just a hole to get around the spec.
ronag commentedon Mar 28, 2022
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.AlttiRi commentedon Mar 28, 2022
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.29 remaining items
mjesun commentedon Dec 4, 2024
Didn't want to mean this was a bad solution (though it was faster for me to rely on the good ol'
httpmodule). My comment around the irony was for Matteo's X message about being API compliant 😅ronag commentedon Dec 4, 2024
You clearly don't care about isomorphic. Why not just use the better and faster undici.request API?
The only reason to use fetch is to make code work both in node and browser.
mcollina commentedon Dec 5, 2024
@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 thatKhafraDev commentedon Dec 5, 2024
http-network-cache-or-fetch step 8.13 tells us to append the Fetch metadata headers for a request, so for whatever reason we aren't appending all of the headers we are supposed to. I assume people wouldn't be happy if we ended up doing that, though.
sec-fetch-modeis set to a request's mode. The mode iscors, by default, unless set in RequestInit.mode. If you set mode manually,fetch('...', { mode: 'same-origin' })for example, you should see the value change with it.this would be a fun project, but no one would use it
based on the definition of the header, it's not confusing APIs -- they are rejecting requests made from fetch purposefully. In these cases, as others have mentioned, node:http/s, undici.requests, and others work fine.
mcollina commentedon Dec 6, 2024
My point is that we are not doing point 8.12 (appending
Origin) and based on https://fetch.spec.whatwg.org/#cors-request, doing 8.13 doesn't seem correct to me.KhafraDev commentedon Dec 6, 2024
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.
ljharb commentedon Dec 6, 2024
another more likely interpretation is that since there's no origin, no request can be same origin (since it's the "same origin policy", not the "cross origin policy").
adam-arthur commentedon Dec 6, 2024
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.
ronag commentedon Dec 6, 2024
@adam-arthur I ask that you to self moderate your last comment. While I understand your frustration it does not contribute anything positivie.
ronag commentedon Dec 6, 2024
I believe most of this should be handled in https://github.com/wintercg/fetch
KhafraDev commentedon Dec 27, 2024
Then the reddit API is not blocking the request based (solely) on the sec-fetch-mode header. Please open a separate issue.
fix(fetch): only send Sec-Fetch-Mode to potentially trustworthy URLs (#…
fix(fetch): honor user-provided Sec-Fetch-Mode (fixes nodejs#1305)
feat(html): a request says how it was made, in Sec-Fetch-Site, -Mode,…
feat(html): a page's fetch goes through the HTTP cache, with its cach…