RequestParser.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. settings = require("settings-sharelatex")
  2. module.exports = RequestParser =
  3. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  4. MAX_TIMEOUT: 300
  5. parse: (body, callback = (error, data) ->) ->
  6. response = {}
  7. if !body.compile?
  8. return callback "top level object should have a compile attribute"
  9. compile = body.compile
  10. compile.options ||= {}
  11. try
  12. response.compiler = @_parseAttribute "compiler",
  13. compile.options.compiler,
  14. validValues: @VALID_COMPILERS
  15. default: "pdflatex"
  16. type: "string"
  17. response.timeout = @_parseAttribute "timeout",
  18. compile.options.timeout
  19. default: RequestParser.MAX_TIMEOUT
  20. type: "number"
  21. response.imageName = @_parseAttribute "imageName",
  22. compile.options.imageName,
  23. type: "string"
  24. response.draft = @_parseAttribute "draft",
  25. compile.options.draft,
  26. default: false,
  27. type: "boolean"
  28. response.check = @_parseAttribute "check",
  29. compile.options.check,
  30. type: "string"
  31. # The syncType specifies whether the request contains all
  32. # resources (full) or only those resources to be updated
  33. # in-place (incremental).
  34. response.syncType = @_parseAttribute "syncType",
  35. compile.options.syncType,
  36. validValues: ["full", "incremental"]
  37. type: "string"
  38. # The syncState is an identifier passed in with the request
  39. # which has the property that it changes when any resource is
  40. # added, deleted, moved or renamed.
  41. #
  42. # on syncType full the syncState identifier is passed in and
  43. # stored
  44. #
  45. # on syncType incremental the syncState identifier must match
  46. # the stored value
  47. response.syncState = @_parseAttribute "syncState",
  48. compile.options.syncState,
  49. type: "string"
  50. if response.timeout > RequestParser.MAX_TIMEOUT
  51. response.timeout = RequestParser.MAX_TIMEOUT
  52. response.timeout = response.timeout * 1000 # milliseconds
  53. response.resources = (@_parseResource(resource) for resource in (compile.resources or []))
  54. rootResourcePath = @_parseAttribute "rootResourcePath",
  55. compile.rootResourcePath
  56. default: "main.tex"
  57. type: "string"
  58. originalRootResourcePath = rootResourcePath
  59. sanitizedRootResourcePath = RequestParser._sanitizePath(rootResourcePath)
  60. response.rootResourcePath = RequestParser._checkPath(sanitizedRootResourcePath)
  61. for resource in response.resources
  62. if resource.path == originalRootResourcePath
  63. resource.path = sanitizedRootResourcePath
  64. catch error
  65. return callback error
  66. callback null, response
  67. _parseResource: (resource) ->
  68. if !resource.path? or typeof resource.path != "string"
  69. throw "all resources should have a path attribute"
  70. if resource.modified?
  71. modified = new Date(resource.modified)
  72. if isNaN(modified.getTime())
  73. throw "resource modified date could not be understood: #{resource.modified}"
  74. if !resource.url? and !resource.content?
  75. throw "all resources should have either a url or content attribute"
  76. if resource.content? and typeof resource.content != "string"
  77. throw "content attribute should be a string"
  78. if resource.url? and typeof resource.url != "string"
  79. throw "url attribute should be a string"
  80. return {
  81. path: resource.path
  82. modified: modified
  83. url: resource.url
  84. content: resource.content
  85. }
  86. _parseAttribute: (name, attribute, options) ->
  87. if attribute?
  88. if options.validValues?
  89. if options.validValues.indexOf(attribute) == -1
  90. throw "#{name} attribute should be one of: #{options.validValues.join(", ")}"
  91. if options.type?
  92. if typeof attribute != options.type
  93. throw "#{name} attribute should be a #{options.type}"
  94. else
  95. return options.default if options.default?
  96. return attribute
  97. _sanitizePath: (path) ->
  98. # See http://php.net/manual/en/function.escapeshellcmd.php
  99. path.replace(/[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g, "")
  100. _checkPath: (path) ->
  101. # check that the request does not use a relative path
  102. for dir in path.split('/')
  103. if dir == '..'
  104. throw "relative path in root resource"
  105. return path