RequestParser.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const settings = require('@overleaf/settings')
  2. const VALID_COMPILERS = ['pdflatex', 'latex', 'xelatex', 'lualatex']
  3. const MAX_TIMEOUT = 600
  4. function parse(body, callback) {
  5. let resource
  6. const response = {}
  7. if (body.compile == null) {
  8. return callback(
  9. new Error('top level object should have a compile attribute')
  10. )
  11. }
  12. const { compile } = body
  13. if (!compile.options) {
  14. compile.options = {}
  15. }
  16. try {
  17. response.metricsOpts = {
  18. path: _parseAttribute('metricsPath', compile.options.metricsPath, {
  19. default: '',
  20. type: 'string',
  21. }),
  22. method: _parseAttribute('metricsMethod', compile.options.metricsMethod, {
  23. default: '',
  24. type: 'string',
  25. }),
  26. }
  27. response.compiler = _parseAttribute('compiler', compile.options.compiler, {
  28. validValues: VALID_COMPILERS,
  29. default: 'pdflatex',
  30. type: 'string',
  31. })
  32. response.enablePdfCaching = _parseAttribute(
  33. 'enablePdfCaching',
  34. compile.options.enablePdfCaching,
  35. {
  36. default: false,
  37. type: 'boolean',
  38. }
  39. )
  40. response.timeout = _parseAttribute('timeout', compile.options.timeout, {
  41. default: MAX_TIMEOUT,
  42. type: 'number',
  43. })
  44. response.imageName = _parseAttribute(
  45. 'imageName',
  46. compile.options.imageName,
  47. {
  48. type: 'string',
  49. validValues:
  50. settings.clsi &&
  51. settings.clsi.docker &&
  52. settings.clsi.docker.allowedImages,
  53. }
  54. )
  55. response.draft = _parseAttribute('draft', compile.options.draft, {
  56. default: false,
  57. type: 'boolean',
  58. })
  59. response.stopOnFirstError = _parseAttribute(
  60. 'stopOnFirstError',
  61. compile.options.stopOnFirstError,
  62. {
  63. default: false,
  64. type: 'boolean',
  65. }
  66. )
  67. response.check = _parseAttribute('check', compile.options.check, {
  68. type: 'string',
  69. })
  70. response.flags = _parseAttribute('flags', compile.options.flags, {
  71. default: [],
  72. type: 'object',
  73. })
  74. if (settings.allowedCompileGroups) {
  75. response.compileGroup = _parseAttribute(
  76. 'compileGroup',
  77. compile.options.compileGroup,
  78. {
  79. validValues: settings.allowedCompileGroups,
  80. default: '',
  81. type: 'string',
  82. }
  83. )
  84. }
  85. // The syncType specifies whether the request contains all
  86. // resources (full) or only those resources to be updated
  87. // in-place (incremental).
  88. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  89. validValues: ['full', 'incremental'],
  90. type: 'string',
  91. })
  92. // The syncState is an identifier passed in with the request
  93. // which has the property that it changes when any resource is
  94. // added, deleted, moved or renamed.
  95. //
  96. // on syncType full the syncState identifier is passed in and
  97. // stored
  98. //
  99. // on syncType incremental the syncState identifier must match
  100. // the stored value
  101. response.syncState = _parseAttribute(
  102. 'syncState',
  103. compile.options.syncState,
  104. { type: 'string' }
  105. )
  106. if (response.timeout > MAX_TIMEOUT) {
  107. response.timeout = MAX_TIMEOUT
  108. }
  109. response.timeout = response.timeout * 1000 // milliseconds
  110. response.resources = (compile.resources || []).map(resource =>
  111. _parseResource(resource)
  112. )
  113. const rootResourcePath = _parseAttribute(
  114. 'rootResourcePath',
  115. compile.rootResourcePath,
  116. {
  117. default: 'main.tex',
  118. type: 'string',
  119. }
  120. )
  121. const originalRootResourcePath = rootResourcePath
  122. const sanitizedRootResourcePath = _sanitizePath(rootResourcePath)
  123. response.rootResourcePath = _checkPath(sanitizedRootResourcePath)
  124. for (resource of response.resources) {
  125. if (resource.path === originalRootResourcePath) {
  126. resource.path = sanitizedRootResourcePath
  127. }
  128. }
  129. } catch (error1) {
  130. const error = error1
  131. return callback(error)
  132. }
  133. callback(null, response)
  134. }
  135. function _parseResource(resource) {
  136. let modified
  137. if (resource.path == null || typeof resource.path !== 'string') {
  138. throw new Error('all resources should have a path attribute')
  139. }
  140. if (resource.modified != null) {
  141. modified = new Date(resource.modified)
  142. if (isNaN(modified.getTime())) {
  143. throw new Error(
  144. `resource modified date could not be understood: ${resource.modified}`
  145. )
  146. }
  147. }
  148. if (resource.url == null && resource.content == null) {
  149. throw new Error(
  150. 'all resources should have either a url or content attribute'
  151. )
  152. }
  153. if (resource.content != null && typeof resource.content !== 'string') {
  154. throw new Error('content attribute should be a string')
  155. }
  156. if (resource.url != null && typeof resource.url !== 'string') {
  157. throw new Error('url attribute should be a string')
  158. }
  159. return {
  160. path: resource.path,
  161. modified,
  162. url: resource.url,
  163. content: resource.content,
  164. }
  165. }
  166. function _parseAttribute(name, attribute, options) {
  167. if (attribute != null) {
  168. if (options.validValues != null) {
  169. if (options.validValues.indexOf(attribute) === -1) {
  170. throw new Error(
  171. `${name} attribute should be one of: ${options.validValues.join(
  172. ', '
  173. )}`
  174. )
  175. }
  176. }
  177. if (options.type != null) {
  178. // eslint-disable-next-line valid-typeof
  179. if (typeof attribute !== options.type) {
  180. throw new Error(`${name} attribute should be a ${options.type}`)
  181. }
  182. }
  183. } else {
  184. if (options.default != null) {
  185. return options.default
  186. }
  187. }
  188. return attribute
  189. }
  190. function _sanitizePath(path) {
  191. // See http://php.net/manual/en/function.escapeshellcmd.php
  192. // eslint-disable-next-line no-control-regex
  193. return path.replace(/[#&;`|*?~<>^()[\]{}$\\\x0A\xFF\x00]/g, '')
  194. }
  195. function _checkPath(path) {
  196. // check that the request does not use a relative path
  197. for (const dir of Array.from(path.split('/'))) {
  198. if (dir === '..') {
  199. throw new Error('relative path in root resource')
  200. }
  201. }
  202. return path
  203. }
  204. module.exports = { parse, MAX_TIMEOUT }