RequestParser.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.check = _parseAttribute('check', compile.options.check, {
  89. type: 'string',
  90. })
  91. response.flags = _parseAttribute('flags', compile.options.flags, {
  92. default: [],
  93. type: 'object',
  94. })
  95. if (settings.allowedCompileGroups) {
  96. response.compileGroup = _parseAttribute(
  97. 'compileGroup',
  98. compile.options.compileGroup,
  99. {
  100. validValues: settings.allowedCompileGroups,
  101. default: '',
  102. type: 'string',
  103. }
  104. )
  105. }
  106. // The syncType specifies whether the request contains all
  107. // resources (full) or only those resources to be updated
  108. // in-place (incremental).
  109. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  110. validValues: ['full', 'incremental'],
  111. type: 'string',
  112. })
  113. // The syncState is an identifier passed in with the request
  114. // which has the property that it changes when any resource is
  115. // added, deleted, moved or renamed.
  116. //
  117. // on syncType full the syncState identifier is passed in and
  118. // stored
  119. //
  120. // on syncType incremental the syncState identifier must match
  121. // the stored value
  122. response.syncState = _parseAttribute(
  123. 'syncState',
  124. compile.options.syncState,
  125. { type: 'string' }
  126. )
  127. if (response.timeout > MAX_TIMEOUT) {
  128. response.timeout = MAX_TIMEOUT
  129. }
  130. response.timeout = response.timeout * 1000 // milliseconds
  131. response.resources = (compile.resources || []).map(resource =>
  132. _parseResource(resource)
  133. )
  134. const rootResourcePath = _parseAttribute(
  135. 'rootResourcePath',
  136. compile.rootResourcePath,
  137. {
  138. default: 'main.tex',
  139. type: 'string',
  140. }
  141. )
  142. response.rootResourcePath = _checkPath(rootResourcePath)
  143. response.editorId = _parseAttribute('editorId', compile.options.editorId, {
  144. type: 'string',
  145. regex: EDITOR_ID_REGEX,
  146. })
  147. response.buildId = _parseAttribute('buildId', compile.options.buildId, {
  148. type: 'string',
  149. regex: OutputCacheManager.BUILD_REGEX,
  150. })
  151. } catch (error1) {
  152. const error = error1
  153. return callback(error)
  154. }
  155. callback(null, response)
  156. }
  157. function _parseResource(resource) {
  158. let modified
  159. if (resource.path == null || typeof resource.path !== 'string') {
  160. throw new Error('all resources should have a path attribute')
  161. }
  162. if (resource.modified != null) {
  163. modified = new Date(resource.modified)
  164. if (isNaN(modified.getTime())) {
  165. throw new Error(
  166. `resource modified date could not be understood: ${resource.modified}`
  167. )
  168. }
  169. }
  170. if (resource.url == null && resource.content == null) {
  171. throw new Error(
  172. 'all resources should have either a url or content attribute'
  173. )
  174. }
  175. if (resource.content != null && typeof resource.content !== 'string') {
  176. throw new Error('content attribute should be a string')
  177. }
  178. if (resource.url != null && typeof resource.url !== 'string') {
  179. throw new Error('url attribute should be a string')
  180. }
  181. if (resource.fallbackURL && typeof resource.fallbackURL !== 'string') {
  182. throw new Error('fallbackURL attribute should be a string')
  183. }
  184. return {
  185. path: resource.path,
  186. modified,
  187. url: resource.url,
  188. fallbackURL: resource.fallbackURL,
  189. content: resource.content,
  190. }
  191. }
  192. function _parseAttribute(name, attribute, options) {
  193. if (attribute != null) {
  194. if (options.validValues != null) {
  195. if (options.validValues.indexOf(attribute) === -1) {
  196. throw new Error(
  197. `${name} attribute should be one of: ${options.validValues.join(
  198. ', '
  199. )}`
  200. )
  201. }
  202. }
  203. if (options.type != null) {
  204. // eslint-disable-next-line valid-typeof
  205. if (typeof attribute !== options.type) {
  206. throw new Error(`${name} attribute should be a ${options.type}`)
  207. }
  208. }
  209. if (options.type === 'string' && options.regex instanceof RegExp) {
  210. if (!options.regex.test(attribute)) {
  211. throw new Error(
  212. `${name} attribute does not match regex ${options.regex}`
  213. )
  214. }
  215. }
  216. } else {
  217. if (options.default != null) {
  218. return options.default
  219. }
  220. }
  221. return attribute
  222. }
  223. function _checkPath(path) {
  224. // check that the request does not use a relative path
  225. for (const dir of Array.from(path.split('/'))) {
  226. if (dir === '..') {
  227. throw new Error('relative path in root resource')
  228. }
  229. }
  230. return path
  231. }
  232. module.exports = { parse, MAX_TIMEOUT }