Secure File Upload in Web Applications: Validation, Storage and Scanning

Secure file upload: type validation with an allowlist and magic bytes, storage outside the web root, signed URLs, quarantine with ClamAV and safe serving headers.
Technical summary
Technology
OWASP, Nginx, Node.js, AWS S3 presigned POST, ClamAV, HTTP security headers
Topic
Developing the file upload feature securely in a web application
Level
Intermediate–advanced
Prerequisites
  • Knowledge of HTTP request/response structure and headers
  • Server-side web development experience
In this article7 sections

In a web application, file upload is the point where the user leaves data on the server with the broadest control: the content, name, size and type come from the user. An innocent field opened for invoice attachments, CVs, technical drawings or profile photos, when set up wrongly, opens the door to remote code execution, a full disk or malicious content being served to other users. A .php file running in the uploads/ directory in the web root is still one of the most common traces found in incident investigations. Below, with configuration examples, we explain the risks and layered measures, from type validation to storage and from signed URLs to scanning and safe serving. You can examine the effect of file and connection controls in the production environment with an independent web application security audit.

The risk surface of file uploads

Classifying the risks clarifies which measure is against which threat and breaks the misconception “we checked the extension, that’s enough”:

Risk How does it happen? Basic measure
Code execution on the server A file such as .php or .jsp lands in an executable directory in the web root Storage outside the web root, disabling execution
Path traversal / overwriting The file name contains ../../ or collides with an existing file A server-generated name
Content type spoofing The extension and Content-Type header are changed on the client Type detection from content (magic bytes)
Malicious content for users A script inside HTML/SVG is served from the same origin (stored XSS) A separate domain, attachment, nosniff
Resource consumption (DoS) Very large files, many uploads, zip bombs Size, count and rate limits
Malware distribution The uploaded file is downloaded by other users Scanning and quarantine

None of these measures is enough on its own. They’re stacked so that when one is bypassed, the next one catches it. The comprehensive reference on the topic is the OWASP File Upload Cheat Sheet.

Layers of file type validation

Type validation uses an allowlist: the types to be accepted are listed explicitly according to business needs. A blocklist of forbidden extensions is always incomplete; variants such as .phtml, .php5 and .phar, or other extensions depending on the server configuration, get overlooked. A blocklist is always breached one day.

  1. Extension: The last extension of the file name is converted to lowercase and compared with the allowlist. Double extensions such as invoice.pdf.php and tricks such as a trailing dot or space are eliminated at this step.
  2. The MIME type declared by the client: The client sets the Content-Type header and changes it easily; it’s used only for initial screening.
  3. Content signature (magic bytes): The first bytes of the file show its real format. If the extension, declared type and content signature aren’t consistent, the file is rejected.
  4. Structural validation or re-processing: If images are opened with an image library and re-encoded (e.g. re-saved as JPEG), hidden extra data and metadata (including location information in EXIF) are removed.

Signatures of common formats:

Format First bytes (hex) Note
PDF 25 50 44 46 2D (%PDF-) PDFs can contain JavaScript and embedded files
PNG 89 50 4E 47 0D 0A 1A 0A Re-encoding recommended
JPEG FF D8 FF EXIF metadata should be stripped
ZIP / DOCX / XLSX 50 4B 03 04 Office documents are ZIP containers; macro-enabled types should be assessed separately
// Node.js: consistency of extension + content signature (allowlist)
import { extname } from 'node:path';

const ALLOWED = {
  '.pdf':  [[0x25, 0x50, 0x44, 0x46, 0x2d]],
  '.png':  [[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]],
  '.jpg':  [[0xff, 0xd8, 0xff]],
  '.jpeg': [[0xff, 0xd8, 0xff]],
} as const;

export function validateType(fileName: string, head: Uint8Array): boolean {
  const ext = extname(fileName).toLowerCase() as keyof typeof ALLOWED;
  const signatures = ALLOWED[ext];
  if (!signatures) return false;                    // not on the allowlist
  return signatures.some((sig) => sig.every((b, i) => head[i] === b));
}

