Pattern: File upload

Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.

Search terms: file upload, presigned url, direct upload, file type validation, magic bytes, size limit, image reprocessing, video validation, orphan files, multipart upload

Last reviewed:

For agents

1. Problem

Uploads let strangers put data on your disk. Typical failures: writing the file before the permission check, trusting the Content-Type header or file name, accepting any size, running the server out of disk, and leaving abandoned files behind.

2. Options

Method Upside Downside
Upload through your server Full control; simple for small files Server carries all bandwidth and memory; slow for large files
Direct upload to storage with a signed, time-limited address (recommended for large files) Server never carries the bytes; scales More moving parts; validation after the upload
Multipart upload for very large files Resumable, parallel Cleanup of unfinished parts needed

The OWASP File Upload Cheat Sheet is the main reference; the AWS S3 documentation covers presigned URLs and multipart uploads (similar features exist in other object stores).

4. Scenarios and edge cases

5. Common mistakes

6. How to audit

Sources