SafePath.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. // This file is shared between the frontend and server code of web, so that
  15. // filename validation is the same in both implementations.
  16. // Both copies must be kept in sync:
  17. // app/coffee/Features/Project/SafePath.coffee
  18. // public/coffee/ide/directives/SafePath.coffee
  19. const load = function() {
  20. let SafePath
  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. const BADFILE_RX = new RegExp(
  36. `\
  37. (^\\.$)\
  38. |(^\\.\\.$)\
  39. |(^\\s+)\
  40. |(\\s+$)\
  41. `,
  42. 'g'
  43. )
  44. // Put a block on filenames which match javascript property names, as they
  45. // can cause exceptions where the code puts filenames into a hash. This is a
  46. // temporary workaround until the code in other places is made safe against
  47. // property names.
  48. //
  49. // The list of property names is taken from
  50. // ['prototype'].concat(Object.getOwnPropertyNames(Object.prototype))
  51. const BLOCKEDFILE_RX = new RegExp(`\
  52. ^(\
  53. prototype\
  54. |constructor\
  55. |toString\
  56. |toLocaleString\
  57. |valueOf\
  58. |hasOwnProperty\
  59. |isPrototypeOf\
  60. |propertyIsEnumerable\
  61. |__defineGetter__\
  62. |__lookupGetter__\
  63. |__defineSetter__\
  64. |__lookupSetter__\
  65. |__proto__\
  66. )$\
  67. `)
  68. const MAX_PATH = 1024 // Maximum path length, in characters. This is fairly arbitrary.
  69. return (SafePath = {
  70. // convert any invalid characters to underscores in the given filename
  71. clean(filename) {
  72. filename = filename.replace(BADCHAR_RX, '_')
  73. // for BADFILE_RX replace any matches with an equal number of underscores
  74. filename = filename.replace(BADFILE_RX, match =>
  75. new Array(match.length + 1).join('_')
  76. )
  77. // replace blocked filenames 'prototype' with '@prototype'
  78. filename = filename.replace(BLOCKEDFILE_RX, '@$1')
  79. return filename
  80. },
  81. // returns whether the filename is 'clean' (does not contain any invalid
  82. // characters or reserved words)
  83. isCleanFilename(filename) {
  84. return (
  85. SafePath.isAllowedLength(filename) &&
  86. !filename.match(BADCHAR_RX) &&
  87. !filename.match(BADFILE_RX)
  88. )
  89. },
  90. isBlockedFilename(filename) {
  91. return BLOCKEDFILE_RX.test(filename)
  92. },
  93. // returns whether a full path is 'clean' - e.g. is a full or relative path
  94. // that points to a file, and each element passes the rules in 'isCleanFilename'
  95. isCleanPath(path) {
  96. const elements = path.split('/')
  97. const lastElementIsEmpty = elements[elements.length - 1].length === 0
  98. if (lastElementIsEmpty) {
  99. return false
  100. }
  101. for (let element of Array.from(elements)) {
  102. if (element.length > 0 && !SafePath.isCleanFilename(element)) {
  103. return false
  104. }
  105. }
  106. // check for a top-level reserved name
  107. if (BLOCKEDFILE_RX.test(path.replace(/^\/?/, ''))) {
  108. return false
  109. } // remove leading slash if present
  110. return true
  111. },
  112. isAllowedLength(pathname) {
  113. return pathname.length > 0 && pathname.length <= MAX_PATH
  114. }
  115. })
  116. }
  117. module.exports = load()