StaticServerForbidSymlinks.js 2.8 KB

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