path-sanitizer 3.0.1 → 3.1.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.
Files changed (2) hide show
  1. package/dist/index.js +12 -0
  2. package/package.json +27 -27
package/dist/index.js CHANGED
@@ -74,6 +74,18 @@ function sanitize(pathstr, options = exports.DEFAULT_OPTIONS) {
74
74
  sanitizedPath = sanitizedPath.replace(/[\\]/g, '/');
75
75
  // Replace double (back)slashes with a single slash
76
76
  sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/');
77
+ // Replace /../ with /
78
+ sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/');
79
+ // Remove ./ or / at start
80
+ while (sanitizedPath.startsWith('/') || sanitizedPath.startsWith('./') || sanitizedPath.endsWith('/..') || sanitizedPath.endsWith('/../') || sanitizedPath.startsWith('../') || sanitizedPath.startsWith('/../')) {
81
+ sanitizedPath = sanitizedPath.replace(/^\.\//g, ''); // ^./
82
+ sanitizedPath = sanitizedPath.replace(/^\//g, ''); // ^/
83
+ // Remove ../ | /../ at pos 0 and /.. | /../ at end
84
+ sanitizedPath = sanitizedPath.replace(/^[\/\\]\.\.[\/\\]/g, '/');
85
+ sanitizedPath = sanitizedPath.replace(/^\.\.[\/\\]/g, '/');
86
+ sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.$/g, '/');
87
+ sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.\/$/g, '/');
88
+ }
77
89
  // Make sure out is not "."
78
90
  sanitizedPath = sanitizedPath.trim() === '.' ? '' : sanitizedPath;
79
91
  return sanitizedPath.trim();
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "path-sanitizer",
3
- "version": "3.0.1",
4
- "description": "A simple lightweight npm package for sanitizing paths to prevent Path Traversal",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "test": "vitest",
8
- "build": "tsc"
9
- },
10
- "keywords": [],
11
- "author": "Cabra (https://github.com/cabraviva)",
12
- "license": "MIT",
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/cabraviva/path-sanitizer.git"
16
- },
17
- "bugs": {
18
- "url": "https://github.com/cabraviva/path-sanitizer/issues"
19
- },
20
- "homepage": "https://github.com/cabraviva/path-sanitizer#readme",
21
- "types": "dist/index.d.ts",
22
- "devDependencies": {
23
- "@types/node": "^22.9.3",
24
- "typescript": "^5.7.2",
25
- "vitest": "^2.1.5"
26
- }
27
- }
1
+ {
2
+ "name": "path-sanitizer",
3
+ "version": "3.1.0",
4
+ "description": "A simple lightweight npm package for sanitizing paths to prevent Path Traversal",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "test": "vitest",
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [],
11
+ "author": "Cabra (https://github.com/cabraviva)",
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/cabraviva/path-sanitizer.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/cabraviva/path-sanitizer/issues"
19
+ },
20
+ "homepage": "https://github.com/cabraviva/path-sanitizer#readme",
21
+ "types": "dist/index.d.ts",
22
+ "devDependencies": {
23
+ "@types/node": "^22.10.2",
24
+ "typescript": "^5.7.2",
25
+ "vitest": "^2.1.8"
26
+ }
27
+ }