fix(security): prevent path traversal in deleteFile (#75)
Problem
The deleteFile function in models/files.ts (line 77) constructs a filesystem path directly from the file.path database column using path.resolve(path.normalize(...)) without passing it through the safePathJoin utility that is used everywhere else in the codebase.
If an attacker can control the path value stored in the database (e.g. via backup restore), they can delete arbitrary files on the server when the file is deleted through the application.
Reported in #75 by @N1et.
Fix
Replaced path.resolve(path.normalize(file.path)) with safePathJoin, consistent with the rest of the codebase:
- Looks up the user by
userId - Builds the user's uploads directory using
safePathJoin(FILE_UPLOAD_PATH, user.email) - Joins the file's relative path using
safePathJoin(userUploadsDir, file.path)
If file.path contains path traversal sequences (e.g. ../../etc/passwd), safePathJoin will throw a Path traversal detected error before any file is deleted.
Changes
models/files.ts:- Added import for
safePathJoinandFILE_UPLOAD_PATHfrom@/lib/files - Replaced unsafe path construction with
safePathJoincalls - Added comment explaining the security fix
- Added import for
Closes #75