path-sanitizer 3.0.1 → 4.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.
@@ -1,35 +1,35 @@
1
- name: CI
2
-
3
- on:
4
- push:
5
- branches:
6
- - '**'
7
- pull_request:
8
- branches:
9
- - '**'
10
-
11
- jobs:
12
- test:
13
- runs-on: ubuntu-latest
14
-
15
- steps:
16
- - name: Checkout
17
- uses: actions/checkout@v4
18
-
19
- - uses: pnpm/action-setup@v4
20
- name: Install pnpm
21
- with:
22
- version: 9
23
- run_install: false
24
-
25
- - name: Install Node.js
26
- uses: actions/setup-node@v4
27
- with:
28
- node-version: 22
29
- cache: 'pnpm'
30
-
31
- - name: Install dependencies
32
- run: pnpm install
33
-
34
- - name: Run tests
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - '**'
7
+ pull_request:
8
+ branches:
9
+ - '**'
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - name: Checkout
17
+ uses: actions/checkout@v4
18
+
19
+ - uses: pnpm/action-setup@v4
20
+ name: Install pnpm
21
+ with:
22
+ version: 9
23
+ run_install: false
24
+
25
+ - name: Install Node.js
26
+ uses: actions/setup-node@v4
27
+ with:
28
+ node-version: 22
29
+ cache: 'pnpm'
30
+
31
+ - name: Install dependencies
32
+ run: pnpm install
33
+
34
+ - name: Run tests
35
35
  run: pnpm test
