Skip to content

Can't remove default headers. Can't overwrite "sec-fetch-mode", "connection" headers. #1305

Open

Description

@AlttiRi
  • 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"

Activity

  1. added
    bug
    on Mar 28, 2022
  2. ronag commented on Mar 28, 2022

    @ronag
    Member

    Can you check what Chrome does? I think most of the behavior you are seeing is regulated by the spec.

  3. AlttiRi commented on Mar 28, 2022

    @AlttiRi
    Author

    2 and 3 are obviously bugs.

    For 1, for me:

    • 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 undici forces 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?

  4. ronag commented on Mar 28, 2022

    @ronag
    Member

    2 and 3 are obviously bugs.

    For 1, for me:

    • 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

    https://fetch.spec.whatwg.org/#http-network-or-cache-fetch

    Step 13

    Append the Fetch metadata headers for httpRequest. [FETCH-METADATA]

    https://w3c.github.io/webappsec-fetch-metadata/#sec-fetch-mode-header

    (and maybe accept-language too).

    https://fetch.spec.whatwg.org/#fetching

    Step 13:

    If request’s header list does not contain Accept-Language, then user agents should append (`Accept-Language, an appropriate header value) to request’s header list.


    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 undici forces 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.

  5. ronag commented on Mar 28, 2022

    @ronag
    Member

    Sorry I accidentally edited your response instead of quoting it. Sorry. Tried to restore it.

  6. AlttiRi commented on Mar 28, 2022

    @AlttiRi
    Author

    So, what about "sec-fetch-mode"?

    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?

    Because of the bug №3:

    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.

  7. AlttiRi commented on Mar 28, 2022

    @AlttiRi
    Author

    You can disable the keep-alive header by disabling keep-alive and the other headers

    Wait.
    Could you show me the example?

  8. ronag commented on Mar 28, 2022

    @ronag
    Member

    You can disable the keep-alive header by disabling keep-alive and the other headers

    Wait. Could you show me the example?

    setGlobalDispatcher(new Agent({ pipelining: 0 }))
  9. ronag commented on Mar 28, 2022

    @ronag
    Member

    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?

  10. ronag commented on Mar 28, 2022

    @ronag
    Member

    Actually we were doing it slight wrong, we should set, not append the header. 5b9acf2

  11. ronag commented on Mar 28, 2022

    @ronag
    Member

    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.

  12. AlttiRi commented on Mar 28, 2022

    @AlttiRi
    Author

    setGlobalDispatcher(new Agent({ pipelining: 0 }))

    It just changes connection: keep-alive to connection: close.

    It does not remove any default header.


    neither does Chrome.

    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:

    • CORS,
    • CORB,
    • changing/removing "sec-fetch-*", <--
    • reading "cookies" HTTP header
    • and other things.

    undici is not a browser. Don't overengineer it.

  13. ronag commented on Mar 28, 2022

    @ronag
    Member

    It does not remove any default header.

    We need to set one of the headers. I'm not sure what you want here? #1307 PR welcome.

    As I already said, Chrome allows to do it with web extension.

    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.

  14. ronag commented on Mar 28, 2022

    @ronag
    Member

    undici is not a browser. Don't overengineer it.

    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.

  15. AlttiRi commented on Mar 28, 2022

    @AlttiRi
    Author

    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.

  16. 29 remaining items

  17. mjesun commented on Dec 4, 2024

    @mjesun

    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 😅

  18. ronag commented on Dec 4, 2024

    @ronag
    Member

    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.

  19. mcollina commented on Dec 5, 2024

    @mcollina
    SponsorMember

    From the spec, it seems to be functioning as expected.

    @KhafraDev I don't think so. I think setting it to cors is incorrect because in https://fetch.spec.whatwg.org/#cors-request it's defined that a CORS Request one that includes an Origin header, and we aren't (because we do not have one).

    import { createServer } from 'http';
    import { once } from 'events';
    
    const server = createServer((req, res) => {
      console.log(req.method, req.headers)
      res.end('Hello, world!');
    })
    
    server.listen(3000)
    await once(server, 'listening');
    
    console.log('Server is listening on port 3000');
    
    const res = await fetch('http://localhost:3000', {
      method: 'POST',
      body: JSON.stringify({ hello: 'world' })
    })
    
    console.log(await res.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:

    1. 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
    2. completely remove the header, because we don't have an origin, and it will just confuse APIs that we are calling.
  20. KhafraDev commented on Dec 5, 2024

    @KhafraDev
    Member

    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-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.

  21. mcollina commented on Dec 6, 2024

    @mcollina
    SponsorMember

    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.

    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.

  22. KhafraDev commented on Dec 6, 2024

    @KhafraDev
    Member

    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:

    If the request is cross-origin then it will use the Cross-Origin Resource Sharing (CORS) mechanism

    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.

    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.

    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.

  23. ljharb commented on Dec 6, 2024

    @ljharb
    SponsorMember

    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").

  24. adam-arthur commented on Dec 6, 2024

    @adam-arthur

    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.

  25. ronag commented on Dec 6, 2024

    @ronag
    Member

    That this thread has been ongoing for years at this point is truly embarrassing.

    @adam-arthur I ask that you to self moderate your last comment. While I understand your frustration it does not contribute anything positivie.

  26. ronag commented on Dec 6, 2024

    @ronag
    Member
  27. KhafraDev commented on Dec 27, 2024

    @KhafraDev
    Member

    it's not working on my machine

    Then the reddit API is not blocking the request based (solely) on the sec-fetch-mode header. Please open a separate issue.

  28. added a commit that references this issue on Nov 3, 2026
    eb483d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions