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.
@@ -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 dependencies
32
- run: pnpm install
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
- A simple lightweight npm package for sanitizing paths to prevent Path Traversal
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 = require('path-sanitizer').default
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
- const sanitize = require('../sanitize')
16
- const path = require('path')
18
+ import sanitize from 'path-sanitizer'
19
+ import path from 'path'
17
20
 
18
21
  // 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)
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 "node:path";
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('/') || sanitizedPath.startsWith('./') || sanitizedPath.endsWith('/..') || sanitizedPath.endsWith('/../') || sanitizedPath.startsWith('../') || 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() === '.' ? '' : sanitizedPath;
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
- "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"
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
  }
@@ -1,2 +1,2 @@
1
- onlyBuiltDependencies:
2
- - esbuild
1
+ allowBuilds:
2
+ esbuild: true
package/tsconfig.json CHANGED
@@ -1,16 +1,13 @@
1
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
- ]
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
+ }