RequestParser.js 8.4 KB

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