From c6f74d24f1f85a6b3aee2d642717985f28b87ee2 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 7 Feb 2018 15:08:10 +0000 Subject: [PATCH] add missing SafePath.clean function --- .../coffee/Features/Project/SafePath.coffee | 7 ++++ .../unit/coffee/Project/SafePathTests.coffee | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/services/web/app/coffee/Features/Project/SafePath.coffee b/services/web/app/coffee/Features/Project/SafePath.coffee index 55cb4ac0c5..c1bd215777 100644 --- a/services/web/app/coffee/Features/Project/SafePath.coffee +++ b/services/web/app/coffee/Features/Project/SafePath.coffee @@ -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) && diff --git a/services/web/test/unit/coffee/Project/SafePathTests.coffee b/services/web/test/unit/coffee/Project/SafePathTests.coffee index 93bd275408..50b44b7e2c 100644 --- a/services/web/test/unit/coffee/Project/SafePathTests.coffee +++ b/services/web/test/unit/coffee/Project/SafePathTests.coffee @@ -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' +