Public brand image fetch: the site's configured origin only, no redirect followed, a time and a byte limit

/api/ipal/admin-og resolved a relative logo address against the request's own host and fetched it with
Node's defaults (redirects followed, no timeout, no size cap); /api/ipal/admin-icon resolved the same way.
Both now resolve against serverURL / NEXT_PUBLIC_SERVER_URL (never the request) and fetch through
fetchPublicImage, which asks one address with redirect: 'manual' (a 3xx is no image), within
PUBLIC_IMAGE_TIMEOUT_MS, reading at most 512 KB. The renderer still draws PNG, JPEG, GIF (by their bytes)
and SVG (by its type, as before). Tests against local servers: hostile Host / X-Forwarded-Host, a 302 to an
internal host, an endless body, a hanging server, the normal logo. Docs say 1.17.1 instead of 1.18.0.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 22:03:52 +01:00
co-authored by Claude Fable 5.2
parent 562bf5388d
commit aef1efac73
13 changed files with 244 additions and 29 deletions
+7 -2
View File
@@ -137,7 +137,7 @@ describe('plugin wiring', () => {
describe('preview image endpoints', () => {
const req = (settings: Record<string, unknown>) =>
({ payload: { config: { localization: false }, findGlobal: vi.fn(async () => settings) }, url: 'http://localhost:3000/api/ipal/admin-og' }) as unknown as PayloadRequest
({ payload: { config: { localization: false, serverURL: 'http://localhost:3000' }, findGlobal: vi.fn(async () => settings) }, url: 'http://localhost:3000/api/ipal/admin-og' }) as unknown as PayloadRequest
const [og, icon] = buildAdminImageEndpoints(buildAdminMeta({ agency: DEFAULT_AGENCY }).resolved) as [Endpoint, Endpoint]
test('renders a PNG from the site name when there is no logo', async () => {
@@ -153,14 +153,19 @@ describe('preview image endpoints', () => {
try {
const res = (await og.handler(req({ logo: { url: '/api/media/file/logo.png' }, siteName: 'Acme' })))
expect(res.headers.get('content-type')).toBe('image/png')
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/media/file/logo.png')
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(String(fetchMock.mock.calls[0][0])).toBe('http://localhost:3000/api/media/file/logo.png')
expect((fetchMock.mock.calls[0][1] as RequestInit).redirect).toBe('manual')
} finally {
fetchMock.mockRestore()
}
}, 30_000)
test('icon redirects to the site favicon, else draws initials', async () => {
// the favicon cannot be fetched here (nothing is asked of a real server in this test): the redirect
const fetchMock = vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('ECONNREFUSED'))
const redirect = (await icon.handler(req({ favicon: { url: '/api/media/file/icon.png' }, siteName: 'Acme' })))
fetchMock.mockRestore()
expect(redirect.status).toBe(302)
expect(redirect.headers.get('location')).toBe('http://localhost:3000/api/media/file/icon.png')
const drawn = (await icon.handler(req({ siteName: 'Acme Studio' })))