15 Commits

Author SHA1 Message Date
Christopher Hoskin
b01755ca43 Test updated logger on clsi 2020-07-24 14:22:48 +01:00
Jakob Ackermann
1ee48d0274 Merge pull request #182 from overleaf/msm-fix-npe-community-edition
Fixed NPE when Settings.clsi is defined but Settings.clsi.docker is not
2020-07-15 11:01:08 +02:00
Jakob Ackermann
384d544bf2 Merge pull request #183 from overleaf/jpa-clsi-allowed-image-names
[misc] RequestParser: restrict imageName to an allow list and add tests
2020-07-15 10:58:36 +02:00
Jakob Ackermann
df09caaf46 Merge pull request #186 from overleaf/jpa-import-132
[LocalCommandRunner] run: block a double call of the callback
2020-07-15 10:57:46 +02:00
Jakob Ackermann
e59250d1fd Merge pull request #185 from overleaf/jpa-import-123
[ExampleDocumentTests] drop out in case of an error during compilation
2020-07-15 10:57:34 +02:00
Jakob Ackermann
e0176bbcbc Merge pull request #132 from das7pad/hotfix-double-call
[LocalCommandRunner] run: block a double call of the callback
2020-07-03 12:58:25 +02:00
Jakob Ackermann
53cc80fc7f [misc] fix formatting 2020-07-03 11:47:53 +01:00
Jakob Ackermann
47d1196dde Merge pull request #123 from das7pad/hotfix/test-error-handling
[ExampleDocumentTests] drop out in case of an error during compilation
2020-07-03 12:40:15 +02:00
Jakob Ackermann
267ff9e7f1 [ExampleDocumentTests] drop out in case of an error during compilation
Signed-off-by: Jakob Ackermann <das7pad@outlook.com>
2020-07-03 11:38:12 +01:00
Jakob Ackermann
0cecf26569 [misc] move the image check prior to the base image override 2020-07-01 10:01:25 +01:00
Jakob Ackermann
ee0e8066d3 [misc] apply review feedback
- move setting into clsi.docker namespace
- rename the variable for images to allowedImages / ALLOWED_IMAGES
- add an additional check for the image name into the DockerRunner

Co-Authored-By: Brian Gough <brian.gough@overleaf.com>
2020-06-30 12:01:21 +01:00
Jakob Ackermann
6edb458910 [misc] wordcount: restrict image to an allow list and add tests 2020-06-26 13:28:12 +01:00
Jakob Ackermann
5ed09d1a98 [misc] RequestParser: restrict imageName to an allow list and add tests 2020-06-26 13:28:09 +01:00
Miguel Serrano
ad8fec6a1a Fixed NPE when Settings.clsi is defined but Settings.clsi.docker is not 2020-06-25 12:31:10 +02:00
Jakob Ackermann
b18c9854b6 [LocalCommandRunner] run: block a double call of the callback
The subprocess event handler fires the "error" and "close" event in case
 of a failure.
Both events would call the given callback, resulting in double
 processing of the subprocess result downstream.

Signed-off-by: Jakob Ackermann <das7pad@outlook.com>
2020-04-16 15:55:55 +02:00
15 changed files with 783 additions and 226 deletions

View File

