RequestParser.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. const EDITOR_ID_REGEX = /^[a-f0-9-]{36}$/ // UUID
  6. function parse(body, callback) {
  7. const response = {}
  8. if (body.compile == null) {
  9. return callback(
  10. new Error('top level object should have a compile attribute')
  11. )
  12. }
  13. const { compile } = body
  14. if (!compile.options) {
  15. compile.options = {}
  16. }
  17. try {
  18. response.metricsOpts = {
  19. path: _parseAttribute('metricsPath', compile.options.metricsPath, {
  20. default: '',
  21. type: 'string',
  22. }),
  23. method: _parseAttribute('metricsMethod', compile.options.metricsMethod, {
  24. default: '',
  25. type: 'string',
  26. }),
  27. // Will be populated later. Must always be populated for prom library.
  28. compile: 'initial',
  29. }
  30. response.compiler = _parseAttribute('compiler', compile.options.compiler, {
  31. validValues: VALID_COMPILERS,
  32. default: 'pdflatex',
  33. type: 'string',
  34. })
  35. response.compileFromClsiCache = _parseAttribute(
  36. 'compileFromClsiCache',
  37. compile.options.compileFromClsiCache,
  38. { default: false, type: 'boolean' }
  39. )
  40. response.populateClsiCache = _parseAttribute(
  41. 'populateClsiCache',
  42. compile.options.populateClsiCache,
  43. { default: false, type: 'boolean' }
  44. )
  45. response.enablePdfCaching = _parseAttribute(
  46. 'enablePdfCaching',
  47. compile.options.enablePdfCaching,
  48. {
  49. default: false,
  50. type: 'boolean',
  51. }
  52. )
  53. response.pdfCachingMinChunkSize = _parseAttribute(
  54. 'pdfCachingMinChunkSize',
  55. compile.options.pdfCachingMinChunkSize,
  56. {
  57. default: settings.pdfCachingMinChunkSize,
  58. type: 'number',
  59. }
  60. )
  61. response.timeout = _parseAttribute('timeout', compile.options.timeout, {
  62. default: MAX_TIMEOUT,
  63. type: 'number',
  64. })
  65. response.imageName = _parseAttribute(
  66. 'imageName',
  67. compile.options.imageName,
  68. {
  69. type: 'string',
  70. validValues:
  71. settings.clsi &&
  72. settings.clsi.docker &&
  73. settings.clsi.docker.allowedImages,
  74. }
  75. )
  76. response.draft = _parseAttribute('draft', compile.options.draft, {
  77. default: false,
  78. type: 'boolean',
  79. })
  80. response.stopOnFirstError = _parseAttribute(
  81. 'stopOnFirstError',
  82. compile.options.stopOnFirstError,
  83. {
  84. default: false,
  85. type: 'boolean',
  86. }
  87. )
  88. response.clsiCacheSharded = _parseAttribute(
  89. 'clsiCacheSharded',
  90. compile.options.clsiCacheSharded,
  91. {
  92. default: false,
  93. type: 'boolean',
  94. }
  95. )
  96. response.check = _parseAttribute('check', compile.options.check, {
  97. type: 'string',
  98. })
  99. response.flags = _parseAttribute('flags', compile.options.flags, {
  100. default: [],
  101. type: 'object',
  102. })
  103. if (settings.allowedCompileGroups) {
  104. response.compileGroup = _parseAttribute(
  105. 'compileGroup',
  106. compile.options.compileGroup,
  107. {
  108. validValues: settings.allowedCompileGroups,
  109. default: '',
  110. type: 'string',
  111. }
  112. )
  113. }
  114. // The syncType specifies whether the request contains all
  115. // resources (full) or only those resources to be updated
  116. // in-place (incremental).
  117. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  118. validValues: ['full', 'incremental'],
  119. type: 'string',
  120. })
  121. // The syncState is an identifier passed in with the request
  122. // which has the property that it changes when any resource is
  123. // added, deleted, moved or renamed.
  124. //
  125. // on syncType full the syncState identifier is passed in and
  126. // stored
  127. //
  128. // on syncType incremental the syncState identifier must match
  129. // the stored value
  130. response.syncState = _parseAttribute(
  131. 'syncState',
  132. compile.options.syncState,
  133. { type: 'string' }
  134. )
  135. if (response.timeout > MAX_TIMEOUT) {
  136. response.timeout = MAX_TIMEOUT
  137. }
  138. response.timeout = response.timeout * 1000 // milliseconds
  139. response.resources = (compile.resources || []).map(resource =>
  140. _parseResource(resource)
  141. )
  142. const rootResourcePath = _parseAttribute(
  143. 'rootResourcePath',
  144. compile.rootResourcePath,
  145. {
  146. default: 'main.tex',
  147. type: 'string',
  148. }
  149. )
  150. response.rootResourcePath = _checkPath(rootResourcePath)
  151. response.editorId = _parseAttribute('editorId', compile.options.editorId, {
  152. type: 'string',
  153. regex: EDITOR_ID_REGEX,
  154. })
  155. response.buildId = _parseAttribute('buildId', compile.options.buildId, {
  156. type: 'string',
  157. regex: OutputCacheManager.BUILD_REGEX,
  158. })
  159. } catch (error1) {
  160. const error = error1
  161. return callback(error)
  162. }
  163. callback(null, response)
  164. }
  165. function _parseResource(resource) {
  166. let modified
  167. if (resource.path == null || typeof resource.path !== 'string') {
  168. throw new Error('all resources should have a path attribute')
  169. }
  170. if (resource.modified != null) {
  171. modified = new Date(resource.modified)
  172. if (isNaN(modified.getTime())) {
  173. throw new Error(
  174. `resource modified date could not be understood: ${resource.modified}`
  175. )
  176. }
  177. }
  178. if (resource.url == null && resource.content == null) {
  179. throw new Error(
  180. 'all resources should have either a url or content attribute'
  181. )
  182. }
  183. if (resource.content != null && typeof resource.content !== 'string') {
  184. throw new Error('content attribute should be a string')
  185. }
  186. if (resource.url != null && typeof resource.url !== 'string') {
  187. throw new Error('url attribute should be a string')
  188. }
  189. if (resource.fallbackURL && typeof resource.fallbackURL !== 'string') {
  190. throw new Error('fallbackURL attribute should be a string')
  191. }
  192. return {
  193. path: resource.path,
  194. modified,
  195. url: resource.url,
  196. fallbackURL: resource.fallbackURL,
  197. content: resource.content,
  198. }
  199. }
  200. function _parseAttribute(name, attribute, options) {
  201. if (attribute != null) {
  202. if (options.validValues != null) {
  203. if (options.validValues.indexOf(attribute) === -1) {
  204. throw new Error(
  205. `${name} attribute should be one of: ${options.validValues.join(
  206. ', '
  207. )}`
  208. )
  209. }
  210. }
  211. if (options.type != null) {
  212. // eslint-disable-next-line valid-typeof
  213. if (typeof attribute !== options.type) {
  214. throw new Error(`${name} attribute should be a ${options.type}`)
  215. }
  216. }
  217. if (options.type === 'string' && options.regex instanceof RegExp) {
  218. if (!options.regex.test(attribute)) {
  219. throw new Error(
  220. `${name} attribute does not match regex ${options.regex}`
  221. )
  222. }
  223. }
  224. } else {
  225. if (options.default != null) {
  226. return options.default
  227. }
  228. }
  229. return attribute
  230. }
  231. function _checkPath(path) {
  232. // check that the request does not use a relative path
  233. for (const dir of Array.from(path.split('/'))) {
  234. if (dir === '..') {
  235. throw new Error('relative path in root resource')
  236. }
  237. }
  238. return path
  239. }
  240. module.exports = { parse, MAX_TIMEOUT }