{"success":true,"name":"treat-file-uploads-as-hostile-input-validate-rename-store-ou","description":"Adding an upload feature for images, PDFs, or any user files","category":"ai-brimind","path":"ai-brimind/treat-file-uploads-as-hostile-input-validate-rename-store-ou/SKILL.md","file":"SKILL.md","content":"---\nname: treat-file-uploads-as-hostile-input-validate-rename-store-ou\ndescription: \"Adding an upload feature for images, PDFs, or any user files\"\n---\n\n# Treat File Uploads as Hostile Input: Validate, Rename, Store Outside the Code, Serve Safely\n\n## When to use\nYou are building an endpoint or form where users upload files (avatars, documents, product photos), and the current plan saves whatever arrives, under its original name, wherever is convenient — with no size, type, or storage decision made.\n\n## Example request\nFile saved under its original name → two users upload 'photo.jpg' and one overwrites the other, or a name like '../../app.js' tries to escape the folder; generating your own unique filename kills both problems at once. Trusting the file extension or the client's mime type → anything can be renamed to .jpg; checking the file's actual content type on the server is the real gate. No size limit configured → one 2 GB 'avatar' fills the disk or freezes the server; upload size limits are a built-in of every upload library, they just have to be set. Files written into the project folder next to the code → the next deploy wipes them; user files live in dedicated storage (a separate folder, volume, or a storage service), never inside the codebase.\n\n## Instructions\nAct as a backend developer who treats an uploaded file as the least trustworthy input the app receives — it is a chunk of arbitrary bytes with a self-declared name and type, sent by a stranger. For any upload feature, do this: (1) Use the upload middleware your stack already has (multer or equivalent) and configure its limits explicitly: maximum file size, and which fields accept files — an unconfigured upload endpoint is an open disk-filling service. (2) Validate the TYPE on the server against a short allow-list of what the feature actually needs ('avatars: jpeg, png, webp only'), and do not trust the extension or the client-sent mime type alone — they are just claims; check the file's real content signature where it matters. Reject everything else with a clear message. (3) Never keep the original filename: generate your own (a random ID or hash plus a safe extension you chose) and store the user's original name in the DB as display metadata if needed. This one move eliminates overwrites, weird-character bugs, and path-traversal names in a single stroke. (4) Decide WHERE files live, on purpose: outside the code directory — a dedicated uploads folder or volume in small projects, an object-storage service when the app is meant to scale — and record in the DB the file's path/ID plus who owns it. Files inside the repo folder die on redeploy and bloat everything. (5) Serve files back as deliberately as you accepted them: through a route or static config you chose, with the ownership check if files are private (a guessable public URL to private documents is a leak, not a feature). (6) Plan deletion: when the record dies, its file should not become an orphan forever — say how cleanup happens, even if it is a simple periodic script. Only mark the feature done once you can state the size limit, the allowed types, the naming scheme, the storage location, and who can fetch the file back — if any of the five is 'default', that default was chosen by nobody.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #205\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}