How Attachments Turn Into Incidents
In business systems, attachments are usually treated as a simple "upload a file, download a file" feature. But a file is the most open-ended input a user can send, in both size and content, which is why attackers often go after it first. Real incidents tend to follow a few paths:
Executable uploads: A `.jsp`, `.php` or `.aspx` file saved under the web root becomes a web shell, and anyone with the URL can run commands on your server.Path traversal: A filename containing `../../` overwrites files outside the upload folder, or a download parameter is used to read configuration files.Downloading someone else's file: Change `/download?id=1024` to `1025` and you get another department's or another client's contract. This is an IDOR (Insecure Direct Object Reference), and it is the most common case.Full disks: With no size limit, a few dozen 1 GB uploads can fill the disk your logs and database share and stop the whole server.Validating at Upload Time
Check extension, MIME type and file signature
Users can change both the file extension and the browser-supplied `Content-Type`. Filter extensions with an allowlist, then read the first bytes of the file (its magic number) to confirm the content matches. PDFs start with `%PDF`, PNGs with `89 50 4E 47`, and ZIP-based formats such as DOCX and XLSX with `50 4B 03 04`. Avoid blocklists: there will always be an extension you missed, such as `.phtml` or `.jspx`.
Limit size and regenerate filenames
Enforce size limits at both the web server (for example, Nginx `client_max_body_size`) and the application. For business documents, 20–50 MB per file plus a per-user quota is typical.Store files under a server-generated name such as a UUID, and keep the original filename only in the database. That removes both path traversal and name collisions.Put new uploads in a quarantine area and scan them with an engine such as ClamAV. Only files that pass move to permanent storage. Treat Office documents with macros under a separate policy.Where to Store Files
Store files outside the web root. If no URL can reach a file directly, an uploaded script can't run.As volume grows, object storage such as S3 helps. You worry less about capacity, and a private bucket blocks any access that skips your application.Keep only metadata in the database: original name, storage key, size, SHA-256 hash, uploader and parent document ID. Don't store file contents as BLOBs, or your database backups will grow to hundreds of gigabytes.Serve downloads through presigned URLs. Issue a URL that expires in about five minutes, and only after the permission check passes. A link pasted into a chat app stops working soon after.Download Permissions and Audit Trails
Make attachment permissions follow document permissions
A common mistake: the approval document screen checks permissions, but the attachment download API doesn't. Design attachments as children of their parent document, not standalone resources, and run the same "can this user see the parent document?" check on every download. When a document is withdrawn or its audience changes, the attachment permissions must change with it.
Log downloads and detect bulk downloads
Record who downloaded which file, when, and from which IP.Alert on rules such as "more than 100 files in 10 minutes from one user" or "documents from a department this user never accesses." These catch data theft by departing employees and compromised accounts early.Retention and Cleanup
Orphaned files: Files uploaded to a draft that was never saved, or left behind after a document was deleted, pile up. Run a scheduled job that removes uploads not linked to any document within 24 hours.Retention and deletion policy: Contracts and tax invoices have legal retention periods, and files containing personal data follow different rules. Set a retention period for each document type, and when it expires, delete the original in storage, not just the database row.Check your backup scope: Teams often back up the database but forget the storage bucket, then find every attachment link broken after a restore. Turn on versioning or replication for object storage, and open real attachments during restore drills.Secure by Default with POLYGLOTSOFT Subscription Development
In the business systems POLYGLOTSOFT builds through subscription development, these controls are included by default, without a separate request: signature validation, private storage with presigned URLs, download checks tied to document permissions, download audit logs and orphaned-file cleanup. If you're planning an approval, contract or quality-document system that handles a lot of attachments, send us your requirements and we'll reply right away with a tailored proposal.