FILE UPLOADSInvoices, images and CSVsSooner or later your SaaS takes files from customers: invoices, profile photos, a CSV of contacts. Every one is untrusted input, and uploads usually fail in one of two ways: the app accepts a file it should have refused, or large uploads tie up the server everyone else relies on. Both are cheaper to design out than to patch later.
What should the app check before it accepts a file?
Check everything on the server, and trust nothing the browser reports. OWASP's File Upload Cheat Sheet starts from an allow list: accept only the extensions the feature needs, such as PDF for invoices. Then check what the file really is.
- The
acceptattribute on<input type="file">only guides the file picker; MDN notes users can usually override it. - MDN says the browser's
File.typeis assumed from the extension: a PNG renamed to.txtreportstext/plain. - OWASP calls the
Content-Typeheader trivial to spoof: a quick check, never the decision. - Checking the file's signature, its first bytes, adds a layer, but OWASP warns it is easy to bypass on its own.
- Set a size limit for each kind of upload, so one account can't fill your storage.
Where should the files live, and under what name?
In object storage or on a separate host, never in a folder your web server serves or runs code from. OWASP ranks a different host first and outside the web root second, and warns that an uploaded .htaccess file could change how that folder handles file types where per-folder overrides are allowed. New Amazon S3 buckets and objects don't allow public access by default; keep it that way.
Then rename every file. OWASP calls a generated name, such as a UUID, essential, because user file names can carry restricted names and traversal tricks. It also prevents collisions, since S3 replaces an object when an upload reuses its key. We build keys from the workspace id plus a UUID; keeping each customer's data separate covers that tenant side.
How do customers download files without making them public?
Check permission on every request, then hand out a link that expires in minutes. Run the same server-side check as the rest of the app, then create a presigned URL for that one object. AWS describes presigned URLs as bearer tokens, usable by whoever holds them until they expire, so generate a fresh link per download.
A signed S3 download can also set the Content-Disposition response header. attachment tells the browser to download rather than display the file, offering filename as the save name: keep the customer's original name in your database and return it here.
How do you keep large uploads off your app server?
Let the browser upload straight to object storage with a presigned URL, so your app handles a small request instead of the whole file. The person uploading needs no AWS credentials, only the link. The flow we use:
- The browser asks your app for an upload slot, with the file's name, type and size.
- The app checks permission, the allow list and the size limit, then signs a short-lived URL for a generated key.
- The browser sends the file to the bucket, which needs a CORS rule allowing your app's origin.
- The app confirms the stored object, marks it pending, and uses it only after validation and any scan.
// Server only, after the permission, allow-list and size checks
const key = `${workspaceId}/${randomUUID()}.pdf`;
const url = await getSignedUrl(s3, new PutObjectCommand({
Bucket: process.env.UPLOAD_BUCKET, Key: key, ContentType: "application/pdf",
}), { signableHeaders: new Set(["content-type"]), expiresIn: 300 });
await db.file.create({ data: { key, workspaceId, originalName, status: "pending" } });Signing content-type forces the upload to send the type you approved. That header says nothing about the bytes, so content checks still run afterwards. For a hard size cap at the bucket, a presigned POST can carry a content-length-range condition.
Do you need to scan uploads for malware?
Yes, wherever files reach other people: teammates, your customers' clients or your own staff. OWASP recommends antivirus or a sandbox where available, and Content Disarm and Reconstruct for types such as PDF and DOCX. On AWS, GuardDuty Malware Protection for S3 scans newly uploaded objects in the buckets you select and can tag each one with the result. Whatever the scanner, keep new files pending until the result is in.
What changes for CSV and spreadsheet imports?
Validate every row before anything is saved. OWASP's Input Validation Cheat Sheet asks for size limits before parsing, a maintained parser, and checks on both format and meaning, such as an end date after its start date. For imports that means:
- A maximum file size and row count, enforced before the whole file is parsed.
- Required columns checked against a fixed header list.
- Errors reported by row number, so the customer can fix the file.
- Nothing saved until the file passes, or a clear rule for partial imports.
Then watch the way back out. If imported values later land in a CSV export, a cell starting with = is read as a formula in Excel or LibreOffice Calc. OWASP calls this CSV or formula injection; risky leading characters include =, +, -, @, tab, carriage return and line feed. Escape them on export and test in your customers' spreadsheet: OWASP notes Excel may drop escaping after a save and reopen.
Where to start
List every place your app accepts a file, the types and sizes it allows, and where each file ends up. Our web app and SaaS development service builds uploads with direct-to-storage transfers, scanning and signed downloads built in. To review an upload flow you already run, get in touch.
FAQ
Frequently asked questions
Can we let customers upload SVG files?
Only if the feature truly needs them. An SVG is a document as well as an image, and MDN documents a <script> element that adds scripts to it. If you accept SVGs, serve them as downloads with Content-Disposition: attachment rather than displaying them inside your app, or convert them to PNG on upload and keep only the converted file.
Should we accept ZIP files?
Avoid them unless customers really need to send many files at once. OWASP doesn't recommend ZIP uploads, because an archive can contain any type of file, and it lists ZIP bombs among the ways to fill a server's storage. If you must extract archives, apply your size limit to the decompressed total and run every file inside through the same checks.
Why do our signed download links stop working early?
A presigned URL lasts only as long as the credentials that signed it. AWS says a URL signed with temporary credentials, such as an IAM role session, expires when those credentials do, even if you asked for a longer expiry. That rarely affects links that last minutes. The SDKs also cap any presigned URL at seven days, so generate links on demand rather than sending long-lived ones.
Can someone reuse a signed upload URL?
Yes, until it expires. AWS says a presigned URL can be used multiple times up to its expiry, and an upload to a key that already exists replaces the stored object. Keep upload links to a few minutes, sign each one for a new generated key, and treat the link as a secret: keep it out of logs and analytics.
Should we strip metadata from uploaded photos?
Usually, yes. OWASP suggests decoding images and re-encoding them to an allowed format, removing unnecessary metadata on the way. It also warns that rewriting doesn't guarantee every malicious payload is gone and that the image library itself handles untrusted input, so keep it up to date, limit the formats and resources it accepts, and run it in a sandbox.
Do file uploads need CSRF protection?
Yes. OWASP lists protecting the upload from CSRF among its core rules, because a forged request could make a signed-in user upload or replace a file without meaning to. Use your framework's protection and confirm it covers the upload route. Next.js Server Actions, for example, compare the request's Origin header with your host and reject a mismatch.
