Skip to content

Commit

Permalink
fix: path traversal with more than two dots followed by a leading slash
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 29, 2024
1 parent 5b92cca commit 60cf8bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/key_normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ export class KeyNormalizer {
let normalizedKey = string.condenseWhitespace(key)

/**
* Normalize slashes to unix style
* Remove consecutive '/'
* - Normalize slashes to unix style
* - Remove consecutive '/'
* - Remove more than two dots + slash "..../" to "../"
*/
return slash(normalizedKey).replace(/\/{2,}/g, '/')
return slash(normalizedKey)
.replace(/\/{2,}/g, '/')
.replace(/\.{3,}\//g, '../')
}

/**
Expand Down Expand Up @@ -85,7 +88,7 @@ export class KeyNormalizer {

/**
* Remove leading and ending '/'
* Remove leading and ending '.'
* Remove leading and ending "."
*/
return normalizedKey.replace(/^\/|\/$/g, '').replace(/^\.|\.$/g, '')
}
Expand Down
13 changes: 13 additions & 0 deletions tests/core/key_normalizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ test.group('Key normalizer | Path traversal', () => {
{
key: '/./../some/dir',
},
{
key: '.../foo/bar',
},
{
key: '\\something\\...\\...\\dirname',
},
{
key: 'beyond/root/.../',
},
])
.run(({ assert }, { key }) => {
assert.throws(
Expand Down Expand Up @@ -180,6 +189,10 @@ test.group('Key normalizer | Post normalization', () => {
key: 'C\\dirname\\\\subdir\\\\\\subsubdir',
output: 'C/dirname/subdir/subsubdir',
},
{
key: '...hello-world',
output: '..hello-world',
},
])
.run(({ assert }, { key, output }) => {
assert.equal(new KeyNormalizer().normalize(key), output)
Expand Down

0 comments on commit 60cf8bf

Please sign in to comment.