The tab icon endpoint answers with the image itself instead of a redirect to the media file, and the logo components hand a session that owes its 2FA code (or has not set 2FA up) the picture fetched without credentials, as an anonymous visitor gets it. Who can read media is unchanged; when the file is not public or is larger than 512 KB the site name is used. Co-Authored-By: Claude Fable 5.2 <[email protected]>
75 lines
4.3 KiB
TypeScript
75 lines
4.3 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { AdminLogo } from '../src/modules/branding/adminGraphics.js'
|
|
import { buildAdminImageEndpoints } from '../src/modules/branding/adminImages.js'
|
|
import { fetchPublicImage, publicImageDataUri } from '../src/modules/branding/publicImage.js'
|
|
|
|
const png = Buffer.from('89504e470d0a1a0a', 'hex')
|
|
const respond = (body: BodyInit | null, init: ResponseInit) => vi.fn(async () => new Response(body, init))
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
describe('the brand image for a session the browser cannot load it in', () => {
|
|
test('fetched without credentials; only an image of a sane size comes back', async () => {
|
|
const fetchMock = respond(png, { headers: { 'content-type': 'image/png' }, status: 200 })
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const image = await fetchPublicImage('/api/media/file/logo.png', 'https://example.com')
|
|
expect(image?.type).toBe('image/png')
|
|
const [url, init] = fetchMock.mock.calls[0] as unknown as [URL, RequestInit]
|
|
expect(String(url)).toBe('https://example.com/api/media/file/logo.png')
|
|
expect(init.credentials).toBe('omit')
|
|
expect(init.headers).toBeUndefined() // no cookie, no authorization
|
|
expect(await publicImageDataUri('/api/media/file/logo.png', 'https://example.com')).toMatch(/^data:image\/png;base64,/)
|
|
})
|
|
|
|
test.each([
|
|
['media is not public (403)', respond('no', { status: 403 })],
|
|
['not an image', respond('<html>', { headers: { 'content-type': 'text/html' }, status: 200 })],
|
|
['too large', respond(Buffer.alloc(600 * 1024), { headers: { 'content-type': 'image/png' }, status: 200 })],
|
|
])('%s → nothing', async (_, fetchMock) => {
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
expect(await fetchPublicImage('/x.png', 'https://example.com')).toBeNull()
|
|
})
|
|
|
|
test('no origin for a relative address, or another scheme → nothing, and no request', async () => {
|
|
const fetchMock = respond(png, { headers: { 'content-type': 'image/png' }, status: 200 })
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
expect(await fetchPublicImage('/x.png', null)).toBeNull()
|
|
expect(await fetchPublicImage('file:///etc/passwd', null)).toBeNull()
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('the panel logo: a data address for a session that owes its code, the plain address otherwise', async () => {
|
|
vi.stubGlobal('fetch', respond(png, { headers: { 'content-type': 'image/png' }, status: 200 }))
|
|
const payload = {
|
|
config: { custom: { ipal: { twoFactor: { collection: 'users' } } }, serverURL: 'https://example.com' },
|
|
findGlobal: async () => ({ logo: { url: '/api/media/file/logo.png' }, siteName: 'Example' }),
|
|
}
|
|
const src = async (user: unknown) => ((await AdminLogo({ payload: payload as never, user: user as never })) as { props: { src?: string } }).props.src
|
|
expect(await src({ _strategy: 'local-jwt', collection: 'users', hasTotp: true })).toMatch(/^data:image\/png;base64,/)
|
|
expect(await src({ _strategy: 'totp', collection: 'users', hasTotp: true })).toBe('/api/media/file/logo.png')
|
|
expect(await src(null)).toBe('/api/media/file/logo.png') // the login screen: an anonymous visitor loads it as before
|
|
})
|
|
|
|
test('the tab icon endpoint serves the picture itself, so no session is sent to a file it is refused', async () => {
|
|
const fetchMock = respond(png, { headers: { 'content-type': 'image/png' }, status: 200 })
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const icon = buildAdminImageEndpoints({ agency: false, language: 'en' } as never).find((e) => e.path.endsWith('admin-icon'))!
|
|
const req = {
|
|
payload: { config: {}, findGlobal: async () => ({ favicon: { url: '/api/media/file/icon.png' }, siteName: 'Example' }) },
|
|
url: 'https://example.com/api/ipal/admin-icon',
|
|
user: { _strategy: 'local-jwt', collection: 'users', hasTotp: true },
|
|
}
|
|
const res = await icon.handler(req as never)
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('image/png')
|
|
expect(res.headers.get('location')).toBeNull()
|
|
expect(String((fetchMock.mock.calls[0] as unknown as [URL])[0])).toBe('https://example.com/api/media/file/icon.png')
|
|
// not fetchable → the redirect, as before
|
|
vi.stubGlobal('fetch', respond('no', { status: 403 }))
|
|
expect((await icon.handler(req as never)).status).toBe(302)
|
|
})
|
|
})
|