RequestParserTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../app/js/RequestParser'
  7. )
  8. const tk = require('timekeeper')
  9. describe('RequestParser', function () {
  10. beforeEach(function () {
  11. tk.freeze()
  12. this.callback = sinon.stub()
  13. this.validResource = {
  14. path: 'main.tex',
  15. date: '12:00 01/02/03',
  16. content: 'Hello world',
  17. }
  18. this.validRequest = {
  19. compile: {
  20. token: 'token-123',
  21. options: {
  22. imageName: 'basicImageName/here:2017-1',
  23. compiler: 'pdflatex',
  24. timeout: 42,
  25. },
  26. resources: [],
  27. },
  28. }
  29. this.RequestParser = SandboxedModule.require(modulePath, {
  30. requires: {
  31. '@overleaf/settings': (this.settings = {}),
  32. },
  33. })
  34. })
  35. afterEach(function () {
  36. tk.reset()
  37. })
  38. describe('without a top level object', function () {
  39. beforeEach(function () {
  40. this.RequestParser.parse([], this.callback)
  41. })
  42. it('should return an error', function () {
  43. expect(this.callback).to.have.been.called
  44. expect(this.callback.args[0][0].message).to.equal(
  45. 'top level object should have a compile attribute'
  46. )
  47. })
  48. })
  49. describe('without a compile attribute', function () {
  50. beforeEach(function () {
  51. this.RequestParser.parse({}, this.callback)
  52. })
  53. it('should return an error', function () {
  54. expect(this.callback).to.have.been.called
  55. expect(this.callback.args[0][0].message).to.equal(
  56. 'top level object should have a compile attribute'
  57. )
  58. })
  59. })
  60. describe('without a valid compiler', function () {
  61. beforeEach(function () {
  62. this.validRequest.compile.options.compiler = 'not-a-compiler'
  63. this.RequestParser.parse(this.validRequest, this.callback)
  64. })
  65. it('should return an error', function () {
  66. this.callback
  67. .calledWithMatch({
  68. message:
  69. 'compiler attribute should be one of: pdflatex, latex, xelatex, lualatex',
  70. })
  71. .should.equal(true)
  72. })
  73. })
  74. describe('without a compiler specified', function () {
  75. beforeEach(function (done) {
  76. delete this.validRequest.compile.options.compiler
  77. this.RequestParser.parse(this.validRequest, (error, data) => {
  78. if (error) return done(error)
  79. this.data = data
  80. done()
  81. })
  82. })
  83. it('should set the compiler to pdflatex by default', function () {
  84. this.data.compiler.should.equal('pdflatex')
  85. })
  86. })
  87. describe('with imageName set', function () {
  88. beforeEach(function (done) {
  89. this.RequestParser.parse(this.validRequest, (error, data) => {
  90. if (error) return done(error)
  91. this.data = data
  92. done()
  93. })
  94. })
  95. it('should set the imageName', function () {
  96. this.data.imageName.should.equal('basicImageName/here:2017-1')
  97. })
  98. })
  99. describe('when image restrictions are present', function () {
  100. beforeEach(function () {
  101. this.settings.clsi = { docker: {} }
  102. this.settings.clsi.docker.allowedImages = [
  103. 'repo/name:tag1',
  104. 'repo/name:tag2',
  105. ]
  106. })
  107. describe('with imageName set to something invalid', function () {
  108. beforeEach(function () {
  109. const request = this.validRequest
  110. request.compile.options.imageName = 'something/different:latest'
  111. this.RequestParser.parse(request, (error, data) => {
  112. this.error = error
  113. this.data = data
  114. })
  115. })
  116. it('should throw an error for imageName', function () {
  117. expect(String(this.error)).to.include(
  118. 'imageName attribute should be one of'
  119. )
  120. })
  121. })
  122. describe('with imageName set to something valid', function () {
  123. beforeEach(function () {
  124. const request = this.validRequest
  125. request.compile.options.imageName = 'repo/name:tag1'
  126. this.RequestParser.parse(request, (error, data) => {
  127. this.error = error
  128. this.data = data
  129. })
  130. })
  131. it('should set the imageName', function () {
  132. this.data.imageName.should.equal('repo/name:tag1')
  133. })
  134. })
  135. })
  136. describe('with flags set', function () {
  137. beforeEach(function (done) {
  138. this.validRequest.compile.options.flags = ['-file-line-error']
  139. this.RequestParser.parse(this.validRequest, (error, data) => {
  140. if (error) return done(error)
  141. this.data = data
  142. done()
  143. })
  144. })
  145. it('should set the flags attribute', function () {
  146. expect(this.data.flags).to.deep.equal(['-file-line-error'])
  147. })
  148. })
  149. describe('with flags not specified', function () {
  150. beforeEach(function (done) {
  151. this.RequestParser.parse(this.validRequest, (error, data) => {
  152. if (error) return done(error)
  153. this.data = data
  154. done()
  155. })
  156. })
  157. it('it should have an empty flags list', function () {
  158. expect(this.data.flags).to.deep.equal([])
  159. })
  160. })
  161. describe('without a timeout specified', function () {
  162. beforeEach(function (done) {
  163. delete this.validRequest.compile.options.timeout
  164. this.RequestParser.parse(this.validRequest, (error, data) => {
  165. if (error) return done(error)
  166. this.data = data
  167. done()
  168. })
  169. })
  170. it('should set the timeout to MAX_TIMEOUT', function () {
  171. this.data.timeout.should.equal(this.RequestParser.MAX_TIMEOUT * 1000)
  172. })
  173. })
  174. describe('with a timeout larger than the maximum', function () {
  175. beforeEach(function (done) {
  176. this.validRequest.compile.options.timeout =
  177. this.RequestParser.MAX_TIMEOUT + 1
  178. this.RequestParser.parse(this.validRequest, (error, data) => {
  179. if (error) return done(error)
  180. this.data = data
  181. done()
  182. })
  183. })
  184. it('should set the timeout to MAX_TIMEOUT', function () {
  185. this.data.timeout.should.equal(this.RequestParser.MAX_TIMEOUT * 1000)
  186. })
  187. })
  188. describe('with a timeout', function () {
  189. beforeEach(function (done) {
  190. this.RequestParser.parse(this.validRequest, (error, data) => {
  191. if (error) return done(error)
  192. this.data = data
  193. done()
  194. })
  195. })
  196. it('should set the timeout (in milliseconds)', function () {
  197. this.data.timeout.should.equal(
  198. this.validRequest.compile.options.timeout * 1000
  199. )
  200. })
  201. })
  202. describe('with a resource without a path', function () {
  203. beforeEach(function () {
  204. delete this.validResource.path
  205. this.validRequest.compile.resources.push(this.validResource)
  206. this.RequestParser.parse(this.validRequest, this.callback)
  207. })
  208. it('should return an error', function () {
  209. this.callback
  210. .calledWithMatch({
  211. message: 'all resources should have a path attribute',
  212. })
  213. .should.equal(true)
  214. })
  215. })
  216. describe('with a resource with a path', function () {
  217. beforeEach(function () {
  218. this.validResource.path = this.path = 'test.tex'
  219. this.validRequest.compile.resources.push(this.validResource)
  220. this.RequestParser.parse(this.validRequest, this.callback)
  221. this.data = this.callback.args[0][1]
  222. })
  223. it('should return the path in the parsed response', function () {
  224. this.data.resources[0].path.should.equal(this.path)
  225. })
  226. })
  227. describe('with a resource with a malformed modified date', function () {
  228. beforeEach(function () {
  229. this.validResource.modified = 'not-a-date'
  230. this.validRequest.compile.resources.push(this.validResource)
  231. this.RequestParser.parse(this.validRequest, this.callback)
  232. })
  233. it('should return an error', function () {
  234. this.callback
  235. .calledWithMatch({
  236. message:
  237. 'resource modified date could not be understood: ' +
  238. this.validResource.modified,
  239. })
  240. .should.equal(true)
  241. })
  242. })
  243. describe('with a resource with a valid date', function () {
  244. beforeEach(function () {
  245. this.date = '12:00 01/02/03'
  246. this.validResource.modified = this.date
  247. this.validRequest.compile.resources.push(this.validResource)
  248. this.RequestParser.parse(this.validRequest, this.callback)
  249. this.data = this.callback.args[0][1]
  250. })
  251. it('should return the date as a Javascript Date object', function () {
  252. ;(this.data.resources[0].modified instanceof Date).should.equal(true)
  253. this.data.resources[0].modified
  254. .getTime()
  255. .should.equal(Date.parse(this.date))
  256. })
  257. })
  258. describe('with a resource without either a content or URL attribute', function () {
  259. beforeEach(function () {
  260. delete this.validResource.url
  261. delete this.validResource.content
  262. this.validRequest.compile.resources.push(this.validResource)
  263. this.RequestParser.parse(this.validRequest, this.callback)
  264. })
  265. it('should return an error', function () {
  266. this.callback
  267. .calledWithMatch({
  268. message:
  269. 'all resources should have either a url or content attribute',
  270. })
  271. .should.equal(true)
  272. })
  273. })
  274. describe('with a resource where the content is not a string', function () {
  275. beforeEach(function () {
  276. this.validResource.content = []
  277. this.validRequest.compile.resources.push(this.validResource)
  278. this.RequestParser.parse(this.validRequest, this.callback)
  279. })
  280. it('should return an error', function () {
  281. this.callback
  282. .calledWithMatch({ message: 'content attribute should be a string' })
  283. .should.equal(true)
  284. })
  285. })
  286. describe('with a resource where the url is not a string', function () {
  287. beforeEach(function () {
  288. this.validResource.url = []
  289. this.validRequest.compile.resources.push(this.validResource)
  290. this.RequestParser.parse(this.validRequest, this.callback)
  291. })
  292. it('should return an error', function () {
  293. this.callback
  294. .calledWithMatch({ message: 'url attribute should be a string' })
  295. .should.equal(true)
  296. })
  297. })
  298. describe('with a resource with a url', function () {
  299. beforeEach(function () {
  300. this.validResource.url = this.url = 'www.example.com'
  301. this.validRequest.compile.resources.push(this.validResource)
  302. this.RequestParser.parse(this.validRequest, this.callback)
  303. this.data = this.callback.args[0][1]
  304. })
  305. it('should return the url in the parsed response', function () {
  306. this.data.resources[0].url.should.equal(this.url)
  307. })
  308. })
  309. describe('with a resource with a content attribute', function () {
  310. beforeEach(function () {
  311. this.validResource.content = this.content = 'Hello world'
  312. this.validRequest.compile.resources.push(this.validResource)
  313. this.RequestParser.parse(this.validRequest, this.callback)
  314. this.data = this.callback.args[0][1]
  315. })
  316. it('should return the content in the parsed response', function () {
  317. this.data.resources[0].content.should.equal(this.content)
  318. })
  319. })
  320. describe('without a root resource path', function () {
  321. beforeEach(function () {
  322. delete this.validRequest.compile.rootResourcePath
  323. this.RequestParser.parse(this.validRequest, this.callback)
  324. this.data = this.callback.args[0][1]
  325. })
  326. it("should set the root resource path to 'main.tex' by default", function () {
  327. this.data.rootResourcePath.should.equal('main.tex')
  328. })
  329. })
  330. describe('with a root resource path', function () {
  331. beforeEach(function () {
  332. this.validRequest.compile.rootResourcePath = this.path = 'test.tex'
  333. this.RequestParser.parse(this.validRequest, this.callback)
  334. this.data = this.callback.args[0][1]
  335. })
  336. it('should return the root resource path in the parsed response', function () {
  337. this.data.rootResourcePath.should.equal(this.path)
  338. })
  339. })
  340. describe('with a root resource path that is not a string', function () {
  341. beforeEach(function () {
  342. this.validRequest.compile.rootResourcePath = []
  343. this.RequestParser.parse(this.validRequest, this.callback)
  344. })
  345. it('should return an error', function () {
  346. this.callback
  347. .calledWithMatch({
  348. message: 'rootResourcePath attribute should be a string',
  349. })
  350. .should.equal(true)
  351. })
  352. })
  353. describe('with a root resource path that needs escaping', function () {
  354. beforeEach(function () {
  355. this.badPath = '`rm -rf foo`.tex'
  356. this.goodPath = 'rm -rf foo.tex'
  357. this.validRequest.compile.rootResourcePath = this.badPath
  358. this.validRequest.compile.resources.push({
  359. path: this.badPath,
  360. date: '12:00 01/02/03',
  361. content: 'Hello world',
  362. })
  363. this.RequestParser.parse(this.validRequest, this.callback)
  364. this.data = this.callback.args[0][1]
  365. })
  366. it('should return the escaped resource', function () {
  367. this.data.rootResourcePath.should.equal(this.goodPath)
  368. })
  369. it('should also escape the resource path', function () {
  370. this.data.resources[0].path.should.equal(this.goodPath)
  371. })
  372. })
  373. describe('with a root resource path that has a relative path', function () {
  374. beforeEach(function () {
  375. this.validRequest.compile.rootResourcePath = 'foo/../../bar.tex'
  376. this.RequestParser.parse(this.validRequest, this.callback)
  377. this.data = this.callback.args[0][1]
  378. })
  379. it('should return an error', function () {
  380. this.callback
  381. .calledWithMatch({ message: 'relative path in root resource' })
  382. .should.equal(true)
  383. })
  384. })
  385. describe('with a root resource path that has unescaped + relative path', function () {
  386. beforeEach(function () {
  387. this.validRequest.compile.rootResourcePath = 'foo/#../bar.tex'
  388. this.RequestParser.parse(this.validRequest, this.callback)
  389. this.data = this.callback.args[0][1]
  390. })
  391. it('should return an error', function () {
  392. this.callback
  393. .calledWithMatch({ message: 'relative path in root resource' })
  394. .should.equal(true)
  395. })
  396. })
  397. describe('with an unknown syncType', function () {
  398. beforeEach(function () {
  399. this.validRequest.compile.options.syncType = 'unexpected'
  400. this.RequestParser.parse(this.validRequest, this.callback)
  401. this.data = this.callback.args[0][1]
  402. })
  403. it('should return an error', function () {
  404. this.callback
  405. .calledWithMatch({
  406. message: 'syncType attribute should be one of: full, incremental',
  407. })
  408. .should.equal(true)
  409. })
  410. })
  411. })