safe_pathname.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const ot = require('../..')
  4. const safePathname = ot.safePathname
  5. describe('safePathname', function () {
  6. function expectClean(input, output, reason = '') {
  7. // check expected output and also idempotency
  8. const [cleanedInput, gotReason] = safePathname.cleanDebug(input)
  9. expect(cleanedInput).to.equal(output)
  10. expect(gotReason).to.equal(reason)
  11. expect(safePathname.clean(cleanedInput)).to.equal(cleanedInput)
  12. expect(safePathname.isClean(cleanedInput)).to.be.true
  13. }
  14. it('cleans pathnames', function () {
  15. // preserve valid pathnames
  16. expectClean('llama.jpg', 'llama.jpg')
  17. expectClean('DSC4056.JPG', 'DSC4056.JPG')
  18. // detects unclean pathnames
  19. expect(safePathname.isClean('rm -rf /')).to.be.falsy
  20. // replace invalid characters with underscores
  21. expectClean(
  22. 'test-s*\u0001\u0002m\u0007st\u0008.jpg',
  23. 'test-s___m_st_.jpg',
  24. 'cleanPart'
  25. )
  26. // keep slashes, normalize paths, replace ..
  27. expectClean('./foo', 'foo', 'normalize')
  28. expectClean('../foo', '__/foo', 'cleanPart')
  29. expectClean('foo/./bar', 'foo/bar', 'normalize')
  30. expectClean('foo/../bar', 'bar', 'normalize')
  31. expectClean('../../tricky/foo.bar', '__/__/tricky/foo.bar', 'cleanPart')
  32. expectClean(
  33. 'foo/../../tricky/foo.bar',
  34. '__/tricky/foo.bar',
  35. 'normalize,cleanPart'
  36. )
  37. expectClean('foo/bar/../../tricky/foo.bar', 'tricky/foo.bar', 'normalize')
  38. expectClean(
  39. 'foo/bar/baz/../../tricky/foo.bar',
  40. 'foo/tricky/foo.bar',
  41. 'normalize'
  42. )
  43. // remove illegal chars even when there is no extension
  44. expectClean('**foo', '__foo', 'cleanPart')
  45. // remove windows file paths
  46. expectClean('c:\\temp\\foo.txt', 'c:/temp/foo.txt', 'workaround for IE')
  47. // do not allow a leading slash (relative paths only)
  48. expectClean('/foo', '_/foo', 'no leading /')
  49. expectClean('//foo', '_/foo', 'normalize,no leading /')
  50. // do not allow multiple leading slashes
  51. expectClean('//foo', '_/foo', 'normalize,no leading /')
  52. // do not allow a trailing slash
  53. expectClean('/', '_', 'no leading /,no trailing /')
  54. expectClean('foo/', 'foo', 'no trailing /')
  55. expectClean('foo.tex/', 'foo.tex', 'no trailing /')
  56. // do not allow multiple trailing slashes
  57. expectClean('//', '_', 'normalize,no leading /,no trailing /')
  58. expectClean('///', '_', 'normalize,no leading /,no trailing /')
  59. expectClean('foo//', 'foo', 'normalize,no trailing /')
  60. // file and folder names that consist of . and .. are not OK
  61. expectClean('.', '_', 'cleanPart')
  62. expectClean('..', '__', 'cleanPart')
  63. // we will allow name with more dots e.g. ... and ....
  64. expectClean('...', '...')
  65. expectClean('....', '....')
  66. expectClean('foo/...', 'foo/...')
  67. expectClean('foo/....', 'foo/....')
  68. expectClean('foo/.../bar', 'foo/.../bar')
  69. expectClean('foo/..../bar', 'foo/..../bar')
  70. // leading dots are OK
  71. expectClean('._', '._')
  72. expectClean('.gitignore', '.gitignore')
  73. // trailing dots are not OK on Windows but we allow them
  74. expectClean('_.', '_.')
  75. expectClean('foo/_.', 'foo/_.')
  76. expectClean('foo/_./bar', 'foo/_./bar')
  77. expectClean('foo/_../bar', 'foo/_../bar')
  78. // spaces are allowed
  79. expectClean('a b.png', 'a b.png')
  80. // leading and trailing spaces are not OK
  81. expectClean(' foo', 'foo', 'no leading spaces')
  82. expectClean(' foo', 'foo', 'no leading spaces')
  83. expectClean('foo ', 'foo', 'no trailing spaces')
  84. expectClean('foo ', 'foo', 'no trailing spaces')
  85. // reserved file names on Windows should not be OK, but we already have
  86. // some in the old system, so have to allow them for now
  87. expectClean('AUX', 'AUX')
  88. expectClean('foo/AUX', 'foo/AUX')
  89. expectClean('AUX/foo', 'AUX/foo')
  90. // multiple dots are OK
  91. expectClean('a.b.png', 'a.b.png')
  92. expectClean('a.code.tex', 'a.code.tex')
  93. // there's no particular reason to allow multiple slashes; sometimes people
  94. // seem to rename files to URLs (https://domain/path) in an attempt to
  95. // upload a file, and this results in an empty directory name
  96. expectClean('foo//bar.png', 'foo/bar.png', 'normalize')
  97. expectClean('foo///bar.png', 'foo/bar.png', 'normalize')
  98. // Check javascript property handling
  99. expectClean('foo/prototype', 'foo/prototype') // OK as part of a pathname
  100. expectClean('prototype/test.txt', 'prototype/test.txt')
  101. expectClean('prototype', '@prototype', 'BLOCKED_FILE_RX') // not OK as whole pathname
  102. expectClean('hasOwnProperty', '@hasOwnProperty', 'BLOCKED_FILE_RX')
  103. expectClean('**proto**', '@__proto__', 'cleanPart,BLOCKED_FILE_RX')
  104. })
  105. })