fix(import): validate JSON-import media bytes + destination like the archive path
What & why
The JSON SiteBundle import endpoint (POST /admin/api/cms/import) wrote each media[].bytesBase64 to join(uploadsDir, storagePath) guarded only by a traversal check — no magic-byte MIME detection, no SVG sanitisation, no extension check (the archive import path already had all three).
Because published/ lives under uploadsDir and the public router serves it verbatim as text/html with no CSP, on the same origin as /admin, a caller holding only data.import (merge-add needs no step-up and no content.manage) could POST storagePath: "published/current/index.html" with <script> bytes and land arbitrary same-origin HTML/JS → stored XSS → admin account takeover (mint an owner role / reset the owner password / exfiltrate).
Fix (at the source, shared)
Made the existing gate shared and applied it to both import paths:
- Renamed
importArchiveMediaValidation.ts→importMediaValidation.ts(now used by both routes), split into two focused gates:validateAndSanitizeMediaBytes(bytes, {storagePath, mimeType})— content: detected MIME must match the declared type and the storagePath extension; SVG is sanitised. No accepted media MIME maps to.html/.js, so script writes are rejected outright.resolveMediaWriteTarget(uploadsDir, storagePath)— destination: no traversal (viaassertPathWithin), and never a reserved served subtree (published/,plugins/,fonts/).
- The JSON import media write now runs through both gates (was unguarded).
- The archive import now calls the same two gates — behaviour-preserving (it already buffered bytes for MIME detection and still moves non-SVG files zero-copy).
Impact
- Developer: one shared media-import gate instead of a duplicated/absent one.
- User: closes a
data.import→ same-origin account-takeover primitive. No API/behaviour change for legitimate imports.
Verification
bun test server/handlers/cms/__tests__/importMediaValidation.test.ts server/handlers/cms/__tests__/importArchiveMedia.test.ts— 14 pass (HTML/JS, MIME-mismatch, extension-laundering, reserved-subtree writes all rejected; SVG scripts stripped).bun test src/__tests__/architecture/cms-handlers-capability-gated.test.ts— pass (allowlist updated for the rename).bunx tsc -b— clean.eslinton touched files — clean.- Pre-existing unrelated failures (
icon-catalog-integrity,bundle-size-budgets) confirmed failing on the untouched base; not part of this change.
🤖 Generated with Claude Code