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.
This commit is contained in:
@@ -26,22 +26,26 @@ function getRequestContext(
|
|||||||
const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
|
const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
|
||||||
/**
|
/**
|
||||||
* *NOTE*
|
* *NOTE*
|
||||||
* If no URL is provided, the local API was called directly outside
|
* If no URL is provided, the local API was called outside
|
||||||
* the context of a request. Therefore we create a fake URL object.
|
* the context of a request. Therefore we create a fake URL object.
|
||||||
* `ts-expect-error` is used below for properties that are 'read-only'
|
* `ts-expect-error` is used below for properties that are 'read-only'.
|
||||||
* since they do not exist yet we can safely ignore the error.
|
* Since they do not exist yet we can safely ignore the error.
|
||||||
*/
|
*/
|
||||||
let urlObject
|
let urlObject: undefined | URL
|
||||||
|
|
||||||
function getURLObject() {
|
function getURLObject() {
|
||||||
if (urlObject) {
|
if (urlObject) {
|
||||||
return urlObject
|
return urlObject
|
||||||
}
|
}
|
||||||
const urlToUse = req?.url || req.payload.config?.serverURL || 'http://localhost'
|
|
||||||
|
const fallbackURL = `http://${req.host || 'localhost'}`
|
||||||
|
|
||||||
|
const urlToUse = req?.url || req.payload.config?.serverURL || fallbackURL
|
||||||
|
|
||||||
try {
|
try {
|
||||||
urlObject = new URL(urlToUse)
|
urlObject = new URL(urlToUse)
|
||||||
} catch (error) {
|
} catch (_err) {
|
||||||
urlObject = new URL('http://localhost')
|
urlObject = new URL(fallbackURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
return urlObject
|
return urlObject
|
||||||
@@ -50,20 +54,25 @@ const attachFakeURLProperties = (req: Partial<PayloadRequest>) => {
|
|||||||
if (!req.host) {
|
if (!req.host) {
|
||||||
req.host = getURLObject().host
|
req.host = getURLObject().host
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.protocol) {
|
if (!req.protocol) {
|
||||||
req.protocol = getURLObject().protocol
|
req.protocol = getURLObject().protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.pathname) {
|
if (!req.pathname) {
|
||||||
req.pathname = getURLObject().pathname
|
req.pathname = getURLObject().pathname
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.searchParams) {
|
if (!req.searchParams) {
|
||||||
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
||||||
req.searchParams = getURLObject().searchParams
|
req.searchParams = getURLObject().searchParams
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.origin) {
|
if (!req.origin) {
|
||||||
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
||||||
req.origin = getURLObject().origin
|
req.origin = getURLObject().origin
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req?.url) {
|
if (!req?.url) {
|
||||||
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
||||||
req.url = getURLObject().href
|
req.url = getURLObject().href
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@payload-config": ["./test/admin/config.ts"],
|
"@payload-config": ["./test/_community/config.ts"],
|
||||||
"@payloadcms/live-preview": ["./packages/live-preview/src"],
|
"@payloadcms/live-preview": ["./packages/live-preview/src"],
|
||||||
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
|
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
|
||||||
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],
|
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],
|
||||||
|
|||||||
Reference in New Issue
Block a user