package/README.md CHANGED
@@ -1,39 +1,39 @@
1
- # path-sanitizer
2
- A simple lightweight npm package for sanitizing paths to prevent Path Traversal
3
-
4
- # Usage
5
- ```js
6
- import sanitize from 'path-sanitizer'
7
- // or
8
- const sanitize = require('path-sanitizer').default
9
-
10
- console.log(sanitize('path/to/file.txt'))
11
- ```
12
-
13
- Or here is a more complex example:
14
- ```js
15
- const sanitize = require('../sanitize')
16
- const path = require('path')
17
-
18
- // Real scenario:
19
- function routeHandler (myPath) {
20
- // Lets just assume that the path was extracted from the request
21
- // We want to read a file in the C:\Users\user\Desktop\myApp\ directory
22
- // But the user should not be able to access C:\Users\user\Desktop\
23
- // So we need to sanitize the path
24
-
25
- const APP_DIR = 'C:\\Users\\user\\Desktop\\myApp\\'
26
- const sanitized = path.join(APP_DIR, sanitize(myPath))
27
-
28
- // Now we would usually read the file
29
- // But in this case we will just print the path
30
- console.log(sanitized)
31
- }
32
-
33
- routeHandler('my-video.mp4') // Just fine...
34
- routeHandler('../../Documents/grades.docx') // But what if we try to access a file outside the app directory?
35
- routeHandler('/hello.txt') // What if the path starts with a slash, and we try to use an absolute path?
36
- routeHandler('////////a\\\\../hi.txt') // What if the path is not valid?
37
- routeHandler(500) // What if the path is not a string?
38
- routeHandler('/:11^^') // What if the path contains not allowed characters (like ":")?
1
+ # path-sanitizer
2
+ A simple lightweight npm package for sanitizing paths to prevent Path Traversal
3
+
4
+ # Usage
5
+ ```js
6
+ import sanitize from 'path-sanitizer'
7
+ // or
8
+ const sanitize = require('path-sanitizer').default
9
+
10
+ console.log(sanitize('path/to/file.txt'))
11
+ ```
12
+
13
+ Or here is a more complex example:
14
+ ```js
15
+ const sanitize = require('../sanitize')
16
+ const path = require('path')
17
+
18
+ // Real scenario:
19
+ function routeHandler (myPath) {
20
+ // Lets just assume that the path was extracted from the request
21
+ // We want to read a file in the C:\Users\user\Desktop\myApp\ directory
22
+ // But the user should not be able to access C:\Users\user\Desktop\
23
+ // So we need to sanitize the path
24
+
25
+ const APP_DIR = 'C:\\Users\\user\\Desktop\\myApp\\'
26
+ const sanitized = path.join(APP_DIR, sanitize(myPath))
27
+
28
+ // Now we would usually read the file
29
+ // But in this case we will just print the path
30
+ console.log(sanitized)
31
+ }
32
+
33
+ routeHandler('my-video.mp4') // Just fine...
34
+ routeHandler('../../Documents/grades.docx') // But what if we try to access a file outside the app directory?
35
+ routeHandler('/hello.txt') // What if the path starts with a slash, and we try to use an absolute path?
36
+ routeHandler('////////a\\\\../hi.txt') // What if the path is not valid?
37
+ routeHandler(500) // What if the path is not a string?
38
+ routeHandler('/:11^^') // What if the path contains not allowed characters (like ":")?
39
39
  ```
package/dist/index.js CHANGED
@@ -1,9 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_OPTIONS = void 0;
4
- exports.default = sanitize;
5
- const node_path_1 = require("node:path");
6
- exports.DEFAULT_OPTIONS = {
1
+ import { normalize, join } from "node:path";
2
+ export const DEFAULT_OPTIONS = {
7
3
  decode: [
8
4
  {
9
5
  regex: /%2e/g,
@@ -24,17 +20,17 @@ exports.DEFAULT_OPTIONS = {
24
20
  /**
25
21
  * Sanitizes a portion of a path to avoid Path Traversal
26
22
  */
27
- function sanitize(pathstr, options = exports.DEFAULT_OPTIONS) {
23
+ export default function sanitize(pathstr, options = DEFAULT_OPTIONS) {
28
24
  if (!options)
29
- options = exports.DEFAULT_OPTIONS;
25
+ options = DEFAULT_OPTIONS;
30
26
  if (typeof options !== 'object')
31
27
  throw new Error('options must be an object');
32
28
  if (!Array.isArray(options.decode))
33
- options.decode = exports.DEFAULT_OPTIONS.decode;
29
+ options.decode = DEFAULT_OPTIONS.decode;
34
30
  if (!options.parentDirectoryRegEx)
35
- options.parentDirectoryRegEx = exports.DEFAULT_OPTIONS.parentDirectoryRegEx;
31
+ options.parentDirectoryRegEx = DEFAULT_OPTIONS.parentDirectoryRegEx;
36
32
  if (!options.notAllowedRegEx)
37
- options.notAllowedRegEx = exports.DEFAULT_OPTIONS.notAllowedRegEx;
33
+ options.notAllowedRegEx = DEFAULT_OPTIONS.notAllowedRegEx;
38
34
  if (typeof pathstr !== 'string') {
39
35
  // Stringify the path
40
36
  pathstr = `${pathstr}`;
@@ -57,7 +53,7 @@ function sanitize(pathstr, options = exports.DEFAULT_OPTIONS) {
57
53
  // Replace double (back)slashes with a single slash
58
54
  sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/');
59
55
  // Normalize path
60
- sanitizedPath = (0, node_path_1.normalize)(sanitizedPath);
56
+ sanitizedPath = normalize(sanitizedPath);
61
57
  // Remove / or \ in the end
62
58
  while (sanitizedPath.endsWith('/') || sanitizedPath.endsWith('\\')) {
63
59
  sanitizedPath = sanitizedPath.slice(0, -1);
@@ -67,13 +63,25 @@ function sanitize(pathstr, options = exports.DEFAULT_OPTIONS) {
67
63
  sanitizedPath = sanitizedPath.slice(1);
68
64
  }
69
65
  // Validate path
70
- sanitizedPath = (0, node_path_1.join)('', sanitizedPath);
66
+ sanitizedPath = join('', sanitizedPath);
71
67
  // Remove not allowed characters
72
68
  sanitizedPath = sanitizedPath.replace(options.notAllowedRegEx, '');
73
69
  // Again change all \ to /
74
70
  sanitizedPath = sanitizedPath.replace(/[\\]/g, '/');
75
71
  // Replace double (back)slashes with a single slash
76
72
  sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/');
73
+ // Replace /../ with /
74
+ sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/');
75
+ // Remove ./ or / at start
76
+ while (sanitizedPath.startsWith('/') || sanitizedPath.startsWith('./') || sanitizedPath.endsWith('/..') || sanitizedPath.endsWith('/../') || sanitizedPath.startsWith('../') || sanitizedPath.startsWith('/../')) {
77
+ sanitizedPath = sanitizedPath.replace(/^\.\//g, ''); // ^./
78
+ sanitizedPath = sanitizedPath.replace(/^\//g, ''); // ^/
79
+ // Remove ../ | /../ at pos 0 and /.. | /../ at end
80
+ sanitizedPath = sanitizedPath.replace(/^[\/\\]\.\.[\/\\]/g, '/');
81
+ sanitizedPath = sanitizedPath.replace(/^\.\.[\/\\]/g, '/');
82
+ sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.$/g, '/');
83
+ sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.\/$/g, '/');
84
+ }
77
85
  // Make sure out is not "."
78
86
  sanitizedPath = sanitizedPath.trim() === '.' ? '' : sanitizedPath;
79
87
  return sanitizedPath.trim();
package/package.json CHANGED
@@ -1,27 +1,34 @@
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": "4.0.0",
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"
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"
33
+ }
34
+ }
@@ -0,0 +1,2 @@
1
+ onlyBuiltDependencies:
2
+ - esbuild
package/tsconfig.json CHANGED
@@ -1,15 +1,16 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNEXT",
4
- "module": "commonjs",
5
- "declaration": true,
6
- "outDir": "./dist",
7
- "strict": true
8
- },
9
- "include": [
10
- "lib/*.ts"
11
- ],
12
- "exclude": [
13
- "lib/*.test.ts"
14
- ]
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNEXT",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "declaration": true,
7
+ "outDir": "./dist",
8
+ "strict": true
9
+ },
10
+ "include": [
11
+ "lib/*.ts"
12
+ ],
13
+ "exclude": [
14
+ "lib/*.test.ts"
15
+ ]
15
16
  }