Skip to content

Commit

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

Fixes #2124

* fix: origin is not a url
  • Loading branch information
KhafraDev committed May 15, 2023
1 parent 09e9068 commit f5f7c18
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:https://client', 'about:https://client:1234']) {
const request = new Request('http:https://a', { referrer })

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

t.end()
})

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

0 comments on commit f5f7c18

Please sign in to comment.