RequestParser.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. const response = {}
  6. if (body.compile == null) {
  7. return callback(
  8. new Error('top level object should have a compile attribute')
  9. )
  10. }
  11. const { compile } = body
  12. if (!compile.options) {
  13. compile.options = {}
  14. }
  15. try {
  16. response.metricsOpts = {
  17. path: _parseAttribute('metricsPath', compile.options.metricsPath, {
  18. default: '',
  19. type: 'string',
  20. }),
  21. method: _parseAttribute('metricsMethod', compile.options.metricsMethod, {
  22. default: '',
  23. type: 'string',
  24. }),
  25. }
  26. response.compiler = _parseAttribute('compiler', compile.options.compiler, {
  27. validValues: VALID_COMPILERS,
  28. default: 'pdflatex',
  29. type: 'string',
  30. })
  31. response.enablePdfCaching = _parseAttribute(
  32. 'enablePdfCaching',
  33. compile.options.enablePdfCaching,
  34. {
  35. default: false,
  36. type: 'boolean',
  37. }
  38. )
  39. response.timeout = _parseAttribute('timeout', compile.options.timeout, {
  40. default: MAX_TIMEOUT,
  41. type: 'number',
  42. })
  43. response.imageName = _parseAttribute(
  44. 'imageName',
  45. compile.options.imageName,
  46. {
  47. type: 'string',
  48. validValues:
  49. settings.clsi &&
  50. settings.clsi.docker &&
  51. settings.clsi.docker.allowedImages,
  52. }
  53. )
  54. response.draft = _parseAttribute('draft', compile.options.draft, {
  55. default: false,
  56. type: 'boolean',
  57. })
  58. response.stopOnFirstError = _parseAttribute(
  59. 'stopOnFirstError',
  60. compile.options.stopOnFirstError,
  61. {
  62. default: false,
  63. type: 'boolean',
  64. }
  65. )
  66. response.check = _parseAttribute('check', compile.options.check, {
  67. type: 'string',
  68. })
  69. response.flags = _parseAttribute('flags', compile.options.flags, {
  70. default: [],
  71. type: 'object',
  72. })
  73. if (settings.allowedCompileGroups) {
  74. response.compileGroup = _parseAttribute(
  75. 'compileGroup',
  76. compile.options.compileGroup,
  77. {
  78. validValues: settings.allowedCompileGroups,
  79. default: '',
  80. type: 'string',
  81. }
  82. )
  83. }
  84. // The syncType specifies whether the request contains all
  85. // resources (full) or only those resources to be updated
  86. // in-place (incremental).
  87. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  88. validValues: ['full', 'incremental'],
  89. type: 'string',
  90. })
  91. // The syncState is an identifier passed in with the request
  92. // which has the property that it changes when any resource is
  93. // added, deleted, moved or renamed.
  94. //
  95. // on syncType full the syncState identifier is passed in and
  96. // stored
  97. //
  98. // on syncType incremental the syncState identifier must match
  99. // the stored value
  100. response.syncState = _parseAttribute(
  101. 'syncState',
  102. compile.options.syncState,
  103. { type: 'string' }
  104. )
  105. if (response.timeout > MAX_TIMEOUT) {
  106. response.timeout = MAX_TIMEOUT
  107. }
  108. response.timeout = response.timeout * 1000 // milliseconds
  109. response.resources = (compile.resources || []).map(resource =>
  110. _parseResource(resource)
  111. )
  112. const rootResourcePath = _parseAttribute(
  113. 'rootResourcePath',
  114. compile.rootResourcePath,
  115. {
  116. default: 'main.tex',
  117. type: 'string',
  118. }
  119. )
  120. response.rootResourcePath = _checkPath(rootResourcePath)
  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 _checkPath(path) {
  183. // check that the request does not use a relative path
  184. for (const dir of Array.from(path.split('/'))) {
  185. if (dir === '..') {
  186. throw new Error('relative path in root resource')
  187. }
  188. }
  189. return path
  190. }
  191. module.exports = { parse, MAX_TIMEOUT }