Skip to content

Conversation

@abdulfarid-dev
Copy link

Title: fix(http): allow undefined value for Location header

Description:
This patch fixes an issue where setting the "Location" header to undefined on a ServerResponse
would throw TypeError [ERR_HTTP_INVALID_HEADER_VALUE]. According to the official type definition,
the location header can be undefined:

    location?: string | undefined;

Previously, the Node.js HTTP implementation did not allow this, causing unexpected errors when
developers tried to set the header to undefined. This fix ensures that if the Location header
value is undefined, it is automatically converted to an empty string before being set.

Notes:

  • Local tests have been created in test-location.js to verify the fix.
  • No other headers are affected.
  • Confirmed working on Node.js versions 20 through 23 on Linux.

Test:
Run node test-location.js locally to verify that setting Location to undefined does not throw.

Refs: #60418
CC: @ericmorand

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Nov 2, 2025
Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tho adds an unexpected special case. I would rather say that the type definitions are incorrect. Also as far as I know we do not have any "official" type definitions.

@abdulfarid-dev
Copy link
Author

make sense that adding a special case for Location isn’t ideal. Node.js expects headers to always be valid strings. So, is it okay to remove the special case and restored the general setHeader behavior to keep it consistent with other headers.
Would love to have your thoughts on this ?

@ericmorand
Copy link

@abdulfarid-dev I think the problem can be summarized as:

Is {foo: undefined} loosely equal to {} - i.e. is the absence of a property the same as its presence with a value set to undefined?

I don't have a definitive answer. Node.js assumes that the answer is no; TypeScript type-checker assumes that the answer is yes:

interface Headers {
  foo?: string;
}

const headers1: Headers = {}; // valid
const headers2: Headers = {foo: undefined}; // also valid

My feeling is that everybody expects that the answer is yes, but I don't have a strong opinion about that, and I think the opposite is as much legit.

Maybe some ECMAScript maintainers can answer - or maybe the answer is in the specification themselves and I missed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants