SafePathTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 'isAllowedLength', ->
  67. it 'should accept a valid path "main.tex"', ->
  68. result = @SafePath.isAllowedLength 'main.tex'
  69. result.should.equal true
  70. it 'should not accept an extremely long path', ->
  71. longPath = new Array(1000).join("/subdir") + '/main.tex'
  72. result = @SafePath.isAllowedLength longPath
  73. result.should.equal false
  74. it 'should not accept an empty path', ->
  75. result = @SafePath.isAllowedLength ''
  76. result.should.equal false
  77. describe 'clean', ->
  78. it 'should not modify a valid filename', ->
  79. result = @SafePath.clean 'main.tex'
  80. result.should.equal 'main.tex'
  81. it 'should replace invalid characters with _', ->
  82. result = @SafePath.clean 'foo/bar*/main.tex'
  83. result.should.equal 'foo_bar__main.tex'
  84. it 'should replace "." with "_"', ->
  85. result = @SafePath.clean '.'
  86. result.should.equal '_'
  87. it 'should replace ".." with "__"', ->
  88. result = @SafePath.clean '..'
  89. result.should.equal '__'
  90. it 'should replace a single trailing space with _', ->
  91. result = @SafePath.clean 'foo '
  92. result.should.equal 'foo_'
  93. it 'should replace a multiple trailing spaces with ___', ->
  94. result = @SafePath.clean 'foo '
  95. result.should.equal 'foo__'
  96. it 'should replace a single leading space with _', ->
  97. result = @SafePath.clean ' foo'
  98. result.should.equal '_foo'
  99. it 'should replace a multiple leading spaces with ___', ->
  100. result = @SafePath.clean ' foo'
  101. result.should.equal '__foo'
  102. it 'should prefix javascript property names with @', ->
  103. result = @SafePath.clean 'prototype'
  104. result.should.equal '@prototype'
  105. it 'should prefix javascript property names in the prototype with @', ->
  106. result = @SafePath.clean 'hasOwnProperty'
  107. result.should.equal '@hasOwnProperty'