TokenAccessTests.mjs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. import { expect } from 'chai'
  2. import async from 'async'
  3. import User from './helpers/User.mjs'
  4. import request from './helpers/request.js'
  5. import settings from '@overleaf/settings'
  6. import { db } from '../../../app/src/infrastructure/mongodb.mjs'
  7. import expectErrorResponse from './helpers/expectErrorResponse.mjs'
  8. import logger from '@overleaf/logger'
  9. import sinon from 'sinon'
  10. const tryEditorAccess = (user, projectId, test, callback) =>
  11. async.series(
  12. [
  13. cb =>
  14. user.request.get(`/project/${projectId}`, (error, response, body) => {
  15. if (error != null) {
  16. return cb(error)
  17. }
  18. test(response, body)
  19. cb()
  20. }),
  21. cb =>
  22. user.request.get(
  23. `/project/${projectId}/download/zip`,
  24. (error, response, body) => {
  25. if (error != null) {
  26. return cb(error)
  27. }
  28. test(response, body)
  29. cb()
  30. }
  31. ),
  32. ],
  33. callback
  34. )
  35. const tryReadOnlyTokenAccess = (
  36. user,
  37. token,
  38. testPageLoad,
  39. testFormPost,
  40. callback
  41. ) => {
  42. _doTryTokenAccess(
  43. `/read/${token}`,
  44. user,
  45. token,
  46. testPageLoad,
  47. testFormPost,
  48. callback
  49. )
  50. }
  51. const tryReadAndWriteTokenAccess = (
  52. user,
  53. token,
  54. testPageLoad,
  55. testFormPost,
  56. callback
  57. ) => {
  58. _doTryTokenAccess(
  59. `/${token}`,
  60. user,
  61. token,
  62. testPageLoad,
  63. testFormPost,
  64. callback
  65. )
  66. }
  67. const _doTryTokenAccess = (
  68. url,
  69. user,
  70. token,
  71. testPageLoad,
  72. testFormPost,
  73. callback
  74. ) => {
  75. user.request.get(url, (err, response, body) => {
  76. if (err) {
  77. return callback(err)
  78. }
  79. testPageLoad(response, body)
  80. if (!testFormPost) {
  81. return callback()
  82. }
  83. user.request.post(
  84. `${url}/grant`,
  85. { json: { token } },
  86. (err, response, body) => {
  87. if (err) {
  88. return callback(err)
  89. }
  90. testFormPost(response, body)
  91. callback()
  92. }
  93. )
  94. })
  95. }
  96. const tryReadOnlyTokenAccept = (
  97. user,
  98. token,
  99. testPageLoad,
  100. testFormPost,
  101. callback
  102. ) => {
  103. _doTryTokenAccept(
  104. `/read/${token}`,
  105. user,
  106. token,
  107. testPageLoad,
  108. testFormPost,
  109. callback
  110. )
  111. }
  112. const _doTryTokenAccept = (
  113. url,
  114. user,
  115. token,
  116. testPageLoad,
  117. testFormPost,
  118. callback
  119. ) => {
  120. user.request.get(url, (err, response, body) => {
  121. if (err) {
  122. return callback(err)
  123. }
  124. testPageLoad(response, body)
  125. if (!testFormPost) {
  126. return callback()
  127. }
  128. user.request.post(
  129. `${url}/grant`,
  130. { json: { token, confirmedByUser: true } },
  131. (err, response, body) => {
  132. if (err) {
  133. return callback(err)
  134. }
  135. testFormPost(response, body)
  136. callback()
  137. }
  138. )
  139. })
  140. }
  141. const tryContentAccess = (user, projcetId, test, callback) => {
  142. // The real-time service calls this end point to determine the user's
  143. // permissions.
  144. let userId
  145. if (user.id != null) {
  146. userId = user.id
  147. } else {
  148. userId = 'anonymous-user'
  149. }
  150. request.post(
  151. {
  152. url: `/project/${projcetId}/join`,
  153. auth: {
  154. user: settings.apis.web.user,
  155. pass: settings.apis.web.pass,
  156. sendImmediately: true,
  157. },
  158. json: {
  159. userId,
  160. },
  161. jar: false,
  162. },
  163. (error, response, body) => {
  164. if (error != null) {
  165. return callback(error)
  166. }
  167. test(response, body)
  168. callback()
  169. }
  170. )
  171. }
  172. const tryAnonContentAccess = (user, projectId, token, test, callback) => {
  173. // The real-time service calls this end point to determine the user's
  174. // permissions.
  175. let userId
  176. if (user.id != null) {
  177. userId = user.id
  178. } else {
  179. userId = 'anonymous-user'
  180. }
  181. request.post(
  182. {
  183. url: `/project/${projectId}/join`,
  184. auth: {
  185. user: settings.apis.web.user,
  186. pass: settings.apis.web.pass,
  187. sendImmediately: true,
  188. },
  189. json: {
  190. userId,
  191. anonymousAccessToken: token,
  192. },
  193. jar: false,
  194. },
  195. (error, response, body) => {
  196. if (error != null) {
  197. return callback(error)
  198. }
  199. test(response, body)
  200. callback()
  201. }
  202. )
  203. }
  204. const tryFetchProjectTokens = (user, projectId, callback) => {
  205. user.request.get(
  206. { url: `/project/${projectId}/tokens`, json: true },
  207. (error, response, body) => {
  208. if (error) {
  209. return callback(error)
  210. }
  211. callback(null, response, body)
  212. }
  213. )
  214. }
  215. const trySharingUpdatesPage = (user, projectId, test, callback) =>
  216. user.request.get(
  217. `/project/${projectId}/sharing-updates`,
  218. (error, response, body) => {
  219. if (error != null) {
  220. return callback(error)
  221. }
  222. test(response, body, projectId)
  223. callback()
  224. }
  225. )
  226. const trySharingUpdatesJoin = (user, projectId, test, callback) =>
  227. user.request.post(
  228. `/project/${projectId}/sharing-updates/join`,
  229. (error, response, body) => {
  230. if (error != null) {
  231. return callback(error)
  232. }
  233. test(response, body, projectId)
  234. callback()
  235. }
  236. )
  237. const trySharingUpdatesView = (user, projectId, test, callback) =>
  238. user.request.post(
  239. `/project/${projectId}/sharing-updates/view`,
  240. (error, response, body) => {
  241. if (error != null) {
  242. return callback(error)
  243. }
  244. test(response, body, projectId)
  245. callback()
  246. }
  247. )
  248. const expectRedirectToProject = (response, body, projectId) => {
  249. expect(response.statusCode).to.equal(302)
  250. expect(response.headers.location).to.equal(`/project/${projectId}`)
  251. }
  252. describe('TokenAccess', function () {
  253. beforeEach(function (done) {
  254. this.timeout(90000)
  255. this.owner = new User()
  256. this.other1 = new User()
  257. this.other2 = new User()
  258. this.anon = new User()
  259. this.siteAdmin = new User({ email: 'admin@example.com' })
  260. async.parallel(
  261. [
  262. cb =>
  263. this.siteAdmin.login(err => {
  264. if (err) {
  265. return cb(err)
  266. }
  267. this.siteAdmin.ensureAdmin(cb)
  268. }),
  269. cb => this.owner.login(cb),
  270. cb => this.other1.login(cb),
  271. cb => this.other2.login(cb),
  272. cb => this.anon.getCsrfToken(cb),
  273. ],
  274. done
  275. )
  276. })
  277. describe('no token-access', function () {
  278. beforeEach(function (done) {
  279. this.owner.createProject(
  280. `token-ro-test${Math.random()}`,
  281. (err, projectId) => {
  282. if (err != null) {
  283. return done(err)
  284. }
  285. this.projectId = projectId
  286. // Note, never made token-based,
  287. // thus no tokens
  288. done()
  289. }
  290. )
  291. })
  292. it('should deny access ', function (done) {
  293. async.series(
  294. [
  295. cb => {
  296. tryEditorAccess(
  297. this.other1,
  298. this.projectId,
  299. expectErrorResponse.restricted.html,
  300. cb
  301. )
  302. },
  303. cb => {
  304. tryContentAccess(
  305. this.other1,
  306. this.projectId,
  307. (response, body) => {
  308. expect(response.statusCode).to.equal(403)
  309. expect(body).to.equal('Forbidden')
  310. },
  311. cb
  312. )
  313. },
  314. ],
  315. done
  316. )
  317. })
  318. it('should deny access to access tokens', function (done) {
  319. tryFetchProjectTokens(this.other1, this.projectId, (error, response) => {
  320. expect(error).to.equal(null)
  321. expect(response.statusCode).to.equal(403)
  322. done()
  323. })
  324. })
  325. })
  326. describe('owner', function () {
  327. beforeEach(function (done) {
  328. this.projectName = `token-owner-test${Math.random()}`
  329. this.owner.createProject(this.projectName, (err, projectId) => {
  330. if (err != null) {
  331. return done(err)
  332. }
  333. this.projectId = projectId
  334. this.owner.makeTokenBased(this.projectId, err => {
  335. if (err != null) {
  336. return done(err)
  337. }
  338. this.owner.getProject(this.projectId, (err, project) => {
  339. if (err != null) {
  340. return done(err)
  341. }
  342. this.tokens = project.tokens
  343. done()
  344. })
  345. })
  346. })
  347. })
  348. it('should be able to fetch read-only and read-write tokens', function (done) {
  349. tryFetchProjectTokens(
  350. this.owner,
  351. this.projectId,
  352. (error, response, body) => {
  353. expect(error).to.equal(null)
  354. expect(response.statusCode).to.equal(200)
  355. expect(body).to.include({
  356. readOnly: this.tokens.readOnly,
  357. readAndWrite: this.tokens.readAndWrite,
  358. readAndWritePrefix: this.tokens.readAndWritePrefix,
  359. })
  360. expect(body.readOnlyHashPrefix).to.exist
  361. expect(body.readAndWriteHashPrefix).to.exist
  362. expect(Object.keys(body).length).to.equal(5)
  363. done()
  364. }
  365. )
  366. })
  367. })
  368. describe('read-only token', function () {
  369. beforeEach(function (done) {
  370. this.projectName = `token-ro-test${Math.random()}`
  371. this.owner.createProject(this.projectName, (err, projectId) => {
  372. if (err != null) {
  373. return done(err)
  374. }
  375. this.projectId = projectId
  376. this.owner.makeTokenBased(this.projectId, err => {
  377. if (err != null) {
  378. return done(err)
  379. }
  380. this.owner.getProject(this.projectId, (err, project) => {
  381. if (err != null) {
  382. return done(err)
  383. }
  384. this.tokens = project.tokens
  385. done()
  386. })
  387. })
  388. })
  389. })
  390. it('allow the user read-only access to the project', function (done) {
  391. async.series(
  392. [
  393. cb => {
  394. // deny access before token is used
  395. tryEditorAccess(
  396. this.other1,
  397. this.projectId,
  398. expectErrorResponse.restricted.html,
  399. cb
  400. )
  401. },
  402. cb => {
  403. // try token
  404. tryReadOnlyTokenAccess(
  405. this.other1,
  406. this.tokens.readOnly,
  407. (response, body) => {
  408. expect(response.statusCode).to.equal(200)
  409. },
  410. (response, body) => {
  411. expect(response.statusCode).to.equal(200)
  412. expect(body.requireAccept.projectName).to.equal(
  413. this.projectName
  414. )
  415. },
  416. cb
  417. )
  418. },
  419. cb => {
  420. // deny access before token is accepted
  421. tryEditorAccess(
  422. this.other1,
  423. this.projectId,
  424. expectErrorResponse.restricted.html,
  425. cb
  426. )
  427. },
  428. cb => {
  429. // accept token
  430. tryReadOnlyTokenAccept(
  431. this.other1,
  432. this.tokens.readOnly,
  433. (response, body) => {
  434. expect(response.statusCode).to.equal(200)
  435. },
  436. (response, body) => {
  437. expect(response.statusCode).to.equal(200)
  438. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  439. expect(body.tokenAccessGranted).to.equal('readOnly')
  440. },
  441. cb
  442. )
  443. },
  444. cb => {
  445. // allow content access read-only
  446. tryContentAccess(
  447. this.other1,
  448. this.projectId,
  449. (response, body) => {
  450. expect(body.privilegeLevel).to.equal('readOnly')
  451. expect(body.isRestrictedUser).to.equal(true)
  452. expect(body.isTokenMember).to.equal(true)
  453. expect(body.isInvitedMember).to.equal(false)
  454. expect(body.project.owner).to.have.keys('_id')
  455. expect(body.project.owner).to.not.have.any.keys(
  456. 'email',
  457. 'first_name',
  458. 'last_name'
  459. )
  460. },
  461. cb
  462. )
  463. },
  464. cb => {
  465. tryEditorAccess(
  466. this.other1,
  467. this.projectId,
  468. (response, body) => {
  469. expect(response.statusCode).to.equal(200)
  470. },
  471. cb
  472. )
  473. },
  474. ],
  475. done
  476. )
  477. })
  478. it('should allow the user to fetch the read-only token', function (done) {
  479. async.series(
  480. [
  481. cb => {
  482. // accept token
  483. tryReadOnlyTokenAccept(
  484. this.other1,
  485. this.tokens.readOnly,
  486. (response, body) => {
  487. expect(response.statusCode).to.equal(200)
  488. },
  489. (response, body) => {
  490. expect(response.statusCode).to.equal(200)
  491. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  492. expect(body.tokenAccessGranted).to.equal('readOnly')
  493. },
  494. cb
  495. )
  496. },
  497. cb => {
  498. tryFetchProjectTokens(
  499. this.other1,
  500. this.projectId,
  501. (error, response, body) => {
  502. expect(error).to.equal(null)
  503. expect(response.statusCode).to.equal(200)
  504. expect(body).to.include({ readOnly: this.tokens.readOnly })
  505. expect(body.readOnlyHashPrefix).to.exist
  506. cb()
  507. }
  508. )
  509. },
  510. ],
  511. done
  512. )
  513. })
  514. it('should redirect the admin to the project (with rw access)', function (done) {
  515. async.series(
  516. [
  517. cb => {
  518. // use token
  519. tryReadOnlyTokenAccess(
  520. this.siteAdmin,
  521. this.tokens.readOnly,
  522. (response, body) => {
  523. expect(response.statusCode).to.equal(200)
  524. },
  525. (response, body) => {
  526. expect(response.statusCode).to.equal(200)
  527. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  528. },
  529. cb
  530. )
  531. },
  532. cb => {
  533. // allow content access read-and-write
  534. tryContentAccess(
  535. this.siteAdmin,
  536. this.projectId,
  537. (response, body) => {
  538. expect(body.privilegeLevel).to.equal('owner')
  539. expect(body.isRestrictedUser).to.equal(false)
  540. expect(body.isTokenMember).to.equal(false)
  541. expect(body.isInvitedMember).to.equal(false)
  542. },
  543. cb
  544. )
  545. },
  546. ],
  547. done
  548. )
  549. })
  550. describe('made private again', function () {
  551. beforeEach(function (done) {
  552. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  553. })
  554. it('should not allow the user to access the project', function (done) {
  555. async.series(
  556. [
  557. // no access before token is used
  558. cb =>
  559. tryEditorAccess(
  560. this.other1,
  561. this.projectId,
  562. expectErrorResponse.restricted.html,
  563. cb
  564. ),
  565. // token goes nowhere
  566. cb =>
  567. tryReadOnlyTokenAccess(
  568. this.other1,
  569. this.tokens.readOnly,
  570. (response, body) => {
  571. expect(response.statusCode).to.equal(200)
  572. },
  573. (response, body) => {
  574. expect(response.statusCode).to.equal(404)
  575. },
  576. cb
  577. ),
  578. // still no access
  579. cb =>
  580. tryEditorAccess(
  581. this.other1,
  582. this.projectId,
  583. expectErrorResponse.restricted.html,
  584. cb
  585. ),
  586. cb =>
  587. tryContentAccess(
  588. this.other1,
  589. this.projectId,
  590. (response, body) => {
  591. expect(response.statusCode).to.equal(403)
  592. expect(body).to.equal('Forbidden')
  593. },
  594. cb
  595. ),
  596. ],
  597. done
  598. )
  599. })
  600. })
  601. it('should deny access to access tokens', function (done) {
  602. tryFetchProjectTokens(this.other1, this.projectId, (error, response) => {
  603. expect(error).to.equal(null)
  604. expect(response.statusCode).to.equal(403)
  605. done()
  606. })
  607. })
  608. })
  609. describe('anonymous read-only token', function () {
  610. beforeEach(function (done) {
  611. this.owner.createProject(
  612. `token-anon-ro-test${Math.random()}`,
  613. (err, projectId) => {
  614. if (err != null) {
  615. return done(err)
  616. }
  617. this.projectId = projectId
  618. this.owner.makeTokenBased(this.projectId, err => {
  619. if (err != null) {
  620. return done(err)
  621. }
  622. this.owner.getProject(this.projectId, (err, project) => {
  623. if (err != null) {
  624. return done(err)
  625. }
  626. this.tokens = project.tokens
  627. done()
  628. })
  629. })
  630. }
  631. )
  632. })
  633. it('should allow the user to access project via read-only token url', function (done) {
  634. async.series(
  635. [
  636. cb =>
  637. tryEditorAccess(
  638. this.anon,
  639. this.projectId,
  640. expectErrorResponse.restricted.html,
  641. cb
  642. ),
  643. cb =>
  644. tryReadOnlyTokenAccess(
  645. this.anon,
  646. this.tokens.readOnly,
  647. (response, body) => {
  648. expect(response.statusCode).to.equal(200)
  649. },
  650. (response, body) => {
  651. expect(response.statusCode).to.equal(200)
  652. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  653. expect(body.grantAnonymousAccess).to.equal('readOnly')
  654. },
  655. cb
  656. ),
  657. cb =>
  658. tryEditorAccess(
  659. this.anon,
  660. this.projectId,
  661. (response, body) => {
  662. expect(response.statusCode).to.equal(200)
  663. },
  664. cb
  665. ),
  666. cb =>
  667. tryAnonContentAccess(
  668. this.anon,
  669. this.projectId,
  670. this.tokens.readOnly,
  671. (response, body) => {
  672. expect(body.privilegeLevel).to.equal('readOnly')
  673. expect(body.isRestrictedUser).to.equal(true)
  674. expect(body.isTokenMember).to.equal(false)
  675. expect(body.isInvitedMember).to.equal(false)
  676. expect(body.project.owner).to.have.keys('_id')
  677. expect(body.project.owner).to.not.have.any.keys(
  678. 'email',
  679. 'first_name',
  680. 'last_name'
  681. )
  682. },
  683. cb
  684. ),
  685. ],
  686. done
  687. )
  688. })
  689. it('should allow the anonymous user to fetch the read-only token', function (done) {
  690. async.series(
  691. [
  692. cb =>
  693. tryReadOnlyTokenAccess(
  694. this.anon,
  695. this.tokens.readOnly,
  696. (response, body) => {
  697. expect(response.statusCode).to.equal(200)
  698. },
  699. (response, body) => {
  700. expect(response.statusCode).to.equal(200)
  701. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  702. expect(body.grantAnonymousAccess).to.equal('readOnly')
  703. },
  704. cb
  705. ),
  706. cb => {
  707. tryFetchProjectTokens(
  708. this.anon,
  709. this.projectId,
  710. (error, response, body) => {
  711. expect(error).to.equal(null)
  712. expect(response.statusCode).to.equal(200)
  713. expect(body).to.include({ readOnly: this.tokens.readOnly })
  714. expect(body.readOnlyHashPrefix).to.exist
  715. expect(Object.keys(body).length).to.equal(2)
  716. cb()
  717. }
  718. )
  719. },
  720. ],
  721. done
  722. )
  723. })
  724. describe('made private again', function () {
  725. beforeEach(function (done) {
  726. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  727. })
  728. it('should deny access to project', function (done) {
  729. async.series(
  730. [
  731. cb =>
  732. tryEditorAccess(
  733. this.anon,
  734. this.projectId,
  735. expectErrorResponse.restricted.html,
  736. cb
  737. ),
  738. // should not allow the user to access read-only token
  739. cb =>
  740. tryReadOnlyTokenAccess(
  741. this.anon,
  742. this.tokens.readOnly,
  743. (response, body) => {
  744. expect(response.statusCode).to.equal(200)
  745. },
  746. (response, body) => {
  747. expect(response.statusCode).to.equal(404)
  748. },
  749. cb
  750. ),
  751. // still no access
  752. cb =>
  753. tryEditorAccess(
  754. this.anon,
  755. this.projectId,
  756. expectErrorResponse.restricted.html,
  757. cb
  758. ),
  759. // should not allow the user to join the project
  760. cb =>
  761. tryAnonContentAccess(
  762. this.anon,
  763. this.projectId,
  764. this.tokens.readOnly,
  765. (response, body) => {
  766. expect(response.statusCode).to.equal(403)
  767. expect(body).to.equal('Forbidden')
  768. },
  769. cb
  770. ),
  771. ],
  772. done
  773. )
  774. })
  775. it('should deny access to access tokens', function (done) {
  776. tryFetchProjectTokens(this.anon, this.projectId, (error, response) => {
  777. expect(error).to.equal(null)
  778. expect(response.statusCode).to.equal(403)
  779. done()
  780. })
  781. })
  782. })
  783. describe('link sharing disabled', function () {
  784. const previous = settings.disableLinkSharing
  785. let loggerStub
  786. beforeEach(function () {
  787. settings.disableLinkSharing = true
  788. loggerStub = sinon.spy(logger, 'error')
  789. })
  790. afterEach(function () {
  791. settings.disableLinkSharing = previous
  792. loggerStub.restore()
  793. })
  794. it('should deny access to project', function (done) {
  795. async.series(
  796. [
  797. cb =>
  798. tryEditorAccess(
  799. this.anon,
  800. this.projectId,
  801. expectErrorResponse.restricted.html,
  802. cb
  803. ),
  804. // should not allow the user to access read-only token
  805. cb =>
  806. tryReadOnlyTokenAccess(
  807. this.anon,
  808. this.tokens.readOnly,
  809. (response, body) => {
  810. // NOTE: This would be 404 when recreating the router. The Server Pro E2E tests cover this.
  811. expect(response.statusCode).to.equal(200)
  812. },
  813. (response, body) => {
  814. // NOTE: This would be 404 when recreating the router. The Server Pro E2E tests cover this.
  815. expect(response.statusCode).to.equal(500)
  816. expect(loggerStub).to.have.been.calledWithMatch(
  817. {
  818. err: { message: 'link sharing is disabled' },
  819. },
  820. '%s %s',
  821. 'POST',
  822. `/read/${this.tokens.readOnly}/grant`
  823. )
  824. },
  825. cb
  826. ),
  827. // still no access
  828. cb =>
  829. tryEditorAccess(
  830. this.anon,
  831. this.projectId,
  832. expectErrorResponse.restricted.html,
  833. cb
  834. ),
  835. // should not allow the user to join the project
  836. cb =>
  837. tryAnonContentAccess(
  838. this.anon,
  839. this.projectId,
  840. this.tokens.readOnly,
  841. (response, body) => {
  842. expect(response.statusCode).to.equal(403)
  843. expect(body).to.equal('Forbidden')
  844. },
  845. cb
  846. ),
  847. ],
  848. done
  849. )
  850. })
  851. it('should deny access to access tokens', function (done) {
  852. tryFetchProjectTokens(this.anon, this.projectId, (error, response) => {
  853. expect(error).to.equal(null)
  854. expect(response.statusCode).to.equal(403)
  855. done()
  856. })
  857. })
  858. it('should deny access to legacy public project', function (done) {
  859. async.series(
  860. [
  861. cb => this.owner.makePublic(this.projectId, 'readOnly', cb),
  862. cb =>
  863. tryAnonContentAccess(
  864. this.anon,
  865. this.projectId,
  866. this.tokens.readOnly,
  867. (response, body) => {
  868. expect(response.statusCode).to.equal(403)
  869. expect(body).to.equal('Forbidden')
  870. },
  871. cb
  872. ),
  873. ],
  874. done
  875. )
  876. })
  877. })
  878. })
  879. describe('anonymous read-and-write token, disabled (feature is deprecated)', function () {
  880. beforeEach(function (done) {
  881. this.owner.createProject(
  882. `token-anon-rw-test${Math.random()}`,
  883. (err, projectId) => {
  884. if (err != null) {
  885. return done(err)
  886. }
  887. this.projectId = projectId
  888. this.owner.makeTokenBased(this.projectId, err => {
  889. if (err != null) {
  890. return done(err)
  891. }
  892. this.owner.getProject(this.projectId, (err, project) => {
  893. if (err != null) {
  894. return done(err)
  895. }
  896. this.tokens = project.tokens
  897. done()
  898. })
  899. })
  900. }
  901. )
  902. })
  903. it('should not allow the user to access read-and-write token', function (done) {
  904. async.series(
  905. [
  906. cb =>
  907. tryEditorAccess(
  908. this.anon,
  909. this.projectId,
  910. expectErrorResponse.restricted.html,
  911. cb
  912. ),
  913. cb =>
  914. tryReadAndWriteTokenAccess(
  915. this.anon,
  916. this.tokens.readAndWrite,
  917. (response, body) => {
  918. expect(response.statusCode).to.equal(200)
  919. },
  920. (response, body) => {
  921. expect(response.statusCode).to.equal(200)
  922. expect(body).to.deep.equal({
  923. redirect: '/restricted',
  924. anonWriteAccessDenied: true,
  925. })
  926. },
  927. cb
  928. ),
  929. cb =>
  930. tryAnonContentAccess(
  931. this.anon,
  932. this.projectId,
  933. this.tokens.readAndWrite,
  934. (response, body) => {
  935. expect(response.statusCode).to.equal(403)
  936. expect(body).to.equal('Forbidden')
  937. },
  938. cb
  939. ),
  940. cb =>
  941. this.anon.login((err, response, body) => {
  942. expect(err).to.not.exist
  943. expect(response.statusCode).to.equal(200)
  944. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  945. cb()
  946. }),
  947. ],
  948. done
  949. )
  950. })
  951. it('should deny access to access tokens', function (done) {
  952. tryFetchProjectTokens(this.anon, this.projectId, (error, response) => {
  953. expect(error).to.equal(null)
  954. expect(response.statusCode).to.equal(403)
  955. done()
  956. })
  957. })
  958. it('should require login if project does not exist', function (done) {
  959. async.series(
  960. [
  961. // delete project
  962. cb => {
  963. this.owner.deleteProject(this.projectId, cb)
  964. },
  965. cb =>
  966. tryReadAndWriteTokenAccess(
  967. this.anon,
  968. this.tokens.readAndWrite,
  969. (response, body) => {
  970. expect(response.statusCode).to.equal(200)
  971. },
  972. (response, body) => {
  973. expect(response.statusCode).to.equal(200)
  974. expect(body).to.deep.equal({
  975. redirect: '/restricted',
  976. anonWriteAccessDenied: true,
  977. })
  978. },
  979. cb
  980. ),
  981. cb =>
  982. this.anon.login((err, response, body) => {
  983. expect(err).to.not.exist
  984. expect(response.statusCode).to.equal(200)
  985. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  986. cb()
  987. }),
  988. ],
  989. done
  990. )
  991. })
  992. it('should save URL hash in redirect', function (done) {
  993. const urlFragment = '#123456'
  994. const tokenWithUrlFragment = `${this.tokens.readAndWrite}${urlFragment}`
  995. async.series(
  996. [
  997. cb =>
  998. tryEditorAccess(
  999. this.anon,
  1000. this.projectId,
  1001. expectErrorResponse.restricted.html,
  1002. cb
  1003. ),
  1004. cb =>
  1005. this.anon.request.get(
  1006. tokenWithUrlFragment,
  1007. (err, response, body) => {
  1008. if (err) {
  1009. return cb(err)
  1010. }
  1011. expect(response.statusCode).to.equal(200)
  1012. this.anon.request.post(
  1013. `${this.tokens.readAndWrite}/grant`,
  1014. {
  1015. json: {
  1016. token: this.tokens.readAndWrite,
  1017. tokenHashPrefix: urlFragment,
  1018. },
  1019. },
  1020. (err, response, body) => {
  1021. if (err) {
  1022. return cb(err)
  1023. }
  1024. expect(response.statusCode).to.equal(200)
  1025. expect(body).to.deep.equal({
  1026. redirect: '/restricted',
  1027. anonWriteAccessDenied: true,
  1028. })
  1029. cb()
  1030. }
  1031. )
  1032. }
  1033. ),
  1034. cb =>
  1035. tryAnonContentAccess(
  1036. this.anon,
  1037. this.projectId,
  1038. this.tokens.readAndWrite,
  1039. (response, body) => {
  1040. expect(response.statusCode).to.equal(403)
  1041. expect(body).to.equal('Forbidden')
  1042. },
  1043. cb
  1044. ),
  1045. cb =>
  1046. this.anon.login((err, response, body) => {
  1047. expect(err).to.not.exist
  1048. expect(response.statusCode).to.equal(200)
  1049. expect(body.redir).to.equal(`/${tokenWithUrlFragment}`)
  1050. cb()
  1051. }),
  1052. ],
  1053. done
  1054. )
  1055. })
  1056. })
  1057. describe('private overleaf project', function () {
  1058. beforeEach(function (done) {
  1059. this.owner.createProject('overleaf-import', (err, projectId) => {
  1060. expect(err).not.to.exist
  1061. this.projectId = projectId
  1062. this.owner.makeTokenBased(this.projectId, err => {
  1063. expect(err).not.to.exist
  1064. this.owner.getProject(this.projectId, (err, project) => {
  1065. expect(err).not.to.exist
  1066. this.tokens = project.tokens
  1067. this.owner.makePrivate(this.projectId, () => {
  1068. db.projects.updateOne(
  1069. { _id: project._id },
  1070. { $set: { 'overleaf.id': 1234 } },
  1071. err => {
  1072. expect(err).not.to.exist
  1073. done()
  1074. }
  1075. )
  1076. })
  1077. })
  1078. })
  1079. })
  1080. })
  1081. it('should only allow the owner access to the project', function (done) {
  1082. async.series(
  1083. [
  1084. // should redirect to canonical path, when owner uses read-write token
  1085. cb =>
  1086. tryReadAndWriteTokenAccess(
  1087. this.owner,
  1088. this.tokens.readAndWrite,
  1089. (response, body) => {
  1090. expect(response.statusCode).to.equal(200)
  1091. },
  1092. (response, body) => {
  1093. expect(response.statusCode).to.equal(200)
  1094. expect(response.body.redirect).to.equal(
  1095. `/project/${this.projectId}`
  1096. )
  1097. expect(response.body.higherAccess).to.equal(true)
  1098. },
  1099. cb
  1100. ),
  1101. cb =>
  1102. tryEditorAccess(
  1103. this.owner,
  1104. this.projectId,
  1105. (response, body) => {
  1106. expect(response.statusCode).to.equal(200)
  1107. },
  1108. cb
  1109. ),
  1110. cb =>
  1111. tryContentAccess(
  1112. this.owner,
  1113. this.projectId,
  1114. (response, body) => {
  1115. expect(body.privilegeLevel).to.equal('owner')
  1116. },
  1117. cb
  1118. ),
  1119. // non-owner should be denied access
  1120. cb =>
  1121. tryContentAccess(
  1122. this.other2,
  1123. this.projectId,
  1124. (response, body) => {
  1125. expect(response.statusCode).to.equal(403)
  1126. expect(body).to.equal('Forbidden')
  1127. },
  1128. cb
  1129. ),
  1130. ],
  1131. done
  1132. )
  1133. })
  1134. })
  1135. describe('private project, with higher access', function () {
  1136. beforeEach(function (done) {
  1137. this.owner.createProject(
  1138. `higher-access-test-${Math.random()}`,
  1139. (err, projectId) => {
  1140. expect(err).not.to.exist
  1141. this.projectId = projectId
  1142. this.owner.addUserToProject(
  1143. this.projectId,
  1144. this.other1,
  1145. 'readAndWrite',
  1146. err => {
  1147. expect(err).not.to.exist
  1148. this.owner.makeTokenBased(this.projectId, err => {
  1149. expect(err).not.to.exist
  1150. this.owner.getProject(this.projectId, (err, project) => {
  1151. expect(err).not.to.exist
  1152. this.tokens = project.tokens
  1153. this.owner.makePrivate(this.projectId, () => {
  1154. setTimeout(done, 1000)
  1155. })
  1156. })
  1157. })
  1158. }
  1159. )
  1160. }
  1161. )
  1162. })
  1163. it('should allow the user access to the project', function (done) {
  1164. async.series(
  1165. [
  1166. // should redirect to canonical path, when user uses read-write token
  1167. cb =>
  1168. tryReadAndWriteTokenAccess(
  1169. this.other1,
  1170. this.tokens.readAndWrite,
  1171. (response, body) => {
  1172. expect(response.statusCode).to.equal(200)
  1173. },
  1174. (response, body) => {
  1175. expect(response.statusCode).to.equal(200)
  1176. expect(response.body.redirect).to.equal(
  1177. `/project/${this.projectId}`
  1178. )
  1179. expect(response.body.higherAccess).to.equal(true)
  1180. },
  1181. cb
  1182. ),
  1183. // should redirect to canonical path, when user uses read-only token
  1184. cb =>
  1185. tryReadOnlyTokenAccess(
  1186. this.other1,
  1187. this.tokens.readOnly,
  1188. (response, body) => {
  1189. expect(response.statusCode).to.equal(200)
  1190. },
  1191. (response, body) => {
  1192. expect(response.statusCode).to.equal(200)
  1193. expect(response.body.redirect).to.equal(
  1194. `/project/${this.projectId}`
  1195. )
  1196. expect(response.body.higherAccess).to.equal(true)
  1197. },
  1198. cb
  1199. ),
  1200. // should allow the user access to the project
  1201. cb =>
  1202. tryEditorAccess(
  1203. this.other1,
  1204. this.projectId,
  1205. (response, body) => {
  1206. expect(response.statusCode).to.equal(200)
  1207. },
  1208. cb
  1209. ),
  1210. // should allow user to join the project
  1211. cb =>
  1212. tryContentAccess(
  1213. this.other1,
  1214. this.projectId,
  1215. (response, body) => {
  1216. expect(body.privilegeLevel).to.equal('readAndWrite')
  1217. expect(body.isRestrictedUser).to.equal(false)
  1218. expect(body.isTokenMember).to.equal(false)
  1219. expect(body.isInvitedMember).to.equal(true)
  1220. },
  1221. cb
  1222. ),
  1223. // should not allow a different user to join the project
  1224. cb =>
  1225. tryEditorAccess(
  1226. this.other2,
  1227. this.projectId,
  1228. expectErrorResponse.restricted.html,
  1229. cb
  1230. ),
  1231. cb =>
  1232. tryContentAccess(
  1233. this.other2,
  1234. this.projectId,
  1235. (response, body) => {
  1236. expect(response.statusCode).to.equal(403)
  1237. expect(body).to.equal('Forbidden')
  1238. },
  1239. cb
  1240. ),
  1241. ],
  1242. done
  1243. )
  1244. })
  1245. })
  1246. describe('deleted project', function () {
  1247. beforeEach(function (done) {
  1248. this.owner.createProject(
  1249. `delete-test${Math.random()}`,
  1250. (err, projectId) => {
  1251. if (err != null) {
  1252. return done(err)
  1253. }
  1254. this.projectId = projectId
  1255. this.owner.makeTokenBased(this.projectId, err => {
  1256. if (err != null) {
  1257. return done(err)
  1258. }
  1259. this.owner.getProject(this.projectId, (err, project) => {
  1260. if (err != null) {
  1261. return done(err)
  1262. }
  1263. this.tokens = project.tokens
  1264. done()
  1265. })
  1266. })
  1267. }
  1268. )
  1269. })
  1270. it('should 404', function (done) {
  1271. async.series(
  1272. [
  1273. // delete project
  1274. cb => {
  1275. this.owner.deleteProject(this.projectId, cb)
  1276. },
  1277. cb => {
  1278. // use read-only token
  1279. tryReadOnlyTokenAccess(
  1280. this.other1,
  1281. this.tokens.readOnly,
  1282. (response, body) => {
  1283. expect(response.statusCode).to.equal(200)
  1284. },
  1285. (response, body) => {
  1286. expect(response.statusCode).to.equal(404)
  1287. },
  1288. cb
  1289. )
  1290. },
  1291. cb => {
  1292. // use read-write token
  1293. tryReadAndWriteTokenAccess(
  1294. this.other1,
  1295. this.tokens.readAndWrite,
  1296. (response, body) => {
  1297. expect(response.statusCode).to.equal(200)
  1298. },
  1299. (response, body) => {
  1300. expect(response.statusCode).to.equal(404)
  1301. },
  1302. cb
  1303. )
  1304. },
  1305. ],
  1306. done
  1307. )
  1308. })
  1309. it('should deny access to access tokens with a 404', function (done) {
  1310. async.series(
  1311. [
  1312. // delete project
  1313. cb => {
  1314. this.owner.deleteProject(this.projectId, cb)
  1315. },
  1316. cb => {
  1317. // use read-only token
  1318. tryReadOnlyTokenAccess(
  1319. this.other1,
  1320. this.tokens.readOnly,
  1321. (response, body) => {
  1322. expect(response.statusCode).to.equal(200)
  1323. },
  1324. (response, body) => {
  1325. expect(response.statusCode).to.equal(404)
  1326. },
  1327. cb
  1328. )
  1329. },
  1330. cb => {
  1331. tryFetchProjectTokens(
  1332. this.other1,
  1333. this.projectId,
  1334. (error, response) => {
  1335. expect(error).to.equal(null)
  1336. expect(response.statusCode).to.equal(404)
  1337. cb()
  1338. }
  1339. )
  1340. },
  1341. ],
  1342. done
  1343. )
  1344. })
  1345. })
  1346. describe('sharing updates consent page for read-and-write token deprecation', function () {
  1347. describe('not a member of the project', function () {
  1348. beforeEach(function (done) {
  1349. this.projectName = `token-link-sharing-changes${Math.random()}`
  1350. this.owner.createProject(this.projectName, (err, projectId) => {
  1351. if (err != null) {
  1352. return done(err)
  1353. }
  1354. this.projectId = projectId
  1355. this.owner.makeTokenBased(this.projectId, err => {
  1356. if (err != null) {
  1357. return done(err)
  1358. }
  1359. this.owner.getProject(this.projectId, (err, project) => {
  1360. if (err != null) {
  1361. return done(err)
  1362. }
  1363. this.tokens = project.tokens
  1364. done()
  1365. })
  1366. })
  1367. })
  1368. })
  1369. it('should deny access', function (done) {
  1370. async.series(
  1371. [
  1372. cb => {
  1373. trySharingUpdatesPage(
  1374. this.other1,
  1375. this.projectId,
  1376. expectErrorResponse.restricted.html,
  1377. cb
  1378. )
  1379. },
  1380. cb => {
  1381. trySharingUpdatesJoin(
  1382. this.other1,
  1383. this.projectId,
  1384. expectErrorResponse.restricted.html,
  1385. cb
  1386. )
  1387. },
  1388. cb => {
  1389. trySharingUpdatesView(
  1390. this.other1,
  1391. this.projectId,
  1392. expectErrorResponse.restricted.html,
  1393. cb
  1394. )
  1395. },
  1396. ],
  1397. done
  1398. )
  1399. })
  1400. })
  1401. describe('read-only token member of project', function () {
  1402. beforeEach(function (done) {
  1403. this.projectName = `token-link-sharing-changes${Math.random()}`
  1404. this.owner.createProject(this.projectName, (err, projectId) => {
  1405. if (err != null) {
  1406. return done(err)
  1407. }
  1408. this.projectId = projectId
  1409. this.owner.makeTokenBased(this.projectId, err => {
  1410. if (err != null) {
  1411. return done(err)
  1412. }
  1413. this.owner.getProject(this.projectId, (err, project) => {
  1414. if (err != null) {
  1415. return done(err)
  1416. }
  1417. this.tokens = project.tokens
  1418. tryReadOnlyTokenAccept(
  1419. this.other1,
  1420. this.tokens.readOnly,
  1421. (response, body) => {
  1422. expect(response.statusCode).to.equal(200)
  1423. },
  1424. (response, body) => {
  1425. expect(response.statusCode).to.equal(200)
  1426. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  1427. expect(body.tokenAccessGranted).to.equal('readOnly')
  1428. },
  1429. done
  1430. )
  1431. })
  1432. })
  1433. })
  1434. })
  1435. it('should redirect to project', function (done) {
  1436. async.series(
  1437. [
  1438. cb => {
  1439. trySharingUpdatesPage(
  1440. this.other1,
  1441. this.projectId,
  1442. expectRedirectToProject,
  1443. cb
  1444. )
  1445. },
  1446. cb => {
  1447. trySharingUpdatesJoin(
  1448. this.other1,
  1449. this.projectId,
  1450. expectRedirectToProject,
  1451. cb
  1452. )
  1453. },
  1454. cb => {
  1455. trySharingUpdatesView(
  1456. this.other1,
  1457. this.projectId,
  1458. expectRedirectToProject,
  1459. cb
  1460. )
  1461. },
  1462. cb => {
  1463. // allow content access read-only
  1464. tryContentAccess(
  1465. this.other1,
  1466. this.projectId,
  1467. (response, body) => {
  1468. expect(body.privilegeLevel).to.equal('readOnly')
  1469. expect(body.isRestrictedUser).to.equal(true)
  1470. expect(body.isTokenMember).to.equal(true)
  1471. expect(body.isInvitedMember).to.equal(false)
  1472. expect(body.project.owner).to.have.keys('_id')
  1473. expect(body.project.owner).to.not.have.any.keys(
  1474. 'email',
  1475. 'first_name',
  1476. 'last_name'
  1477. )
  1478. },
  1479. cb
  1480. )
  1481. },
  1482. cb => {
  1483. tryEditorAccess(
  1484. this.other1,
  1485. this.projectId,
  1486. (response, body) => {
  1487. expect(response.statusCode).to.equal(200)
  1488. },
  1489. cb
  1490. )
  1491. },
  1492. ],
  1493. done
  1494. )
  1495. })
  1496. })
  1497. })
  1498. })