Unrestricted File Upload

Unrestricted Upload of File with Dangerous Type

CWE-434 · Critical severity · Updated November 6, 2026

What it is

CWE-434 exists when a web application accepts file uploads without restricting type, size, or storage location, allowing an attacker to upload a file that is then executed or served as a different type than expected. The classic exploitation is uploading a PHP or JSP script to a web root and requesting it to obtain remote code execution.

Why it matters

File upload vulnerabilities map to immediate remote code execution in PHP, JSP, ASP, and CGI environments, and to stored XSS or content-spoofing in static asset stores. The pattern recurs in profile-picture uploaders, resume submission forms, document management features, and image conversion pipelines. Cloud storage with overly permissive ACLs amplifies the impact: a malicious file uploaded to a public-read S3 bucket can be linked from phishing sites under a trusted-looking domain.

Common patterns

  • Saving uploaded files inside a web-server document root using the client-supplied filename.
  • Validating type by checking only the file extension or the Content-Type header (both attacker-controlled).
  • Storing uploads without resetting executable bits or without an explicit Content-Disposition header.
  • Allowing archive uploads (zip, tar) that extract without validating entry paths (Zip Slip, CWE-22 overlap).
  • Accepting SVG uploads served with image/svg+xml: SVG can carry executable JavaScript (stored XSS).
  • Image preview pipelines that pass user-controlled files to ImageMagick, ffmpeg, or LibreOffice without sandboxing (CWE-78 overlap).

Languages affected

Python, JavaScript, TypeScript, Ruby, Java, Go, PHP

What Deva detects

Deva flags Flask upload handlers that save a file under a client-supplied filename. Other frameworks, extension allowlists, and archive extraction are not yet covered, so review those paths by hand.

Example

Vulnerable

import express from 'express'
import multer from 'multer'

const storage = multer.diskStorage({
  destination: 'public/uploads/',
  filename: (req, file, cb) => cb(null, file.originalname),
})
const upload = multer({ storage })
const app = express()
app.use('/uploads', express.static('public/uploads'))

app.post('/upload', upload.single('file'), (req, res) => {
  res.json({ url: `/uploads/${req.file.originalname}` })
})

Fixed

import express from 'express'
import multer from 'multer'
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
import { fileTypeFromFile } from 'file-type'

const ALLOWED_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif'])
const ALLOWED_MIME = new Set(['image/png', 'image/jpeg', 'image/gif'])

const upload = multer({
  dest: '/var/uploads-private/',
  limits: { fileSize: 5 * 1024 * 1024 },
  fileFilter: (req, file, cb) => {
    // Rejected files leave req.file unset, so the route returns 400.
    cb(null, ALLOWED_EXT.has(path.extname(file.originalname).toLowerCase()))
  },
})

const app = express()
app.post('/upload', upload.single('file'), async (req, res) => {
  if (!req.file) return res.status(400).json({ error: 'No file' })
  try {
    // Verify content by sniffing magic bytes, not by trusting client metadata.
    const detected = await fileTypeFromFile(req.file.path)
    if (!detected || !ALLOWED_MIME.has(detected.mime)) {
      await fs.promises.unlink(req.file.path)
      return res.status(400).json({ error: 'Bad file content' })
    }
    // Rename to a server-generated filename so attacker cannot control path.
    const safeName = crypto.randomBytes(16).toString('hex') + '.' + detected.ext
    const target = path.join('/var/uploads-private/', safeName)
    await fs.promises.rename(req.file.path, target)
    res.json({ id: safeName })
  } catch {
    res.status(500).json({ error: 'Upload failed' })
  }
})

// Not shown: serve files via a controlled route that sets Content-Disposition,
// not via static middleware from a web-accessible upload directory.

The vulnerable version stores uploads in a publicly-served directory using the attacker's filename. Uploading evil.html gives the attacker stored XSS on your origin at /uploads/evil.html, and on a server that executes uploaded scripts, evil.php becomes remote code execution. The fix stores uploads outside the web root, validates the extension against an allowlist, verifies content by sniffing magic bytes (file-type), and generates a server-side filename. Serving files through a controlled route that forces download via Content-Disposition (not shown) is the final layer, because a file can pass the magic-byte check and still contain script.

Where this fits in OWASP Top 10

  • A06 Insecure DesignArchitectural decisions that create vulnerabilities no amount of clean implementation can fix.

Compliance framework references

These references explain how the weakness relates to control topics. They do not mean Deva offers evaluation for every framework listed. CMMC evaluation and SPRS scoring are currently unavailable. See the FAQ for offered frameworks and preview status. Deva provides code-level evidence for your assessment, without certifying compliance.

FrameworkControls
OWASP Top 10:2025A06:2025 Insecure Design
NIST 800-53 Rev 5SI-10 Information Input Validation, SC-18 Mobile Code
PCI-DSS v4.0.16.2.4 Software engineering techniques
CMMC L2SI.L2-3.14.1 Flaw remediation

Related CWEs

  • CWE-22 · Path traversal: archive extraction often combines both.
  • CWE-78 · OS command injection via downstream image processing.
  • CWE-94 · Code injection via uploaded executable files.
  • CWE-73 · External control of file name or path.

Related glossary terms