Skip to content

Commit 46699ec

Browse files
authored
test: skip cookies filter for internal URLs in getExternalFile (#13476)
Test for #13475
1 parent cdd90f9 commit 46699ec

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

test/uploads/int.spec.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import type { AddressInfo } from 'net'
12
import type { CollectionSlug, Payload } from 'payload'
23

34
import { randomUUID } from 'crypto'
45
import fs from 'fs'
6+
import { createServer } from 'http'
57
import path from 'path'
6-
import { _internal_safeFetchGlobal, getFileByPath } from 'payload'
8+
import { _internal_safeFetchGlobal, createPayloadRequest, getFileByPath } from 'payload'
79
import { fileURLToPath } from 'url'
810
import { promisify } from 'util'
911

1012
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
1113
import type { Enlarge, Media } from './payload-types.js'
1214

15+
// eslint-disable-next-line payload/no-relative-monorepo-imports
16+
import { getExternalFile } from '../../packages/payload/src/uploads/getExternalFile.js'
1317
import { initPayloadInt } from '../helpers/initPayloadInt.js'
1418
import { createStreamableFile } from './createStreamableFile.js'
1519
import {
@@ -632,6 +636,50 @@ describe('Collections - Uploads', () => {
632636
fetchSpy.mockRestore()
633637
})
634638

639+
it('getExternalFile should not filter out payload cookies when externalFileHeaderFilter is not defined and the URL is not external', async () => {
640+
const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join(
641+
'; ',
642+
)
643+
644+
const fetchSpy = jest.spyOn(global, 'fetch')
645+
646+
// spin up a temporary server so fetch to the local doesn't fail
647+
const server = createServer((req, res) => {
648+
res.writeHead(200, { 'Content-Type': 'application/json' })
649+
res.end(JSON.stringify({ ok: true }))
650+
})
651+
await new Promise((res) => server.listen(0, undefined, undefined, res))
652+
653+
const port = (server.address() as AddressInfo).port
654+
const baseUrl = `http://localhost:${port}`
655+
656+
const req = await createPayloadRequest({
657+
config: payload.config,
658+
request: new Request(baseUrl, {
659+
headers: new Headers({
660+
cookie: testCookies,
661+
origin: baseUrl,
662+
}),
663+
}),
664+
})
665+
666+
await getExternalFile({
667+
data: { url: '/api/media/image.png' },
668+
req,
669+
uploadConfig: { skipSafeFetch: true },
670+
})
671+
672+
const [[, options]] = fetchSpy.mock.calls
673+
const cookieHeader = options.headers.cookie
674+
675+
expect(cookieHeader).toContain('payload-token=123')
676+
expect(cookieHeader).toContain('payload-something=789')
677+
expect(cookieHeader).toContain('other-cookie=456')
678+
679+
fetchSpy.mockRestore()
680+
await new Promise((res) => server.close(res))
681+
})
682+
635683
it('should keep all cookies when externalFileHeaderFilter is defined', async () => {
636684
const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join(
637685
'; ',

0 commit comments

Comments
 (0)