-
Notifications
You must be signed in to change notification settings - Fork 539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't remove default headers. Can't overwrite "sec-fetch-mode"
, "connection"
headers.
#1305
Comments
Can you check what Chrome does? I think most of the behavior you are seeing is regulated by the spec. |
2 and 3 are obviously bugs. For 1, for me:
What does Chrome have to do with it? Chrome, as a browser, also follows CORS limitations, for example.
HTTP specification does not say that these headers are mandatory. So, removing If someone want to emulate a browser's behaviour he will add both Why a tool that going to be a part of Node.js have too much own vision of which headers are mandatory to use? |
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. |
Sorry I accidentally edited your response instead of quoting it. Sorry. Tried to restore it. |
So, what about It's a pure browser's thing, like CORS.
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
Because of the bug №3:
|
Wait. |
setGlobalDispatcher(new Agent({ pipelining: 0 })) |
Regarding the |
Actually we were doing it slight wrong, we should set, not append the header. 5b9acf2 |
One way to do what you want would be: await fetch(url, {
mode: 'navigate'
}); But again... the spec explicitly disallows it. But maybe it would make sense to bypass that? I don't know. |
It just changes 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:
|
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. |
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. |
ModHeader allows to edit and remove entirely any header (except
So, I assume the next things will be implementing of CORS, CORB limitations, restricting of the access to 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 Or just add a way to remove the default headers. With some option for
|
That's kind of non-standard. I wouldn't use that as a point of reference.
Then you kind of have to live with its limitations. You are kind of asking for fetch but not quite fetch.
Feel free to open PR with a suggestion on how that would look/work. |
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. |
Yeah, I also would like if Node.js have some permission list in While Also, why it sends
What if I do a request to the server from the same process where I host that HTTP server? Is it still "cors"? I see default adding 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 I think for people Fetch API is about "promises" and "streaming", it's about |
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. |
Those are called policies and already exist in Node.js! if you'd like to work on them (improving them etc) please do! They are mostly blocked on feedback and people actually using them to make progress. |
I don't know about Cloudflare Workers, but Deno's
That's all. Also look at the headers order. While the article about 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 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. BTW, Mimic browsers requests by adding additional headers ( * Wait, I can't reorder them. It looks that In particular, it's the addiction case why I need to set |
Just FYI
We are going to discuss the sorting in: #1309 (comment) |
I wish Node.js’ This is 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’ |
@issuefiler essentially you want a non-spec compliant The goal for |
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. |
Could this be reopened? It was incorrectly closed by #1829, which only addresses |
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. |
Summary: Fixing node 18 fetch error due to attempt to set the "Connection" header which is not supported on that version of node (nodejs/undici#1305). However "Connection" is set to "close" by default in node 18 anyway comparing with later version so we just don't touch it on that version. Pull Request resolved: #1341 Test Plan: `yarn jest packages/metro/src/integration_tests/__tests__/server-test.js` Tested ad hoc on Node JS versions: * `18.0.0` * `18.14.1` * `18.14.2` Plus the versions in the github actions Reviewed By: robhogan Differential Revision: D62026647 Pulled By: vzaidman fbshipit-source-id: fe99cfd0a7cdedc0119b1c68c38125fe464b4742
"connection": "keep-alive"
throws an error."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 (evenhost
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 ofnull
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'
).It's important for code compatibility reason — since
node-fetch
usesconnection: 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:
Will produce a request with
sec-fetch-mode: navigate, cors
, while it should besec-fetch-mode: navigate
."undici": "4.16.0"
The text was updated successfully, but these errors were encountered: