RequestParser.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. const settings = require('@overleaf/settings')
  2. const OutputCacheManager = require('./OutputCacheManager')
  3. const VALID_COMPILERS = ['pdflatex', 'latex', 'xelatex', 'lualatex']
  4. const MAX_TIMEOUT = 600
  5. function parse(body, callback) {
  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.pdfCachingMinChunkSize = _parseAttribute(
  41. 'pdfCachingMinChunkSize',
  42. compile.options.pdfCachingMinChunkSize,
  43. {
  44. default: settings.pdfCachingMinChunkSize,
  45. type: 'number',
  46. }
  47. )
  48. response.timeout = _parseAttribute('timeout', compile.options.timeout, {
  49. default: MAX_TIMEOUT,
  50. type: 'number',
  51. })
  52. response.imageName = _parseAttribute(
  53. 'imageName',
  54. compile.options.imageName,
  55. {
  56. type: 'string',
  57. validValues:
  58. settings.clsi &&
  59. settings.clsi.docker &&
  60. settings.clsi.docker.allowedImages,
  61. }
  62. )
  63. response.draft = _parseAttribute('draft', compile.options.draft, {
  64. default: false,
  65. type: 'boolean',
  66. })
  67. response.stopOnFirstError = _parseAttribute(
  68. 'stopOnFirstError',
  69. compile.options.stopOnFirstError,
  70. {
  71. default: false,
  72. type: 'boolean',
  73. }
  74. )
  75. response.check = _parseAttribute('check', compile.options.check, {
  76. type: 'string',
  77. })
  78. response.flags = _parseAttribute('flags', compile.options.flags, {
  79. default: [],
  80. type: 'object',
  81. })
  82. if (settings.allowedCompileGroups) {
  83. response.compileGroup = _parseAttribute(
  84. 'compileGroup',
  85. compile.options.compileGroup,
  86. {
  87. validValues: settings.allowedCompileGroups,
  88. default: '',
  89. type: 'string',
  90. }
  91. )
  92. }
  93. // The syncType specifies whether the request contains all
  94. // resources (full) or only those resources to be updated
  95. // in-place (incremental).
  96. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  97. validValues: ['full', 'incremental'],
  98. type: 'string',
  99. })
  100. // The syncState is an identifier passed in with the request
  101. // which has the property that it changes when any resource is
  102. // added, deleted, moved or renamed.
  103. //
  104. // on syncType full the syncState identifier is passed in and
  105. // stored
  106. //
  107. // on syncType incremental the syncState identifier must match
  108. // the stored value
  109. response.syncState = _parseAttribute(
  110. 'syncState',
  111. compile.options.syncState,
  112. { type: 'string' }
  113. )
  114. if (response.timeout > MAX_TIMEOUT) {
  115. response.timeout = MAX_TIMEOUT
  116. }
  117. response.timeout = response.timeout * 1000 // milliseconds
  118. response.resources = (compile.resources || []).map(resource =>
  119. _parseResource(resource)
  120. )
  121. const rootResourcePath = _parseAttribute(
  122. 'rootResourcePath',
  123. compile.rootResourcePath,
  124. {
  125. default: 'main.tex',
  126. type: 'string',
  127. }
  128. )
  129. response.rootResourcePath = _checkPath(rootResourcePath)
  130. response.buildId = _parseAttribute('buildId', compile.options.buildId, {
  131. type: 'string',
  132. regex: OutputCacheManager.BUILD_REGEX,
  133. })
  134. } catch (error1) {
  135. const error = error1
  136. return callback(error)
  137. }
  138. callback(null, response)
  139. }
  140. function _parseResource(resource) {
  141. let modified
  142. if (resource.path == null || typeof resource.path !== 'string') {
  143. throw new Error('all resources should have a path attribute')
  144. }
  145. if (resource.modified != null) {
  146. modified = new Date(resource.modified)
  147. if (isNaN(modified.getTime())) {
  148. throw new Error(
  149. `resource modified date could not be understood: ${resource.modified}`
  150. )
  151. }
  152. }
  153. if (resource.url == null && resource.content == null) {
  154. throw new Error(
  155. 'all resources should have either a url or content attribute'
  156. )
  157. }
  158. if (resource.content != null && typeof resource.content !== 'string') {
  159. throw new Error('content attribute should be a string')
  160. }
  161. if (resource.url != null && typeof resource.url !== 'string') {
  162. throw new Error('url attribute should be a string')
  163. }
  164. if (resource.fallbackURL && typeof resource.fallbackURL !== 'string') {
  165. throw new Error('fallbackURL attribute should be a string')
  166. }
  167. return {
  168. path: resource.path,
  169. modified,
  170. url: resource.url,
  171. fallbackURL: resource.fallbackURL,
  172. content: resource.content,
  173. }
  174. }
  175. function _parseAttribute(name, attribute, options) {
  176. if (attribute != null) {
  177. if (options.validValues != null) {
  178. if (options.validValues.indexOf(attribute) === -1) {
  179. throw new Error(
  180. `${name} attribute should be one of: ${options.validValues.join(
  181. ', '
  182. )}`
  183. )
  184. }
  185. }
  186. if (options.type != null) {
  187. // eslint-disable-next-line valid-typeof
  188. if (typeof attribute !== options.type) {
  189. throw new Error(`${name} attribute should be a ${options.type}`)
  190. }
  191. }
  192. if (options.type === 'string' && options.regex instanceof RegExp) {
  193. if (!options.regex.test(attribute)) {
  194. throw new Error(
  195. `${name} attribute does not match regex ${options.regex}`
  196. )
  197. }
  198. }
  199. } else {
  200. if (options.default != null) {
  201. return options.default
  202. }
  203. }
  204. return attribute
  205. }
  206. function _checkPath(path) {
  207. // check that the request does not use a relative path
  208. for (const dir of Array.from(path.split('/'))) {
  209. if (dir === '..') {
  210. throw new Error('relative path in root resource')
  211. }
  212. }
  213. return path
  214. }
  215. module.exports = { parse, MAX_TIMEOUT }