--- name: file-upload-security description: Accept user file uploads without introducing remote code execution, stored XSS, or polyglot attacks. Covers magic-byte validation, strict type allowlists, image re-encoding to defang embedded payloads, EXIF stripping, virus scanning, path-safe storage keys, and serving via a separate origin with Content-Disposition. Invoke when adding upload to a new endpoint or migrating from local-disk storage to object storage. --- # File Upload Security User-uploaded files are one of the highest-leverage attack surfaces. A single endpoint that accepts `*` MIME types and writes to a server-served path is a path to RCE, XSS, SSRF, stored-XSS in PDFs, and a half-dozen other failure modes. The defaults of every web framework do **less** than they should — this skill is what to add on top. Generic, not CMS-specific. For Payload-specific tuning see [`payload-cms-security`](../payload-cms-security/SKILL.md). For WordPress see [`wordpress-hardening`](../wordpress-hardening/SKILL.md). For storage choice / backend architecture see [`backend-architecture`](../backend-architecture/SKILL.md). ## When to invoke - Adding file upload to an endpoint for the first time - A user-uploaded file caused or contributed to an incident - Migrating from local-disk uploads to object storage (R2 / S3 / Spaces) - Reviewing an existing upload feature you inherited - Adding new file types (e.g. previously images only, now PDFs too) ## The threat model A user can upload anything labeled as anything. From your perspective: - The **filename** is attacker-controlled — `../../etc/passwd`, `🔥.jpg.exe`, `index.html` - The **`Content-Type` header** is attacker-controlled — JPG bytes claiming to be `application/json`, executable bytes claiming to be `image/png` - The **file extension** is attacker-controlled - The **file contents** may be a polyglot — bytes that are valid as image *and* valid as HTML / JavaScript / PHP Defense: trust nothing the client says about the file. Inspect the bytes, re-derive the type, generate your own key for storage, serve from an origin that cannot execute anything. ## Step 1 — Validate at the boundary ### Size limits, server-side ```ts import multer from 'multer'; const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 10 * 1024 * 1024, // 10 MB hard cap files: 5, fields: 20, fieldSize: 1024, fieldNameSize: 100, }, }); app.post('/api/upload', requireAuth, upload.single('file'), async (req, res) => { if (!req.file) return res.status(400).json({ error: 'no file' }); // ... continue with content validation }); ``` Defaults from middleware libraries are too generous (50MB+). Set what you actually need. Different endpoints get different caps — avatar upload doesn't need 10MB. ### Magic-byte detection (not MIME) `Content-Type` is a hint. Read the first bytes to determine the *actual* type. ```ts import { fileTypeFromBuffer } from 'file-type'; const detected = await fileTypeFromBuffer(req.file.buffer); if (!detected) { return res.status(400).json({ error: 'unrecognized file type' }); } const ALLOWED_MIMES = new Set([ 'image/jpeg', 'image/png', 'image/webp', 'application/pdf', ]); if (!ALLOWED_MIMES.has(detected.mime)) { return res.status(400).json({ error: `type ${detected.mime} not allowed` }); } ``` For Python: `python-magic` (`libmagic` bindings). For Go: `mimetype`. For Ruby: `marcel`. Allowlist, not denylist. "Anything except `.exe` and `.php`" misses `.phtml`, `.phar`, `.shtml`, `.cgi`, `.pl`, `.jsp`, `.aspx`, and tomorrow's bypass. ### Filename — never trust the client's Never store with the client-supplied name. Generate one server-side. ```ts import { randomUUID } from 'node:crypto'; const ext = detected.ext; // from file-type, not from filename const storageKey = `uploads/${userId}/${randomUUID()}.${ext}`; ``` Patterns to enforce in the storage key: - **Generated, not user-supplied** — UUID, ULID, or content-hash (SHA-256 of bytes — also gives you free dedup) - **No path traversal possible** — no `..`, no leading `/`, no special characters. With a UUID this is automatic. - **Scoped by owner** — `uploads//` makes per-user access policies trivial - **No executable extensions** — extension comes from server-detected type, not client filename If you must keep the original filename for display, store it as a separate database field, never as part of the storage key. ## Step 2 — Defang content The bytes are the problem. After validation, transform the file so any embedded payload becomes inert. ### Images: re-encode Run every image through `sharp` (or equivalent) to a known format. This: - Strips polyglot bytes — the re-encoded image is just the visual content - Removes EXIF metadata — including geotags (PII leak) - Normalizes the format — your storage layer holds known-good images ```ts import sharp from 'sharp'; const processed = await sharp(req.file.buffer) .rotate() // honor EXIF orientation, then drop EXIF .resize({ width: 4096, height: 4096, fit: 'inside', withoutEnlargement: true }) .jpeg({ quality: 85, progressive: true }) // or .webp() for modern .withMetadata({ exif: {} }) // strip EXIF .toBuffer(); // Upload `processed`, not `req.file.buffer` ``` A PHP webshell embedded in a JPEG file does not survive re-encoding. So does most ImageTragick-style abuse. The cost is a few hundred ms of CPU per upload. ### PDFs and documents Re-encoding a PDF is harder than re-encoding an image, but you can: - **Strip JavaScript** with `qpdf` / `pdfcpu` — removes embedded JS that some PDFs use for "interactive" forms (or for malware) - **Convert to PDF/A** — a more restricted PDF subtype, no JS allowed - **Render to image + new PDF** — destroys text but eliminates everything else; only when you really need it (legal redaction, etc.) ```bash # Strip JavaScript and re-write the PDF qpdf --decrypt --remove-restrictions input.pdf cleaned.pdf # Or convert to PDF/A qpdf --object-streams=disable --linearize input.pdf cleaned.pdf ``` ### Office documents Active content is the issue (VBA macros, OOXML scripting). Three options, increasingly strict: 1. **Scan for macros** — reject if present (LibreOffice headless can introspect) 2. **Convert to PDF** server-side and store the PDF — destroys macros and most other active content 3. **Don't accept Office docs** — accept PDF only, instruct users to convert client-side ### Generic / unknown types If you really must accept "any file", treat the upload like a quarantine zone: - Never serve from a domain that can execute - Always with `Content-Disposition: attachment` (forces download, not inline render) - Pass through a virus scanner (ClamAV is free, decent for known malware) — see Step 4 ## Step 3 — Storage and serving ### Storage: object storage, not local disk See [`backend-architecture`](../backend-architecture/SKILL.md) for the "images disappear on redeploy" trap. For uploads specifically: - **S3-compatible storage** (S3, R2, B2, Spaces, MinIO if self-hosted) - **Private bucket** with signed URLs for access — never make the bucket public unless the content really is public - **Path scoped by owner** — `uploads//` so IAM policies can restrict access by prefix ### Serving: separate origin, no script execution The cardinal sin: serving user uploads from the same origin as your app, with `Content-Type` derived from extension and no `Content-Disposition`. An attacker uploads `xss.html`, your server serves it as `text/html` from your app's origin, the attacker links to it, and now they have stored XSS with same-origin access to your auth cookies. Patterns: - **Serve from a separate domain** — `cdn.example.com` or `uploads.example.com`. Even if XSS lands here, your auth cookies are not in scope. - **`Content-Disposition: attachment`** for non-image uploads — forces download instead of inline rendering for browsers that would otherwise sniff into something dangerous - **Strict `Content-Type`** — match the server-detected type from Step 1, not the original - **`X-Content-Type-Options: nosniff`** — disables browser MIME-sniffing - **`Content-Security-Policy: default-src 'none'`** for the uploads origin — even if HTML lands there, the CSP refuses to render scripts/images/anything - **No directory listing** on the bucket ```nginx # Example serving config for the upload origin (nginx fronting R2/S3) server { listen 443 ssl; server_name uploads.example.com; add_header X-Content-Type-Options nosniff always; add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'unsafe-inline';" always; add_header Strict-Transport-Security "max-age=31536000" always; # Force download for anything not in the image allowlist location ~* \.(jpg|jpeg|png|webp|gif|svg)$ { # served inline as images } location / { add_header Content-Disposition "attachment" always; } } ``` ### SVG is special SVG is XML with `