safe_pathname.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @ts-check
  2. 'use strict'
  3. const path = require('path-browserify')
  4. /**
  5. * Regular expressions for Overleaf v2 taken from
  6. * https://github.com/overleaf/internal/blob/f7b287b6a07354000a6b463ca3a5828104e4a811/services/web/app/src/Features/Project/SafePath.js
  7. */
  8. //
  9. // Regex of characters that are invalid in filenames
  10. //
  11. // eslint-disable-next-line no-control-regex
  12. const BAD_CHAR_RX = /[/*\u0000-\u001F\u007F\u0080-\u009F\uD800-\uDFFF]/g
  13. //
  14. // Regex of filename patterns that are invalid ("." ".." and leading/trailing
  15. // whitespace)
  16. //
  17. const BAD_FILE_RX = /(^\.$)|(^\.\.$)|(^\s+)|(\s+$)/g
  18. //
  19. // Put a block on filenames which match javascript property names, as they
  20. // can cause exceptions where the code puts filenames into a hash. This is a
  21. // temporary workaround until the code in other places is made safe against
  22. // property names.
  23. //
  24. // See https://github.com/overleaf/write_latex/wiki/Using-javascript-Objects-as-Maps
  25. //
  26. const BLOCKED_FILE_RX =
  27. /^(prototype|constructor|toString|toLocaleString|valueOf|hasOwnProperty|isPrototypeOf|propertyIsEnumerable|__defineGetter__|__lookupGetter__|__defineSetter__|__lookupSetter__|__proto__)$/
  28. //
  29. // Maximum path length, in characters. This is fairly arbitrary.
  30. //
  31. const MAX_PATH = 1024
  32. /**
  33. * Replace invalid characters and filename patterns in a filename with
  34. * underscores.
  35. * @param {string} filename
  36. */
  37. function cleanPart(filename) {
  38. filename = filename.replace(BAD_CHAR_RX, '_')
  39. filename = filename.replace(BAD_FILE_RX, function (match) {
  40. return new Array(match.length + 1).join('_')
  41. })
  42. return filename
  43. }
  44. /**
  45. * All pathnames in a Snapshot must be clean. We want pathnames that:
  46. *
  47. * 1. are unambiguous (e.g. no `.`s or redundant path separators)
  48. * 2. do not allow directory traversal attacks (e.g. no `..`s or absolute paths)
  49. * 3. do not contain leading/trailing space
  50. * 4. do not contain the character '*' in filenames
  51. *
  52. * We normalise the pathname, split it by the separator and then clean each part
  53. * as a filename
  54. *
  55. * @param {string} pathname
  56. * @return {String}
  57. */
  58. exports.clean = function (pathname) {
  59. pathname = path.normalize(pathname)
  60. pathname = pathname.replace(/\\/g, '/') // workaround for IE
  61. pathname = pathname.replace(/\/+/g, '/') // no multiple slashes
  62. pathname = pathname.replace(/^(\/.*)$/, '_$1') // no leading /
  63. pathname = pathname.replace(/^(.+)\/$/, '$1') // no trailing /
  64. pathname = pathname.replace(/^ *(.*)$/, '$1') // no leading spaces
  65. pathname = pathname.replace(/^(.*[^ ]) *$/, '$1') // no trailing spaces
  66. if (pathname.length === 0) pathname = '_'
  67. pathname = pathname.split('/').map(cleanPart).join('/')
  68. pathname = pathname.replace(BLOCKED_FILE_RX, '@$1')
  69. return pathname
  70. }
  71. /**
  72. * A pathname is clean (see clean) and not too long.
  73. *
  74. * @param {string} pathname
  75. * @return {Boolean}
  76. */
  77. exports.isClean = function pathnameIsClean(pathname) {
  78. return (
  79. exports.clean(pathname) === pathname &&
  80. pathname.length <= MAX_PATH &&
  81. pathname.length > 0
  82. )
  83. }