Skip to content

Commit 8d4e7f5

Browse files
authored
fix: filter payload- cookies in getExternalFile only if the URL is external (#13475)
Fixes a regression from #13215. Fixes the issue when `skipSafeFetch: true` is set #13146 (comment) This PR makes it so we still send the cookies if we do `fetch` to our server, but filter them when we `fetch` to an external server (usually a third party storage, for which we don't want to expose those cookies)
1 parent b426052 commit 8d4e7f5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/payload/src/uploads/getExternalFile.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@ type Args = {
1313
export const getExternalFile = async ({ data, req, uploadConfig }: Args): Promise<File> => {
1414
const { filename, url } = data
1515

16+
let trimAuthCookies = true
1617
if (typeof url === 'string') {
1718
let fileURL = url
1819
if (!url.startsWith('http')) {
20+
// URL points to the same server - we can send any cookies safely to our server.
21+
trimAuthCookies = false
1922
const baseUrl = req.headers.get('origin') || `${req.protocol}://${req.headers.get('host')}`
2023
fileURL = `${baseUrl}${url}`
2124
}
2225

26+
let cookies = (req.headers.get('cookie') ?? '').split(';')
27+
28+
if (trimAuthCookies) {
29+
cookies = cookies.filter(
30+
(cookie) => !cookie.trim().startsWith(req.payload.config.cookiePrefix),
31+
)
32+
}
33+
2334
const headers = uploadConfig.externalFileHeaderFilter
2435
? uploadConfig.externalFileHeaderFilter(Object.fromEntries(new Headers(req.headers)))
2536
: {
26-
cookie:
27-
req.headers
28-
.get('cookie')
29-
?.split(';')
30-
.filter((cookie) => !cookie.trim().startsWith(req.payload.config.cookiePrefix))
31-
.join(';') || '',
37+
cookie: cookies.join(';'),
3238
}
3339

3440
// Check if URL is allowed because of skipSafeFetch allowList

0 commit comments

Comments
 (0)