add missing SafePath.clean function

This commit is contained in:
Brian Gough 2018-02-07 15:08:10 +00:00
parent 7f727d434e
commit c6f74d24f1
2 changed files with 41 additions and 0 deletions

View file

@ -22,6 +22,13 @@ MAX_PATH = 1024 # Maximum path length, in characters. This is fairly arbitrary.
module.exports = SafePath =
clean: (filename) ->
filename = filename.replace BADCHAR_RX, '_'
# for BADFILE_RX replace any matches with an equal number of underscores
filename = filename.replace BADFILE_RX, (match) ->
return new Array(match.length + 1).join("_")
return filename
isCleanFilename: (filename) ->
return SafePath.isAllowedLength(filename) &&
not filename.match(BADCHAR_RX) &&

View file

@ -87,3 +87,37 @@ describe 'SafePath', ->
it 'should not accept an empty path', ->
result = @SafePath.isAllowedLength ''
result.should.equal false
describe 'clean', ->
it 'should not modify a valid filename', ->
result = @SafePath.clean 'main.tex'
result.should.equal 'main.tex'
it 'should replace invalid characters with _', ->
result = @SafePath.clean 'foo/bar*/main.tex'
result.should.equal 'foo_bar__main.tex'
it 'should replace "." with "_"', ->
result = @SafePath.clean '.'
result.should.equal '_'
it 'should replace ".." with "__"', ->
result = @SafePath.clean '..'
result.should.equal '__'
it 'should replace a single trailing space with _', ->
result = @SafePath.clean 'foo '
result.should.equal 'foo_'
it 'should replace a multiple trailing spaces with ___', ->
result = @SafePath.clean 'foo '
result.should.equal 'foo__'
it 'should replace a single leading space with _', ->
result = @SafePath.clean ' foo'
result.should.equal '_foo'
it 'should replace a multiple leading spaces with ___', ->
result = @SafePath.clean ' foo'
result.should.equal '__foo'