Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions docs/upload/overview.mdx

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion packages/payload/src/uploads/getExternalFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ export const getExternalFile = async ({ data, req, uploadConfig }: Args): Promis

const headers = uploadConfig.externalFileHeaderFilter
? uploadConfig.externalFileHeaderFilter(Object.fromEntries(new Headers(req.headers)))
: { cookie: req.headers.get('cookie')! }
: {
cookie:
req.headers
.get('cookie')
?.split(';')
.filter((cookie) => !cookie.trim().startsWith(req.payload.config.cookiePrefix))
.join(';') || '',
}

// Check if URL is allowed because of skipSafeFetch allowList
const skipSafeFetch: boolean =
Expand Down
7 changes: 6 additions & 1 deletion packages/payload/src/uploads/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ export type UploadConfig = {
*/
displayPreview?: boolean
/**
* Ability to filter/modify Request Headers when fetching a file.
*
* Accepts existing headers and returns the headers after filtering or modifying.
* If using this option, you should handle the removal of any sensitive cookies
* (like payload-prefixed cookies) to prevent leaking session information to external
* services. By default, Payload automatically filters out payload-prefixed cookies
* when this option is NOT defined.
*
* Useful for adding custom headers to fetch from external providers.
* @default undefined
Expand Down
10 changes: 10 additions & 0 deletions test/uploads/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
relationSlug,
restrictFileTypesSlug,
skipAllowListSafeFetchMediaSlug,
skipSafeFetchHeaderFilterSlug,
skipSafeFetchMediaSlug,
svgOnlySlug,
threeDimensionalSlug,
Expand Down Expand Up @@ -465,6 +466,15 @@ export default buildConfigWithDefaults({
staticDir: path.resolve(dirname, './media'),
},
},
{
slug: skipSafeFetchHeaderFilterSlug,
fields: [],
upload: {
skipSafeFetch: true,
staticDir: path.resolve(dirname, './media'),
externalFileHeaderFilter: (headers) => headers, // Keep all headers including cookies
},
},
{
slug: skipAllowListSafeFetchMediaSlug,
fields: [],
Expand Down
63 changes: 63 additions & 0 deletions test/uploads/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
relationSlug,
restrictFileTypesSlug,
skipAllowListSafeFetchMediaSlug,
skipSafeFetchHeaderFilterSlug,
skipSafeFetchMediaSlug,
svgOnlySlug,
unstoredMediaSlug,
Expand Down Expand Up @@ -567,6 +568,68 @@ describe('Collections - Uploads', () => {
})
})

describe('cookie filtering', () => {
it('should filter out payload cookies when externalFileHeaderFilter is not defined', async () => {
const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join(
'; ',
)

const fetchSpy = jest.spyOn(global, 'fetch')

await payload.create({
collection: skipSafeFetchMediaSlug,
data: {
filename: 'fat-head-nate.png',
url: 'https://www.payload.marketing/fat-head-nate.png',
},
req: {
headers: new Headers({
cookie: testCookies,
}),
},
})

const [[, options]] = fetchSpy.mock.calls
const cookieHeader = options.headers.cookie

expect(cookieHeader).not.toContain('payload-token=123')
expect(cookieHeader).not.toContain('payload-something=789')
expect(cookieHeader).toContain('other-cookie=456')

fetchSpy.mockRestore()
})

it('should keep all cookies when externalFileHeaderFilter is defined', async () => {
const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join(
'; ',
)

const fetchSpy = jest.spyOn(global, 'fetch')

await payload.create({
collection: skipSafeFetchHeaderFilterSlug,
data: {
filename: 'fat-head-nate.png',
url: 'https://www.payload.marketing/fat-head-nate.png',
},
req: {
headers: new Headers({
cookie: testCookies,
}),
},
})

const [[, options]] = fetchSpy.mock.calls
const cookieHeader = options.headers.cookie

expect(cookieHeader).toContain('other-cookie=456')
expect(cookieHeader).toContain('payload-token=123')
expect(cookieHeader).toContain('payload-something=789')

fetchSpy.mockRestore()
})
})

describe('filters', () => {
it.each`
url | collection | errorContains
Expand Down
45 changes: 43 additions & 2 deletions test/uploads/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface Config {
media: Media;
'allow-list-media': AllowListMedia;
'skip-safe-fetch-media': SkipSafeFetchMedia;
'skip-safe-fetch-header-filter': SkipSafeFetchHeaderFilter;
'skip-allow-list-safe-fetch-media': SkipAllowListSafeFetchMedia;
'restrict-file-types': RestrictFileType;
'no-restrict-file-types': NoRestrictFileType;
Expand Down Expand Up @@ -141,6 +142,7 @@ export interface Config {
media: MediaSelect<false> | MediaSelect<true>;
'allow-list-media': AllowListMediaSelect<false> | AllowListMediaSelect<true>;
'skip-safe-fetch-media': SkipSafeFetchMediaSelect<false> | SkipSafeFetchMediaSelect<true>;
'skip-safe-fetch-header-filter': SkipSafeFetchHeaderFilterSelect<false> | SkipSafeFetchHeaderFilterSelect<true>;
'skip-allow-list-safe-fetch-media': SkipAllowListSafeFetchMediaSelect<false> | SkipAllowListSafeFetchMediaSelect<true>;
'restrict-file-types': RestrictFileTypesSelect<false> | RestrictFileTypesSelect<true>;
'no-restrict-file-types': NoRestrictFileTypesSelect<false> | NoRestrictFileTypesSelect<true>;
Expand Down Expand Up @@ -830,6 +832,24 @@ export interface SkipSafeFetchMedia {
focalX?: number | null;
focalY?: number | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "skip-safe-fetch-header-filter".
*/
export interface SkipSafeFetchHeaderFilter {
id: string;
updatedAt: string;
createdAt: string;
url?: string | null;
thumbnailURL?: string | null;
filename?: string | null;
mimeType?: string | null;
filesize?: number | null;
width?: number | null;
height?: number | null;
focalX?: number | null;
focalY?: number | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "skip-allow-list-safe-fetch-media".
Expand Down Expand Up @@ -1698,6 +1718,10 @@ export interface PayloadLockedDocument {
relationTo: 'skip-safe-fetch-media';
value: string | SkipSafeFetchMedia;
} | null)
| ({
relationTo: 'skip-safe-fetch-header-filter';
value: string | SkipSafeFetchHeaderFilter;
} | null)
| ({
relationTo: 'skip-allow-list-safe-fetch-media';
value: string | SkipAllowListSafeFetchMedia;
Expand Down Expand Up @@ -2533,6 +2557,23 @@ export interface SkipSafeFetchMediaSelect<T extends boolean = true> {
focalX?: T;
focalY?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "skip-safe-fetch-header-filter_select".
*/
export interface SkipSafeFetchHeaderFilterSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
url?: T;
thumbnailURL?: T;
filename?: T;
mimeType?: T;
filesize?: T;
width?: T;
height?: T;
focalX?: T;
focalY?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "skip-allow-list-safe-fetch-media_select".
Expand Down Expand Up @@ -3409,6 +3450,6 @@ export interface Auth {


declare module 'payload' {
// @ts-ignore
// @ts-ignore
export interface GeneratedTypes extends Config {}
}
}
1 change: 1 addition & 0 deletions test/uploads/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const restrictFileTypesSlug = 'restrict-file-types'
export const noRestrictFileTypesSlug = 'no-restrict-file-types'
export const noRestrictFileMimeTypesSlug = 'no-restrict-file-mime-types'
export const skipSafeFetchMediaSlug = 'skip-safe-fetch-media'
export const skipSafeFetchHeaderFilterSlug = 'skip-safe-fetch-header-filter'
export const skipAllowListSafeFetchMediaSlug = 'skip-allow-list-safe-fetch-media'
export const listViewPreviewSlug = 'list-view-preview'
export const threeDimensionalSlug = 'three-dimensional'
Expand Down
Loading