|
| 1 | +import assert from 'node:assert/strict' |
| 2 | + |
| 3 | +import { describe, test } from 'vitest' |
| 4 | + |
| 5 | +import type { PayloadRequest } from 'payload' |
| 6 | + |
| 7 | +import type { AltTextPluginConfig } from '../src/types/AltTextPluginConfig.ts' |
| 8 | + |
| 9 | +import { bulkGenerateAltTextsEndpoint } from '../src/endpoints/bulkGenerateAltTexts.ts' |
| 10 | +import { generateAltTextEndpoint } from '../src/endpoints/generateAltText.ts' |
| 11 | + |
| 12 | +/** |
| 13 | + * The endpoints must treat the configured `collections` as an allowlist. A |
| 14 | + * request naming a collection the plugin does not manage (e.g. `users`) must be |
| 15 | + * rejected with `403` before any Local API call — otherwise the endpoints can be |
| 16 | + * pointed at any collection in the app, widening the blast radius of the |
| 17 | + * access-control surface to the entire data model. |
| 18 | + */ |
| 19 | + |
| 20 | +const user = { id: 'low-priv-user', email: 'user@example.com', role: 'user' } |
| 21 | + |
| 22 | +type LocalApiCall = Record<string, unknown> |
| 23 | + |
| 24 | +function buildPluginConfig(): AltTextPluginConfig { |
| 25 | + return { |
| 26 | + access: ({ req }) => !!req.user, |
| 27 | + collections: [{ slug: 'media', mimeTypes: ['image/*'] }], |
| 28 | + enabled: true, |
| 29 | + getImageThumbnail: () => 'https://example.com/thumb.png', |
| 30 | + healthCheck: true, |
| 31 | + healthCheckAccess: ({ req }) => !!req.user, |
| 32 | + locale: 'en', |
| 33 | + locales: [], |
| 34 | + maxBulkGenerateConcurrency: 1, |
| 35 | + resolver: { |
| 36 | + key: 'mock', |
| 37 | + resolve: async () => ({ |
| 38 | + success: true, |
| 39 | + result: { altText: 'generated alt', keywords: ['a', 'b'] }, |
| 40 | + }), |
| 41 | + resolveBulk: async () => ({ |
| 42 | + success: true, |
| 43 | + results: { en: { altText: 'generated alt', keywords: ['a', 'b'] } }, |
| 44 | + }), |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function buildRequest(body: unknown): { |
| 50 | + findByIDCalls: LocalApiCall[] |
| 51 | + req: PayloadRequest |
| 52 | + updateCalls: LocalApiCall[] |
| 53 | +} { |
| 54 | + const findByIDCalls: LocalApiCall[] = [] |
| 55 | + const updateCalls: LocalApiCall[] = [] |
| 56 | + |
| 57 | + const req = { |
| 58 | + json: async () => body, |
| 59 | + payload: { |
| 60 | + config: { custom: { altTextPluginConfig: buildPluginConfig() } }, |
| 61 | + findByID: async (args: LocalApiCall) => { |
| 62 | + findByIDCalls.push(args) |
| 63 | + return { id: args.id, filename: 'photo.png', mimeType: 'image/png' } |
| 64 | + }, |
| 65 | + update: async (args: LocalApiCall) => { |
| 66 | + updateCalls.push(args) |
| 67 | + return { id: args.id } |
| 68 | + }, |
| 69 | + }, |
| 70 | + user, |
| 71 | + } as unknown as PayloadRequest |
| 72 | + |
| 73 | + return { findByIDCalls, req, updateCalls } |
| 74 | +} |
| 75 | + |
| 76 | +describe('generate endpoint collection allowlist', () => { |
| 77 | + test('rejects a collection the plugin does not manage with 403 and no Local API call', async () => { |
| 78 | + const { findByIDCalls, req, updateCalls } = buildRequest({ |
| 79 | + id: 'doc-1', |
| 80 | + collection: 'users', |
| 81 | + locale: 'en', |
| 82 | + update: true, |
| 83 | + }) |
| 84 | + |
| 85 | + const response = await generateAltTextEndpoint(buildPluginConfig().access)(req) |
| 86 | + |
| 87 | + assert.equal(response.status, 403) |
| 88 | + assert.equal(findByIDCalls.length, 0) |
| 89 | + assert.equal(updateCalls.length, 0) |
| 90 | + }) |
| 91 | + |
| 92 | + test('allows a configured collection', async () => { |
| 93 | + const { findByIDCalls, req } = buildRequest({ |
| 94 | + id: 'doc-1', |
| 95 | + collection: 'media', |
| 96 | + locale: 'en', |
| 97 | + update: false, |
| 98 | + }) |
| 99 | + |
| 100 | + const response = await generateAltTextEndpoint(buildPluginConfig().access)(req) |
| 101 | + |
| 102 | + assert.equal(response.status, 200) |
| 103 | + assert.equal(findByIDCalls.length, 1) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('bulk generate endpoint collection allowlist', () => { |
| 108 | + test('rejects a collection the plugin does not manage with 403 and no Local API call', async () => { |
| 109 | + const { findByIDCalls, req, updateCalls } = buildRequest({ |
| 110 | + collection: 'users', |
| 111 | + ids: ['doc-1', 'doc-2'], |
| 112 | + }) |
| 113 | + |
| 114 | + const response = await bulkGenerateAltTextsEndpoint(buildPluginConfig().access)(req) |
| 115 | + |
| 116 | + assert.equal(response.status, 403) |
| 117 | + assert.equal(findByIDCalls.length, 0) |
| 118 | + assert.equal(updateCalls.length, 0) |
| 119 | + }) |
| 120 | + |
| 121 | + test('allows a configured collection', async () => { |
| 122 | + const { findByIDCalls, req } = buildRequest({ collection: 'media', ids: ['doc-1'] }) |
| 123 | + |
| 124 | + const response = await bulkGenerateAltTextsEndpoint(buildPluginConfig().access)(req) |
| 125 | + |
| 126 | + assert.equal(response.status, 200) |
| 127 | + assert.equal(findByIDCalls.length, 1) |
| 128 | + }) |
| 129 | +}) |
0 commit comments