RequestParser.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.timeout = _parseAttribute('timeout', compile.options.timeout, {
  64. default: MAX_TIMEOUT,
  65. type: 'number',
  66. })
  67. response.imageName = _parseAttribute(
  68. 'imageName',
  69. compile.options.imageName,
  70. {
  71. type: 'string',
  72. validValues:
  73. settings.clsi &&
  74. settings.clsi.docker &&
  75. settings.clsi.docker.allowedImages,
  76. }
  77. )
  78. response.draft = _parseAttribute('draft', compile.options.draft, {
  79. default: false,
  80. type: 'boolean',
  81. })
  82. response.stopOnFirstError = _parseAttribute(
  83. 'stopOnFirstError',
  84. compile.options.stopOnFirstError,
  85. {
  86. default: false,
  87. type: 'boolean',
  88. }
  89. )
  90. response.check = _parseAttribute('check', compile.options.check, {
  91. type: 'string',
  92. })
  93. response.flags = _parseAttribute('flags', compile.options.flags, {
  94. default: [],
  95. type: 'object',
  96. })
  97. if (settings.allowedCompileGroups) {
  98. response.compileGroup = _parseAttribute(
  99. 'compileGroup',
  100. compile.options.compileGroup,
  101. {
  102. validValues: settings.allowedCompileGroups,
  103. default: '',
  104. type: 'string',
  105. }
  106. )
  107. }
  108. // The syncType specifies whether the request contains all
  109. // resources (full) or only those resources to be updated
  110. // in-place (incremental).
  111. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  112. validValues: [
  113. 'full',
  114. 'incremental',
  115. 'history-full',
  116. 'history-incremental',
  117. ],
  118. type: 'string',
  119. })
  120. // The syncState is an identifier passed in with the request
  121. // which has the property that it changes when any resource is
  122. // added, deleted, moved or renamed.
  123. //
  124. // on syncType full the syncState identifier is passed in and
  125. // stored
  126. //
  127. // on syncType incremental the syncState identifier must match
  128. // the stored value
  129. response.syncState = _parseAttribute(
  130. 'syncState',
  131. compile.options.syncState,
  132. { type: 'string' }
  133. )
  134. if (response.timeout > MAX_TIMEOUT) {
  135. response.timeout = MAX_TIMEOUT
  136. }
  137. response.timeout = response.timeout * 1000 // milliseconds
  138. response.resources = (compile.resources || []).map(resource =>
  139. _parseResource(resource)
  140. )
  141. response.historyId = _parseAttribute(
  142. 'historyId',
  143. compile.options.historyId,
  144. { type: 'string', regex: HISTORY_ID_REGEX }
  145. )
  146. response.baseHistoryVersion = _parseAttribute(
  147. 'baseHistoryVersion',
  148. compile.baseHistoryVersion,
  149. { type: 'number' }
  150. )
  151. response.globalBlobs = _parseAttribute('globalBlobs', compile.globalBlobs, {
  152. type: 'array',
  153. })
  154. // The snapshot and changes are validated when loading them in editor-core.
  155. response.rawSnapshot = compile.rawSnapshot
  156. response.rawChangeOperations = compile.rawChangeOperations
  157. response.isCompileFromHistory = !!response.rawChangeOperations
  158. // v1 conversions / submissions
  159. if (compile.filestoreBlobPrefix) {
  160. response.filestoreBlobPrefix = _checkPath(compile.filestoreBlobPrefix)
  161. }
  162. // clsi-perf
  163. response.clsiPerfVariant = _parseAttribute(
  164. 'clsiPerfVariant',
  165. compile.options.clsiPerfVariant,
  166. { type: 'string' }
  167. )
  168. const rootResourcePath = _parseAttribute(
  169. 'rootResourcePath',
  170. compile.rootResourcePath,
  171. {
  172. default: 'main.tex',
  173. type: 'string',
  174. }
  175. )
  176. response.rootResourcePath = _checkPath(rootResourcePath)
  177. response.editorId = _parseAttribute('editorId', compile.options.editorId, {
  178. type: 'string',
  179. regex: EDITOR_ID_REGEX,
  180. })
  181. response.buildId = _parseAttribute('buildId', compile.options.buildId, {
  182. type: 'string',
  183. regex: OutputCacheManager.BUILD_REGEX,
  184. })
  185. } catch (error1) {
  186. const error = error1
  187. return callback(error)
  188. }
  189. callback(null, response)
  190. }
  191. function _parseResource(resource) {
  192. let modified
  193. if (resource.path == null || typeof resource.path !== 'string') {
  194. throw new Error('all resources should have a path attribute')
  195. }
  196. if (resource.modified != null) {
  197. modified = new Date(resource.modified)
  198. if (isNaN(modified.getTime())) {
  199. throw new Error(
  200. `resource modified date could not be understood: ${resource.modified}`
  201. )
  202. }
  203. }
  204. if (resource.url == null && resource.content == null) {
  205. throw new Error(
  206. 'all resources should have either a url or content attribute'
  207. )
  208. }
  209. if (resource.content != null && typeof resource.content !== 'string') {
  210. throw new Error('content attribute should be a string')
  211. }
  212. if (resource.url != null && typeof resource.url !== 'string') {
  213. throw new Error('url attribute should be a string')
  214. }
  215. if (resource.fallbackURL && typeof resource.fallbackURL !== 'string') {
  216. throw new Error('fallbackURL attribute should be a string')
  217. }
  218. return {
  219. path: resource.path,
  220. modified,
  221. url: resource.url,
  222. fallbackURL: resource.fallbackURL,
  223. content: resource.content,
  224. }
  225. }
  226. function _parseAttribute(name, attribute, options) {
  227. if (attribute != null) {
  228. if (options.validValues != null) {
  229. if (options.validValues.indexOf(attribute) === -1) {
  230. throw new Error(
  231. `${name} attribute should be one of: ${options.validValues.join(
  232. ', '
  233. )}`
  234. )
  235. }
  236. }
  237. if (options.type === 'array') {
  238. if (!Array.isArray(attribute)) {
  239. throw new Error(`${name} attribute should be an array`)
  240. }
  241. } else if (options.type != null) {
  242. // eslint-disable-next-line valid-typeof
  243. if (typeof attribute !== options.type) {
  244. throw new Error(`${name} attribute should be a ${options.type}`)
  245. }
  246. }
  247. if (options.type === 'string' && options.regex instanceof RegExp) {
  248. if (!options.regex.test(attribute)) {
  249. throw new Error(
  250. `${name} attribute does not match regex ${options.regex}`
  251. )
  252. }
  253. }
  254. } else {
  255. if (options.default != null) {
  256. return options.default
  257. }
  258. }
  259. return attribute
  260. }
  261. function _checkPath(path) {
  262. // check that the request does not use a relative path
  263. for (const dir of Array.from(path.split('/'))) {
  264. if (dir === '..') {
  265. throw new Error('relative path in root resource')
  266. }
  267. }
  268. return path
  269. }
  270. export default { parse, MAX_TIMEOUT, promises: { parse: promisify(parse) } }