StaticServerForbidSymlinksTests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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('assert')
  14. const path = require('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. 'settings-sharelatex': 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 symlink file', function () {
  88. beforeEach(function () {
  89. return (this.fs.realpath = sinon
  90. .stub()
  91. .callsArgWith(1, null, `/etc/${this.req.params.project_id}/output.pdf`))
  92. })
  93. return it('should send a 404', function (done) {
  94. this.res.sendStatus = function (resCode) {
  95. resCode.should.equal(404)
  96. return done()
  97. }
  98. return this.StaticServerForbidSymlinks(this.req, this.res)
  99. })
  100. })
  101. describe('with a relative file', function () {
  102. beforeEach(function () {
  103. return (this.req.url = '/12345/../67890/output.pdf')
  104. })
  105. return it('should send a 404', function (done) {
  106. this.res.sendStatus = function (resCode) {
  107. resCode.should.equal(404)
  108. return done()
  109. }
  110. return this.StaticServerForbidSymlinks(this.req, this.res)
  111. })
  112. })
  113. describe('with a unnormalized file containing .', function () {
  114. beforeEach(function () {
  115. return (this.req.url = '/12345/foo/./output.pdf')
  116. })
  117. return it('should send a 404', function (done) {
  118. this.res.sendStatus = function (resCode) {
  119. resCode.should.equal(404)
  120. return done()
  121. }
  122. return this.StaticServerForbidSymlinks(this.req, this.res)
  123. })
  124. })
  125. describe('with a file containing an empty path', function () {
  126. beforeEach(function () {
  127. return (this.req.url = '/12345/foo//output.pdf')
  128. })
  129. return it('should send a 404', function (done) {
  130. this.res.sendStatus = function (resCode) {
  131. resCode.should.equal(404)
  132. return done()
  133. }
  134. return this.StaticServerForbidSymlinks(this.req, this.res)
  135. })
  136. })
  137. describe('with a non-project file', function () {
  138. beforeEach(function () {
  139. return (this.req.url = '/.foo/output.pdf')
  140. })
  141. return it('should send a 404', function (done) {
  142. this.res.sendStatus = function (resCode) {
  143. resCode.should.equal(404)
  144. return done()
  145. }
  146. return this.StaticServerForbidSymlinks(this.req, this.res)
  147. })
  148. })
  149. describe('with a file outside the compiledir', function () {
  150. beforeEach(function () {
  151. return (this.req.url = '/../bar/output.pdf')
  152. })
  153. return it('should send a 404', function (done) {
  154. this.res.sendStatus = function (resCode) {
  155. resCode.should.equal(404)
  156. return done()
  157. }
  158. return this.StaticServerForbidSymlinks(this.req, this.res)
  159. })
  160. })
  161. describe('with a file with no leading /', function () {
  162. beforeEach(function () {
  163. return (this.req.url = './../bar/output.pdf')
  164. })
  165. return it('should send a 404', function (done) {
  166. this.res.sendStatus = function (resCode) {
  167. resCode.should.equal(404)
  168. return done()
  169. }
  170. return this.StaticServerForbidSymlinks(this.req, this.res)
  171. })
  172. })
  173. describe('with a github style path', function () {
  174. beforeEach(function () {
  175. this.req.url = '/henryoswald-latex_example/output/output.log'
  176. return (this.fs.realpath = sinon
  177. .stub()
  178. .callsArgWith(
  179. 1,
  180. null,
  181. `${this.settings.path.compilesDir}/henryoswald-latex_example/output/output.log`
  182. ))
  183. })
  184. return it('should call next', function (done) {
  185. this.res.sendStatus = function (resCode) {
  186. resCode.should.equal(200)
  187. return done()
  188. }
  189. return this.StaticServerForbidSymlinks(this.req, this.res, done)
  190. })
  191. })
  192. return describe('with an error from fs.realpath', function () {
  193. beforeEach(function () {
  194. return (this.fs.realpath = sinon.stub().callsArgWith(1, 'error'))
  195. })
  196. return it('should send a 500', function (done) {
  197. this.res.sendStatus = function (resCode) {
  198. resCode.should.equal(500)
  199. return done()
  200. }
  201. return this.StaticServerForbidSymlinks(this.req, this.res)
  202. })
  203. })
  204. })