SVG needs separate attention: it’s XML-based and can carry <script> and event attributes. If there’s no business need, don’t accept SVG; if it’s needed, run it through a sanitization library on the server side or convert it to a raster format such as PNG. HTML files aren’t accepted as user content either.

In an app running behind Nginx, the request body limit is set with client_max_body_size; when exceeded, Nginx returns 413 and the request never reaches the app. Give the upload endpoint a generous limit and other endpoints a narrow one. When a user calls saying “the file won’t upload”, this is also the first place to look: the line “client intended to send too large body” in the Nginx error log. Other details of reverse proxy configuration are in the Nginx reverse proxy configuration guide.

server {
    client_max_body_size 1m;                 # general default

    location /api/documents/upload {
        client_max_body_size 20m;            # upload endpoint only
        proxy_pass http://app_backend;
    }
}

Storage location and naming

An uploaded file isn’t written to a directory the web server serves directly as files. The safe options are a directory outside the web root or object storage (S3-compatible storage, Azure Blob Storage); files are served through the app, with an authorization check. If files must stay in the web root in an old app, disable script execution in that directory:

# Nginx: block PHP execution in the upload directory
location ^~ /uploads/ {
    location ~* \.(php|phtml|phar)$ { return 403; }
    add_header X-Content-Type-Options nosniff always;
}

Don’t take the file name from the user. The server generates a random ID (UUID) and stores the file under this ID; the original name is kept only as metadata in the database and is used, sanitized, in the Content-Disposition header at download. Path traversal, name collisions and information leakage through the file name are closed off at the same time. The database record should include the file’s owner, the business record it’s linked to, its size, the detected type, a hash value (SHA-256) and the scan status.

In custom business applications, access control is done not at file level but at the level of the linked record: if a user has access to a purchase request, they also have access to its attachments. Don’t use predictable incrementing IDs (/file/1043); if you have to, check authorization on every request. Otherwise an IDOR (insecure direct object reference) vulnerability arises, and anyone who increments the number in the address bar by one downloads someone else’s invoice.

Direct upload with a signed URL

Large files passing through the application server create memory, bandwidth and timeout problems. If object storage is used, a better pattern is for the client to upload the file directly to storage. The flow works like this:

  1. The client requests upload permission from the app with the file name, size and type.
  2. The app checks the user’s authorization and the declared metadata, generates a random object key, creates the record in “pending” status and returns a short-lived signed URL.
  3. The client uploads the file directly to this URL.
  4. When the upload finishes (via a client notification or a storage event notification), a server-side handler validates the object, scans it and updates the status.
// AWS SDK v3: a signed POST that can enforce a size limit
import { S3Client } from '@aws-sdk/client-s3';
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
import { randomUUID } from 'node:crypto';

const s3 = new S3Client({ region: 'eu-central-1' });

export async function getUploadPermission(userId: string) {
  const key = `quarantine/${userId}/${randomUUID()}`;
  return createPresignedPost(s3, {
    Bucket: 'company-documents',
    Key: key,
    Conditions: [
      ['content-length-range', 1, 20 * 1024 * 1024],   // 1 byte – 20 MB
      ['eq', '$Content-Type', 'application/pdf'],
    ],
    Fields: { 'Content-Type': 'application/pdf' },
    Expires: 300,                                     // seconds
  });
}

A signed PUT URL (getSignedUrl with PutObjectCommand) is simpler, but in S3 it can’t define the allowed size range as a policy condition. If the size limit matters, choose a signed POST and the content-length-range condition. In both cases, the type and size declared by the client are only a pre-check; the file lands in the quarantine prefix and isn’t served to anyone until it passes server-side validation and scanning. The bucket isn’t public; reading is also done with short-lived signed URLs. The configuration mistake we see most often in the field is public access opened “temporarily” during testing and forgotten on the bucket.

Malware scanning and quarantine

