Skip to content

Commit b6d2319

Browse files
authored
Merge pull request #2 from cabraviva/dev
Fixed a security vulnerability
2 parents b37caa7 + bb2e758 commit b6d2319

File tree

4 files changed

+80
-62
lines changed

4 files changed

+80
-62
lines changed

lib/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,8 @@ describe('sanitize() - Vulnerability Tests', () => {
192192
it('Protects reported vulnerability #1', () => {
193193
expect(linuxSlash(join('/var/app-dir', sanitize("..=%5c..=%5c..=%5c..=%5c..=%5c..=%5c..=%5cetc/passwd")))).not.toBe('/etc/passwd')
194194
})
195+
196+
it('Protects reported vulnerability #2', () => {
197+
expect(linuxSlash(join('/var/app', sanitize("./../../test/../../../../../../../../../../etc/passwd")))).not.toBe('/etc/passwd')
198+
})
195199
})

lib/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ export default function sanitize(pathstr: string, options: SanitizeOptions = DEF
103103
// Replace double (back)slashes with a single slash
104104
sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/')
105105

106+
// Replace /../ with /
107+
sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/')
108+
109+
// Remove ./ or / at start
110+
while (sanitizedPath.startsWith('/') || sanitizedPath.startsWith('./') || sanitizedPath.endsWith('/..') || sanitizedPath.endsWith('/../') || sanitizedPath.startsWith('../') || sanitizedPath.startsWith('/../')) {
111+
sanitizedPath = sanitizedPath.replace(/^\.\//g, '') // ^./
112+
sanitizedPath = sanitizedPath.replace(/^\//g, '') // ^/
113+
// Remove ../ | /../ at pos 0 and /.. | /../ at end
114+
sanitizedPath = sanitizedPath.replace(/^[\/\\]\.\.[\/\\]/g, '/')
115+
sanitizedPath = sanitizedPath.replace(/^\.\.[\/\\]/g, '/')
116+
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.$/g, '/')
117+
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.\/$/g, '/')
118+
}
119+
106120
// Make sure out is not "."
107121
sanitizedPath = sanitizedPath.trim() === '.' ? '' : sanitizedPath
108122

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"homepage": "https://github.com/cabraviva/path-sanitizer#readme",
2121
"types": "dist/index.d.ts",
2222
"devDependencies": {
23-
"@types/node": "^22.9.3",
23+
"@types/node": "^22.10.2",
2424
"typescript": "^5.7.2",
25-
"vitest": "^2.1.5"
25+
"vitest": "^2.1.8"
2626
}
2727
}

pnpm-lock.yaml

Lines changed: 60 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)