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.
- package/.github/workflows/ci.yml +31 -35
- package/README.md +42 -39
- package/dist/index.js +9 -13
- package/package.json +9 -2
- package/pnpm-workspace.yaml +2 -0
- package/tsconfig.json +15 -14
package/.github/workflows/ci.yml
CHANGED
|
@@ -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
|
-
-
|
|
20
|
-
|
|
21
|
-
with:
|
|
22
|
-
version:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
routeHandler('
|
|
37
|
-
routeHandler(
|
|
38
|
-
routeHandler('
|
|
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
|
-
|
|
2
|
-
|
|
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 =
|
|
23
|
+
export default function sanitize(pathstr, options = DEFAULT_OPTIONS) {
|
|
28
24
|
if (!options)
|
|
29
|
-
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 =
|
|
29
|
+
options.decode = DEFAULT_OPTIONS.decode;
|
|
34
30
|
if (!options.parentDirectoryRegEx)
|
|
35
|
-
options.parentDirectoryRegEx =
|
|
31
|
+
options.parentDirectoryRegEx = DEFAULT_OPTIONS.parentDirectoryRegEx;
|
|
36
32
|
if (!options.notAllowedRegEx)
|
|
37
|
-
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 =
|
|
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 =
|
|
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
|
+
"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",
|
package/tsconfig.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNEXT",
|
|
4
|
-
"module": "
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
}
|