fix(security): prevent path traversal in deleteFile (#75)#85
Open
Vitalcheffe wants to merge 1 commit intovas3k:mainfrom
Open
fix(security): prevent path traversal in deleteFile (#75)#85Vitalcheffe wants to merge 1 commit intovas3k:mainfrom
Vitalcheffe wants to merge 1 commit intovas3k:mainfrom
Conversation
The deleteFile function used path.resolve(path.normalize(file.path)) without validating that the resulting path stays within the user's uploads directory. If an attacker could control the path value stored in the database (e.g. via backup restore), they could delete arbitrary files on the server. Now uses safePathJoin (consistent with the rest of the codebase) to resolve file.path relative to the user's uploads directory, which throws if the resolved path escapes the base directory. Fixes vas3k#75
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
deleteFilefunction inmodels/files.ts(line 77) constructs a filesystem path directly from thefile.pathdatabase column usingpath.resolve(path.normalize(...))without passing it through thesafePathJoinutility that is used everywhere else in the codebase.If an attacker can control the
pathvalue 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))withsafePathJoin, consistent with the rest of the codebase:userIdsafePathJoin(FILE_UPLOAD_PATH, user.email)safePathJoin(userUploadsDir, file.path)If
file.pathcontains path traversal sequences (e.g.../../etc/passwd),safePathJoinwill throw aPath traversal detectederror before any file is deleted.Changes
models/files.ts:safePathJoinandFILE_UPLOAD_PATHfrom@/lib/filessafePathJoincallsCloses #75