Add method to sanitize full paths

For convenience, add a method to SafePath to break a path into components and verify the status of each one.

bug: overleaf/sharelatex#908
Signed-off-by: Simon Detheridge <s@sd.ai>
This commit is contained in:
Simon Detheridge 2018-10-06 18:09:41 +01:00
parent 50993f59c0
commit e66210d2af
2 changed files with 65 additions and 3 deletions

View file

@ -67,6 +67,15 @@ load = () ->
!BADFILE_RX.test(filename) &&
!BLOCKEDFILE_RX.test(filename)
isCleanPath: (path) ->
elements = path.split('/')
return false if elements[elements.length - 1].length == 0
for element in elements
return false if element.length > 0 && !SafePath.isCleanFilename element
return true
isAllowedLength: (pathname) ->
return pathname.length > 0 && pathname.length <= MAX_PATH

View file

@ -83,6 +83,59 @@ describe 'SafePath', ->
result = @SafePath.isCleanFilename 'foo\\bar'
result.should.equal false
describe 'isCleanPath', ->
it 'should accept a valid filename "main.tex"', ->
result = @SafePath.isCleanPath 'main.tex'
result.should.equal true
it 'should accept a valid path "foo/main.tex"', ->
result = @SafePath.isCleanPath 'foo/main.tex'
result.should.equal true
it 'should accept empty path elements', ->
result = @SafePath.isCleanPath 'foo//main.tex'
result.should.equal true
it 'should not accept an empty filename', ->
result = @SafePath.isCleanPath 'foo/bar/'
result.should.equal false
it 'should accept a path that starts with a slash', ->
result = @SafePath.isCleanPath '/etc/passwd'
result.should.equal true
it 'should not accept a path that has an asterisk as the 0th element', ->
result = @SafePath.isCleanPath '*/foo/bar'
result.should.equal false
it 'should not accept a path that has an asterisk as a middle element', ->
result = @SafePath.isCleanPath 'foo/*/bar'
result.should.equal false
it 'should not accept a path that has an asterisk as the filename', ->
result = @SafePath.isCleanPath 'foo/bar/*'
result.should.equal false
it 'should not accept a path that contains an asterisk in the 0th element', ->
result = @SafePath.isCleanPath 'f*o/bar/baz'
result.should.equal false
it 'should not accept a path that contains an asterisk in a middle element', ->
result = @SafePath.isCleanPath 'foo/b*r/baz'
result.should.equal false
it 'should not accept a path that contains an asterisk in the filename', ->
result = @SafePath.isCleanPath 'foo/bar/b*z'
result.should.equal false
it 'should not accept multiple problematic elements', ->
result = @SafePath.isCleanPath 'f*o/b*r/b*z'
result.should.equal false
it 'should not accept a problematic path with an empty element', ->
result = @SafePath.isCleanPath 'foo//*/bar'
result.should.equal false
describe 'isAllowedLength', ->
it 'should accept a valid path "main.tex"', ->
result = @SafePath.isAllowedLength 'main.tex'