|
|
|
@@ -12,9 +12,10 @@ import { PUBLIC_IMAGE_MAX_BYTES, rasterImageType, siteOrigin } from '../publicIm
|
|
|
|
|
* What may reach a page that anyone can open, before signing in:
|
|
|
|
|
* - the name as plain text (React escapes it), one line of at most `SIGN_IN_NAME_MAX` characters: no markup,
|
|
|
|
|
* no link, nothing an address inside it could turn into;
|
|
|
|
|
* - the logo as an `<img>`, and only a bitmap: JPEG, PNG, WebP, AVIF or GIF, as the upload says AND as the
|
|
|
|
|
* file's first bytes say (the file is looked at before it is printed; a deleted, broken or renamed file
|
|
|
|
|
* gives the name, never a broken picture). No SVG unless the project asks (`svgLogo: true`);
|
|
|
|
|
* - the logo as an `<img>` — every picture type a site's login screen showed before the sign-in look (1.17.0:
|
|
|
|
|
* whatever Site settings → Logo held, an SVG included; in `<img>` no picture runs script). The file is
|
|
|
|
|
* looked at before it is printed, only to avoid a broken picture: a deleted file or one that is no picture
|
|
|
|
|
* gives the name. `svgLogo: false` refuses SVG for a project that wants bitmaps only;
|
|
|
|
|
* - no account, no e-mail address, no hint whether an account exists: the forms are Payload's own.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
@@ -45,8 +46,8 @@ export type SignInLookProps = {
|
|
|
|
|
nameField?: string
|
|
|
|
|
/** Slug of the settings global. Default `'site-settings'`. */
|
|
|
|
|
settingsSlug?: string
|
|
|
|
|
/** `true`: an SVG logo is shown too (as a picture). Default: bitmaps only, an SVG logo gives the name. */
|
|
|
|
|
svgLogo?: true
|
|
|
|
|
/** `false`: an SVG logo is not shown on the sign-in screens (the name is). Default: shown, as a picture. */
|
|
|
|
|
svgLogo?: false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type SignInLogo = { height?: number; src: string; width?: number }
|
|
|
|
@@ -54,9 +55,6 @@ export type SignInBrand = { logo: null | SignInLogo; name: string }
|
|
|
|
|
|
|
|
|
|
type MediaLike = { filename?: unknown; height?: unknown; mimeType?: unknown; sizes?: unknown; url?: unknown; width?: unknown }
|
|
|
|
|
|
|
|
|
|
/** The picture types a logo may have on the sign-in screens: the bitmaps a browser shows. */
|
|
|
|
|
export const SIGN_IN_LOGO_TYPES = ['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif'] as const
|
|
|
|
|
|
|
|
|
|
const usableAddress = (url: unknown): url is string => typeof url === 'string' && /^(?:https?:\/\/|\/(?!\/))/i.test(url.trim())
|
|
|
|
|
|
|
|
|
|
/** A localized text field may arrive as a string or a { locale: value } map. */
|
|
|
|
@@ -103,7 +101,8 @@ function servedAddress(m: MediaLike, shownWidth: number): string {
|
|
|
|
|
if (!size || typeof size.width !== 'number' || typeof size.height !== 'number' || !usableAddress(size.url)) {continue}
|
|
|
|
|
if (size.width < 2 * shownWidth || size.width >= m.width || size.height < 1) {continue}
|
|
|
|
|
if (Math.abs(size.width / size.height - m.width / m.height) > 0.02) {continue}
|
|
|
|
|
if (typeof size.mimeType === 'string' && !(SIGN_IN_LOGO_TYPES as readonly string[]).includes(size.mimeType.toLowerCase())) {continue}
|
|
|
|
|
// a generated size is a bitmap; anything else under `sizes` is not what the logo's own file is
|
|
|
|
|
if (typeof size.mimeType === 'string' && (!size.mimeType.toLowerCase().startsWith('image/') || size.mimeType.toLowerCase().includes('svg'))) {continue}
|
|
|
|
|
if (!best || size.width < best.width) {best = { url: size.url.trim(), width: size.width }}
|
|
|
|
|
}
|
|
|
|
|
return best?.url ?? full
|
|
|
|
@@ -111,14 +110,15 @@ function servedAddress(m: MediaLike, shownWidth: number): string {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The logo of an upload document, or null when there is nothing to show: no address, an address that is not
|
|
|
|
|
* http(s) or site-relative, or a type that is not one of `SIGN_IN_LOGO_TYPES` (SVG only with `svgLogo: true`).
|
|
|
|
|
* http(s) or site-relative, an upload whose type says it is no picture (a PDF), or an SVG with `svgLogo: false`.
|
|
|
|
|
* An upload without a stated type is taken as 1.17.0 took it: its address is used.
|
|
|
|
|
*/
|
|
|
|
|
export function signInLogo(media: unknown, look: Pick<SignInLookProps, 'svgLogo'> = {}): null | SignInLogo {
|
|
|
|
|
if (!media || typeof media !== 'object') {return null}
|
|
|
|
|
const m = media as MediaLike
|
|
|
|
|
if (!usableAddress(m.url)) {return null}
|
|
|
|
|
if (isSvgUpload(m)) {return look.svgLogo === true ? { src: m.url.trim(), ...(fitLogo(m.width, m.height) ?? {}) } : null}
|
|
|
|
|
if (typeof m.mimeType !== 'string' || !(SIGN_IN_LOGO_TYPES as readonly string[]).includes(m.mimeType.toLowerCase())) {return null}
|
|
|
|
|
if (isSvgUpload(m)) {return look.svgLogo === false ? null : { src: m.url.trim(), ...(fitLogo(m.width, m.height) ?? {}) }}
|
|
|
|
|
if (typeof m.mimeType === 'string' && m.mimeType && !m.mimeType.toLowerCase().startsWith('image/')) {return null}
|
|
|
|
|
const fit = fitLogo(m.width, m.height)
|
|
|
|
|
return { src: fit ? servedAddress(m, fit.width) : m.url.trim(), ...(fit ?? {}) }
|
|
|
|
|
}
|
|
|
|
@@ -126,17 +126,19 @@ export function signInLogo(media: unknown, look: Pick<SignInLookProps, 'svgLogo'
|
|
|
|
|
export type LogoCheck = 'bad' | 'unknown' | { bytes: Buffer; type: string }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Looks at the logo's file the way an anonymous visitor gets it (no cookies, at most 512 KB):
|
|
|
|
|
* - the picture itself when it answers and its first bytes are one of the allowed bitmaps (an SVG, allowed
|
|
|
|
|
* by `svgLogo: true`, is taken by its `image/svg+xml` type);
|
|
|
|
|
* - `'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.
|
|
|
|
|
* Looks at the logo's file the way an anonymous visitor gets it (no cookies, at most 512 KB read). It exists to
|
|
|
|
|
* avoid a broken picture, never to narrow what a site may show: a logo a browser shows is not taken away.
|
|
|
|
|
* - the picture itself when the server answers 2xx with an `image/*` type and at most 512 KB;
|
|
|
|
|
* - `'bad'` only when the file is certainly not a picture a browser would show at that address: it is gone
|
|
|
|
|
* (404, 410), or the answer is 2xx with another type (an HTML page, a PDF), or it is an SVG and the project
|
|
|
|
|
* refuses SVG (`svgLogo: false`);
|
|
|
|
|
* - `'unknown'` for everything else — nothing answers (the site cannot reach its own address from inside its
|
|
|
|
|
* container), a timeout, a redirect, any other status (a firewall that refuses the server but not
|
|
|
|
|
* browsers), no type, more than 512 KB: the upload document is then believed and the browser decides.
|
|
|
|
|
* 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<LogoCheck> {
|
|
|
|
|
export async function checkLogoFile(url: string, base: null | string | undefined, allowSvg = true): Promise<LogoCheck> {
|
|
|
|
|
let target: URL
|
|
|
|
|
try {
|
|
|
|
|
target = new URL(url, base || undefined)
|
|
|
|
@@ -151,16 +153,31 @@ export async function checkLogoFile(url: string, base: null | string | undefined
|
|
|
|
|
return 'unknown'
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
if ((res.status >= 300 && res.status < 400) || res.type === 'opaqueredirect') {return 'unknown'}
|
|
|
|
|
if (!res.ok) {return res.status >= 500 ? 'unknown' : 'bad'}
|
|
|
|
|
if (res.status === 404 || res.status === 410) {return 'bad'}
|
|
|
|
|
if (!res.ok || res.type === 'opaqueredirect') {return 'unknown'}
|
|
|
|
|
const type = (res.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase()
|
|
|
|
|
if (!type) {return 'unknown'}
|
|
|
|
|
if (!type.startsWith('image/')) {return 'bad'}
|
|
|
|
|
if (!allowSvg && type.includes('svg')) {return 'bad'}
|
|
|
|
|
const declared = Number(res.headers.get('content-length'))
|
|
|
|
|
if (Number.isFinite(declared) && declared > PUBLIC_IMAGE_MAX_BYTES) {return 'bad'}
|
|
|
|
|
const bytes = Buffer.from(await res.arrayBuffer())
|
|
|
|
|
if (!bytes.length || bytes.length > PUBLIC_IMAGE_MAX_BYTES) {return 'bad'}
|
|
|
|
|
const declaredType = (res.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase()
|
|
|
|
|
if (allowSvg && declaredType === 'image/svg+xml') {return { type: declaredType, bytes }}
|
|
|
|
|
const type = rasterImageType(bytes)
|
|
|
|
|
return type && (SIGN_IN_LOGO_TYPES as readonly string[]).includes(type) ? { type, bytes } : 'bad'
|
|
|
|
|
if (Number.isFinite(declared) && declared > PUBLIC_IMAGE_MAX_BYTES) {return 'unknown'}
|
|
|
|
|
// read chunk by chunk: a body without a stated length stops at the limit
|
|
|
|
|
const reader = res.body?.getReader()
|
|
|
|
|
if (!reader) {return 'unknown'}
|
|
|
|
|
const chunks: Uint8Array[] = []
|
|
|
|
|
let size = 0
|
|
|
|
|
for (;;) {
|
|
|
|
|
const { done, value } = await reader.read()
|
|
|
|
|
if (done) {break}
|
|
|
|
|
size += value.byteLength
|
|
|
|
|
if (size > PUBLIC_IMAGE_MAX_BYTES) {return 'unknown'}
|
|
|
|
|
chunks.push(value)
|
|
|
|
|
}
|
|
|
|
|
if (!size) {return 'bad'}
|
|
|
|
|
const bytes = Buffer.concat(chunks)
|
|
|
|
|
// an SVG served under another image type is still refused when the project refuses SVG
|
|
|
|
|
if (!allowSvg && !rasterImageType(bytes) && /<svg[\s>]/i.test(bytes.subarray(0, 2048).toString('utf8'))) {return 'bad'}
|
|
|
|
|
return { type, bytes }
|
|
|
|
|
} catch {
|
|
|
|
|
return 'unknown'
|
|
|
|
|
} finally {
|
|
|
|
@@ -220,7 +237,7 @@ export async function loadSignInBrand({ locale, look, payload, user }: LoadArgs)
|
|
|
|
|
// collection: it gets the picture itself (the one just looked at), or the name.
|
|
|
|
|
const pending = Boolean(user && !secondFactorPassed({ payload, user }))
|
|
|
|
|
if (logo && (look.checkLogo !== false || pending) && !(pending && look.inlineImages === false)) {
|
|
|
|
|
const checked = await checkedLogo(logo.src, origin, look.svgLogo === true)
|
|
|
|
|
const checked = await checkedLogo(logo.src, origin, look.svgLogo !== false)
|
|
|
|
|
if (checked === 'bad') {
|
|
|
|
|
logo = null
|
|
|
|
|
} else if (pending) {
|
|
|
|
|