path-sanitizer 4.0.0 → 4.0.2
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/.github/workflows/ci.yml +10 -12
- package/README.md +21 -18
- package/dist/index.d.ts +4 -4
- package/dist/index.js +15 -10
- package/package.json +31 -31
- package/pnpm-workspace.yaml +2 -2
- package/tsconfig.json +12 -15
package/.github/workflows/ci.yml
CHANGED
|
@@ -3,33 +3,31 @@ name: CI
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
5
|
branches:
|
|
6
|
-
-
|
|
6
|
+
- "**"
|
|
7
7
|
pull_request:
|
|
8
8
|
branches:
|
|
9
|
-
-
|
|
9
|
+
- "**"
|
|
10
10
|
|
|
11
11
|
jobs:
|
|
12
12
|
test:
|
|
13
13
|
runs-on: ubuntu-latest
|
|
14
|
+
permissions:
|
|
15
|
+
contents: read
|
|
14
16
|
|
|
15
17
|
steps:
|
|
16
18
|
- name: Checkout
|
|
17
19
|
uses: actions/checkout@v4
|
|
18
20
|
|
|
19
|
-
- uses: pnpm/action-setup@v4
|
|
20
|
-
name: Install pnpm
|
|
21
|
-
with:
|
|
22
|
-
version: 9
|
|
23
|
-
run_install: false
|
|
24
|
-
|
|
25
21
|
- name: Install Node.js
|
|
26
22
|
uses: actions/setup-node@v4
|
|
27
23
|
with:
|
|
28
24
|
node-version: 22
|
|
29
|
-
cache: 'pnpm'
|
|
30
25
|
|
|
31
|
-
- name: Install
|
|
32
|
-
|
|
26
|
+
- name: Install pnpm
|
|
27
|
+
uses: pnpm/action-setup@v4
|
|
28
|
+
with:
|
|
29
|
+
version: 9
|
|
30
|
+
run_install: true
|
|
33
31
|
|
|
34
32
|
- name: Run tests
|
|
35
|
-
run: pnpm test
|
|
33
|
+
run: pnpm test
|
package/README.md
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
# path-sanitizer
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
A simple lightweight npm package for sanitizing paths to prevent Path Traversal
|
|
3
4
|
|
|
4
5
|
# Usage
|
|
6
|
+
|
|
5
7
|
```js
|
|
6
8
|
import sanitize from 'path-sanitizer'
|
|
7
|
-
// or
|
|
8
|
-
const sanitize =
|
|
9
|
+
// or in a common js environment
|
|
10
|
+
const sanitize = (await import('path-sanitizer')).default
|
|
9
11
|
|
|
10
12
|
console.log(sanitize('path/to/file.txt'))
|
|
11
13
|
```
|
|
12
14
|
|
|
13
15
|
Or here is a more complex example:
|
|
16
|
+
|
|
14
17
|
```js
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
import sanitize from 'path-sanitizer'
|
|
19
|
+
import path from 'path'
|
|
17
20
|
|
|
18
21
|
// Real scenario:
|
|
19
|
-
function routeHandler
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
function routeHandler(myPath) {
|
|
23
|
+
// Lets just assume that the path was extracted from the request
|
|
24
|
+
// We want to read a file in the C:\Users\user\Desktop\myApp\ directory
|
|
25
|
+
// But the user should not be able to access C:\Users\user\Desktop\
|
|
26
|
+
// So we need to sanitize the path
|
|
27
|
+
|
|
28
|
+
const APP_DIR = 'C:\\Users\\user\\Desktop\\myApp\\'
|
|
29
|
+
const sanitized = path.join(APP_DIR, sanitize(myPath))
|
|
30
|
+
|
|
31
|
+
// Now we would usually read the file
|
|
32
|
+
// But in this case we will just print the path
|
|
33
|
+
console.log(sanitized)
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
routeHandler('my-video.mp4') // Just fine...
|
|
@@ -36,4 +39,4 @@ routeHandler('/hello.txt') // What if the path starts with a slash, and we try t
|
|
|
36
39
|
routeHandler('////////a\\\\../hi.txt') // What if the path is not valid?
|
|
37
40
|
routeHandler(500) // What if the path is not a string?
|
|
38
41
|
routeHandler('/:11^^') // What if the path contains not allowed characters (like ":")?
|
|
39
|
-
```
|
|
42
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export interface SanitizeOptions {
|
|
2
|
-
decode?:
|
|
2
|
+
decode?: {
|
|
3
3
|
regex: RegExp;
|
|
4
4
|
replacement: string;
|
|
5
|
-
}
|
|
5
|
+
}[];
|
|
6
6
|
/**
|
|
7
7
|
* RegEx that filters out stuff like \..\ or /../
|
|
8
8
|
*/
|
|
@@ -13,10 +13,10 @@ export interface SanitizeOptions {
|
|
|
13
13
|
notAllowedRegEx?: RegExp;
|
|
14
14
|
}
|
|
15
15
|
interface SanitizeOptionsComplete {
|
|
16
|
-
decode:
|
|
16
|
+
decode: {
|
|
17
17
|
regex: RegExp;
|
|
18
18
|
replacement: string;
|
|
19
|
-
}
|
|
19
|
+
}[];
|
|
20
20
|
/**
|
|
21
21
|
* RegEx that filters out stuff like \..\ or /../
|
|
22
22
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { normalize, join } from
|
|
1
|
+
import { normalize, join } from 'node:path';
|
|
2
2
|
export const DEFAULT_OPTIONS = {
|
|
3
3
|
decode: [
|
|
4
4
|
{
|
|
5
5
|
regex: /%2e/g,
|
|
6
|
-
replacement: '.'
|
|
6
|
+
replacement: '.',
|
|
7
7
|
},
|
|
8
8
|
{
|
|
9
9
|
regex: /%2f/g,
|
|
10
|
-
replacement: '/'
|
|
10
|
+
replacement: '/',
|
|
11
11
|
},
|
|
12
12
|
{
|
|
13
13
|
regex: /%5c/g,
|
|
14
|
-
replacement: '\\'
|
|
15
|
-
}
|
|
14
|
+
replacement: '\\',
|
|
15
|
+
},
|
|
16
16
|
],
|
|
17
17
|
parentDirectoryRegEx: /[\/\\]\.\.[\/\\]/g,
|
|
18
|
-
notAllowedRegEx: /:|\$|!|'|"|@|\+|`|\||=/g
|
|
18
|
+
notAllowedRegEx: /:|\$|!|'|"|@|\+|`|\||=/g,
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
21
|
* Sanitizes a portion of a path to avoid Path Traversal
|
|
@@ -38,7 +38,7 @@ export default function sanitize(pathstr, options = DEFAULT_OPTIONS) {
|
|
|
38
38
|
let sanitizedPath = pathstr;
|
|
39
39
|
// ################################################################################################################
|
|
40
40
|
// Decode
|
|
41
|
-
options.decode.forEach(decode => {
|
|
41
|
+
options.decode.forEach((decode) => {
|
|
42
42
|
sanitizedPath = sanitizedPath.replace(decode.regex, decode.replacement);
|
|
43
43
|
});
|
|
44
44
|
// Remove not allowed characters
|
|
@@ -73,7 +73,12 @@ export default function sanitize(pathstr, options = DEFAULT_OPTIONS) {
|
|
|
73
73
|
// Replace /../ with /
|
|
74
74
|
sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/');
|
|
75
75
|
// Remove ./ or / at start
|
|
76
|
-
while (sanitizedPath.startsWith('/') ||
|
|
76
|
+
while (sanitizedPath.startsWith('/') ||
|
|
77
|
+
sanitizedPath.startsWith('./') ||
|
|
78
|
+
sanitizedPath.endsWith('/..') ||
|
|
79
|
+
sanitizedPath.endsWith('/../') ||
|
|
80
|
+
sanitizedPath.startsWith('../') ||
|
|
81
|
+
sanitizedPath.startsWith('/../')) {
|
|
77
82
|
sanitizedPath = sanitizedPath.replace(/^\.\//g, ''); // ^./
|
|
78
83
|
sanitizedPath = sanitizedPath.replace(/^\//g, ''); // ^/
|
|
79
84
|
// Remove ../ | /../ at pos 0 and /.. | /../ at end
|
|
@@ -82,7 +87,7 @@ export default function sanitize(pathstr, options = DEFAULT_OPTIONS) {
|
|
|
82
87
|
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.$/g, '/');
|
|
83
88
|
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.\/$/g, '/');
|
|
84
89
|
}
|
|
85
|
-
// Make sure out is not "."
|
|
86
|
-
sanitizedPath = sanitizedPath.trim()
|
|
90
|
+
// Make sure out is not "." or ".." or any amount of "only dots"
|
|
91
|
+
sanitizedPath = sanitizedPath.trim().replace(/^(\.*)$/g, '');
|
|
87
92
|
return sanitizedPath.trim();
|
|
88
93
|
}
|
package/package.json
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"test": "vitest",
|
|
16
|
-
"build": "tsc"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [],
|
|
19
|
-
"author": "Cabra (https://github.com/cabraviva)",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"repository": {
|
|
22
|
-
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/cabraviva/path-sanitizer.git"
|
|
24
|
-
},
|
|
25
|
-
"bugs": {
|
|
26
|
-
"url": "https://github.com/cabraviva/path-sanitizer/issues"
|
|
27
|
-
},
|
|
28
|
-
"homepage": "https://github.com/cabraviva/path-sanitizer#readme",
|
|
29
|
-
"devDependencies": {
|
|
30
|
-
"@types/node": "^22.10.2",
|
|
31
|
-
"typescript": "^5.7.2",
|
|
32
|
-
"vitest": "^2.1.8"
|
|
2
|
+
"name": "path-sanitizer",
|
|
3
|
+
"version": "4.0.2",
|
|
4
|
+
"description": "A simple lightweight npm package for sanitizing paths to prevent Path Traversal",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
33
12
|
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "vitest",
|
|
16
|
+
"build": "tsc"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [],
|
|
19
|
+
"author": "Cabra (https://github.com/cabraviva)",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/cabraviva/path-sanitizer.git"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/cabraviva/path-sanitizer/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/cabraviva/path-sanitizer#readme",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^25.3.2",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"vitest": "^4.0.18"
|
|
33
|
+
}
|
|
34
34
|
}
|
package/pnpm-workspace.yaml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
allowBuilds:
|
|
2
|
+
esbuild: true
|
package/tsconfig.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"lib/*.test.ts"
|
|
15
|
-
]
|
|
16
|
-
}
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNEXT",
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"moduleResolution": "nodenext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"rootDir": "./lib",
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"strict": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["lib/*.ts"],
|
|
12
|
+
"exclude": ["lib/*.test.ts"]
|
|
13
|
+
}
|