Sign-in look: the logo follows IAWL's rules; no plate by default; the brand fields are admin-only
Bitmaps only (JPEG, PNG, WebP, AVIF, GIF; SVG with svgLogo: true), the file fetched and checked by its bytes before it is printed (checkLogo; 512 KB; ten-minute memory; an unreachable address is believed), served from a generated size at least twice the shown width. logoPlate replaces logoMadeFor: no plate unless asked. With the look on, the name, logo and icon of Site settings are changed by admins only (brandAccess: 'editor' gives them back). Docs, skills, changelog draft, tests and pictures follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3ceeab8469
commit
939ab679c7
+100
-28
@@ -5,11 +5,11 @@ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { adminTexts } from '../src/modules/admin-i18n/index.js'
|
||||
import { AdminLogo } from '../src/modules/branding/adminGraphics.js'
|
||||
import { fitLogo, languageLinks, loadSignInBrand, SIGN_IN_LOOK, SIGN_IN_NAME_MAX, signInLogo, signInName, signInProviderActive } from '../src/modules/branding/signIn/brand.js'
|
||||
import { checkLogoFile, fitLogo, forgetCheckedLogos, languageLinks, loadSignInBrand, SIGN_IN_LOOK, SIGN_IN_NAME_MAX, signInLogo, signInName, signInProviderActive } from '../src/modules/branding/signIn/brand.js'
|
||||
import { lockBrandFields } from '../src/modules/branding/signIn/brandAccess.js'
|
||||
import { signInCss } from '../src/modules/branding/signIn/css.js'
|
||||
import { adminPathFromReferer, buildAdminLanguageEndpoint } from '../src/modules/branding/signIn/languageEndpoint.js'
|
||||
@@ -38,6 +38,14 @@ const panel = async (props: Parameters<typeof SignInPanel>[0]) => {
|
||||
const html = renderToStaticMarkup(await SignInPanel(props))
|
||||
return html.slice(html.indexOf('<header'))
|
||||
}
|
||||
const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0])
|
||||
/** The logo's file as the site would answer an anonymous request for it. */
|
||||
const serve = (body: null | Uint8Array, init: ResponseInit = {}) =>
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(() => Promise.resolve(new Response(body as never, { headers: { 'content-type': 'image/png' }, ...init })))
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
forgetCheckedLogos()
|
||||
})
|
||||
const fakePayload = (settings: Record<string, unknown>, extra: Record<string, unknown> = {}) =>
|
||||
({
|
||||
config: { i18n: { supportedLanguages: { en: {}, pl: {} } }, routes: { api: '/api' }, serverURL: 'https://site.test', ...extra },
|
||||
@@ -60,11 +68,11 @@ describe('whiteLabel.signIn: the option', () => {
|
||||
logoField: 'brandLogo',
|
||||
nameField: 'brandName',
|
||||
settingsSlug: 'settings',
|
||||
signIn: { accent: '#0B6E4F', allScreens: true, languages: false, logoMadeFor: 'dark', svgLogo: false },
|
||||
signIn: { accent: '#0B6E4F', allScreens: true, checkLogo: false, languages: false, logoPlate: true, svgLogo: true },
|
||||
}),
|
||||
).toEqual({
|
||||
allScreens: true,
|
||||
props: { accent: '#0B6E4F', imageField: 'brandLogo', inlineImages: false, languages: false, logoMadeFor: 'dark', nameField: 'brandName', settingsSlug: 'settings', svgLogo: false },
|
||||
props: { accent: '#0B6E4F', checkLogo: false, imageField: 'brandLogo', inlineImages: false, languages: false, logoPlate: true, nameField: 'brandName', settingsSlug: 'settings', svgLogo: true },
|
||||
})
|
||||
})
|
||||
|
||||
@@ -166,23 +174,74 @@ describe('what the public sign-in page may show', () => {
|
||||
expect(long.endsWith('…')).toBe(true)
|
||||
})
|
||||
|
||||
test('the logo: only an http(s) or site-relative address; SVG out when asked; never enlarged', () => {
|
||||
expect(signInLogo({ height: 100, url: '/api/media/file/logo.png', width: 400 })).toEqual({ height: 60, src: '/api/media/file/logo.png', width: 240 })
|
||||
expect(signInLogo({ height: 20, url: 'https://cdn.test/logo.webp', width: 60 })).toEqual({ height: 20, src: 'https://cdn.test/logo.webp', width: 60 })
|
||||
expect(signInLogo({ url: '/logo.png' })).toEqual({ src: '/logo.png' })
|
||||
test('the logo: a bitmap by its type, an http(s) or site-relative address, never enlarged; SVG only when asked', () => {
|
||||
const png = (extra: Record<string, unknown>) => ({ mimeType: 'image/png', ...extra })
|
||||
expect(signInLogo(png({ height: 100, url: '/api/media/file/logo.png', width: 400 }))).toEqual({ height: 60, src: '/api/media/file/logo.png', width: 240 })
|
||||
expect(signInLogo({ height: 20, mimeType: 'image/webp', url: 'https://cdn.test/logo.webp', width: 60 })).toEqual({ height: 20, src: 'https://cdn.test/logo.webp', width: 60 })
|
||||
expect(signInLogo(png({ url: '/logo.png' }))).toEqual({ src: '/logo.png' })
|
||||
for (const mimeType of ['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif', 'IMAGE/PNG']) {expect(signInLogo({ mimeType, url: '/l' })).toEqual({ src: '/l' })}
|
||||
for (const mimeType of ['application/pdf', 'text/html', 'image/x-icon', 'image/tiff', 'video/mp4', undefined, 7]) {expect(signInLogo({ mimeType, url: '/l' })).toBeNull()}
|
||||
for (const url of ['javascript:alert(1)', 'data:text/html,<script>1</script>', '//evil.test/x.png', 'file:///etc/passwd', '', ' ']) {
|
||||
expect(signInLogo({ url })).toBeNull()
|
||||
expect(signInLogo(png({ url }))).toBeNull()
|
||||
}
|
||||
expect(signInLogo('64f0…id-only')).toBeNull()
|
||||
expect(signInLogo(null)).toBeNull()
|
||||
const svg = { mimeType: 'image/svg+xml', url: '/api/media/file/logo.svg' }
|
||||
expect(signInLogo(svg)).toEqual({ src: svg.url })
|
||||
expect(signInLogo(svg, { svgLogo: false })).toBeNull()
|
||||
expect(signInLogo({ mimeType: 'image/png', url: '/api/media/file/renamed.SVG?v=2' }, { svgLogo: false })).toBeNull()
|
||||
expect(signInLogo(svg)).toBeNull()
|
||||
expect(signInLogo(svg, { svgLogo: true })).toEqual({ src: svg.url })
|
||||
expect(signInLogo(png({ url: '/api/media/file/renamed.SVG?v=2' }))).toBeNull()
|
||||
expect(fitLogo(1000, 100)).toEqual({ height: 24, width: 240 })
|
||||
expect(fitLogo(0, 10)).toBeNull()
|
||||
})
|
||||
|
||||
test('the served file: the smallest generated size at least twice the shown width, same proportions, an allowed type', () => {
|
||||
const logoDoc = (sizes: Record<string, unknown>) => ({ height: 400, mimeType: 'image/png', sizes, url: '/full.png', width: 1600 })
|
||||
const size = (width: number, height: number, url: string, mimeType = 'image/webp') => ({ height, mimeType, url, width })
|
||||
// shown 240 wide: 480 or more
|
||||
expect(signInLogo(logoDoc({ card: size(768, 192, '/768.webp'), hero: size(1200, 300, '/1200.webp'), thumb: size(300, 75, '/300.webp') }))?.src).toBe('/768.webp')
|
||||
expect(signInLogo(logoDoc({ square: size(600, 600, '/crop.webp'), thumb: size(300, 75, '/300.webp') }))?.src).toBe('/full.png')
|
||||
expect(signInLogo(logoDoc({ bad: size(600, 150, 'javascript:1'), empty: null, svg: size(600, 150, '/x.svg', 'image/svg+xml') }))?.src).toBe('/full.png')
|
||||
expect(signInLogo(logoDoc({}))).toEqual({ height: 60, src: '/full.png', width: 240 })
|
||||
})
|
||||
|
||||
test('the file is looked at before it is printed: a deleted, broken, foreign or too large file gives the name', async () => {
|
||||
const settings = { logo: { height: 160, mimeType: 'image/png', url: '/api/media/file/logo.png', width: 560 }, siteName: 'Acme' }
|
||||
const brand = () => {
|
||||
forgetCheckedLogos()
|
||||
return loadSignInBrand({ look: {}, payload: fakePayload(settings) })
|
||||
}
|
||||
serve(PNG)
|
||||
expect((await brand()).logo?.src).toBe(settings.logo.url)
|
||||
vi.restoreAllMocks()
|
||||
serve(null, { status: 404 })
|
||||
expect(await brand()).toEqual({ name: 'Acme', logo: null })
|
||||
vi.restoreAllMocks()
|
||||
serve(new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>')) // says image/png, is not
|
||||
expect((await brand()).logo).toBeNull()
|
||||
vi.restoreAllMocks()
|
||||
serve(new TextEncoder().encode('<html>'), { headers: { 'content-type': 'text/html' } })
|
||||
expect((await brand()).logo).toBeNull()
|
||||
vi.restoreAllMocks()
|
||||
serve(PNG, { headers: { 'content-length': String(600 * 1024), 'content-type': 'image/png' } })
|
||||
expect((await brand()).logo).toBeNull()
|
||||
vi.restoreAllMocks()
|
||||
// nothing answers (the site cannot reach its own address): the upload document is believed
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('ECONNREFUSED'))
|
||||
expect((await brand()).logo?.src).toBe(settings.logo.url)
|
||||
vi.restoreAllMocks()
|
||||
// one look per address in ten minutes, and none with checkLogo: false
|
||||
const fetched = serve(PNG)
|
||||
forgetCheckedLogos()
|
||||
await loadSignInBrand({ look: {}, payload: fakePayload(settings) })
|
||||
await loadSignInBrand({ look: {}, payload: fakePayload(settings) })
|
||||
expect(fetched).toHaveBeenCalledTimes(1)
|
||||
forgetCheckedLogos()
|
||||
await loadSignInBrand({ look: { checkLogo: false }, payload: fakePayload(settings) })
|
||||
expect(fetched).toHaveBeenCalledTimes(1)
|
||||
expect(await checkLogoFile('ftp://site.test/logo.png', null)).toBe('bad')
|
||||
expect(await checkLogoFile('/relative.png', null)).toBe('unknown')
|
||||
})
|
||||
|
||||
test('markup in the name is text; the only links are the site, and the language endpoint', async () => {
|
||||
const payload = fakePayload({ siteName: '<script>alert(1)</script> <a href="https://evil.test">reset</a> https://evil.test' })
|
||||
const html = await panel({ i18n: { language: 'en', t: t as never }, look: {}, payload })
|
||||
@@ -195,14 +254,15 @@ describe('what the public sign-in page may show', () => {
|
||||
expect(html).toContain('<span aria-current="true" lang="en">English</span>')
|
||||
})
|
||||
|
||||
test('a logo: an <img> with the name as its text and its size, on a plate unless made for dark grounds', async () => {
|
||||
test('a logo: an <img> with the name as its text and its size, straight on the panel; a plate when asked', async () => {
|
||||
serve(PNG)
|
||||
const payload = fakePayload({ logo: { height: 160, mimeType: 'image/png', url: '/api/media/file/logo.png', width: 560 }, siteName: 'Acme' })
|
||||
const html = await panel({ look: { languages: false }, payload })
|
||||
expect(html).toContain('<img alt="Acme" decoding="async" height="69" src="/api/media/file/logo.png" width="240"/>')
|
||||
expect(html).toContain('iapl-signin__brand--plate')
|
||||
expect(html).toContain('<a class="iapl-signin__brand" href="/">')
|
||||
expect(html).not.toContain('iapl-signin__brand--plate')
|
||||
expect(html).not.toContain('<nav')
|
||||
const dark = await panel({ look: { logoMadeFor: 'dark' }, payload })
|
||||
expect(dark).not.toContain('iapl-signin__brand--plate')
|
||||
expect(await panel({ look: { logoPlate: true }, payload })).toContain('iapl-signin__brand--plate')
|
||||
})
|
||||
|
||||
test('nothing set, or no database: the label only — no vendor, no Payload', async () => {
|
||||
@@ -218,13 +278,23 @@ describe('what the public sign-in page may show', () => {
|
||||
})
|
||||
|
||||
test('a session that owes its 2FA code never gets the media address: the picture itself, or the name', async () => {
|
||||
const settings = { logo: { url: 'https://site.test/api/media/file/logo.png' }, siteName: 'Acme' }
|
||||
const payload = fakePayload(settings, { custom: {} })
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(new Uint8Array([1, 2, 3]), { headers: { 'content-type': 'image/png' } }))
|
||||
// no 2FA guard configured in this fake: a signed-in user counts as passed, the address is used
|
||||
expect((await loadSignInBrand({ look: {}, payload, user: { id: 1, collection: 'users' } })).logo?.src).toBe(settings.logo.url)
|
||||
fetchSpy.mockRestore()
|
||||
expect((await loadSignInBrand({ look: {}, payload })).logo?.src).toBe(settings.logo.url)
|
||||
const settings = { logo: { mimeType: 'image/png', url: 'https://site.test/api/media/file/logo.png' }, siteName: 'Acme' }
|
||||
const guarded = () => fakePayload(settings, { custom: { ipal: { twoFactor: { collection: 'users', requireSetup: true } } } })
|
||||
const pending = { id: 1, _strategy: 'local-jwt', collection: 'users', hasTotp: true }
|
||||
const brandFor = (look: Record<string, unknown>, user?: Record<string, unknown>) => {
|
||||
forgetCheckedLogos()
|
||||
return loadSignInBrand({ look, payload: guarded(), user })
|
||||
}
|
||||
serve(PNG)
|
||||
expect((await brandFor({}, pending)).logo?.src).toBe(`data:image/png;base64,${Buffer.from(PNG).toString('base64')}`)
|
||||
expect((await brandFor({}, { ...pending, _strategy: 'totp' })).logo?.src).toBe(settings.logo.url)
|
||||
expect((await brandFor({})).logo?.src).toBe(settings.logo.url)
|
||||
// never fetched for it (inlineImages: false), not checked (checkLogo: false), or nothing answers: the name
|
||||
expect((await brandFor({ inlineImages: false }, pending)).logo).toBeNull()
|
||||
vi.restoreAllMocks()
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('ECONNREFUSED'))
|
||||
expect((await brandFor({}, pending)).logo).toBeNull()
|
||||
expect((await brandFor({ checkLogo: false }, pending)).logo).toBeNull()
|
||||
})
|
||||
|
||||
test('the stylesheet: no file, no font, no other host, no script; honours reduced motion and forced colours', () => {
|
||||
@@ -306,10 +376,11 @@ describe('whiteLabel.brandAccess', () => {
|
||||
})
|
||||
const fieldsOf = (global: GlobalConfig) => (global.fields[0] as { tabs: { fields: { access?: { update?: (a: unknown) => Promise<boolean> }; name: string }[] }[] }).tabs[0].fields
|
||||
|
||||
test('the default leaves the fields as they are; "admin" locks the brand fields only, without a new field', async () => {
|
||||
const config = await build({ whiteLabel: true })
|
||||
const plain = config.globals!.find((g) => g.slug === 'site-settings')!
|
||||
expect(JSON.stringify(plain.fields)).toBe(JSON.stringify((await build()).globals!.find((g) => g.slug === 'site-settings')!.fields))
|
||||
test('with the sign-in look the brand fields are admin-only; "editor" or no look leaves them as they were', async () => {
|
||||
const fieldsJson = async (options: Record<string, unknown>) => JSON.stringify((await build(options)).globals!.find((g) => g.slug === 'site-settings')!.fields)
|
||||
const plain = await fieldsJson({})
|
||||
expect(await fieldsJson({ whiteLabel: { brandAccess: 'editor' } })).toBe(plain)
|
||||
expect(await fieldsJson({ whiteLabel: { signIn: false } })).toBe(plain)
|
||||
|
||||
const locked = lockBrandFields([settings(), { slug: 'other', fields: [{ name: 'siteName', type: 'text' }] }], 'site-settings', ['siteName', 'logo', 'favicon'])
|
||||
const [siteName, description, logoField] = fieldsOf(locked[0])
|
||||
@@ -319,11 +390,12 @@ describe('whiteLabel.brandAccess', () => {
|
||||
for (const field of [siteName, logoField]) {
|
||||
expect(await field.access!.update!({ req: { payload: staff, user: null } })).toBe(false)
|
||||
}
|
||||
// no field is added or removed: nothing for a database to migrate
|
||||
expect(fieldsOf(locked[0]).map((f) => f.name)).toEqual(['siteName', 'siteDescription', 'logo'])
|
||||
})
|
||||
|
||||
test('through the plugin: an editor may not change the name, an admin may', async () => {
|
||||
const config = await build({ whiteLabel: { brandAccess: 'admin' } }, { collections: [{ slug: 'users', auth: true, fields: [] }, { slug: 'media', fields: [], upload: true }] } as never)
|
||||
const config = await build({ whiteLabel: true }, { collections: [{ slug: 'users', auth: true, fields: [] }, { slug: 'media', fields: [], upload: true }] } as never)
|
||||
const global = config.globals!.find((g) => g.slug === 'site-settings')!
|
||||
const find = (fields: unknown[], name: string): { access?: { update?: (a: unknown) => Promise<boolean> } } | undefined => {
|
||||
for (const f of fields as { fields?: unknown[]; name?: string; tabs?: { fields: unknown[] }[] }[]) {
|
||||
|
||||
Reference in New Issue
Block a user