RequestParser.js 6.0 KB

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