-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sanitaze paths before validating them
- Loading branch information
1 parent
aadeef4
commit c0be00d
Showing
2 changed files
with
61 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import path from 'path'; | ||
|
||
const isWindowsRootDirectory = /[a-zA-Z]:[\\/]/; | ||
const containsNullCharacter = /\0/g; | ||
|
||
const validations = [ | ||
(name: string) => name.includes('../'), | ||
(name: string) => name.includes('..'), | ||
(name: string) => name.startsWith('/'), | ||
(name: string) => isWindowsRootDirectory.test(name), | ||
(name: string) => name.includes('\\'), | ||
(name: string) => containsNullCharacter.test(name), | ||
]; | ||
|
||
export const fileNameIsValid = (fileName: string): boolean => | ||
validations.every((validation) => !validation(fileName)); | ||
const sanitazeRelativePath = (relativePath: string) => | ||
relativePath.replaceAll(path.sep, '/'); | ||
|
||
export const fileNameIsValid = (fileName: string): boolean => { | ||
const sanitazedPath = sanitazeRelativePath(fileName); | ||
|
||
return validations.every((validation) => !validation(sanitazedPath)); | ||
}; |
This file contains 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