SafePathTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. expect = chai.expect
  5. sinon = require 'sinon'
  6. modulePath = "../../../../app/js/Features/Project/SafePath"
  7. SandboxedModule = require('sandboxed-module')
  8. describe 'SafePath', ->
  9. beforeEach ->
  10. @SafePath = SandboxedModule.require modulePath
  11. describe 'isCleanFilename', ->
  12. it 'should accept a valid filename "main.tex"', ->
  13. result = @SafePath.isCleanFilename 'main.tex'
  14. result.should.equal true
  15. it 'should not accept an empty filename', ->
  16. result = @SafePath.isCleanFilename ''
  17. result.should.equal false
  18. it 'should not accept / anywhere', ->
  19. result = @SafePath.isCleanFilename 'foo/bar'
  20. result.should.equal false
  21. it 'should not accept .', ->
  22. result = @SafePath.isCleanFilename '.'
  23. result.should.equal false
  24. it 'should not accept ..', ->
  25. result = @SafePath.isCleanFilename '..'
  26. result.should.equal false
  27. it 'should not accept * anywhere', ->
  28. result = @SafePath.isCleanFilename 'foo*bar'
  29. result.should.equal false
  30. it 'should not accept leading whitespace', ->
  31. result = @SafePath.isCleanFilename ' foobar.tex'
  32. result.should.equal false
  33. it 'should not accept trailing whitespace', ->
  34. result = @SafePath.isCleanFilename 'foobar.tex '
  35. result.should.equal false
  36. it 'should not accept leading and trailing whitespace', ->
  37. result = @SafePath.isCleanFilename ' foobar.tex '
  38. result.should.equal false
  39. it 'should not accept control characters (0-31)', ->
  40. result = @SafePath.isCleanFilename 'foo\u0010bar'
  41. result.should.equal false
  42. it 'should not accept control characters (127, delete)', ->
  43. result = @SafePath.isCleanFilename 'foo\u007fbar'
  44. result.should.equal false
  45. it 'should not accept control characters (128-159)', ->
  46. result = @SafePath.isCleanFilename 'foo\u0080\u0090bar'
  47. result.should.equal false
  48. it 'should not accept surrogate characters (128-159)', ->
  49. result = @SafePath.isCleanFilename 'foo\uD800\uDFFFbar'
  50. result.should.equal false
  51. # it 'should not accept a trailing .', ->
  52. # result = @SafePath.isCleanFilename 'hello.'
  53. # result.should.equal false
  54. # it 'should not accept \\', ->
  55. # result = @SafePath.isCleanFilename 'foo\\bar'
  56. # result.should.equal false
  57. describe 'isAllowedLength', ->
  58. it 'should accept a valid path "main.tex"', ->
  59. result = @SafePath.isAllowedLength 'main.tex'
  60. result.should.equal true
  61. it 'should not accept an extremely long path', ->
  62. longPath = new Array(1000).join("/subdir") + '/main.tex'
  63. result = @SafePath.isAllowedLength longPath
  64. result.should.equal false
  65. it 'should not accept an empty path', ->
  66. result = @SafePath.isAllowedLength ''
  67. result.should.equal false
  68. describe 'clean', ->
  69. it 'should not modify a valid filename', ->
  70. result = @SafePath.clean 'main.tex'
  71. result.should.equal 'main.tex'
  72. it 'should replace invalid characters with _', ->
  73. result = @SafePath.clean 'foo/bar*/main.tex'
  74. result.should.equal 'foo_bar__main.tex'
  75. it 'should replace "." with "_"', ->
  76. result = @SafePath.clean '.'
  77. result.should.equal '_'
  78. it 'should replace ".." with "__"', ->
  79. result = @SafePath.clean '..'
  80. result.should.equal '__'
  81. it 'should replace a single trailing space with _', ->
  82. result = @SafePath.clean 'foo '
  83. result.should.equal 'foo_'
  84. it 'should replace a multiple trailing spaces with ___', ->
  85. result = @SafePath.clean 'foo '
  86. result.should.equal 'foo__'
  87. it 'should replace a single leading space with _', ->
  88. result = @SafePath.clean ' foo'
  89. result.should.equal '_foo'
  90. it 'should replace a multiple leading spaces with ___', ->
  91. result = @SafePath.clean ' foo'
  92. result.should.equal '__foo'