Skip to content

Commit a65289c

Browse files
authored
fix: ensures req.origin includes port on localhost (#11454)
The `req.origin` property on the `PayloadRequest` object does not include the port when running on localhost, a requirement of the [HTML Living Standard](https://html.spec.whatwg.org/#origin). This was because we were initializing the url with a fallback of `http://localhost` (no port). When constructed via `new URL()`, the port is unable to be extracted. This is fixed by using the `host` property off the headers object, if it exists, which includes the port. Partial fix for #11448.
1 parent 4a1b749 commit a65289c

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

packages/payload/src/utilities/createLocalReq.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,26 @@ function getRequestContext(
2626
const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
2727
/**
2828
* *NOTE*
29-
* If no URL is provided, the local API was called directly outside
29+
* If no URL is provided, the local API was called outside
3030
* the context of a request. Therefore we create a fake URL object.
31-
* `ts-expect-error` is used below for properties that are 'read-only'
32-
* since they do not exist yet we can safely ignore the error.
31+
* `ts-expect-error` is used below for properties that are 'read-only'.
32+
* Since they do not exist yet we can safely ignore the error.
3333
*/
34-
let urlObject
34+
let urlObject: undefined | URL
3535

3636
function getURLObject() {
3737
if (urlObject) {
3838
return urlObject
3939
}
40-
const urlToUse = req?.url || req.payload.config?.serverURL || 'http://localhost'
40+
41+
const fallbackURL = `http://${req.host || 'localhost'}`
42+
43+
const urlToUse = req?.url || req.payload.config?.serverURL || fallbackURL
44+
4145
try {
4246
urlObject = new URL(urlToUse)
43-
} catch (error) {
44-
urlObject = new URL('http://localhost')
47+
} catch (_err) {
48+
urlObject = new URL(fallbackURL)
4549
}
4650

4751
return urlObject
@@ -50,20 +54,25 @@ const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
5054
if (!req.host) {
5155
req.host = getURLObject().host
5256
}
57+
5358
if (!req.protocol) {
5459
req.protocol = getURLObject().protocol
5560
}
61+
5662
if (!req.pathname) {
5763
req.pathname = getURLObject().pathname
5864
}
65+
5966
if (!req.searchParams) {
6067
// @ts-expect-error eslint-disable-next-line no-param-reassign
6168
req.searchParams = getURLObject().searchParams
6269
}
70+
6371
if (!req.origin) {
6472
// @ts-expect-error eslint-disable-next-line no-param-reassign
6573
req.origin = getURLObject().origin
6674
}
75+
6776
if (!req?.url) {
6877
// @ts-expect-error eslint-disable-next-line no-param-reassign
6978
req.url = getURLObject().href

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
],
3333
"paths": {
34-
"@payload-config": ["./test/admin/config.ts"],
34+
"@payload-config": ["./test/_community/config.ts"],
3535
"@payloadcms/live-preview": ["./packages/live-preview/src"],
3636
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
3737
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],

0 commit comments

Comments
 (0)