SafePath.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. // This file is shared between the frontend and server code of web, so that
  14. // filename validation is the same in both implementations.
  15. // The logic in all copies must be kept in sync:
  16. // app/src/Features/Project/SafePath.js
  17. // frontend/js/ide/directives/SafePath.js
  18. // frontend/js/features/file-tree/util/safe-path.js
  19. let SafePath
  20. // eslint-disable-next-line prefer-regex-literals
  21. const BADCHAR_RX = new RegExp(
  22. `\
  23. [\
  24. \\/\
  25. \\\\\
  26. \\*\
  27. \\u0000-\\u001F\
  28. \\u007F\
  29. \\u0080-\\u009F\
  30. \\uD800-\\uDFFF\
  31. ]\
  32. `,
  33. 'g'
  34. )
  35. // eslint-disable-next-line prefer-regex-literals
  36. const BADFILE_RX = new RegExp(
  37. `\
  38. (^\\.$)\
  39. |(^\\.\\.$)\
  40. |(^\\s+)\
  41. |(\\s+$)\
  42. `,
  43. 'g'
  44. )
  45. // Put a block on filenames which match javascript property names, as they
  46. // can cause exceptions where the code puts filenames into a hash. This is a
  47. // temporary workaround until the code in other places is made safe against
  48. // property names.
  49. //
  50. // The list of property names is taken from
  51. // ['prototype'].concat(Object.getOwnPropertyNames(Object.prototype))
  52. // eslint-disable-next-line prefer-regex-literals
  53. const BLOCKEDFILE_RX = new RegExp(`\
  54. ^(\
  55. prototype\
  56. |constructor\
  57. |toString\
  58. |toLocaleString\
  59. |valueOf\
  60. |hasOwnProperty\
  61. |isPrototypeOf\
  62. |propertyIsEnumerable\
  63. |__defineGetter__\
  64. |__lookupGetter__\
  65. |__defineSetter__\
  66. |__lookupSetter__\
  67. |__proto__\
  68. )$\
  69. `)
  70. const MAX_PATH = 1024 // Maximum path length, in characters. This is fairly arbitrary.
  71. export default SafePath = {
  72. clean(filename) {
  73. filename = filename.replace(BADCHAR_RX, '_')
  74. // for BADFILE_RX replace any matches with an equal number of underscores
  75. filename = filename.replace(BADFILE_RX, match =>
  76. new Array(match.length + 1).join('_')
  77. )
  78. // replace blocked filenames 'prototype' with '@prototype'
  79. filename = filename.replace(BLOCKEDFILE_RX, '@$1')
  80. return filename
  81. },
  82. isCleanFilename(filename) {
  83. return (
  84. SafePath.isAllowedLength(filename) &&
  85. !filename.match(BADCHAR_RX) &&
  86. !filename.match(BADFILE_RX)
  87. )
  88. },
  89. isAllowedLength(pathname) {
  90. return pathname.length > 0 && pathname.length <= MAX_PATH
  91. },
  92. }