Skip to content

Commit

Permalink
don't append empty origin (#3335)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jun 17, 2024
1 parent 981fb3d commit 1573f69
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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'
})
})

0 comments on commit 1573f69

Please sign in to comment.