StaticServerForbidSymlinks.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* eslint-disable
  2. camelcase,
  3. no-cond-assign,
  4. no-unused-vars,
  5. node/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('settings-sharelatex')
  21. const logger = require('logger-sharelatex')
  22. const url = require('url')
  23. module.exports = ForbidSymlinks = function (staticFn, root, options) {
  24. const expressStatic = staticFn(root, options)
  25. const basePath = Path.resolve(root)
  26. return function (req, res, next) {
  27. let file, project_id, result
  28. const path = __guard__(url.parse(req.url), (x) => x.pathname)
  29. // check that the path is of the form /project_id_or_name/path/to/file.log
  30. if ((result = path.match(/^\/?([a-zA-Z0-9_-]+)\/(.*)/))) {
  31. project_id = result[1]
  32. file = result[2]
  33. } else {
  34. logger.warn({ path }, 'unrecognized file request')
  35. return res.sendStatus(404)
  36. }
  37. // check that the file does not use a relative path
  38. for (const dir of Array.from(file.split('/'))) {
  39. if (dir === '..') {
  40. logger.warn({ path }, 'attempt to use a relative path')
  41. return res.sendStatus(404)
  42. }
  43. }
  44. // check that the requested path is normalized
  45. const requestedFsPath = `${basePath}/${project_id}/${file}`
  46. if (requestedFsPath !== Path.normalize(requestedFsPath)) {
  47. logger.error(
  48. { path: requestedFsPath },
  49. 'requestedFsPath is not normalized'
  50. )
  51. return res.sendStatus(404)
  52. }
  53. // check that the requested path is not a symlink
  54. return fs.realpath(requestedFsPath, function (err, realFsPath) {
  55. if (err != null) {
  56. if (err.code === 'ENOENT') {
  57. return res.sendStatus(404)
  58. } else {
  59. logger.error(
  60. {
  61. err,
  62. requestedFsPath,
  63. realFsPath,
  64. path: req.params[0],
  65. project_id: req.params.project_id
  66. },
  67. 'error checking file access'
  68. )
  69. return res.sendStatus(500)
  70. }
  71. } else if (requestedFsPath !== realFsPath) {
  72. logger.warn(
  73. {
  74. requestedFsPath,
  75. realFsPath,
  76. path: req.params[0],
  77. project_id: req.params.project_id
  78. },
  79. 'trying to access a different file (symlink), aborting'
  80. )
  81. return res.sendStatus(404)
  82. } else {
  83. return expressStatic(req, res, next)
  84. }
  85. })
  86. }
  87. }
  88. function __guard__(value, transform) {
  89. return typeof value !== 'undefined' && value !== null
  90. ? transform(value)
  91. : undefined
  92. }