TaxHacker icon indicating copy to clipboard operation
TaxHacker copied to clipboard

fix(security): prevent path traversal in deleteFile (#75)

Open Vitalcheffe opened this issue 8 months ago • 0 comments

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:

  1. Looks up the user by userId
  2. Builds the user's uploads directory using safePathJoin(FILE_UPLOAD_PATH, user.email)
  3. 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 safePathJoin and FILE_UPLOAD_PATH from @/lib/files
    • Replaced unsafe path construction with safePathJoin calls
    • Added comment explaining the security fix

Closes #75

Vitalcheffe avatar Mar 31 '26 21:03 Vitalcheffe