RequestParser.js 7.8 KB

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