Remediation guides

Fixing Unrestricted File Upload: Validation, Storage and Downloads

Fix insecure file uploads with format allowlists, private storage, processing limits and authorised downloads. Includes PHP validation and retest scenarios.

An unrestricted file upload finding can describe several different weaknesses: executable files stored under a web root, active content served from a trusted origin, unsafe document processing or missing permission checks. Read the demonstrated impact before choosing a fix.

The objective is a controlled file lifecycle: receive, validate, store, process, retrieve and delete. Checking the filename extension alone does not protect that lifecycle.

Define permitted files and limits

List the formats required for the business feature. Set limits for file size, file count, user quota, processing time and expanded archive size where archives are genuinely needed. Reject unexpected formats rather than trying to identify every dangerous extension.

Check the extension, detected content type and the structure accepted by the relevant parser. Browser-supplied Content-Type is untrusted. File signatures can improve validation, but do not prove that a file is harmless or safe for every downstream processor.

Validate uploads on the server

This PHP fragment illustrates basic checks for a JPEG/PNG upload after authentication, authorisation and CSRF validation have succeeded:

$upload = $_FILES['document'] ?? null;
if (!is_array($upload)
    || !isset($upload['error'], $upload['tmp_name'])
    || !is_int($upload['error'])
    || !is_string($upload['tmp_name'])
    || $upload['error'] !== UPLOAD_ERR_OK
    || !is_uploaded_file($upload['tmp_name'])) {
    http_response_code(400);
    exit;
}
$size = filesize($upload['tmp_name']);
if ($size === false || $size < 1 || $size > 5 * 1024 * 1024) {
    http_response_code(413);
    exit;
}
$types = ['image/jpeg' => 'jpg', 'image/png' => 'png'];
$mime = (new finfo(FILEINFO_MIME_TYPE))->file($upload['tmp_name']);
if (!isset($types[$mime])) {
    http_response_code(415);
    exit;
}
$storageName = bin2hex(random_bytes(24)) . '.' . $types[$mime];

The example stops before storage. Production handling still needs safe image parsing or re-encoding where appropriate, private storage, metadata persistence, error cleanup and an authorised download path. Apply matching request limits at the web server and PHP layers so oversized requests fail predictably.

Separate storage from execution

Store files outside the executable web root or in a private object store. Use a server-generated storage key and keep the original filename only as sanitised display metadata. Do not build filesystem paths from an uploaded filename.

The application process should have only the storage permissions it needs. A random name helps prevent collisions and guessing; it does not replace authorisation. Keep quarantine and accepted-file states separate so an unreviewed object cannot be downloaded through an alternate route.

Treat processing as a trust boundary

Run parsers, thumbnailers and document converters with limited permissions, supported libraries and explicit resource limits. Disable network access where processing does not need it. Avoid passing user-controlled filenames into a shell command.

Malware scanning can add useful detection, but a clean verdict is not a guarantee of a safe format or processing path. Record the scanned object version, handle unavailable scanners deliberately and prevent a file from being substituted after validation.

Authorise every download

Look up the stored object through the requesting user's permissions. Return an accurate Content-Type, suitable Content-Disposition and nosniff. Consider a separate content origin for untrusted material that must be displayed in a browser.

When issuing signed URLs, constrain their lifetime and scope. Check that access revocation, replaced files and deleted records behave as intended; a previously issued link may remain usable until it expires.

Retest the complete feature

Try oversized files, mismatched extensions, malformed images, repeated uploads and unauthorised retrieval with controlled test data. Confirm that rejected files leave no public object or orphaned temporary copy. Test a known-valid file to ensure the business feature still works, and verify that files cannot execute through any public route.

Further reading: OWASP file upload guidance and PHP upload handling.

Read our testing methodology or discuss your assessment.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo