The problem
Users need to upload files, browse everything that has been shared, and delete only their own uploads. Anything the frontend enforces can be bypassed with a single crafted request, so every rule has to live on the server.
What could go wrong
- Passwords stored in a readable form.
- Unauthenticated requests reaching the file API.
- User A deleting user B’s file by guessing its ID.
- Huge or unexpected file types filling the disk.
- Filenames with path characters writing outside the uploads folder.
Architecture
The app follows a strict frontend → backend → database structure. The frontend never touches the database or the filesystem directly.
Frontendregister · login · upload · downloads · my-filesHTML, CSS, JavaScript
↓ Authorization: Bearer <JWT>
Express APIverifyToken middlewarejwt.verify → load user → req.user
authRoutesregister · login (bcrypt)
fileRoutesupload · list · delete own
↓
StorageMongoDB (Mongoose)users + file metadata, owner id
uploads/sanitized, timestamped filenames
Problems I solved
01
Anyone could delete anyone’s file
RiskA delete endpoint that only checks “is logged in” lets any user delete any file whose ID they know.
FixThe server loads the file and compares uploaded_by with the authenticated user’s ID, returning 403 Forbidden on a mismatch.
02
Trusting tokens for deleted users
RiskA valid JWT for a user who no longer exists would still pass a signature-only check.
FixThe middleware verifies the token, then looks up the user (without the password field) and rejects the request if the user is gone.
03
Unsafe uploads
RiskUnlimited sizes, arbitrary file types, and user-controlled filenames on disk.
FixMulter enforces a 20 MB limit and a .pdf/.mp4 allow-list, and filenames are sanitized and prefixed with a timestamp, with clear 400 errors for each failure.
04
Readable passwords
RiskA database leak exposing user passwords.
FixPasswords are hashed with bcrypt before storage, and secrets like JWT_SECRET live in .env, not in code.
In the code
The ownership check on delete, from backend/routes/fileRoutes.js:
router.delete("/files/:id", verifyToken, async (req, res) => {
const file = await File.findById(req.params.id);
if (!file) return res.status(404).json({ message: "File not found." });
// the server decides who owns the file, not the browser
if (String(file.uploaded_by) !== String(req.user._id)) {
return res.status(403).json({ message: "Forbidden. You can only delete your own files." });
}
...
});
Upload validation:
const upload = multer({
storage,
limits: { fileSize: 20 * 1024 * 1024 }, // 20 MB
fileFilter // .pdf and .mp4 only
});
How I tested it
I tested it with two separate accounts, because ownership bugs only show up when a second user exists:
- Register and log in as user A, upload a file, and confirm it appears in Downloads and My Files.
- Register and log in as user B, upload a second file, and confirm B sees both files but only owns one.
- Delete B’s own file: it succeeds, and the file is gone from disk and the database.
- Try to delete A’s file as B: the API returns
403.
- Upload a disallowed type, and a file over 20 MB: both are rejected with a clear message.
What I learned
- Authentication says who you are. Authorization has to be checked on every resource.
- Never trust the client: every rule lives on the server.
- Validate uploads at the edge of the system: size, type and name.
- Keep secrets in the environment, not in the repository.
Next case studyRetiring a shared AWS root account
Read →