SafePathTests.coffee 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 accept javascript property names', ->
  52. result = @SafePath.isCleanFilename 'prototype'
  53. result.should.equal true
  54. it 'should accept javascript property names in the prototype', ->
  55. result = @SafePath.isCleanFilename 'hasOwnProperty'
  56. result.should.equal true
  57. # this test never worked correctly because the spaces are not replaced by underscores in isCleanFilename
  58. # it 'should not accept javascript property names resulting from substitutions', ->
  59. # result = @SafePath.isCleanFilename ' proto '
  60. # result.should.equal false
  61. # it 'should not accept a trailing .', ->
  62. # result = @SafePath.isCleanFilename 'hello.'
  63. # result.should.equal false
  64. it 'should not accept \\', ->
  65. result = @SafePath.isCleanFilename 'foo\\bar'
  66. result.should.equal false
  67. describe 'isCleanPath', ->
  68. it 'should accept a valid filename "main.tex"', ->
  69. result = @SafePath.isCleanPath 'main.tex'
  70. result.should.equal true
  71. it 'should accept a valid path "foo/main.tex"', ->
  72. result = @SafePath.isCleanPath 'foo/main.tex'
  73. result.should.equal true
  74. it 'should accept empty path elements', ->
  75. result = @SafePath.isCleanPath 'foo//main.tex'
  76. result.should.equal true
  77. it 'should not accept an empty filename', ->
  78. result = @SafePath.isCleanPath 'foo/bar/'
  79. result.should.equal false
  80. it 'should accept a path that starts with a slash', ->
  81. result = @SafePath.isCleanPath '/etc/passwd'
  82. result.should.equal true
  83. it 'should not accept a path that has an asterisk as the 0th element', ->
  84. result = @SafePath.isCleanPath '*/foo/bar'
  85. result.should.equal false
  86. it 'should not accept a path that has an asterisk as a middle element', ->
  87. result = @SafePath.isCleanPath 'foo/*/bar'
  88. result.should.equal false
  89. it 'should not accept a path that has an asterisk as the filename', ->
  90. result = @SafePath.isCleanPath 'foo/bar/*'
  91. result.should.equal false
  92. it 'should not accept a path that contains an asterisk in the 0th element', ->
  93. result = @SafePath.isCleanPath 'f*o/bar/baz'
  94. result.should.equal false
  95. it 'should not accept a path that contains an asterisk in a middle element', ->
  96. result = @SafePath.isCleanPath 'foo/b*r/baz'
  97. result.should.equal false
  98. it 'should not accept a path that contains an asterisk in the filename', ->
  99. result = @SafePath.isCleanPath 'foo/bar/b*z'
  100. result.should.equal false
  101. it 'should not accept multiple problematic elements', ->
  102. result = @SafePath.isCleanPath 'f*o/b*r/b*z'
  103. result.should.equal false
  104. it 'should not accept a problematic path with an empty element', ->
  105. result = @SafePath.isCleanPath 'foo//*/bar'
  106. result.should.equal false
  107. it 'should not accept javascript property names', ->
  108. result = @SafePath.isCleanPath 'prototype'
  109. result.should.equal false
  110. it 'should not accept javascript property names in the prototype', ->
  111. result = @SafePath.isCleanPath 'hasOwnProperty'
  112. result.should.equal false
  113. it 'should not accept javascript property names resulting from substitutions', ->
  114. result = @SafePath.isCleanPath ' proto '
  115. result.should.equal false
  116. describe 'isAllowedLength', ->
  117. it 'should accept a valid path "main.tex"', ->
  118. result = @SafePath.isAllowedLength 'main.tex'
  119. result.should.equal true
  120. it 'should not accept an extremely long path', ->
  121. longPath = new Array(1000).join("/subdir") + '/main.tex'
  122. result = @SafePath.isAllowedLength longPath
  123. result.should.equal false
  124. it 'should not accept an empty path', ->
  125. result = @SafePath.isAllowedLength ''
  126. result.should.equal false
  127. describe 'clean', ->
  128. it 'should not modify a valid filename', ->
  129. result = @SafePath.clean 'main.tex'
  130. result.should.equal 'main.tex'
  131. it 'should replace invalid characters with _', ->
  132. result = @SafePath.clean 'foo/bar*/main.tex'
  133. result.should.equal 'foo_bar__main.tex'
  134. it 'should replace "." with "_"', ->
  135. result = @SafePath.clean '.'
  136. result.should.equal '_'
  137. it 'should replace ".." with "__"', ->
  138. result = @SafePath.clean '..'
  139. result.should.equal '__'
  140. it 'should replace a single trailing space with _', ->
  141. result = @SafePath.clean 'foo '
  142. result.should.equal 'foo_'
  143. it 'should replace a multiple trailing spaces with ___', ->
  144. result = @SafePath.clean 'foo '
  145. result.should.equal 'foo__'
  146. it 'should replace a single leading space with _', ->
  147. result = @SafePath.clean ' foo'
  148. result.should.equal '_foo'
  149. it 'should replace a multiple leading spaces with ___', ->
  150. result = @SafePath.clean ' foo'
  151. result.should.equal '__foo'
  152. it 'should prefix javascript property names with @', ->
  153. result = @SafePath.clean 'prototype'
  154. result.should.equal '@prototype'
  155. it 'should prefix javascript property names in the prototype with @', ->
  156. result = @SafePath.clean 'hasOwnProperty'
  157. result.should.equal '@hasOwnProperty'