safe-path.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // This file is shared between the frontend and server code of web, so that
  2. // filename validation is the same in both implementations.
  3. // The logic in all copies must be kept in sync:
  4. // app/src/Features/Project/SafePath.js
  5. // frontend/js/ide/directives/SafePath.js
  6. // frontend/js/features/file-tree/util/safe-path.js
  7. // eslint-disable-next-line prefer-regex-literals
  8. const BADCHAR_RX = new RegExp(
  9. `\
  10. [\
  11. \\/\
  12. \\\\\
  13. \\*\
  14. \\u0000-\\u001F\
  15. \\u007F\
  16. \\u0080-\\u009F\
  17. \\uD800-\\uDFFF\
  18. ]\
  19. `,
  20. 'g'
  21. )
  22. // eslint-disable-next-line prefer-regex-literals
  23. const BADFILE_RX = new RegExp(
  24. `\
  25. (^\\.$)\
  26. |(^\\.\\.$)\
  27. |(^\\s+)\
  28. |(\\s+$)\
  29. `,
  30. 'g'
  31. )
  32. // Put a block on filenames which match javascript property names, as they
  33. // can cause exceptions where the code puts filenames into a hash. This is a
  34. // temporary workaround until the code in other places is made safe against
  35. // property names.
  36. //
  37. // The list of property names is taken from
  38. // ['prototype'].concat(Object.getOwnPropertyNames(Object.prototype))
  39. // eslint-disable-next-line prefer-regex-literals
  40. const BLOCKEDFILE_RX = new RegExp(`\
  41. ^(\
  42. prototype\
  43. |constructor\
  44. |toString\
  45. |toLocaleString\
  46. |valueOf\
  47. |hasOwnProperty\
  48. |isPrototypeOf\
  49. |propertyIsEnumerable\
  50. |__defineGetter__\
  51. |__lookupGetter__\
  52. |__defineSetter__\
  53. |__lookupSetter__\
  54. |__proto__\
  55. )$\
  56. `)
  57. const MAX_PATH = 1024 // Maximum path length, in characters. This is fairly arbitrary.
  58. export function clean(filename) {
  59. filename = filename.replace(BADCHAR_RX, '_')
  60. // for BADFILE_RX replace any matches with an equal number of underscores
  61. filename = filename.replace(BADFILE_RX, match =>
  62. new Array(match.length + 1).join('_')
  63. )
  64. // replace blocked filenames 'prototype' with '@prototype'
  65. filename = filename.replace(BLOCKEDFILE_RX, '@$1')
  66. return filename
  67. }
  68. export function isCleanFilename(filename) {
  69. return (
  70. isAllowedLength(filename) &&
  71. !filename.match(BADCHAR_RX) &&
  72. !filename.match(BADFILE_RX)
  73. )
  74. }
  75. export function isBlockedFilename(filename) {
  76. return BLOCKEDFILE_RX.test(filename)
  77. }
  78. // returns whether a full path is 'clean' - e.g. is a full or relative path
  79. // that points to a file, and each element passes the rules in 'isCleanFilename'
  80. export function isCleanPath(path) {
  81. const elements = path.split('/')
  82. const lastElementIsEmpty = elements[elements.length - 1].length === 0
  83. if (lastElementIsEmpty) {
  84. return false
  85. }
  86. for (const element of Array.from(elements)) {
  87. if (element.length > 0 && !isCleanFilename(element)) {
  88. return false
  89. }
  90. }
  91. // check for a top-level reserved name
  92. if (BLOCKEDFILE_RX.test(path.replace(/^\/?/, ''))) {
  93. return false
  94. } // remove leading slash if present
  95. return true
  96. }
  97. export function isAllowedLength(pathname) {
  98. return pathname.length > 0 && pathname.length <= MAX_PATH
  99. }