Design a secure file-upload feature with React and ASP.NET Core, including validation, storage, and malware scanning.
PhonePe · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, React, Authentication & Security
Community answers
Posted by wasAsked editorial collection
Treat uploads as hostile. Require authentication and authorization, cap request and file size, allow-list expected formats, and validate file signatures rather than trusting extensions or Content-Type. Generate storage names; never use a client path.
Stream to quarantined storage outside the web root, scan with malware tooling, and promote only clean files. Serve through an indirect ID or short-lived signed URL with Content-Disposition and nosniff headers; prevent script-capable content from executing in your origin. Encrypt storage, restrict service identity access, record ownership/tenant, define retention, and clean abandoned uploads. Image decoding and archive extraction need limits against decompression bombs and parser vulnerabilities.
Backbencher Developer · · 0 likes
Treat every uploaded file as untrusted.
Frontend (React)
- Restrict choices with the accept attribute and check size before upload for quick feedback — but treat these as UX only, since they can be bypassed.
- For large files, upload directly to storage using a short-lived pre-signed URL (Azure Blob SAS or S3 pre-signed URL) issued by the API, and show progress.
Backend validation (ASP.NET Core)
- Enforce size limits on the server (RequestSizeLimit, Kestrel and form options).
- Allow-list extensions and verify the real type from the file's magic bytes, not the Content-Type header or file name.
- Generate your own random file name and storage path; never use the user's file name in a path (prevents path traversal and overwrites). Keep the original name only as escaped metadata.
- For images, re-encode them (for example with ImageSharp) to strip metadata and malformed payloads.
Storage
- Store files outside the web root, in a private blob container or bucket.
- Upload first to a quarantine container. Only after scanning, move the file to the clean container.
- Serve downloads through authorized endpoints or short-lived signed URLs, with Content-Disposition: attachment and X-Content-Type-Options: nosniff, preferably from a separate domain so uploaded HTML or SVG cannot run scripts on your main site.
Malware scanning
- Scan asynchronously: a blob-created event or queue message triggers a scanner (Microsoft Defender for Storage, ClamAV in a container, or a commercial API).
- Track status (Pending, Clean, Infected) in the database and only allow downloads of Clean files; delete or isolate infected ones and alert.
Authorization and auditing
- Check that the user may upload to and read the target resource, scoped by tenant.
- Apply rate limits and per-user quotas.
- Log uploads, scans and downloads for auditing.