2FA screens: the brand image loads for a session that still owes its code; browser spec checks it
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]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
2aaaaa115a
commit
5d44ac3135
@@ -2,6 +2,8 @@ import type { Payload } from 'payload'
|
||||
|
||||
import { ipalT } from '../admin-i18n/t.js'
|
||||
import { settingsTexts } from '../admin-i18n/texts/settings.js'
|
||||
import { secondFactorPassed } from '../security/totp/secondFactor.js'
|
||||
import { publicImageDataUri, siteOrigin } from './publicImage.js'
|
||||
|
||||
/**
|
||||
* White-label admin branding: the Payload admin panel's logo (login + nav) and
|
||||
@@ -33,6 +35,8 @@ type BrandingProps = {
|
||||
payload?: Payload
|
||||
/** SiteSettings global slug. Default 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
/** Injected by Payload into admin server components — the account looking at the panel. */
|
||||
user?: null | Record<string, unknown>
|
||||
}
|
||||
|
||||
type MediaLike = { url?: null | string } | null | undefined
|
||||
@@ -70,6 +74,13 @@ async function loadBranding(
|
||||
const image = settings[imageField] as MediaLike
|
||||
const url =
|
||||
image && typeof image === 'object' && typeof image.url === 'string' ? image.url : null
|
||||
// A session that still owes its 2FA code (or has not set 2FA up) is refused `media` like every
|
||||
// other collection, so the browser would show a broken image on the verify / setup screens.
|
||||
// Give it the picture itself, fetched as an anonymous visitor would get it; when that fails
|
||||
// (media is not public), the site name is shown instead.
|
||||
if (url && props.user && !secondFactorPassed({ payload, user: props.user })) {
|
||||
return { name, url: await publicImageDataUri(url, siteOrigin(payload.config.serverURL)) }
|
||||
}
|
||||
return { name, url }
|
||||
} catch (error) {
|
||||
console.warn('[ipal] admin branding: falling back to Payload default:', error)
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Endpoint, PayloadRequest } from 'payload'
|
||||
import type { AgencyOption, ResolvedAdminMeta } from './adminMeta.js'
|
||||
|
||||
import { ADMIN_ICON_PATH, ADMIN_META_TEXTS, ADMIN_OG_PATH } from './adminMeta.js'
|
||||
import { fetchPublicImage } from './publicImage.js'
|
||||
|
||||
/**
|
||||
* The panel's link-preview image (1200×630) and icon, drawn from SiteSettings:
|
||||
@@ -105,6 +106,13 @@ export function buildAdminImageEndpoints(resolved: ResolvedAdminMeta, fields: Fi
|
||||
handler: async (req) => {
|
||||
const brand = await loadBranding(req, f)
|
||||
if (brand.icon) {
|
||||
// The picture itself, fetched the way an anonymous visitor gets it — not a redirect to the
|
||||
// media file: a panel session that still owes its 2FA code is refused that file (it is
|
||||
// refused every collection), and a cached redirect would send it there anyway. The answer
|
||||
// is the same for everyone, so it can be cached like the redirect was.
|
||||
const image = await fetchPublicImage(absolute(brand.icon, req), null)
|
||||
if (image) {return new Response(new Uint8Array(image.bytes), { headers: { 'Cache-Control': CACHE, 'Content-Type': image.type } })}
|
||||
// Not fetchable from here (too large, or media is not public): let the browser try.
|
||||
return new Response(null, { headers: { 'Cache-Control': CACHE, Location: absolute(brand.icon, req) }, status: 302 })
|
||||
}
|
||||
const ImageResponse = await imageResponse()
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* An image of the site's brand fetched the way an anonymous visitor would get
|
||||
* it: no cookies, no credentials. For the moments when the browser itself
|
||||
* cannot load it — a panel session that still owes its 2FA code (or has not
|
||||
* set 2FA up) is refused every collection, `media` included, although the
|
||||
* same file is public. Serving the bytes from here opens nothing: only what
|
||||
* answers an anonymous request comes back, and only an image of a sane size.
|
||||
*/
|
||||
const MAX_BYTES = 512 * 1024
|
||||
|
||||
export async function fetchPublicImage(url: null | string | undefined, base: null | string | undefined): Promise<{ bytes: Buffer; type: string } | null> {
|
||||
if (!url) {return null}
|
||||
let target: URL
|
||||
try {
|
||||
target = new URL(url, base || undefined)
|
||||
} catch {
|
||||
return null // a relative address and no site origin to resolve it against
|
||||
}
|
||||
if (target.protocol !== 'http:' && target.protocol !== 'https:') {return null}
|
||||
try {
|
||||
const res = await fetch(target, { credentials: 'omit', redirect: 'follow', signal: AbortSignal.timeout(5000) })
|
||||
if (!res.ok) {return null}
|
||||
const type = (res.headers.get('content-type') ?? '').split(';')[0].trim()
|
||||
if (!type.startsWith('image/')) {return null}
|
||||
const bytes = Buffer.from(await res.arrayBuffer())
|
||||
if (!bytes.length || bytes.length > MAX_BYTES) {return null}
|
||||
return { type, bytes }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** The same as a `data:` address for an `<img>`. */
|
||||
export async function publicImageDataUri(url: null | string | undefined, base: null | string | undefined): Promise<null | string> {
|
||||
const image = await fetchPublicImage(url, base)
|
||||
return image ? `data:${image.type};base64,${image.bytes.toString('base64')}` : null
|
||||
}
|
||||
|
||||
/** The site's own origin as the config or the environment states it, for resolving `/api/media/file/…`. */
|
||||
export const siteOrigin = (serverURL: null | string | undefined): null | string => serverURL || process.env.NEXT_PUBLIC_SERVER_URL || null
|
||||
Reference in New Issue
Block a user