2FA setup required: review round 2 — no SVG from the icon endpoint, migrations at container start
R2-1: /api/ipal/admin-icon answers with bytes only for a bitmap recognised by its first bytes (PNG, JPEG, GIF, WebP, ICO, AVIF). An SVG favicon keeps the redirect to the media file: a site-wide CSP rule overrides a route's own sandbox header, so the header alone protected nothing in a built site. buildUploadSecurityHeaders also returns a rule for the icon path, as a second layer. R2-2: the SQL migration runs when the new container starts (prodMigrations, or the start command), never in Coolify's pre-deployment command; docs and the log line say so and point to deployment.md 4.3. R2-3: requireSetup: null is the default. R2-4: the missing-column log line only for a column error. R2-5, R2-6: the resetAccess example keeps the code check; with guard: false the reset endpoint does not exist. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
75a6f5209e
commit
01e3813723
+1
-1
@@ -40,7 +40,7 @@ export type { SectionHeaderFieldOptions, SectionSettingsFieldOptions } from './m
|
||||
export { buildAdminImageEndpoints } from './modules/branding/adminImages.js'
|
||||
export { ADMIN_META_TEXTS, adminMetaForSite, buildAdminMeta, DEFAULT_AGENCY, syncAdminMeta } from './modules/branding/adminMeta.js'
|
||||
export type { AdminMetaOptions, AgencyOption, ResolvedAdminMeta } from './modules/branding/adminMeta.js'
|
||||
export { fetchPublicImage, PUBLIC_IMAGE_MAX_BYTES, publicImageDataUri, siteOrigin } from './modules/branding/publicImage.js'
|
||||
export { fetchPublicImage, PUBLIC_IMAGE_MAX_BYTES, publicImageDataUri, rasterImageType, siteOrigin } from './modules/branding/publicImage.js'
|
||||
export {
|
||||
ACCEPT_ALL_CONSENT,
|
||||
buildConsentLogCollection,
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { AgencyOption, ResolvedAdminMeta } from './adminMeta.js'
|
||||
|
||||
import { UPLOAD_SECURITY_HEADERS } from '../security/buildSecurityHeaders.js'
|
||||
import { ADMIN_ICON_PATH, ADMIN_META_TEXTS, ADMIN_OG_PATH } from './adminMeta.js'
|
||||
import { fetchPublicImage } from './publicImage.js'
|
||||
import { fetchPublicImage, rasterImageType } from './publicImage.js'
|
||||
|
||||
/**
|
||||
* The panel's link-preview image (1200×630) and icon, drawn from SiteSettings:
|
||||
@@ -112,14 +112,18 @@ export function buildAdminImageEndpoints(resolved: ResolvedAdminMeta, fields: Fi
|
||||
// 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 = fields.serveIcon === false ? null : await fetchPublicImage(absolute(brand.icon, req), null)
|
||||
// An uploaded file from the site's own origin: the same headers as the upload routes
|
||||
// (an SVG may be displayed, never run script — sandbox, no sniffing).
|
||||
if (image) {
|
||||
// Only a bitmap is answered from here, recognised by its first bytes — never an SVG, which
|
||||
// is a document that can run script in the site's origin. The headers below are the upload
|
||||
// routes' (sandbox, no sniffing), but a Next project's site-wide CSP rule overrides a
|
||||
// route's own header, so they are a second layer, not the protection.
|
||||
const type = image ? rasterImageType(image.bytes) : null
|
||||
if (image && type) {
|
||||
return new Response(new Uint8Array(image.bytes), {
|
||||
headers: { 'Cache-Control': CACHE, 'Content-Type': image.type, ...Object.fromEntries(UPLOAD_SECURITY_HEADERS.map((h) => [h.key, h.value])) },
|
||||
headers: { 'Cache-Control': CACHE, 'Content-Type': type, ...Object.fromEntries(UPLOAD_SECURITY_HEADERS.map((h) => [h.key, h.value])) },
|
||||
})
|
||||
}
|
||||
// Not fetchable from here (too large, or media is not public): let the browser try.
|
||||
// An SVG favicon, or not fetchable from here (too large, media is not public): the browser
|
||||
// goes to the media file itself, where the upload route's own headers apply.
|
||||
return new Response(null, { headers: { 'Cache-Control': CACHE, Location: absolute(brand.icon, req) }, status: 302 })
|
||||
}
|
||||
const ImageResponse = await imageResponse()
|
||||
|
||||
@@ -37,6 +37,24 @@ export async function fetchPublicImage(url: null | string | undefined, base: nul
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The media type of a bitmap image, read from the first bytes — never from what a server or a
|
||||
* file name claims: PNG, JPEG, GIF, WebP, ICO, AVIF. Null for anything else, **SVG included**:
|
||||
* an SVG is a document that can carry script, and bytes answered from the site's own origin
|
||||
* must never be one (the site-wide CSP of a Next project overrides a route's own sandbox header).
|
||||
*/
|
||||
export function rasterImageType(bytes: Uint8Array): null | string {
|
||||
const b = bytes
|
||||
const ascii = (from: number, text: string) => text.split('').every((ch, i) => b[from + i] === ch.charCodeAt(0))
|
||||
if (b.length >= 8 && b[0] === 0x89 && ascii(1, 'PNG') && b[4] === 0x0d && b[5] === 0x0a && b[6] === 0x1a && b[7] === 0x0a) {return 'image/png'}
|
||||
if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) {return 'image/jpeg'}
|
||||
if (b.length >= 6 && (ascii(0, 'GIF87a') || ascii(0, 'GIF89a'))) {return 'image/gif'}
|
||||
if (b.length >= 12 && ascii(0, 'RIFF') && ascii(8, 'WEBP')) {return 'image/webp'}
|
||||
if (b.length >= 4 && b[0] === 0 && b[1] === 0 && b[2] === 1 && b[3] === 0) {return 'image/x-icon'}
|
||||
if (b.length >= 12 && ascii(4, 'ftyp') && (ascii(8, 'avif') || ascii(8, 'avis'))) {return 'image/avif'}
|
||||
return null
|
||||
}
|
||||
|
||||
/** The body, or null as soon as it grows past `limit` (the rest is not downloaded). */
|
||||
async function readUpTo(res: Response, limit: number): Promise<Buffer | null> {
|
||||
const reader = res.body?.getReader()
|
||||
|
||||
@@ -157,11 +157,25 @@ export function buildSecurityHeaders(args: BuildSecurityHeadersArgs = {}): Secur
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
export function buildUploadSecurityHeaders({ collections = ['media'] }: { collections?: string[] } = {}): {
|
||||
export function buildUploadSecurityHeaders({
|
||||
adminIcon = '/api/ipal/admin-icon',
|
||||
collections = ['media'],
|
||||
}: {
|
||||
/**
|
||||
* Path of the plugin's panel icon endpoint, which answers with the favicon's bytes. It only ever
|
||||
* answers with a bitmap; the rule is a second layer. Another API route: `'/cms-api/ipal/admin-icon'`;
|
||||
* `false` = no rule.
|
||||
*/
|
||||
adminIcon?: false | string
|
||||
collections?: string[]
|
||||
} = {}): {
|
||||
headers: SecurityHeader[]
|
||||
source: string
|
||||
}[] {
|
||||
return collections.map((slug) => ({ headers: [...UPLOAD_SECURITY_HEADERS], source: `/api/${slug}/file/:path*` }))
|
||||
return [
|
||||
...collections.map((slug) => ({ headers: [...UPLOAD_SECURITY_HEADERS], source: `/api/${slug}/file/:path*` })),
|
||||
...(adminIcon ? [{ headers: [...UPLOAD_SECURITY_HEADERS], source: adminIcon }] : []),
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -195,14 +195,15 @@ export async function backfillSetupFrom(payload: BasePayload, collection: string
|
||||
return totalDocs
|
||||
} catch (err) {
|
||||
const reason = err instanceof Error ? err.message : String(err)
|
||||
// On Postgres / SQLite this is the missing column — and then it is not only this step that fails:
|
||||
// every query on the collection selects the column, so EVERY login answers 500 until it exists.
|
||||
const adapter = (payload.db as { name?: unknown } | undefined)?.name
|
||||
if (/postgres|sqlite/i.test(typeof adapter === 'string' ? adapter : '') || /column|no such/i.test(reason)) {
|
||||
// On Postgres / SQLite without the migration the column is missing — and then it is not only this step
|
||||
// that fails: every query on the collection selects the column, so EVERY login answers 500 until it exists.
|
||||
// Only the missing column gets this message — a database that cannot be reached is another problem.
|
||||
if (/column .+ does not exist|no such column|unknown column|has no column/i.test(reason)) {
|
||||
payload.logger.error(
|
||||
`[ipal] 2FA: the column for "${SETUP_FROM_FIELD}" is missing on "${collection}" (${reason}). NOBODY CAN LOG IN to the panel until the ` +
|
||||
'migration that adds it has run — administrators with 2FA included. Run `pnpm payload migrate:create`, commit the migration and ' +
|
||||
'run it (`pnpm payload migrate`) before this version serves requests (docs/security.md "Setup required" → Data).',
|
||||
'migration that adds it has run — administrators with 2FA included. Run `pnpm payload migrate:create`, commit the migration, and ' +
|
||||
'have it applied when the new container starts (`prodMigrations` in the database adapter, or `pnpm payload migrate && pnpm start`) — ' +
|
||||
'not in a pre-deployment command, which runs in the old container (docs/deployment.md §4.3, docs/security.md "Setup required" → Data).',
|
||||
)
|
||||
} else {
|
||||
payload.logger.warn(`[ipal] 2FA: could not start the setup window for existing accounts (${reason}).`)
|
||||
|
||||
+1
-1
@@ -305,7 +305,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
// "Setup required" (default): an account without TOTP reaches nothing but the setup
|
||||
// screen, and only within the window; requireSetup: false = the package's rule.
|
||||
const setup = resolveRequireSetup(tf.requireSetup)
|
||||
const setupParts = typeof tf.requireSetup === 'object' ? tf.requireSetup : {}
|
||||
const setupParts = tf.requireSetup && typeof tf.requireSetup === 'object' ? tf.requireSetup : {} // null = the default, like undefined
|
||||
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, twoFactor: { collection: target, ...setup } } }
|
||||
if (setup.requireSetup) {
|
||||
setupRequiredFor = target
|
||||
|
||||
Reference in New Issue
Block a user