path-sanitizer 3.1.0 → 4.0.1

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,31 @@
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
- run: pnpm test
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
+ - name: Install Node.js
20
+ uses: actions/setup-node@v4
21
+ with:
22
+ node-version: 22
23
+
24
+ - name: Install pnpm
25
+ uses: pnpm/action-setup@v4
26
+ with:
27
+ version: 9
28
+ run_install: true
29
+
30
+ - name: Run tests
31
+ run: pnpm test
package/README.md CHANGED
@@ -1,39 +1,42 @@
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
- ```
1
+ # path-sanitizer
2
+
3
+ A simple lightweight npm package for sanitizing paths to prevent Path Traversal
4
+
5
+ # Usage
6
+
7
+ ```js
8
+ import sanitize from 'path-sanitizer'
9
+ // or in a common js environment
10
+ const sanitize = (await import('path-sanitizer')).default
11
+
12
+ console.log(sanitize('path/to/file.txt'))
13
+ ```
14
+
15
+ Or here is a more complex example:
16
+
17
+ ```js
18
+ import sanitize from 'path-sanitizer'
19
+ import path from 'path'
20
+
21
+ // Real scenario:
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)
34
+ }
35
+
36
+ routeHandler('my-video.mp4') // Just fine...
37
+ routeHandler('../../Documents/grades.docx') // But what if we try to access a file outside the app directory?
38
+ routeHandler('/hello.txt') // What if the path starts with a slash, and we try to use an absolute path?
39
+ routeHandler('////////a\\\\../hi.txt') // What if the path is not valid?
40
+ routeHandler(500) // What if the path is not a string?
41
+ routeHandler('/:11^^') // What if the path contains not allowed characters (like ":")?
42
+ ```
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,7 +63,7 @@ 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 /
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "path-sanitizer",
3
- "version": "3.1.0",
3
+ "version": "4.0.1",
4
4
  "description": "A simple lightweight npm package for sanitizing paths to prevent Path Traversal",
5
+ "type": "module",
5
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
+ },
6
14
  "scripts": {
7
15
  "test": "vitest",
8
16
  "build": "tsc"
@@ -18,7 +26,6 @@
18
26
  "url": "https://github.com/cabraviva/path-sanitizer/issues"
19
27
  },
20
28
  "homepage": "https://github.com/cabraviva/path-sanitizer#readme",
21
- "types": "dist/index.d.ts",
22
29
  "devDependencies": {
23
30
  "@types/node": "^22.10.2",
24
31
  "typescript": "^5.7.2",
@@ -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
  }