diff --git a/docs/white-label.md b/docs/white-label.md index 3b2fae9..eb1dbea 100644 --- a/docs/white-label.md +++ b/docs/white-label.md @@ -136,7 +136,7 @@ client's sign-in page (the credit line under the form is separate: `credit: fals the sign-in screens until it uploads a bitmap or sets the option). - **Looked at before it is printed:** the server fetches the file as an anonymous visitor would (at most 512 KB) and reads its first bytes. A deleted, broken, renamed, non-image or too large file gives the site's name, never - a broken picture. The answer is remembered for ten minutes. When nothing answers at all (a container that cannot + a broken picture. A redirect is never followed by the server. The answer is remembered for ten minutes. When nothing answers at all (a container that cannot reach its own public address), the upload document is believed. `checkLogo: false` turns the look off. - **Size:** fits 240 × 72 px (192 × 56 on a phone), never enlarged or cropped, `width` and `height` set (no layout shift). Served from the smallest generated size (`imageSizes` of the media collection) that is at least diff --git a/package.json b/package.json index 7bea0ea..9468db8 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "files": [ "dist", "docs", + "!docs/dev/login-page/*.png", "bin", "src/modules/block-library" ], diff --git a/src/modules/branding/signIn/brand.ts b/src/modules/branding/signIn/brand.ts index 322d020..a721927 100644 --- a/src/modules/branding/signIn/brand.ts +++ b/src/modules/branding/signIn/brand.ts @@ -132,6 +132,9 @@ export type LogoCheck = 'bad' | 'unknown' | { bytes: Buffer; type: string } * - `'bad'` when the server ANSWERS and it is no such picture: 404, another type, too large, broken bytes; * - `'unknown'` when nothing answers (the site cannot reach its own address from inside its container, a * timeout): the upload document is then believed, so a network quirk never takes the logo away. + * A redirect is NEVER followed: the server asks only the address the upload itself has, so a logo address + * that answers "go to http://169.254.169.254/…" (or any internal host) makes the server fetch nothing there. + * Such a logo counts as `'unknown'`: the browser may follow it, the server does not. */ export async function checkLogoFile(url: string, base: null | string | undefined, allowSvg = false): Promise { let target: URL @@ -143,11 +146,12 @@ export async function checkLogoFile(url: string, base: null | string | undefined if (target.protocol !== 'http:' && target.protocol !== 'https:') {return 'bad'} let res: Response try { - res = await fetch(target, { credentials: 'omit', redirect: 'follow', signal: AbortSignal.timeout(4000) }) + res = await fetch(target, { credentials: 'omit', redirect: 'manual', signal: AbortSignal.timeout(4000) }) } catch { return 'unknown' } try { + if ((res.status >= 300 && res.status < 400) || res.type === 'opaqueredirect') {return 'unknown'} if (!res.ok) {return res.status >= 500 ? 'unknown' : 'bad'} const declared = Number(res.headers.get('content-length')) if (Number.isFinite(declared) && declared > PUBLIC_IMAGE_MAX_BYTES) {return 'bad'} diff --git a/test/signInLook.test.ts b/test/signInLook.test.ts index 0fef82f..97a5763 100644 --- a/test/signInLook.test.ts +++ b/test/signInLook.test.ts @@ -225,6 +225,15 @@ describe('what the public sign-in page may show', () => { serve(PNG, { headers: { 'content-length': String(600 * 1024), 'content-type': 'image/png' } }) expect((await brand()).logo).toBeNull() vi.restoreAllMocks() + // a redirect is never followed by the server (it could point at an internal address): one request, to the + // logo's own address, asked not to follow; the upload document is believed and nothing is inlined from it + const redirected = serve(null, { headers: { location: 'http://169.254.169.254/latest/meta-data/' }, status: 302 }) + expect((await brand()).logo?.src).toBe(settings.logo.url) + expect(redirected).toHaveBeenCalledTimes(1) + expect(String(redirected.mock.calls[0][0])).toBe('https://site.test/api/media/file/logo.png') + expect((redirected.mock.calls[0][1] as RequestInit).redirect).toBe('manual') + expect(await checkLogoFile('https://site.test/l.png', null)).toBe('unknown') + 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)