Defense
Defending file upload pipelines
Hardening validation gates, isolating storage, and sandboxing parsers.
No single extension check ever secured a file upload pipeline for long. Real defense assumes the first gate will eventually leak, so each downstream station enforces its own boundary.
What to fix at each station
Section titled “What to fix at each station”Drop the matching items below straight into the remediation section of your report.
Station 1: Hardening the validation gate
Section titled “Station 1: Hardening the validation gate”- Use a strict allowlist of permitted file extensions.
- Reject any file containing null bytes, path separators, or double extensions.
- Verify file magic bytes using server-side signature libraries.
- Re-encode images using server libraries to strip hidden polyglot payloads.
Station 2: Securing file storage
Section titled “Station 2: Securing file storage”- Generate random file names using cryptographically secure identifiers like UUIDv4.
- Discard original user-supplied file names for storage paths.
- Save uploaded files outside the public web server root directory.
- Disable script execution permissions inside storage directories.
- Save uploads to a private temporary directory until validation finishes, then move them to final storage.
Station 3: Hardening the serving layer
Section titled “Station 3: Hardening the serving layer”- Serve user uploads from an isolated domain separate from primary application cookies.
- Set the
Content-Disposition: attachmentresponse header to prevent automatic browser execution. - Include the
X-Content-Type-Options: nosniffheader on every file response. - Apply a strict Content Security Policy that forbids script execution.
HTTP/1.1 200 OKContent-Type: image/jpegContent-Disposition: attachment; filename="document.jpg"X-Content-Type-Options: nosniffContent-Security-Policy: default-src 'none'Station 4: Sandboxing parser libraries
Section titled “Station 4: Sandboxing parser libraries”Run media processing libraries inside unprivileged containers. Apply process sandboxing using seccomp, AppArmor, or minimal container environments. Keep libraries such as ImageMagick, ExifTool, Ghostscript, and FFmpeg updated to current patch levels. Strip EXIF metadata from uploaded images before invoking complex rendering functions.
Station 5: Securing archives and structured documents
Section titled “Station 5: Securing archives and structured documents”When extracting zip archives, verify the canonical destination path of every archive entry. Reject any archive entry whose target path resolves outside the destination directory.
import os
def safe_extract(archive, target_directory): for entry in archive.namelist(): resolved_path = os.path.realpath(os.path.join(target_directory, entry)) if not resolved_path.startswith(os.path.realpath(target_directory) + os.sep): raise Exception("Directory traversal detected in archive entry") archive.extract(entry, target_directory)Disable external entity resolution in all server-side XML parsers.
Station 6: Hardening dormant exports and client files
Section titled “Station 6: Hardening dormant exports and client files”- Prefix spreadsheet fields starting with
=,+,-, or@with a single quote character. - Strip macros and embedded objects from uploaded Office files before storage.
- Warn staff before they open user-supplied attachments on their own workstation.
Next, the glossary maps every station here to its CWE identifier and historic CVEs.
File UploadFor authorized testing only.