safe_pathname.test.js 3.9 KB

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