How to fix: File Upload Without Type Validation
A file upload handler was detected without content-type or extension validation. AI-generated upload scaffolding frequently accepts all file types without restriction, enabling malicious file upload attacks.
Why it matters
Unrestricted file upload allows attackers to upload web shells, malicious executables, or oversized files. If uploaded files are served from the same domain, an attacker can upload an HTML file and steal cookies via XSS.
AI tools generate upload handlers that call `formData.get('file')` and save it without checking the type. The fix requires validating both the declared content type and the actual magic bytes of the file content.
How to fix it
- 1Validate content-type against an allowed list: ['image/jpeg', 'image/png', 'application/pdf'].
- 2Validate magic bytes (not just extension): check the first bytes of the file content match the declared type.
- 3Rename uploaded files; never preserve the original filename.
- 4Store uploaded files outside the web root or in a separate storage bucket, never serving them from the app domain.
- 5Enforce file size limits.
Code examples
File upload with type validation
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
export async function POST(req: Request) {
const form = await req.formData();
const file = form.get('file') as File | null;
if (!file) return badRequest('No file');
if (!ALLOWED_TYPES.includes(file.type)) return badRequest('Invalid file type');
if (file.size > MAX_SIZE) return badRequest('File too large');
// Rename to random UUID + validated extension
const ext = file.type.split('/')[1];
const filename = `${crypto.randomUUID()}.${ext}`;
// Store in S3/blob storage, not local disk
}References
Detected automatically by VulnRadar. The check id is vibe-unrestricted-file-upload, stable across every scan so you can filter or diff on it.
More ai code checks
- LowGeneric Error Messages Leak No Context
- MediumTODO/FIXME Security Note in Response
- Higheval() Usage in Page Scripts
- HighSSL Certificate Verification Disabled
- HighMath.random() Used for Security Tokens
- CriticalHardcoded Credential in Authentication Logic
- CriticalJWT 'none' Algorithm Accepted
- CriticalSQL String Concatenation Pattern Detected
Questions
- How severe is File Upload Without Type Validation?
- VulnRadar rates this high severity in the ai code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id vibe-unrestricted-file-upload, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does File Upload Without Type Validation map to?
- It maps to CWE-434. In the OWASP Top 10 it falls under A08:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.