Contact Form File Uploads: A Security Checklist
Contact form file upload security in seven checks: allow-list file types, enforce size limits on the server, store files outside the webroot, scan and delete.
In this post
A contact form that accepts file uploads can receive any file the visitor chooses, not only the one you expected. To secure it: check the type on the server against an allow-list, limit the size, rename the file, store it where it cannot run or be reached by strangers, scan it, and decide how long you keep it. The browser's file picker helps a visitor choose a PDF, but it does not stop anyone else. This checklist covers each step, with the OWASP and MDN guidance behind it.
If your form does not need uploads, the simplest safe option is to remove the field. Many enquiries work fine with a link the visitor can send instead, and a form without a file field has none of the risks below.
Why uploads are risky
A file is not only data. Depending on its type and where it ends up, it can carry a script, a macro or a payload that runs when someone opens it. It can also be very large, which fills your storage or slows your inbox. The OWASP File Upload Cheat Sheet lists client-side active content such as XSS and CSRF among the risks, since it "could endanger other users if the files are publicly retrievable."
The risk is not limited to malware. A file may contain personal data the visitor did not intend to send, such as an identity document, and once it is stored it becomes something you must protect and eventually delete.
Allow-lists for file type
Decide in advance which types you accept. The OWASP guidance on extensions is direct: "Ensure the usage of business-critical extensions only, without allowing any type of non-required extensions." It also says that checking the MIME type "can be done preferably in an allowlist approach; otherwise, this can be done in a denylist approach." Accept only what the form needs, such as PDF and PNG, and reject everything else.
A deny-list of dangerous types is weaker, because it has to anticipate every type you did not think of. OWASP adds that "blocking specific extensions is a weak protection method on its own", so treat the extension as one check among several.
The browser does not enforce the list. MDN states that the accept attribute "doesn't validate the types of the selected files; it provides hints for browsers to guide users towards selecting the correct file types." The server has to check the type. For the same reason, OWASP advises not trusting the Content-Type header, because it can be spoofed.
Checking the file's signature, the first bytes that identify its format, adds a second check. OWASP notes that this "should not be used on its own, as bypassing it is pretty common and easy." Use it together with the extension and MIME checks.
Size limits and rate limits
Set a maximum size per file and a maximum total per submission. OWASP's guidance is to "Set a file size limit" and to set "proper size limits for the upload service". Enforce the limit on the server, because a browser check can be skipped. Reject oversized files before storing them, not after.
Limit how often one visitor can submit. A rate limit on the form stops a single source from filling your storage with files in a loop. The right numbers depend on your traffic, so choose a value you can explain, and log when it triggers so you can see whether real visitors are hitting it.
Where to store files
Keep uploads out of the place where the web server serves pages. OWASP recommends storing files "on a different server" and, if that is not possible, "outside of the webroot", where "only administrative access is allowed." A file that is stored outside the webroot cannot be requested by a URL, so a script inside it cannot be run by a visitor.
Rename every file on arrival. OWASP recommends changing "the filename to something generated by the application", and says that a random string such as a UUID is "essential". The visitor's original filename can be stored as text in the record, but it should not decide where the file lives or what it is called on disk.
Avoid putting uploads into a folder your site serves directly. A random name makes a file hard to guess, but it does not change what a browser does with a file that is served publicly.
Scanning and access control
OWASP recommends running each upload "through an antivirus or a sandbox if available to validate that it doesn't contain malicious data". If your host or storage provider offers scanning, use it. If it does not, say so in your own notes, so the team knows that files are checked only by type and size.
Limit who can open the files. The people who read the enquiry need access, and nobody else does. Use the access controls of the storage service, and give each person the smallest set of permissions that lets them do the job. A file link that anyone can open is a public file, however obscure its name.
Open attachments on a machine or in a viewer that does not run scripts, and treat them as untrusted until checked.
Privacy and retention
An uploaded file can contain personal data, and a form may receive ID cards, medical letters or payslips without anyone asking for them. Decide what the form is for, and state what happens to a file after the enquiry is closed. Delete files on a schedule that you can describe and carry out, and check that the deletion also removes the copies in backups and in any mailbox the file was sent to.
The shortest rule is the safest: ask for a file only when the enquiry cannot be answered without it, and tell the visitor on the form what the file is for. For the wider legal checklist on a contact form, see the GDPR checklist for contact forms. For the setup of a form without a backend, read contact form without a backend. Prosphere is in build, so for the shared inbox that will hold these files, see the shared inbox page for the plan.
Sources
- OWASP, "File Upload Cheat Sheet": https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html
- MDN Web Docs, "input type=file" element reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/file