2021-07-12 12:47:21 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
const VALID_COMPILERS = ['pdflatex', 'latex', 'xelatex', 'lualatex']
|
|
|
|
const MAX_TIMEOUT = 600
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
function parse(body, callback) {
|
|
|
|
let resource
|
|
|
|
const response = {}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
if (body.compile == null) {
|
|
|
|
return callback(
|
|
|
|
new Error('top level object should have a compile attribute')
|
|
|
|
)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
const { compile } = body
|
|
|
|
if (!compile.options) {
|
|
|
|
compile.options = {}
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
try {
|
|
|
|
response.metricsOpts = {
|
|
|
|
path: _parseAttribute('metricsPath', compile.options.metricsPath, {
|
|
|
|
default: '',
|
|
|
|
type: 'string',
|
|
|
|
}),
|
|
|
|
method: _parseAttribute('metricsMethod', compile.options.metricsMethod, {
|
|
|
|
default: '',
|
|
|
|
type: 'string',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
response.compiler = _parseAttribute('compiler', compile.options.compiler, {
|
|
|
|
validValues: VALID_COMPILERS,
|
|
|
|
default: 'pdflatex',
|
|
|
|
type: 'string',
|
|
|
|
})
|
|
|
|
response.enablePdfCaching = _parseAttribute(
|
|
|
|
'enablePdfCaching',
|
|
|
|
compile.options.enablePdfCaching,
|
|
|
|
{
|
2020-02-19 06:14:37 -05:00
|
|
|
default: false,
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'boolean',
|
2020-06-11 11:01:44 -04:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
)
|
|
|
|
response.timeout = _parseAttribute('timeout', compile.options.timeout, {
|
|
|
|
default: MAX_TIMEOUT,
|
|
|
|
type: 'number',
|
|
|
|
})
|
|
|
|
response.imageName = _parseAttribute(
|
|
|
|
'imageName',
|
|
|
|
compile.options.imageName,
|
|
|
|
{
|
|
|
|
type: 'string',
|
|
|
|
validValues:
|
|
|
|
settings.clsi &&
|
|
|
|
settings.clsi.docker &&
|
|
|
|
settings.clsi.docker.allowedImages,
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
)
|
|
|
|
response.draft = _parseAttribute('draft', compile.options.draft, {
|
|
|
|
default: false,
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2022-06-06 07:41:36 -04:00
|
|
|
response.stopOnFirstError = _parseAttribute(
|
|
|
|
'stopOnFirstError',
|
|
|
|
compile.options.stopOnFirstError,
|
|
|
|
{
|
|
|
|
default: false,
|
|
|
|
type: 'boolean',
|
|
|
|
}
|
|
|
|
)
|
2022-06-06 07:41:07 -04:00
|
|
|
response.check = _parseAttribute('check', compile.options.check, {
|
|
|
|
type: 'string',
|
|
|
|
})
|
|
|
|
response.flags = _parseAttribute('flags', compile.options.flags, {
|
|
|
|
default: [],
|
|
|
|
type: 'object',
|
|
|
|
})
|
|
|
|
if (settings.allowedCompileGroups) {
|
|
|
|
response.compileGroup = _parseAttribute(
|
|
|
|
'compileGroup',
|
|
|
|
compile.options.compileGroup,
|
2020-02-19 06:14:37 -05:00
|
|
|
{
|
2022-06-06 07:41:07 -04:00
|
|
|
validValues: settings.allowedCompileGroups,
|
|
|
|
default: '',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'string',
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
)
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
// The syncType specifies whether the request contains all
|
|
|
|
// resources (full) or only those resources to be updated
|
|
|
|
// in-place (incremental).
|
|
|
|
response.syncType = _parseAttribute('syncType', compile.options.syncType, {
|
|
|
|
validValues: ['full', 'incremental'],
|
|
|
|
type: 'string',
|
|
|
|
})
|
|
|
|
|
|
|
|
// The syncState is an identifier passed in with the request
|
|
|
|
// which has the property that it changes when any resource is
|
|
|
|
// added, deleted, moved or renamed.
|
|
|
|
//
|
|
|
|
// on syncType full the syncState identifier is passed in and
|
|
|
|
// stored
|
|
|
|
//
|
|
|
|
// on syncType incremental the syncState identifier must match
|
|
|
|
// the stored value
|
|
|
|
response.syncState = _parseAttribute(
|
|
|
|
'syncState',
|
|
|
|
compile.options.syncState,
|
|
|
|
{ type: 'string' }
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
if (response.timeout > MAX_TIMEOUT) {
|
|
|
|
response.timeout = MAX_TIMEOUT
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
response.timeout = response.timeout * 1000 // milliseconds
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
response.resources = (compile.resources || []).map(resource =>
|
|
|
|
_parseResource(resource)
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
const rootResourcePath = _parseAttribute(
|
|
|
|
'rootResourcePath',
|
|
|
|
compile.rootResourcePath,
|
|
|
|
{
|
|
|
|
default: 'main.tex',
|
|
|
|
type: 'string',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const originalRootResourcePath = rootResourcePath
|
|
|
|
const sanitizedRootResourcePath = _sanitizePath(rootResourcePath)
|
|
|
|
response.rootResourcePath = _checkPath(sanitizedRootResourcePath)
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
for (resource of response.resources) {
|
|
|
|
if (resource.path === originalRootResourcePath) {
|
|
|
|
resource.path = sanitizedRootResourcePath
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
} catch (error1) {
|
|
|
|
const error = error1
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
callback(null, response)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
function _parseResource(resource) {
|
|
|
|
let modified
|
|
|
|
if (resource.path == null || typeof resource.path !== 'string') {
|
|
|
|
throw new Error('all resources should have a path attribute')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.modified != null) {
|
|
|
|
modified = new Date(resource.modified)
|
|
|
|
if (isNaN(modified.getTime())) {
|
|
|
|
throw new Error(
|
|
|
|
`resource modified date could not be understood: ${resource.modified}`
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.url == null && resource.content == null) {
|
|
|
|
throw new Error(
|
|
|
|
'all resources should have either a url or content attribute'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (resource.content != null && typeof resource.content !== 'string') {
|
|
|
|
throw new Error('content attribute should be a string')
|
|
|
|
}
|
|
|
|
if (resource.url != null && typeof resource.url !== 'string') {
|
|
|
|
throw new Error('url attribute should be a string')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: resource.path,
|
|
|
|
modified,
|
|
|
|
url: resource.url,
|
|
|
|
content: resource.content,
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
function _parseAttribute(name, attribute, options) {
|
|
|
|
if (attribute != null) {
|
|
|
|
if (options.validValues != null) {
|
|
|
|
if (options.validValues.indexOf(attribute) === -1) {
|
|
|
|
throw new Error(
|
|
|
|
`${name} attribute should be one of: ${options.validValues.join(
|
2020-02-19 06:14:37 -05:00
|
|
|
', '
|
|
|
|
)}`
|
2022-06-06 07:41:07 -04:00
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
if (options.type != null) {
|
|
|
|
// eslint-disable-next-line valid-typeof
|
|
|
|
if (typeof attribute !== options.type) {
|
|
|
|
throw new Error(`${name} attribute should be a ${options.type}`)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
} else {
|
|
|
|
if (options.default != null) {
|
|
|
|
return options.default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attribute
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
function _sanitizePath(path) {
|
|
|
|
// See http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
return path.replace(/[#&;`|*?~<>^()[\]{}$\\\x0A\xFF\x00]/g, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
function _checkPath(path) {
|
|
|
|
// check that the request does not use a relative path
|
|
|
|
for (const dir of Array.from(path.split('/'))) {
|
|
|
|
if (dir === '..') {
|
|
|
|
throw new Error('relative path in root resource')
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
return path
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
|
|
|
|
module.exports = { parse, MAX_TIMEOUT }
|