2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const assert = require('assert')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/StaticServerForbidSymlinks'
|
|
|
|
)
|
|
|
|
const { expect } = require('chai')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('StaticServerForbidSymlinks', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.settings = {
|
|
|
|
path: {
|
2021-07-13 07:04:48 -04:00
|
|
|
compilesDir: '/compiles/here',
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.fs = {}
|
|
|
|
this.ForbidSymlinks = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': this.settings,
|
2021-07-13 07:04:48 -04:00
|
|
|
fs: this.fs,
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
this.dummyStatic = (rootDir, options) => (req, res, next) =>
|
|
|
|
// console.log "dummyStatic serving file", rootDir, "called with", req.url
|
|
|
|
// serve it
|
|
|
|
next()
|
|
|
|
|
|
|
|
this.StaticServerForbidSymlinks = this.ForbidSymlinks(
|
|
|
|
this.dummyStatic,
|
|
|
|
this.settings.path.compilesDir
|
|
|
|
)
|
|
|
|
this.req = {
|
|
|
|
params: {
|
2021-07-13 07:04:48 -04:00
|
|
|
project_id: '12345',
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.res = {}
|
|
|
|
return (this.req.url = '/12345/output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('sending a normal file through', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.fs.realpath = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
`${this.settings.path.compilesDir}/${this.req.params.project_id}/output.pdf`
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call next', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(200)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res, done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a missing file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.fs.realpath = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
{ code: 'ENOENT' },
|
|
|
|
`${this.settings.path.compilesDir}/${this.req.params.project_id}/unknown.pdf`
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-07-05 12:27:06 -04:00
|
|
|
describe('with a new line', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.req.url = '/12345/output.pdf\nother file'
|
|
|
|
this.fs.realpath = sinon.stub().yields()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should process the correct file', function (done) {
|
|
|
|
this.res.sendStatus = () => {
|
|
|
|
this.fs.realpath.should.have.been.calledWith(
|
|
|
|
`${this.settings.path.compilesDir}/12345/output.pdf\nother file`
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a symlink file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.fs.realpath = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, `/etc/${this.req.params.project_id}/output.pdf`))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a relative file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = '/12345/../67890/output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a unnormalized file containing .', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = '/12345/foo/./output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a file containing an empty path', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = '/12345/foo//output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a non-project file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = '/.foo/output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a file outside the compiledir', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = '/../bar/output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a file with no leading /', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.req.url = './../bar/output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 404', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(404)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a github style path', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.req.url = '/henryoswald-latex_example/output/output.log'
|
|
|
|
return (this.fs.realpath = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
`${this.settings.path.compilesDir}/henryoswald-latex_example/output/output.log`
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call next', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(200)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res, done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('with an error from fs.realpath', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.fs.realpath = sinon.stub().callsArgWith(1, 'error'))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should send a 500', function (done) {
|
|
|
|
this.res.sendStatus = function (resCode) {
|
2020-02-19 06:15:37 -05:00
|
|
|
resCode.should.equal(500)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.StaticServerForbidSymlinks(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|