path-sanitizer 4.0.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 +8 -12
- package/README.md +21 -18
- package/package.json +1 -1
package/.github/workflows/ci.yml
CHANGED
|
@@ -3,10 +3,10 @@ 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:
|
|
@@ -16,20 +16,16 @@ jobs:
|
|
|
16
16
|
- name: Checkout
|
|
17
17
|
uses: actions/checkout@v4
|
|
18
18
|
|
|
19
|
-
- uses: pnpm/action-setup@v4
|
|
20
|
-
name: Install pnpm
|
|
21
|
-
with:
|
|
22
|
-
version: 9
|
|
23
|
-
run_install: false
|
|
24
|
-
|
|
25
19
|
- name: Install Node.js
|
|
26
20
|
uses: actions/setup-node@v4
|
|
27
21
|
with:
|
|
28
22
|
node-version: 22
|
|
29
|
-
cache: 'pnpm'
|
|
30
23
|
|
|
31
|
-
- name: Install
|
|
32
|
-
|
|
24
|
+
- name: Install pnpm
|
|
25
|
+
uses: pnpm/action-setup@v4
|
|
26
|
+
with:
|
|
27
|
+
version: 9
|
|
28
|
+
run_install: true
|
|
33
29
|
|
|
34
30
|
- name: Run tests
|
|
35
|
-
run: pnpm test
|
|
31
|
+
run: pnpm test
|
package/README.md
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
# path-sanitizer
|
|
2
|
-
|
|
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 =
|
|
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
|
-
|
|
16
|
-
|
|
18
|
+
import sanitize from 'path-sanitizer'
|
|
19
|
+
import path from 'path'
|
|
17
20
|
|
|
18
21
|
// Real scenario:
|
|
19
|
-
function routeHandler
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
```
|