-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed: Uncontrolled data used in path expression
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export type Patterns = { | ||
[K: string]: (path: string) => string; | ||
} | ||
|
||
/* Allows letters, digits, dashes, dots, slashes. | ||
Denies: leading/trailing/multiple dots, | ||
leading/trailing/multiple slashes, | ||
dots-slashes combinations. | ||
Returns: resulting string. | ||
*/ | ||
|
||
export const filterSafePath: Patterns = { | ||
trimRepeatedSymbols: (path: string) => { | ||
const pattern = new RegExp('([\\.]{2,})|([\\/]{2,})|([\\\\]{2,})', 'g'); | ||
return path.replace(pattern, (match) => { return match[0]; }) | ||
}, | ||
removeDottedSlashes: (path: string) => { | ||
const pattern = new RegExp('(\\.\\\\)|(\\.\\/)|(\\\\\\.)|(\\/\\.)', 'g'); | ||
return path.replace(pattern, ''); | ||
}, | ||
removeUnallowedSymbols: (path: string) => { | ||
const pattern = new RegExp('([^\\w\\d\\.\\/\\\\-]+)', 'g'); | ||
return path.replace(pattern, ''); | ||
}, | ||
trimSymbols: (path: string) => { | ||
const pattern = new RegExp('(^[\\.\\\\\\/]+)|([\\.\\\\\\/]+)$', 'g'); | ||
return path.replace(pattern, ''); | ||
} | ||
} | ||
|
||
/* Allows letters, digits, dashes. | ||
Returns if matched: result string, | ||
otherwise: empty string; | ||
*/ | ||
export const filterAllowedSymbolsOnly: Patterns = { | ||
...filterSafePath, | ||
getPath: (path: string) => { | ||
const matched = path.match(new RegExp('^([\\w\\d-]+)$', 'gi')); | ||
return matched === null ? '' : matched[0]; | ||
} | ||
} | ||
|
||
/* Allows alphanumeric symbols. | ||
Returns if matched: result string, | ||
otherwise: empty string; | ||
*/ | ||
export const filterAlphaNumericOnly: Patterns = { | ||
...filterSafePath, | ||
getPath: (path: string) => { | ||
const matched = path.match(new RegExp('^([\\w]+)$', 'gi')); | ||
return matched === null ? '' : matched[0]; | ||
} | ||
} | ||
|
||
const useFilteredRequestQuery = (path: string, patterns: Patterns, defaultPath: string = '') => { | ||
let threatedPath = path; | ||
for (const pattern of Object.values(patterns)) { | ||
threatedPath = pattern(threatedPath); | ||
} | ||
|
||
return threatedPath.length | ||
? threatedPath | ||
: defaultPath; | ||
}; | ||
|
||
export default useFilteredRequestQuery; |