SafePathTests.coffee 6.2 KB

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