RequestParser.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. { type: 'string' }
  60. )
  61. response.draft = this._parseAttribute('draft', compile.options.draft, {
  62. default: false,
  63. type: 'boolean'
  64. })
  65. response.check = this._parseAttribute('check', compile.options.check, {
  66. type: 'string'
  67. })
  68. response.flags = this._parseAttribute('flags', compile.options.flags, {
  69. default: [],
  70. type: 'object'
  71. })
  72. if (settings.allowedCompileGroups) {
  73. response.compileGroup = this._parseAttribute(
  74. 'compileGroup',
  75. compile.options.compileGroup,
  76. {
  77. validValues: settings.allowedCompileGroups,
  78. default: '',
  79. type: 'string'
  80. }
  81. )
  82. }
  83. // The syncType specifies whether the request contains all
  84. // resources (full) or only those resources to be updated
  85. // in-place (incremental).
  86. response.syncType = this._parseAttribute(
  87. 'syncType',
  88. compile.options.syncType,
  89. {
  90. validValues: ['full', 'incremental'],
  91. type: 'string'
  92. }
  93. )
  94. // The syncState is an identifier passed in with the request
  95. // which has the property that it changes when any resource is
  96. // added, deleted, moved or renamed.
  97. //
  98. // on syncType full the syncState identifier is passed in and
  99. // stored
  100. //
  101. // on syncType incremental the syncState identifier must match
  102. // the stored value
  103. response.syncState = this._parseAttribute(
  104. 'syncState',
  105. compile.options.syncState,
  106. { type: 'string' }
  107. )
  108. if (response.timeout > RequestParser.MAX_TIMEOUT) {
  109. response.timeout = RequestParser.MAX_TIMEOUT
  110. }
  111. response.timeout = response.timeout * 1000 // milliseconds
  112. response.resources = (() => {
  113. const result = []
  114. for (resource of Array.from(compile.resources || [])) {
  115. result.push(this._parseResource(resource))
  116. }
  117. return result
  118. })()
  119. const rootResourcePath = this._parseAttribute(
  120. 'rootResourcePath',
  121. compile.rootResourcePath,
  122. {
  123. default: 'main.tex',
  124. type: 'string'
  125. }
  126. )
  127. const originalRootResourcePath = rootResourcePath
  128. const sanitizedRootResourcePath = RequestParser._sanitizePath(
  129. rootResourcePath
  130. )
  131. response.rootResourcePath = RequestParser._checkPath(
  132. sanitizedRootResourcePath
  133. )
  134. for (resource of Array.from(response.resources)) {
  135. if (resource.path === originalRootResourcePath) {
  136. resource.path = sanitizedRootResourcePath
  137. }
  138. }
  139. } catch (error1) {
  140. const error = error1
  141. return callback(error)
  142. }
  143. return callback(null, response)
  144. },
  145. _parseResource(resource) {
  146. let modified
  147. if (resource.path == null || typeof resource.path !== 'string') {
  148. throw 'all resources should have a path attribute'
  149. }
  150. if (resource.modified != null) {
  151. modified = new Date(resource.modified)
  152. if (isNaN(modified.getTime())) {
  153. throw `resource modified date could not be understood: ${resource.modified}`
  154. }
  155. }
  156. if (resource.url == null && resource.content == null) {
  157. throw 'all resources should have either a url or content attribute'
  158. }
  159. if (resource.content != null && typeof resource.content !== 'string') {
  160. throw 'content attribute should be a string'
  161. }
  162. if (resource.url != null && typeof resource.url !== 'string') {
  163. throw 'url attribute should be a string'
  164. }
  165. return {
  166. path: resource.path,
  167. modified,
  168. url: resource.url,
  169. content: resource.content
  170. }
  171. },
  172. _parseAttribute(name, attribute, options) {
  173. if (attribute != null) {
  174. if (options.validValues != null) {
  175. if (options.validValues.indexOf(attribute) === -1) {
  176. throw `${name} attribute should be one of: ${options.validValues.join(
  177. ', '
  178. )}`
  179. }
  180. }
  181. if (options.type != null) {
  182. if (typeof attribute !== options.type) {
  183. throw `${name} attribute should be a ${options.type}`
  184. }
  185. }
  186. } else {
  187. if (options.default != null) {
  188. return options.default
  189. }
  190. }
  191. return attribute
  192. },
  193. _sanitizePath(path) {
  194. // See http://php.net/manual/en/function.escapeshellcmd.php
  195. return path.replace(
  196. /[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g,
  197. ''
  198. )
  199. },
  200. _checkPath(path) {
  201. // check that the request does not use a relative path
  202. for (const dir of Array.from(path.split('/'))) {
  203. if (dir === '..') {
  204. throw 'relative path in root resource'
  205. }
  206. }
  207. return path
  208. }
  209. }