Skip to content

Defense

Defending file upload pipelines

Hardening validation gates, isolating storage, and sandboxing parsers.

2 min readFile Upload

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.

Drop the matching items below straight into the remediation section of your report.

  1. Use a strict allowlist of permitted file extensions.
  2. Reject any file containing null bytes, path separators, or double extensions.
  3. Verify file magic bytes using server-side signature libraries.
  4. Re-encode images using server libraries to strip hidden polyglot payloads.
  1. Generate random file names using cryptographically secure identifiers like UUIDv4.
  2. Discard original user-supplied file names for storage paths.
  3. Save uploaded files outside the public web server root directory.
  4. Disable script execution permissions inside storage directories.
  5. Save uploads to a private temporary directory until validation finishes, then move them to final storage.
  1. Serve user uploads from an isolated domain separate from primary application cookies.
  2. Set the Content-Disposition: attachment response header to prevent automatic browser execution.
  3. Include the X-Content-Type-Options: nosniff header on every file response.
  4. Apply a strict Content Security Policy that forbids script execution.
HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Disposition: attachment; filename="document.jpg"
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'none'

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”
  1. Prefix spreadsheet fields starting with =, +, -, or @ with a single quote character.
  2. Strip macros and embedded objects from uploaded Office files before storage.
  3. 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.