StaticServerForbidSymlinksTests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const assert = require('node:assert')
  14. const path = require('node:path')
  15. const sinon = require('sinon')
  16. const modulePath = path.join(
  17. __dirname,
  18. '../../../app/js/StaticServerForbidSymlinks'
  19. )
  20. const { expect } = require('chai')
  21. describe('StaticServerForbidSymlinks', function () {
  22. beforeEach(function () {
  23. this.settings = {
  24. path: {
  25. compilesDir: '/compiles/here',
  26. },
  27. }
  28. this.fs = {}
  29. this.ForbidSymlinks = SandboxedModule.require(modulePath, {
  30. requires: {
  31. '@overleaf/settings': this.settings,
  32. fs: this.fs,
  33. },
  34. })
  35. this.dummyStatic = (rootDir, options) => (req, res, next) =>
  36. // console.log "dummyStatic serving file", rootDir, "called with", req.url
  37. // serve it
  38. next()
  39. this.StaticServerForbidSymlinks = this.ForbidSymlinks(
  40. this.dummyStatic,
  41. this.settings.path.compilesDir
  42. )
  43. this.req = {
  44. params: {
  45. project_id: '12345',
  46. },
  47. }
  48. this.res = {}
  49. return (this.req.url = '/12345/output.pdf')
  50. })
  51. describe('sending a normal file through', function () {
  52. beforeEach(function () {
  53. return (this.fs.realpath = sinon
  54. .stub()
  55. .callsArgWith(
  56. 1,
  57. null,
  58. `${this.settings.path.compilesDir}/${this.req.params.project_id}/output.pdf`
  59. ))
  60. })
  61. return it('should call next', function (done) {
  62. this.res.sendStatus = function (resCode) {
  63. resCode.should.equal(200)
  64. return done()
  65. }
  66. return this.StaticServerForbidSymlinks(this.req, this.res, done)
  67. })
  68. })
  69. describe('with a missing file', function () {
  70. beforeEach(function () {
  71. return (this.fs.realpath = sinon
  72. .stub()
  73. .callsArgWith(
  74. 1,
  75. { code: 'ENOENT' },
  76. `${this.settings.path.compilesDir}/${this.req.params.project_id}/unknown.pdf`
  77. ))
  78. })
  79. return it('should send a 404', function (done) {
  80. this.res.sendStatus = function (resCode) {
  81. resCode.should.equal(404)
  82. return done()
  83. }
  84. return this.StaticServerForbidSymlinks(this.req, this.res)
  85. })
  86. })
  87. describe('with a new line', function () {
  88. beforeEach(function () {
  89. this.req.url = '/12345/output.pdf\nother file'
  90. this.fs.realpath = sinon.stub().yields()
  91. })
  92. it('should process the correct file', function (done) {
  93. this.res.sendStatus = () => {
  94. this.fs.realpath.should.have.been.calledWith(
  95. `${this.settings.path.compilesDir}/12345/output.pdf\nother file`
  96. )
  97. done()
  98. }
  99. this.StaticServerForbidSymlinks(this.req, this.res)
  100. })
  101. })
  102. describe('with a symlink file', function () {
  103. beforeEach(function () {
  104. return (this.fs.realpath = sinon
  105. .stub()
  106. .callsArgWith(1, null, `/etc/${this.req.params.project_id}/output.pdf`))
  107. })
  108. return it('should send a 404', function (done) {
  109. this.res.sendStatus = function (resCode) {
  110. resCode.should.equal(404)
  111. return done()
  112. }
  113. return this.StaticServerForbidSymlinks(this.req, this.res)
  114. })
  115. })
  116. describe('with a relative file', function () {
  117. beforeEach(function () {
  118. return (this.req.url = '/12345/../67890/output.pdf')
  119. })
  120. return it('should send a 404', function (done) {
  121. this.res.sendStatus = function (resCode) {
  122. resCode.should.equal(404)
  123. return done()
  124. }
  125. return this.StaticServerForbidSymlinks(this.req, this.res)
  126. })
  127. })
  128. describe('with a unnormalized file containing .', function () {
  129. beforeEach(function () {
  130. return (this.req.url = '/12345/foo/./output.pdf')
  131. })
  132. return it('should send a 404', function (done) {
  133. this.res.sendStatus = function (resCode) {
  134. resCode.should.equal(404)
  135. return done()
  136. }
  137. return this.StaticServerForbidSymlinks(this.req, this.res)
  138. })
  139. })
  140. describe('with a file containing an empty path', function () {
  141. beforeEach(function () {
  142. return (this.req.url = '/12345/foo//output.pdf')
  143. })
  144. return it('should send a 404', function (done) {
  145. this.res.sendStatus = function (resCode) {
  146. resCode.should.equal(404)
  147. return done()
  148. }
  149. return this.StaticServerForbidSymlinks(this.req, this.res)
  150. })
  151. })
  152. describe('with a non-project file', function () {
  153. beforeEach(function () {
  154. return (this.req.url = '/.foo/output.pdf')
  155. })
  156. return it('should send a 404', function (done) {
  157. this.res.sendStatus = function (resCode) {
  158. resCode.should.equal(404)
  159. return done()
  160. }
  161. return this.StaticServerForbidSymlinks(this.req, this.res)
  162. })
  163. })
  164. describe('with a file outside the compiledir', function () {
  165. beforeEach(function () {
  166. return (this.req.url = '/../bar/output.pdf')
  167. })
  168. return it('should send a 404', function (done) {
  169. this.res.sendStatus = function (resCode) {
  170. resCode.should.equal(404)
  171. return done()
  172. }
  173. return this.StaticServerForbidSymlinks(this.req, this.res)
  174. })
  175. })
  176. describe('with a file with no leading /', function () {
  177. beforeEach(function () {
  178. return (this.req.url = './../bar/output.pdf')
  179. })
  180. return it('should send a 404', function (done) {
  181. this.res.sendStatus = function (resCode) {
  182. resCode.should.equal(404)
  183. return done()
  184. }
  185. return this.StaticServerForbidSymlinks(this.req, this.res)
  186. })
  187. })
  188. describe('with a github style path', function () {
  189. beforeEach(function () {
  190. this.req.url = '/henryoswald-latex_example/output/output.log'
  191. return (this.fs.realpath = sinon
  192. .stub()
  193. .callsArgWith(
  194. 1,
  195. null,
  196. `${this.settings.path.compilesDir}/henryoswald-latex_example/output/output.log`
  197. ))
  198. })
  199. return it('should call next', function (done) {
  200. this.res.sendStatus = function (resCode) {
  201. resCode.should.equal(200)
  202. return done()
  203. }
  204. return this.StaticServerForbidSymlinks(this.req, this.res, done)
  205. })
  206. })
  207. return describe('with an error from fs.realpath', function () {
  208. beforeEach(function () {
  209. return (this.fs.realpath = sinon.stub().callsArgWith(1, 'error'))
  210. })
  211. return it('should send a 500', function (done) {
  212. this.res.sendStatus = function (resCode) {
  213. resCode.should.equal(500)
  214. return done()
  215. }
  216. return this.StaticServerForbidSymlinks(this.req, this.res)
  217. })
  218. })
  219. })