path-sanitizer 4.0.1 → 4.0.3

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.
@@ -11,6 +11,8 @@ on:
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
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.1",
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.3",
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
+ }