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:
Radosław Smoliński
2026-11-24 20:18:47 +01:00
co-authored by Claude Fable 5.2
parent 75a6f5209e
commit 01e3813723
20 changed files with 155 additions and 53 deletions
+10 -6
View File
@@ -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()
+18
View File
@@ -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()