RequestParser.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-control-regex,
  4. no-throw-literal,
  5. no-unused-vars,
  6. no-useless-escape,
  7. standard/no-callback-literal,
  8. valid-typeof,
  9. */
  10. // TODO: This file was created by bulk-decaffeinate.
  11. // Fix any style issues and re-enable lint.
  12. /*
  13. * decaffeinate suggestions:
  14. * DS101: Remove unnecessary use of Array.from
  15. * DS102: Remove unnecessary code created because of implicit returns
  16. * DS205: Consider reworking code to avoid use of IIFEs
  17. * DS207: Consider shorter variations of null checks
  18. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  19. */
  20. let RequestParser
  21. const settings = require('settings-sharelatex')
  22. module.exports = RequestParser = {
  23. VALID_COMPILERS: ['pdflatex', 'latex', 'xelatex', 'lualatex'],
  24. MAX_TIMEOUT: 600,
  25. parse(body, callback) {
  26. let resource
  27. if (callback == null) {
  28. callback = function (error, data) {}
  29. }
  30. const response = {}
  31. if (body.compile == null) {
  32. return callback('top level object should have a compile attribute')
  33. }
  34. const { compile } = body
  35. if (!compile.options) {
  36. compile.options = {}
  37. }
  38. try {
  39. response.compiler = this._parseAttribute(
  40. 'compiler',
  41. compile.options.compiler,
  42. {
  43. validValues: this.VALID_COMPILERS,
  44. default: 'pdflatex',
  45. type: 'string'
  46. }
  47. )
  48. response.timeout = this._parseAttribute(
  49. 'timeout',
  50. compile.options.timeout,
  51. {
  52. default: RequestParser.MAX_TIMEOUT,
  53. type: 'number'
  54. }
  55. )
  56. response.imageName = this._parseAttribute(
  57. 'imageName',
  58. compile.options.imageName,
  59. {
  60. type: 'string',
  61. validValues:
  62. settings.clsi &&
  63. settings.clsi.docker &&
  64. settings.clsi.docker.allowedImages
  65. }
  66. )
  67. response.draft = this._parseAttribute('draft', compile.options.draft, {
  68. default: false,
  69. type: 'boolean'
  70. })
  71. response.check = this._parseAttribute('check', compile.options.check, {
  72. type: 'string'
  73. })
  74. response.flags = this._parseAttribute('flags', compile.options.flags, {
  75. default: [],
  76. type: 'object'
  77. })
  78. if (settings.allowedCompileGroups) {
  79. response.compileGroup = this._parseAttribute(
  80. 'compileGroup',
  81. compile.options.compileGroup,
  82. {
  83. validValues: settings.allowedCompileGroups,
  84. default: '',
  85. type: 'string'
  86. }
  87. )
  88. }
  89. // The syncType specifies whether the request contains all
  90. // resources (full) or only those resources to be updated
  91. // in-place (incremental).
  92. response.syncType = this._parseAttribute(
  93. 'syncType',
  94. compile.options.syncType,
  95. {
  96. validValues: ['full', 'incremental'],
  97. type: 'string'
  98. }
  99. )
  100. // The syncState is an identifier passed in with the request
  101. // which has the property that it changes when any resource is
  102. // added, deleted, moved or renamed.
  103. //
  104. // on syncType full the syncState identifier is passed in and
  105. // stored
  106. //
  107. // on syncType incremental the syncState identifier must match
  108. // the stored value
  109. response.syncState = this._parseAttribute(
  110. 'syncState',
  111. compile.options.syncState,
  112. { type: 'string' }
  113. )
  114. if (response.timeout > RequestParser.MAX_TIMEOUT) {
  115. response.timeout = RequestParser.MAX_TIMEOUT
  116. }
  117. response.timeout = response.timeout * 1000 // milliseconds
  118. response.resources = (() => {
  119. const result = []
  120. for (resource of Array.from(compile.resources || [])) {
  121. result.push(this._parseResource(resource))
  122. }
  123. return result
  124. })()
  125. const rootResourcePath = this._parseAttribute(
  126. 'rootResourcePath',
  127. compile.rootResourcePath,
  128. {
  129. default: 'main.tex',
  130. type: 'string'
  131. }
  132. )
  133. const originalRootResourcePath = rootResourcePath
  134. const sanitizedRootResourcePath = RequestParser._sanitizePath(
  135. rootResourcePath
  136. )
  137. response.rootResourcePath = RequestParser._checkPath(
  138. sanitizedRootResourcePath
  139. )
  140. for (resource of Array.from(response.resources)) {
  141. if (resource.path === originalRootResourcePath) {
  142. resource.path = sanitizedRootResourcePath
  143. }
  144. }
  145. } catch (error1) {
  146. const error = error1
  147. return callback(error)
  148. }
  149. return callback(null, response)
  150. },
  151. _parseResource(resource) {
  152. let modified
  153. if (resource.path == null || typeof resource.path !== 'string') {
  154. throw 'all resources should have a path attribute'
  155. }
  156. if (resource.modified != null) {
  157. modified = new Date(resource.modified)
  158. if (isNaN(modified.getTime())) {
  159. throw `resource modified date could not be understood: ${resource.modified}`
  160. }
  161. }
  162. if (resource.url == null && resource.content == null) {
  163. throw 'all resources should have either a url or content attribute'
  164. }
  165. if (resource.content != null && typeof resource.content !== 'string') {
  166. throw 'content attribute should be a string'
  167. }
  168. if (resource.url != null && typeof resource.url !== 'string') {
  169. throw 'url attribute should be a string'
  170. }
  171. return {
  172. path: resource.path,
  173. modified,
  174. url: resource.url,
  175. content: resource.content
  176. }
  177. },
  178. _parseAttribute(name, attribute, options) {
  179. if (attribute != null) {
  180. if (options.validValues != null) {
  181. if (options.validValues.indexOf(attribute) === -1) {
  182. throw `${name} attribute should be one of: ${options.validValues.join(
  183. ', '
  184. )}`
  185. }
  186. }
  187. if (options.type != null) {
  188. if (typeof attribute !== options.type) {
  189. throw `${name} attribute should be a ${options.type}`
  190. }
  191. }
  192. } else {
  193. if (options.default != null) {
  194. return options.default
  195. }
  196. }
  197. return attribute
  198. },
  199. _sanitizePath(path) {
  200. // See http://php.net/manual/en/function.escapeshellcmd.php
  201. return path.replace(
  202. /[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g,
  203. ''
  204. )
  205. },
  206. _checkPath(path) {
  207. // check that the request does not use a relative path
  208. for (const dir of Array.from(path.split('/'))) {
  209. if (dir === '..') {
  210. throw 'relative path in root resource'
  211. }
  212. }
  213. return path
  214. }
  215. }