StaticServerForbidSymlinks.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-disable
  2. camelcase,
  3. no-cond-assign,
  4. no-unused-vars,
  5. n/no-deprecated-api,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS103: Rewrite code to no longer use __guard__
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. let ForbidSymlinks
  18. const Path = require('path')
  19. const fs = require('fs')
  20. const Settings = require('@overleaf/settings')
  21. const logger = require('@overleaf/logger')
  22. module.exports = ForbidSymlinks = function (staticFn, root, options) {
  23. const expressStatic = staticFn(root, options)
  24. const basePath = Path.resolve(root)
  25. return function (req, res, next) {
  26. let file, project_id, result
  27. const path = req.url
  28. // check that the path is of the form /project_id_or_name/path/to/file.log
  29. if ((result = path.match(/^\/?([a-zA-Z0-9_-]+)\/(.*)/))) {
  30. project_id = result[1]
  31. file = result[2]
  32. } else {
  33. logger.warn({ path }, 'unrecognized file request')
  34. return res.sendStatus(404)
  35. }
  36. // check that the file does not use a relative path
  37. for (const dir of Array.from(file.split('/'))) {
  38. if (dir === '..') {
  39. logger.warn({ path }, 'attempt to use a relative path')
  40. return res.sendStatus(404)
  41. }
  42. }
  43. // check that the requested path is normalized
  44. const requestedFsPath = `${basePath}/${project_id}/${file}`
  45. if (requestedFsPath !== Path.normalize(requestedFsPath)) {
  46. logger.error(
  47. { path: requestedFsPath },
  48. 'requestedFsPath is not normalized'
  49. )
  50. return res.sendStatus(404)
  51. }
  52. // check that the requested path is not a symlink
  53. return fs.realpath(requestedFsPath, function (err, realFsPath) {
  54. if (err != null) {
  55. if (err.code === 'ENOENT') {
  56. return res.sendStatus(404)
  57. } else {
  58. logger.error(
  59. {
  60. err,
  61. requestedFsPath,
  62. realFsPath,
  63. path: req.params[0],
  64. project_id: req.params.project_id,
  65. },
  66. 'error checking file access'
  67. )
  68. return res.sendStatus(500)
  69. }
  70. } else if (requestedFsPath !== realFsPath) {
  71. logger.warn(
  72. {
  73. requestedFsPath,
  74. realFsPath,
  75. path: req.params[0],
  76. project_id: req.params.project_id,
  77. },
  78. 'trying to access a different file (symlink), aborting'
  79. )
  80. return res.sendStatus(404)
  81. } else {
  82. return expressStatic(req, res, next)
  83. }
  84. })
  85. }
  86. }
  87. function __guard__(value, transform) {
  88. return typeof value !== 'undefined' && value !== null
  89. ? transform(value)
  90. : undefined
  91. }