Skip to content

Commit

Permalink
Fixed a security advisory
Browse files Browse the repository at this point in the history
  • Loading branch information
cabraviva committed Dec 31, 2024
1 parent 3953ebd commit bb2e758
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ describe('sanitize() - Vulnerability Tests', () => {
it('Protects reported vulnerability #1', () => {
expect(linuxSlash(join('/var/app-dir', sanitize("..=%5c..=%5c..=%5c..=%5c..=%5c..=%5c..=%5cetc/passwd")))).not.toBe('/etc/passwd')
})

it('Protects reported vulnerability #2', () => {
expect(linuxSlash(join('/var/app', sanitize("./../../test/../../../../../../../../../../etc/passwd")))).not.toBe('/etc/passwd')
})
})
14 changes: 14 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ export default function sanitize(pathstr: string, options: SanitizeOptions = DEF
// Replace double (back)slashes with a single slash
sanitizedPath = sanitizedPath.replace(/[\/\\]+/g, '/')

// Replace /../ with /
sanitizedPath = sanitizedPath.replace(options.parentDirectoryRegEx, '/')

// Remove ./ or / at start
while (sanitizedPath.startsWith('/') || sanitizedPath.startsWith('./') || sanitizedPath.endsWith('/..') || sanitizedPath.endsWith('/../') || sanitizedPath.startsWith('../') || sanitizedPath.startsWith('/../')) {
sanitizedPath = sanitizedPath.replace(/^\.\//g, '') // ^./
sanitizedPath = sanitizedPath.replace(/^\//g, '') // ^/
// Remove ../ | /../ at pos 0 and /.. | /../ at end
sanitizedPath = sanitizedPath.replace(/^[\/\\]\.\.[\/\\]/g, '/')
sanitizedPath = sanitizedPath.replace(/^\.\.[\/\\]/g, '/')
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.$/g, '/')
sanitizedPath = sanitizedPath.replace(/[\/\\]\.\.\/$/g, '/')
}

// Make sure out is not "."
sanitizedPath = sanitizedPath.trim() === '.' ? '' : sanitizedPath

Expand Down

0 comments on commit bb2e758

Please sign in to comment.