Skip to content
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

don't append empty origin #3335

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,13 @@ function appendRequestOriginHeader (request) {
// TODO: implement "byte-serializing a request origin"
let serializedOrigin = request.origin

// "'client' is changed to an origin during fetching."
// This doesn't happen in undici (in most cases) because undici, by default,
// has no concept of origin.
if (serializedOrigin === 'client') {
// - "'client' is changed to an origin during fetching."
// This doesn't happen in undici (in most cases) because undici, by default,
// has no concept of origin.
// - request.origin can also be set to request.client.origin (client being
// an environment settings object), which is undefined without using
// setGlobalOrigin.
if (serializedOrigin === 'client' || serializedOrigin === undefined) {
return
}

Expand Down
27 changes: 27 additions & 0 deletions test/fetch/issue-3334.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const { once } = require('node:events')
const { createServer } = require('node:http')
const { fetch } = require('../..')

test('a non-empty origin is not appended (issue #3334)', async (t) => {
const { strictEqual } = tspl(t, { plan: 1 })
const origin = 'https://origin.example.com'

const server = createServer((req, res) => {
strictEqual(req.headers.origin, origin)
res.end()
}).listen(0)

t.after(server.close.bind(server))
await once(server, 'listening')

await fetch(`http://localhost:${server.address().port}`, {
headers: { origin },
body: '',
method: 'POST',
redirect: 'error'
})
})
Loading