-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: disambiguate media files by prefix via query parameter #15844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tschwartz
wants to merge
2
commits into
payloadcms:main
Choose a base branch
from
tschwartz:media-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+334
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { Collection } from '../collections/config/types.js' | ||
| import type { PayloadRequest } from '../types/index.js' | ||
|
|
||
| vi.mock('../auth/executeAccess.js', () => ({ | ||
| executeAccess: vi.fn(), | ||
| })) | ||
|
|
||
| import { executeAccess } from '../auth/executeAccess.js' | ||
|
|
||
| import { checkFileAccess } from './checkFileAccess.js' | ||
|
|
||
| const makeFindOne = (result: unknown = { id: '1', filename: 'logo.png' }) => | ||
| vi.fn().mockResolvedValue(result) | ||
|
|
||
| const makeCollection = (): Collection => | ||
| ({ | ||
| config: { | ||
| slug: 'test-media', | ||
| access: { read: vi.fn() }, | ||
| upload: {}, | ||
| }, | ||
| }) as unknown as Collection | ||
|
|
||
| const makeReq = (findOne: ReturnType<typeof vi.fn>): PayloadRequest => | ||
| ({ | ||
| t: vi.fn(), | ||
| payload: { | ||
| db: { findOne }, | ||
| }, | ||
| }) as unknown as PayloadRequest | ||
|
|
||
| describe('checkFileAccess', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(executeAccess).mockResolvedValue({}) | ||
| }) | ||
|
|
||
| describe('prefix filtering', () => { | ||
| it('should add prefix clause to where query when prefix is provided', async () => { | ||
| const findOne = makeFindOne() | ||
| const req = makeReq(findOne) | ||
| const collection = makeCollection() | ||
|
|
||
| await checkFileAccess({ collection, filename: 'logo.png', prefix: 'abc123', req }) | ||
|
|
||
| const whereArg = findOne.mock.calls[0]?.[0]?.where | ||
| expect(whereArg?.and).toEqual(expect.arrayContaining([{ prefix: { equals: 'abc123' } }])) | ||
| }) | ||
|
|
||
| it('should not add prefix clause to where query when prefix is omitted', async () => { | ||
| const findOne = makeFindOne() | ||
| const req = makeReq(findOne) | ||
| const collection = makeCollection() | ||
|
|
||
| await checkFileAccess({ collection, filename: 'logo.png', req }) | ||
|
|
||
| const whereArg = findOne.mock.calls[0]?.[0]?.where | ||
| const hasPrefixCondition = whereArg?.and?.some( | ||
| (clause: Record<string, unknown>) => 'prefix' in clause, | ||
| ) | ||
| expect(hasPrefixCondition).toBeFalsy() | ||
| }) | ||
|
|
||
| it('should still include filename in where query when prefix is provided', async () => { | ||
| const findOne = makeFindOne() | ||
| const req = makeReq(findOne) | ||
| const collection = makeCollection() | ||
|
|
||
| await checkFileAccess({ collection, filename: 'logo.png', prefix: 'abc123', req }) | ||
|
|
||
| const whereArg = findOne.mock.calls[0]?.[0]?.where | ||
| const filenameCondition = whereArg?.and?.[0] | ||
| expect(filenameCondition?.or).toEqual( | ||
| expect.arrayContaining([{ filename: { equals: 'logo.png' } }]), | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw when no doc matches the given prefix', async () => { | ||
| const findOne = makeFindOne(null) | ||
| const req = makeReq(findOne) | ||
| const collection = makeCollection() | ||
|
|
||
| await expect( | ||
| checkFileAccess({ collection, filename: 'logo.png', prefix: 'nonexistent', req }), | ||
| ).rejects.toThrow() | ||
| }) | ||
|
|
||
| it('should throw when filename contains path traversal sequence', async () => { | ||
| const findOne = makeFindOne() | ||
| const req = makeReq(findOne) | ||
| const collection = makeCollection() | ||
|
|
||
| await expect( | ||
| checkFileAccess({ collection, filename: '../etc/passwd', req }), | ||
| ).rejects.toThrow() | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/plugin-cloud-storage/src/utilities/getFilePrefix.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { CollectionConfig, PayloadRequest } from 'payload' | ||
|
|
||
| import { getFilePrefix } from './getFilePrefix.js' | ||
|
|
||
| const makeReq = (docs: unknown[] = []) => | ||
| ({ | ||
| payload: { | ||
| find: vi.fn().mockResolvedValue({ docs }), | ||
| }, | ||
| }) as unknown as PayloadRequest | ||
|
|
||
| const makeCollection = (): CollectionConfig => | ||
| ({ slug: 'media', upload: {} }) as unknown as CollectionConfig | ||
|
|
||
| describe('getFilePrefix', () => { | ||
| describe('explicitPrefix shortcut', () => { | ||
| it('should return explicitPrefix immediately without querying the database', async () => { | ||
| const req = makeReq() | ||
|
|
||
| const result = await getFilePrefix({ | ||
| collection: makeCollection(), | ||
| explicitPrefix: 'uuid-prefix', | ||
| filename: 'logo.png', | ||
| req, | ||
| }) | ||
|
|
||
| expect(result).toBe('uuid-prefix') | ||
| expect(req.payload.find).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should return an empty string when explicitPrefix is an empty string', async () => { | ||
| const req = makeReq() | ||
|
|
||
| const result = await getFilePrefix({ | ||
| collection: makeCollection(), | ||
| explicitPrefix: '', | ||
| filename: 'logo.png', | ||
| req, | ||
| }) | ||
|
|
||
| expect(result).toBe('') | ||
| expect(req.payload.find).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe('database fallback', () => { | ||
| it('should query the database when explicitPrefix is not provided', async () => { | ||
| const req = makeReq([{ prefix: 'db-prefix' }]) | ||
|
|
||
| const result = await getFilePrefix({ | ||
| collection: makeCollection(), | ||
| filename: 'logo.png', | ||
| req, | ||
| }) | ||
|
|
||
| expect(result).toBe('db-prefix') | ||
| expect(req.payload.find).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('should return empty string when no document is found', async () => { | ||
| const req = makeReq([]) | ||
|
|
||
| const result = await getFilePrefix({ | ||
| collection: makeCollection(), | ||
| filename: 'logo.png', | ||
| req, | ||
| }) | ||
|
|
||
| expect(result).toBe('') | ||
| }) | ||
|
|
||
| it('should prioritize clientUploadContext prefix over database query', async () => { | ||
| const req = makeReq([{ prefix: 'db-prefix' }]) | ||
|
|
||
| const result = await getFilePrefix({ | ||
| clientUploadContext: { prefix: 'context-prefix' }, | ||
| collection: makeCollection(), | ||
| filename: 'logo.png', | ||
| req, | ||
| }) | ||
|
|
||
| expect(result).toBe('context-prefix') | ||
| expect(req.payload.find).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ import { | |
| noRestrictFileMimeTypesSlug, | ||
| noRestrictFileTypesSlug, | ||
| pdfOnlySlug, | ||
| prefixMediaSlug, | ||
| reduceSlug, | ||
| relationPreviewSlug, | ||
| relationSlug, | ||
|
|
@@ -1061,6 +1062,18 @@ export default buildConfigWithDefaults({ | |
| ], | ||
| }, | ||
| }, | ||
| { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its this correct? Really unsure about this particular bit. |
||
| slug: prefixMediaSlug, | ||
| fields: [ | ||
| { | ||
| name: 'prefix', | ||
| type: 'text', | ||
| }, | ||
| ], | ||
| upload: { | ||
| staticDir: path.resolve(dirname, './prefix-media'), | ||
| }, | ||
| }, | ||
| ], | ||
| onInit: async (payload) => { | ||
| if (process.env.SEED_IN_CONFIG_ONINIT !== 'false') { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would have been nice to have added the prefix to the
clientUploadContextbut theunknowntype would require a bunch of checks here in addition to all the checks already in thegetFilePrefixfunction.