path-sanitizer 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -1
- package/package.json +1 -1
- package/sanitize.js +69 -2
package/README.md
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
1
|
# path-sanitizer
|
|
2
|
-
A simple node.js package for sanitizing paths to prevent Path Traversal
|
|
2
|
+
A simple leightweight node.js package for sanitizing paths to prevent Path Traversal
|
|
3
|
+
|
|
4
|
+
# Usage
|
|
5
|
+
```js
|
|
6
|
+
const sanitize = require('path-sanitizer')
|
|
7
|
+
|
|
8
|
+
console.log(sanitize('path/to/file.txt'))
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or here is a more complex example:
|
|
12
|
+
```js
|
|
13
|
+
const sanitize = require('../sanitize')
|
|
14
|
+
const path = require('path')
|
|
15
|
+
|
|
16
|
+
// Real scenario:
|
|
17
|
+
function routeHandler (myPath) {
|
|
18
|
+
// Lets just assume that the path was extracted from the request
|
|
19
|
+
// We want to read a file in the C:\Users\user\Desktop\myApp\ directory
|
|
20
|
+
// But the user should be able to access C:\Users\user\Desktop\
|
|
21
|
+
// So we need to sanitize the path
|
|
22
|
+
|
|
23
|
+
const APP_DIR = 'C:\\Users\\user\\Desktop\\myApp\\'
|
|
24
|
+
const sanitized = path.join(APP_DIR, sanitize(myPath))
|
|
25
|
+
|
|
26
|
+
// Now we would usally read the file
|
|
27
|
+
// But in this case we just gonna print the path
|
|
28
|
+
console.log(sanitized)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
routeHandler('myvideo.mp4') // Just fine...
|
|
32
|
+
routeHandler('../../Documents/grades.docx') // But what if we try to access a file outside the app directory?
|
|
33
|
+
routeHandler('/hello.txt') // What if the path starts with a slash, and we try to use an absolute path?
|
|
34
|
+
routeHandler('////////a\\\\../hhuio.txt') // What if the path is not valid?
|
|
35
|
+
routeHandler(500) // What if the path is not a string?
|
|
36
|
+
routeHandler('/:11^^') // What if the path contains not allowed characters (like ":")?
|
|
37
|
+
```
|
package/package.json
CHANGED
package/sanitize.js
CHANGED
|
@@ -1,5 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const path = require('path')
|
|
2
|
+
|
|
3
|
+
const DEFAULT_OPTIONS = {
|
|
4
|
+
decode: [
|
|
5
|
+
{
|
|
6
|
+
regex: /%2e/g,
|
|
7
|
+
replacement: '.'
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
regex: /%2f/g,
|
|
11
|
+
replacement: '/'
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
regex: /%5c/g,
|
|
15
|
+
replacement: '\\'
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
parentDirectoryRegEx: /[\/\\]\.\.[\/\\]/g,
|
|
19
|
+
notAllowedRegEx: /:|\$|!|'|"|@|\+|`|\||=/g
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function sanitize (pathToSanitize, options = DEFAULT_OPTIONS) {
|
|
23
|
+
if (!options) options = DEFAULT_OPTIONS
|
|
24
|
+
if (typeof options !== 'object') throw new Error('options must be an object')
|
|
25
|
+
if (!options.decode) options.decode = DEFAULT_OPTIONS.decode
|
|
26
|
+
if (!options.parentDirectoryRegEx) options.parentDirectoryRegEx = DEFAULT_OPTIONS.parentDirectoryRegEx
|
|
27
|
+
if (!options.notAllowedRegEx) options.notAllowedRegEx = DEFAULT_OPTIONS.notAllowedRegEx
|
|
28
|
+
|
|
29
|
+
if (typeof pathToSanitize !== 'string') {
|
|
30
|
+
// Stringify the path
|
|
31
|
+
pathToSanitize = `${pathToSanitize}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let sanitizedPath = pathToSanitize
|
|
35
|
+
|
|
36
|
+
// Decode
|
|
37
|
+
options.decode.forEach(decode => {
|
|
38
|
+
sanitizedPath = sanitizedPath.replace(decode.regex, decode.replacement)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// Replace first (back)slash with a normal slash
|
|
42
|
+
sanitizedPath = sanitizedPath.replace(/^[\/\\]?/, '/')
|
|
43
|
+
|
|
44
|
+
// Replace /../ with /
|
|
45
|
+
sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/')
|
|
46
|
+
|
|
47
|
+
// Replace double (back)slashes with a single slash
|
|
48
|
+
sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/')
|
|
49
|
+
|
|
50
|
+
// Normalize path
|
|
51
|
+
sanitizedPath = path.normalize(sanitizedPath)
|
|
52
|
+
|
|
53
|
+
// Remove / or \ in the end
|
|
54
|
+
while (sanitizedPath.endsWith('/') || sanitizedPath.endsWith('\\')) {
|
|
55
|
+
sanitizedPath = sanitizedPath.slice(0, -1)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Remove / or \ in the beginning
|
|
59
|
+
while (sanitizedPath.startsWith('/') || sanitizedPath.startsWith('\\')) {
|
|
60
|
+
sanitizedPath = sanitizedPath.slice(1)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Validate path
|
|
64
|
+
sanitizedPath = path.join('', sanitizedPath)
|
|
65
|
+
|
|
66
|
+
// Remove not allowed characters
|
|
67
|
+
sanitizedPath = sanitizedPath.replace(options.notAllowedRegEx, '')
|
|
68
|
+
|
|
69
|
+
return sanitizedPath
|
|
3
70
|
}
|
|
4
71
|
|
|
5
72
|
module.exports = sanitize
|