path-sanitizer 4.0.1 → 4.0.3
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 +2 -0
- 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
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.3",
|
|
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
|
+
}
|