|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import type { Collection } from '../collections/config/types.js' |
| 4 | +import type { PayloadRequest } from '../types/index.js' |
| 5 | + |
| 6 | +vi.mock('../auth/executeAccess.js', () => ({ |
| 7 | + executeAccess: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +import { executeAccess } from '../auth/executeAccess.js' |
| 11 | + |
| 12 | +import { checkFileAccess } from './checkFileAccess.js' |
| 13 | + |
| 14 | +const makeFindOne = (result: unknown = { id: '1', filename: 'logo.png' }) => |
| 15 | + vi.fn().mockResolvedValue(result) |
| 16 | + |
| 17 | +const makeCollection = (): Collection => |
| 18 | + ({ |
| 19 | + config: { |
| 20 | + slug: 'test-media', |
| 21 | + access: { read: vi.fn() }, |
| 22 | + upload: {}, |
| 23 | + }, |
| 24 | + }) as unknown as Collection |
| 25 | + |
| 26 | +const makeReq = (findOne: ReturnType<typeof vi.fn>): PayloadRequest => |
| 27 | + ({ |
| 28 | + t: vi.fn(), |
| 29 | + payload: { |
| 30 | + db: { findOne }, |
| 31 | + }, |
| 32 | + }) as unknown as PayloadRequest |
| 33 | + |
| 34 | +describe('checkFileAccess', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.mocked(executeAccess).mockResolvedValue({}) |
| 37 | + }) |
| 38 | + |
| 39 | + describe('prefix filtering', () => { |
| 40 | + it('should add prefix clause to where query when prefix is provided', async () => { |
| 41 | + const findOne = makeFindOne() |
| 42 | + const req = makeReq(findOne) |
| 43 | + const collection = makeCollection() |
| 44 | + |
| 45 | + await checkFileAccess({ collection, filename: 'logo.png', prefix: 'abc123', req }) |
| 46 | + |
| 47 | + const whereArg = findOne.mock.calls[0]?.[0]?.where |
| 48 | + expect(whereArg?.and).toEqual(expect.arrayContaining([{ prefix: { equals: 'abc123' } }])) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should not add prefix clause to where query when prefix is omitted', async () => { |
| 52 | + const findOne = makeFindOne() |
| 53 | + const req = makeReq(findOne) |
| 54 | + const collection = makeCollection() |
| 55 | + |
| 56 | + await checkFileAccess({ collection, filename: 'logo.png', req }) |
| 57 | + |
| 58 | + const whereArg = findOne.mock.calls[0]?.[0]?.where |
| 59 | + const hasPrefixCondition = whereArg?.and?.some( |
| 60 | + (clause: Record<string, unknown>) => 'prefix' in clause, |
| 61 | + ) |
| 62 | + expect(hasPrefixCondition).toBeFalsy() |
| 63 | + }) |
| 64 | + |
| 65 | + it('should still include filename in where query when prefix is provided', async () => { |
| 66 | + const findOne = makeFindOne() |
| 67 | + const req = makeReq(findOne) |
| 68 | + const collection = makeCollection() |
| 69 | + |
| 70 | + await checkFileAccess({ collection, filename: 'logo.png', prefix: 'abc123', req }) |
| 71 | + |
| 72 | + const whereArg = findOne.mock.calls[0]?.[0]?.where |
| 73 | + const filenameCondition = whereArg?.and?.[0] |
| 74 | + expect(filenameCondition?.or).toEqual( |
| 75 | + expect.arrayContaining([{ filename: { equals: 'logo.png' } }]), |
| 76 | + ) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should throw when no doc matches the given prefix', async () => { |
| 80 | + const findOne = makeFindOne(null) |
| 81 | + const req = makeReq(findOne) |
| 82 | + const collection = makeCollection() |
| 83 | + |
| 84 | + await expect( |
| 85 | + checkFileAccess({ collection, filename: 'logo.png', prefix: 'nonexistent', req }), |
| 86 | + ).rejects.toThrow() |
| 87 | + }) |
| 88 | + |
| 89 | + it('should throw when filename contains path traversal sequence', async () => { |
| 90 | + const findOne = makeFindOne() |
| 91 | + const req = makeReq(findOne) |
| 92 | + const collection = makeCollection() |
| 93 | + |
| 94 | + await expect( |
| 95 | + checkFileAccess({ collection, filename: '../etc/passwd', req }), |
| 96 | + ).rejects.toThrow() |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments