RequestParserTests.js 14 KB

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