-
-
Notifications
You must be signed in to change notification settings - Fork 673
fix: correctly handle multi-value headers in fetch when rawHeaders contains array
#4390
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
Open
hexchain
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
hexchain:fix-4389
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| const { describe, test } = require('node:test') | ||
| const assert = require('node:assert') | ||
| const { once } = require('node:events') | ||
| const { createServer: createHttpServer } = require('node:http') | ||
| const { createServer: createSocketServer } = require('node:net') | ||
| const { fetch, Agent } = require('..') | ||
|
|
||
| describe('Interceptor should not break multi-value headers', () => { | ||
| test('Interceptor should not break Set-Cookie', async (t) => { | ||
| const server = createHttpServer({ joinDuplicateHeaders: true }, async (_req, res) => { | ||
| const headers = [ | ||
| ['set-cookie', 'a=1'], | ||
| ['set-cookie', 'b=2'], | ||
| ['set-cookie', 'c=3'] | ||
| ] | ||
| res.writeHead(200, headers) | ||
| res.end() | ||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
| t.after(() => server.close()) | ||
|
|
||
| const dispatcher = new Agent().compose((dispatch) => dispatch) | ||
|
|
||
| const { headers } = await fetch(`http://localhost:${server.address().port}`, { dispatcher }) | ||
| assert.deepStrictEqual(headers.getSetCookie(), ['a=1', 'b=2', 'c=3']) | ||
| }) | ||
|
|
||
| test('Interceptor should not break other multi-value header', async (t) => { | ||
| const server = createSocketServer((socket) => { | ||
| socket.write('HTTP/1.0 204 No Content\r\n') | ||
| socket.write('X-Test-Header: 1\r\n') | ||
| socket.write('X-Test-Header: 2\r\n') | ||
| socket.write('X-Test-Header: 3\r\n') | ||
| socket.end('\r\n\r\n') | ||
| }).listen(0) | ||
| t.after(() => server.close()) | ||
| await once(server, 'listening') | ||
|
|
||
| const dispatcher = new Agent().compose((dispatch) => dispatch) | ||
|
|
||
| const { headers } = await fetch(`http://localhost:${server.address().port}`, { dispatcher }) | ||
| assert.deepStrictEqual(headers.get('x-test-header'), '1, 2, 3') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this only breaks given an interceptor, I'd prefer that we fix it within
undici's rather than fetchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea is that fetch should properly support arrays of header values in
rawHeaders, if such case is valid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but I'd prefer to cross check with the spec to see what it has to say regarding duplicated headers, if the implementation already aligns, most likely is an
undiciissue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, RFC 7230 Section 3.2.2 means that:
Set-Cookie,Set-Cookie.I'd say it is fine to have them separated in Undici/interceptor APIs, and only join them in fetch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You refer to RFC7230 but not the fetch spec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that HTTP-network fetch doesn't really specify how headers should be treated while being received? It merely says:
in which "[HTTP]" refers to RFC9110. In that document, section 5.2 and 5.3 are basically a rephrase of RFC7230 section 3.2.2.
In
undici, the relevant code is inhttpNetworkFetch->dispatch->onHeaders, and I believe is an implementation detail. IIUC, changes to this part of the code should count as fixing the issue in undici.