RequestParser.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.check = _parseAttribute('check', compile.options.check, {
  60. type: 'string',
  61. })
  62. response.flags = _parseAttribute('flags', compile.options.flags, {
  63. default: [],
  64. type: 'object',
  65. })
  66. if (settings.allowedCompileGroups) {
  67. response.compileGroup = _parseAttribute(
  68. 'compileGroup',
  69. compile.options.compileGroup,
  70. {
  71. validValues: settings.allowedCompileGroups,
  72. default: '',
  73. type: 'string',
  74. }
  75. )
  76. }
  77. // The syncType specifies whether the request contains all
  78. // resources (full) or only those resources to be updated
  79. // in-place (incremental).
  80. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  81. validValues: ['full', 'incremental'],
  82. type: 'string',
  83. })
  84. // The syncState is an identifier passed in with the request
  85. // which has the property that it changes when any resource is
  86. // added, deleted, moved or renamed.
  87. //
  88. // on syncType full the syncState identifier is passed in and
  89. // stored
  90. //
  91. // on syncType incremental the syncState identifier must match
  92. // the stored value
  93. response.syncState = _parseAttribute(
  94. 'syncState',
  95. compile.options.syncState,
  96. { type: 'string' }
  97. )
  98. if (response.timeout > MAX_TIMEOUT) {
  99. response.timeout = MAX_TIMEOUT
  100. }
  101. response.timeout = response.timeout * 1000 // milliseconds
  102. response.resources = (compile.resources || []).map(resource =>
  103. _parseResource(resource)
  104. )
  105. const rootResourcePath = _parseAttribute(
  106. 'rootResourcePath',
  107. compile.rootResourcePath,
  108. {
  109. default: 'main.tex',
  110. type: 'string',
  111. }
  112. )
  113. const originalRootResourcePath = rootResourcePath
  114. const sanitizedRootResourcePath = _sanitizePath(rootResourcePath)
  115. response.rootResourcePath = _checkPath(sanitizedRootResourcePath)
  116. for (resource of response.resources) {
  117. if (resource.path === originalRootResourcePath) {
  118. resource.path = sanitizedRootResourcePath
  119. }
  120. }
  121. } catch (error1) {
  122. const error = error1
  123. return callback(error)
  124. }
  125. callback(null, response)
  126. }
  127. function _parseResource(resource) {
  128. let modified
  129. if (resource.path == null || typeof resource.path !== 'string') {
  130. throw new Error('all resources should have a path attribute')
  131. }
  132. if (resource.modified != null) {
  133. modified = new Date(resource.modified)
  134. if (isNaN(modified.getTime())) {
  135. throw new Error(
  136. `resource modified date could not be understood: ${resource.modified}`
  137. )
  138. }
  139. }
  140. if (resource.url == null && resource.content == null) {
  141. throw new Error(
  142. 'all resources should have either a url or content attribute'
  143. )
  144. }
  145. if (resource.content != null && typeof resource.content !== 'string') {
  146. throw new Error('content attribute should be a string')
  147. }
  148. if (resource.url != null && typeof resource.url !== 'string') {
  149. throw new Error('url attribute should be a string')
  150. }
  151. return {
  152. path: resource.path,
  153. modified,
  154. url: resource.url,
  155. content: resource.content,
  156. }
  157. }
  158. function _parseAttribute(name, attribute, options) {
  159. if (attribute != null) {
  160. if (options.validValues != null) {
  161. if (options.validValues.indexOf(attribute) === -1) {
  162. throw new Error(
  163. `${name} attribute should be one of: ${options.validValues.join(
  164. ', '
  165. )}`
  166. )
  167. }
  168. }
  169. if (options.type != null) {
  170. // eslint-disable-next-line valid-typeof
  171. if (typeof attribute !== options.type) {
  172. throw new Error(`${name} attribute should be a ${options.type}`)
  173. }
  174. }
  175. } else {
  176. if (options.default != null) {
  177. return options.default
  178. }
  179. }
  180. return attribute
  181. }
  182. function _sanitizePath(path) {
  183. // See http://php.net/manual/en/function.escapeshellcmd.php
  184. // eslint-disable-next-line no-control-regex
  185. return path.replace(/[#&;`|*?~<>^()[\]{}$\\\x0A\xFF\x00]/g, '')
  186. }
  187. function _checkPath(path) {
  188. // check that the request does not use a relative path
  189. for (const dir of Array.from(path.split('/'))) {
  190. if (dir === '..') {
  191. throw new Error('relative path in root resource')
  192. }
  193. }
  194. return path
  195. }
  196. module.exports = { parse, MAX_TIMEOUT }