Files that other users will download or that will be passed to internal systems go through malware scanning. The open-source option is ClamAV; the clamd service is kept running and files are scanned with clamdscan or through the service socket. In cloud storage, providers’ own scanning services can also be considered.

# ClamAV signature update and single-file scan
sudo freshclam
clamdscan --fdpass --no-summary /var/app/quarantine/3f2a9c1e-...
# Exit code: 0 = clean, 1 = malware found, 2 = error

Design the quarantine flow as a state machine: pending → scanning → clean | malicious | error. Only files in the clean state can be downloaded or passed into the workflow. A scan error doesn’t count as “clean”. It’s retried, and on a permanent error the operations team is notified. If the signature update (freshclam) has stopped, scanning appears to work but scans with old signatures; put the update time under monitoring. Since scanning is signature-based, it can miss new malware, so it doesn’t replace type restrictions, re-processing and safe serving. Password-protected archives can’t be scanned; reject them if there’s no business need, and if you accept archives, take precautions against zip bombs by limiting the total extracted size and the number of files.

Safe serving headers and checklist

Even if a file is accepted and stored safely, the browser may execute it if it’s served with the wrong headers. Recommended headers for user content:

Content-Type: application/pdf                   # the detected real type
Content-Disposition: attachment; filename="fatura-ornek.pdf"; filename*=UTF-8''fatura-%C3%B6rnek.pdf
X-Content-Type-Options: nosniff
Content-Security-Policy: sandbox; default-src 'none'
Cache-Control: private, no-store

attachment makes the file download instead of opening in the browser; if inline display is needed (image preview), use inline only for types known to be safe. nosniff prevents the browser from changing the type through content sniffing. The filename* parameter is the form defined by RFC 6266, based on RFC 8187 encoding, that carries non-ASCII characters; a plain filename is also sent along for older clients. Serving user content from a domain separate from the main application keeps a possible script execution from accessing the app’s session cookies. For files containing personal data, set cache headers deliberately so shared caches don’t store the response; the details are in the Cache-Control and CDN headers guide.

Checklist

  • Accepted types are defined with an allowlist according to business needs; SVG, HTML and executable types are out of scope.
  • The extension, declared MIME and content signature are validated together on the server; images are re-encoded.
  • A size limit is defined at the reverse proxy, in the app and (if used) in the signed upload condition; there are rate and count limits per user.
  • File names are generated on the server; the original name is metadata only.
  • Files are outside the web root or in private object storage; script execution is off in the upload directory.
  • Authorization on the linked record is checked on every download request.
  • There’s a quarantine and scanning flow; only clean files are served, and signature freshness is monitored.
  • attachment, nosniff and a restrictive CSP are used when serving; preferably a separate domain.
  • Upload, rejection and scan results are logged.

To tie the upload field to the same validation contract as the rest of the form, the managing form validation with a single schema guide is complementary. To review the upload features of an existing application or define additional rules at the WAF layer, we carry out reviews within the scope of web application security and WAF.

Frequently asked questions

Is checking the file extension enough?

No. The extension and the content type the client declares are easy to change. An extension allowlist, a content signature check, a server-generated file name, a non-executable storage location and safe serving headers should be applied together.

Should uploaded files be stored in the database?

Usually not. The file itself is kept in a directory outside the web root or in private object storage, and its metadata in the database. This separation keeps the database size and backup time under control.

How is file size limited when uploading with a signed URL?

In S3-compatible storage, the maximum size can be enforced on the storage side by adding a content-length-range condition to the signed POST policy. Since a signed PUT URL has no such range condition, the size check is largely left to post-upload validation.

Does antivirus scanning replace the other measures?

No. Signature-based scanning catches known malware but can miss new or specially crafted files. Scanning is used as one layer together with type restrictions, re-processing, authorization checks and safe serving headers.

Support

Contact us for support or a discovery session on this topic.

Contact form
[email protected]

Eretek Web

We build and maintain corporate websites, WordPress and CMS platforms, intranet and SharePoint portals, document management, and form and approval workflows, and keep them secure and up to date.

Knowledge Center

Other articles