@@ -218,6 +218,15 @@ module.exports = CompileController = {
const { project_id } = req.params
const { user_id } = req.params
const { image } = req.query
if (
image &&
Settings.clsi &&
Settings.clsi.docker &&
Settings.clsi.docker.allowedImages &&
!Settings.clsi.docker.allowedImages.includes(image)
) {
return res.status(400).send('invalid image')
}
logger.log({ image, file, project_id }, 'word count request')
return CompileManager.wordcount(project_id, user_id, file, image, function(

View File

@@ -520,7 +520,9 @@ module.exports = CompileManager = {
compileName,
command,
directory,
Settings.clsi != null ? Settings.clsi.docker.image : undefined,
Settings.clsi && Settings.clsi.docker
? Settings.clsi.docker.image
: undefined,
timeout,
{},
compileGroup,

View File

@@ -86,6 +86,13 @@ module.exports = DockerRunner = {
;({ image } = Settings.clsi.docker)
}
if (
Settings.clsi.docker.allowedImages &&
!Settings.clsi.docker.allowedImages.includes(image)
) {
return callback(new Error('image not allowed'))
}
if (Settings.texliveImageNameOveride != null) {
const img = image.split('/')
image = `${Settings.texliveImageNameOveride}/${img[2]}`

View File

@@ -15,6 +15,7 @@
*/
let CommandRunner
const { spawn } = require('child_process')
const _ = require('underscore')
const logger = require('logger-sharelatex')
logger.info('using standard command runner')
@@ -33,6 +34,8 @@ module.exports = CommandRunner = {
let key, value
if (callback == null) {
callback = function(error) {}
} else {
callback = _.once(callback)
}
command = Array.from(command).map(arg =>
arg.toString().replace('$COMPILE_DIR', directory)

View File

@@ -61,7 +61,13 @@ module.exports = RequestParser = {
response.imageName = this._parseAttribute(
'imageName',
compile.options.imageName,
{ type: 'string' }
{
type: 'string',
validValues:
settings.clsi &&
settings.clsi.docker &&
settings.clsi.docker.allowedImages
}
)
response.draft = this._parseAttribute('draft', compile.options.draft, {
default: false,

View File

@@ -129,6 +129,17 @@ if (process.env.DOCKER_RUNNER) {
process.exit(1)
}
if (process.env.ALLOWED_IMAGES) {
try {
module.exports.clsi.docker.allowedImages = process.env.ALLOWED_IMAGES.split(
' '
)
} catch (error) {
console.error(error, 'could not apply allowed images setting')
process.exit(1)
}
}
module.exports.path.synctexBaseDir = () => '/compile'
module.exports.path.sandboxedCompilesHostDir = process.env.COMPILES_HOST_DIR

View File

@@ -3,6 +3,7 @@ version: "2.3"
services:
dev:
environment:
ALLOWED_IMAGES: "quay.io/sharelatex/texlive-full:2017.1"
TEXLIVE_IMAGE: quay.io/sharelatex/texlive-full:2017.1
TEXLIVE_IMAGE_USER: "tex"
SHARELATEX_CONFIG: /app/config/settings.defaults.coffee
@@ -18,6 +19,7 @@ services:
ci:
environment:
ALLOWED_IMAGES: ${TEXLIVE_IMAGE}
TEXLIVE_IMAGE: quay.io/sharelatex/texlive-full:2017.1
TEXLIVE_IMAGE_USER: "tex"
SHARELATEX_CONFIG: /app/config/settings.defaults.coffee

706
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -28,7 +28,7 @@
"heapdump": "^0.3.15",
"lockfile": "^1.0.4",
"lodash": "^4.17.15",
"logger-sharelatex": "^1.9.1",
"logger-sharelatex": "git+https://github.com/overleaf/logger-module.git#csh-metadata-fallback-node-fetch",
"lynx": "0.2.0",
"metrics-sharelatex": "^2.6.0",
"mysql": "^2.18.1",

View File

@@ -0,0 +1,102 @@
const Client = require('./helpers/Client')
const ClsiApp = require('./helpers/ClsiApp')
const { expect } = require('chai')
describe('AllowedImageNames', function() {
beforeEach(function(done) {
this.project_id = Client.randomId()
this.request = {
options: {
imageName: undefined
},
resources: [
{
path: 'main.tex',
content: `\
\\documentclass{article}
\\begin{document}
Hello world
\\end{document}\
`
}
]
}
ClsiApp.ensureRunning(done)
})
describe('with a valid name', function() {
beforeEach(function(done) {
this.request.options.imageName = process.env.TEXLIVE_IMAGE
Client.compile(this.project_id, this.request, (error, res, body) => {
this.error = error
this.res = res
this.body = body
done(error)
})
})
it('should return success', function() {
expect(this.res.statusCode).to.equal(200)
})
it('should return a PDF', function() {
let pdf
try {
pdf = Client.getOutputFile(this.body, 'pdf')
} catch (e) {}
expect(pdf).to.exist
})
})
describe('with an invalid name', function() {
beforeEach(function(done) {
this.request.options.imageName = 'something/evil:1337'
Client.compile(this.project_id, this.request, (error, res, body) => {
this.error = error
this.res = res
this.body = body
done(error)
})
})
it('should return non success', function() {
expect(this.res.statusCode).to.not.equal(200)
})
it('should not return a PDF', function() {
let pdf
try {
pdf = Client.getOutputFile(this.body, 'pdf')
} catch (e) {}
expect(pdf).to.not.exist
})
})
describe('wordcount', function() {
beforeEach(function(done) {
Client.compile(this.project_id, this.request, done)
})
it('should error out with an invalid imageName', function() {
Client.wordcountWithImage(
this.project_id,
'main.tex',
'something/evil:1337',
(error, result) => {
expect(String(error)).to.include('statusCode=400')
}
)
})
it('should produce a texcout a valid imageName', function() {
Client.wordcountWithImage(
this.project_id,
'main.tex',
process.env.TEXLIVE_IMAGE,
(error, result) => {
expect(error).to.not.exist
expect(result).to.exist
expect(result.texcount).to.exist
}
)
})
})
})

View File

@@ -235,6 +235,7 @@ describe('Example Documents', function() {
) === 'failure'
) {
console.log('DEBUG: error', error, 'body', JSON.stringify(body))
return done(new Error('Compile failed'))
}
const pdf = Client.getOutputFile(body, 'pdf')
return downloadAndComparePdf(
@@ -263,6 +264,7 @@ describe('Example Documents', function() {
) === 'failure'
) {
console.log('DEBUG: error', error, 'body', JSON.stringify(body))
return done(new Error('Compile failed'))
}
const pdf = Client.getOutputFile(body, 'pdf')
return downloadAndComparePdf(

View File

@@ -189,6 +189,11 @@ module.exports = Client = {
},
wordcount(project_id, file, callback) {
const image = undefined
Client.wordcountWithImage(project_id, file, image, callback)
},
wordcountWithImage(project_id, file, image, callback) {
if (callback == null) {
callback = function(error, pdfPositions) {}
}
@@ -196,6 +201,7 @@ module.exports = Client = {
{
url: `${this.host}/project/${project_id}/wordcount`,
qs: {
image,
file
}
},
@@ -203,6 +209,9 @@ module.exports = Client = {
if (error != null) {
return callback(error)
}
if (response.statusCode !== 200) {
return callback(new Error(`statusCode=${response.statusCode}`))
}
return callback(null, JSON.parse(body))
}
)

View File

@@ -12,6 +12,7 @@
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
require('chai').should()
const { expect } = require('chai')
const modulePath = require('path').join(
__dirname,
'../../../app/js/CompileController'
@@ -287,21 +288,60 @@ describe('CompileController', function() {
this.CompileManager.wordcount = sinon
.stub()
.callsArgWith(4, null, (this.texcount = ['mock-texcount']))
return this.CompileController.wordcount(this.req, this.res, this.next)
})
it('should return the word count of a file', function() {
this.CompileController.wordcount(this.req, this.res, this.next)
return this.CompileManager.wordcount
.calledWith(this.project_id, undefined, this.file, this.image)
.should.equal(true)
})
return it('should return the texcount info', function() {
it('should return the texcount info', function() {
this.CompileController.wordcount(this.req, this.res, this.next)
return this.res.json
.calledWith({
texcount: this.texcount
})
.should.equal(true)
})
describe('when allowedImages is set', function() {
beforeEach(function() {
this.Settings.clsi = { docker: {} }
this.Settings.clsi.docker.allowedImages = [
'repo/image:tag1',
'repo/image:tag2'
]
this.res.send = sinon.stub()
this.res.status = sinon.stub().returns({ send: this.res.send })
})
describe('with an invalid image', function() {
beforeEach(function() {
this.req.query.image = 'something/evil:1337'
this.CompileController.wordcount(this.req, this.res, this.next)
})
it('should return a 400', function() {
expect(this.res.status.calledWith(400)).to.equal(true)
})
it('should not run the query', function() {
expect(this.CompileManager.wordcount.called).to.equal(false)
})
})
describe('with a valid image', function() {
beforeEach(function() {
this.req.query.image = 'repo/image:tag1'
this.CompileController.wordcount(this.req, this.res, this.next)
})
it('should not return a 400', function() {
expect(this.res.status.calledWith(400)).to.equal(false)
})
it('should run the query', function() {
expect(this.CompileManager.wordcount.called).to.equal(true)
})
})
})
})
})

View File

@@ -273,7 +273,7 @@ describe('DockerRunner', function() {
})
})
return describe('with image override', function() {
describe('with image override', function() {
beforeEach(function() {
this.Settings.texliveImageNameOveride = 'overrideimage.com/something'
this.DockerRunner._runAndWaitForContainer = sinon
@@ -296,6 +296,62 @@ describe('DockerRunner', function() {
return image.should.equal('overrideimage.com/something/image:2016.2')
})
})
describe('with image restriction', function() {
beforeEach(function() {
this.Settings.clsi.docker.allowedImages = [
'repo/image:tag1',
'repo/image:tag2'
]
this.DockerRunner._runAndWaitForContainer = sinon
.stub()
.callsArgWith(3, null, (this.output = 'mock-output'))
})
describe('with a valid image', function() {
beforeEach(function() {
this.DockerRunner.run(
this.project_id,
this.command,
this.directory,
'repo/image:tag1',
this.timeout,
this.env,
this.compileGroup,
this.callback
)
})
it('should setup the container', function() {
this.DockerRunner._getContainerOptions.called.should.equal(true)
})
})
describe('with a invalid image', function() {
beforeEach(function() {
this.DockerRunner.run(
this.project_id,
this.command,
this.directory,
'something/different:evil',
this.timeout,
this.env,
this.compileGroup,
this.callback
)
})
it('should call the callback with an error', function() {
const err = new Error('image not allowed')
this.callback.called.should.equal(true)
this.callback.args[0][0].message.should.equal(err.message)
})
it('should not setup the container', function() {
this.DockerRunner._getContainerOptions.called.should.equal(false)
})
})
})
})
describe('run with _getOptions', function() {

View File

@@ -114,6 +114,48 @@ describe('RequestParser', function() {
})
})
describe('when image restrictions are present', function() {
beforeEach(function() {
this.settings.clsi = { docker: {} }
this.settings.clsi.docker.allowedImages = [
'repo/name:tag1',
'repo/name:tag2'
]
})
describe('with imageName set to something invalid', function() {
beforeEach(function() {
const request = this.validRequest
request.compile.options.imageName = 'something/different:latest'
this.RequestParser.parse(request, (error, data) => {
this.error = error
this.data = data
})
})
it('should throw an error for imageName', function() {
expect(String(this.error)).to.include(
'imageName attribute should be one of'
)
})
})
describe('with imageName set to something valid', function() {
beforeEach(function() {
const request = this.validRequest
request.compile.options.imageName = 'repo/name:tag1'
this.RequestParser.parse(request, (error, data) => {
this.error = error
this.data = data
})
})
it('should set the imageName', function() {
this.data.imageName.should.equal('repo/name:tag1')
})
})
})
describe('with flags set', function() {
beforeEach(function() {
this.validRequest.compile.options.flags = ['-file-line-error']