Skip to content

Commit

Permalink
fetch: set referrer properly (nodejs#2125)
Browse files Browse the repository at this point in the history
* fetch: set referrer properly

Fixes nodejs#2124

* fix: origin is not a url
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent e734107 commit aa3dd7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,18 @@ class Request {
}

// 3. If one of the following is true
// parsedReferrer’s cannot-be-a-base-URL is true, scheme is "about",
// and path contains a single string "client"
// parsedReferrer’s origin is not same origin with origin
// - parsedReferrer’s scheme is "about" and path is the string "client"
// - parsedReferrer’s origin is not same origin with origin
// then set request’s referrer to "client".
// TODO

// 4. Otherwise, set request’s referrer to parsedReferrer.
request.referrer = parsedReferrer
if (
(parsedReferrer.protocol === 'about:' && parsedReferrer.hostname === 'client') ||
(origin && !sameOrigin(parsedReferrer, this[kRealm].settingsObject.baseUrl))
) {
request.referrer = 'client'
} else {
// 4. Otherwise, set request’s referrer to parsedReferrer.
request.referrer = parsedReferrer
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,15 @@ test('set-cookie headers get cleared when passing a Request as first param', (t)
t.end()
})

// https://github.com/nodejs/undici/issues/2124
test('request.referrer', (t) => {
for (const referrer of ['about://client', 'about://client:1234']) {
const request = new Request('http://a', { referrer })

t.equal(request.referrer, 'about:client')
}

t.end()
})

teardown(() => process.exit())

0 comments on commit aa3dd7c

Please sign in to comment.