TokenAccessControllerTests.mjs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import mongodb from 'mongodb-legacy'
  5. import MockRequest from '../helpers/MockRequest.js'
  6. import MockResponse from '../helpers/MockResponse.js'
  7. import PrivilegeLevels from '../../../../app/src/Features/Authorization/PrivilegeLevels.js'
  8. import { getSafeRedirectPath } from '../../../../app/src/Features/Helpers/UrlHelper.js'
  9. const ObjectId = mongodb.ObjectId
  10. const MODULE_PATH =
  11. '../../../../app/src/Features/TokenAccess/TokenAccessController'
  12. describe('TokenAccessController', function () {
  13. beforeEach(async function () {
  14. this.token = 'abc123'
  15. this.user = { _id: new ObjectId() }
  16. this.project = {
  17. _id: new ObjectId(),
  18. name: 'test',
  19. tokenAccessReadAndWrite_refs: [],
  20. tokenAccessReadOnly_refs: [],
  21. }
  22. this.req = new MockRequest()
  23. this.res = new MockResponse()
  24. this.next = sinon.stub().returns()
  25. this.Settings = {
  26. siteUrl: 'https://www.dev-overleaf.com',
  27. adminPrivilegeAvailable: false,
  28. adminUrl: 'https://admin.dev-overleaf.com',
  29. adminDomains: ['overleaf.com'],
  30. }
  31. this.TokenAccessHandler = {
  32. TOKEN_TYPES: {
  33. READ_ONLY: 'readOnly',
  34. READ_AND_WRITE: 'readAndWrite',
  35. },
  36. isReadAndWriteToken: sinon.stub().returns(true),
  37. isReadOnlyToken: sinon.stub().returns(true),
  38. tokenAccessEnabledForProject: sinon.stub().returns(true),
  39. checkTokenHashPrefix: sinon.stub(),
  40. makeTokenUrl: sinon.stub().returns('/'),
  41. grantSessionTokenAccess: sinon.stub(),
  42. promises: {
  43. addReadAndWriteUserToProject: sinon.stub().resolves(),
  44. addReadOnlyUserToProject: sinon.stub().resolves(),
  45. getProjectByToken: sinon.stub().resolves(this.project),
  46. getV1DocPublishedInfo: sinon.stub().resolves({ allow: true }),
  47. getV1DocInfo: sinon.stub(),
  48. removeReadAndWriteUserFromProject: sinon.stub().resolves(),
  49. moveReadAndWriteUserToReadOnly: sinon.stub().resolves(),
  50. },
  51. }
  52. this.SessionManager = {
  53. getLoggedInUserId: sinon.stub().returns(this.user._id),
  54. getSessionUser: sinon.stub().returns(this.user._id),
  55. }
  56. this.AuthenticationController = {
  57. setRedirectInSession: sinon.stub(),
  58. }
  59. this.AuthorizationManager = {
  60. promises: {
  61. getPrivilegeLevelForProject: sinon
  62. .stub()
  63. .resolves(PrivilegeLevels.NONE),
  64. },
  65. }
  66. this.AuthorizationMiddleware = {}
  67. this.ProjectAuditLogHandler = {
  68. promises: {
  69. addEntry: sinon.stub().resolves(),
  70. },
  71. }
  72. this.SplitTestHandler = {
  73. promises: {
  74. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  75. getAssignmentForUser: sinon.stub().resolves({ variant: 'default' }),
  76. },
  77. }
  78. this.CollaboratorsInviteHandler = {
  79. promises: {
  80. revokeInviteForUser: sinon.stub().resolves(),
  81. },
  82. }
  83. this.CollaboratorsHandler = {
  84. promises: {
  85. addUserIdToProject: sinon.stub().resolves(),
  86. setCollaboratorPrivilegeLevel: sinon.stub().resolves(),
  87. },
  88. }
  89. this.CollaboratorsGetter = {
  90. promises: {
  91. userIsReadWriteTokenMember: sinon.stub().resolves(),
  92. isUserInvitedReadWriteMemberOfProject: sinon.stub().resolves(),
  93. isUserInvitedMemberOfProject: sinon.stub().resolves(),
  94. },
  95. }
  96. this.EditorRealTimeController = { emitToRoom: sinon.stub() }
  97. this.ProjectGetter = {
  98. promises: {
  99. getProject: sinon.stub().resolves(this.project),
  100. },
  101. }
  102. this.AnalyticsManager = {
  103. recordEventForSession: sinon.stub(),
  104. recordEventForUserInBackground: sinon.stub(),
  105. }
  106. this.UserGetter = {
  107. promises: {
  108. getUser: sinon.stub().callsFake(async (userId, filter) => {
  109. if (userId === this.userId) {
  110. return this.user
  111. } else {
  112. return null
  113. }
  114. }),
  115. getUserEmail: sinon.stub().resolves(),
  116. getUserConfirmedEmails: sinon.stub().resolves(),
  117. },
  118. }
  119. this.LimitationsManager = {
  120. promises: {
  121. canAcceptEditCollaboratorInvite: sinon.stub().resolves(),
  122. },
  123. }
  124. this.TokenAccessController = await esmock.strict(MODULE_PATH, {
  125. '@overleaf/settings': this.Settings,
  126. '../../../../app/src/Features/TokenAccess/TokenAccessHandler':
  127. this.TokenAccessHandler,
  128. '../../../../app/src/Features/Authentication/AuthenticationController':
  129. this.AuthenticationController,
  130. '../../../../app/src/Features/Authentication/SessionManager':
  131. this.SessionManager,
  132. '../../../../app/src/Features/Authorization/AuthorizationManager':
  133. this.AuthorizationManager,
  134. '../../../../app/src/Features/Authorization/AuthorizationMiddleware':
  135. this.AuthorizationMiddleware,
  136. '../../../../app/src/Features/Project/ProjectAuditLogHandler':
  137. this.ProjectAuditLogHandler,
  138. '../../../../app/src/Features/SplitTests/SplitTestHandler':
  139. this.SplitTestHandler,
  140. '../../../../app/src/Features/Errors/Errors': (this.Errors = {
  141. NotFoundError: sinon.stub(),
  142. }),
  143. '../../../../app/src/Features/Collaborators/CollaboratorsHandler':
  144. this.CollaboratorsHandler,
  145. '../../../../app/src/Features/Collaborators/CollaboratorsInviteHandler':
  146. this.CollaboratorsInviteHandler,
  147. '../../../../app/src/Features/Collaborators/CollaboratorsGetter':
  148. this.CollaboratorsGetter,
  149. '../../../../app/src/Features/Editor/EditorRealTimeController':
  150. this.EditorRealTimeController,
  151. '../../../../app/src/Features/Project/ProjectGetter': this.ProjectGetter,
  152. '../../../../app/src/Features/Helpers/AsyncFormHelper':
  153. (this.AsyncFormHelper = {
  154. redirect: sinon.stub(),
  155. }),
  156. '../../../../app/src/Features/Helpers/AdminAuthorizationHelper':
  157. (this.AdminAuthorizationHelper = {
  158. canRedirectToAdminDomain: sinon.stub(),
  159. }),
  160. '../../../../app/src/Features/Helpers/UrlHelper': (this.UrlHelper = {
  161. getSafeAdminDomainRedirect: sinon
  162. .stub()
  163. .callsFake(
  164. path => `${this.Settings.adminUrl}${getSafeRedirectPath(path)}`
  165. ),
  166. }),
  167. '../../../../app/src/Features/Analytics/AnalyticsManager':
  168. this.AnalyticsManager,
  169. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  170. '../../../../app/src/Features/Subscription/LimitationsManager':
  171. this.LimitationsManager,
  172. })
  173. })
  174. describe('grantTokenAccessReadAndWrite', function () {
  175. describe('normal case', function () {
  176. beforeEach(function (done) {
  177. this.req.params = { token: this.token }
  178. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  179. this.res.callback = done
  180. this.TokenAccessController.grantTokenAccessReadAndWrite(
  181. this.req,
  182. this.res,
  183. done
  184. )
  185. })
  186. it('grants read and write access', function () {
  187. expect(
  188. this.TokenAccessHandler.promises.addReadAndWriteUserToProject
  189. ).to.have.been.calledWith(this.user._id, this.project._id)
  190. })
  191. it('writes a project audit log', function () {
  192. expect(
  193. this.ProjectAuditLogHandler.promises.addEntry
  194. ).to.have.been.calledWith(
  195. this.project._id,
  196. 'join-via-token',
  197. this.user._id,
  198. this.req.ip,
  199. { privileges: 'readAndWrite' }
  200. )
  201. })
  202. it('checks token hash', function () {
  203. expect(
  204. this.TokenAccessHandler.checkTokenHashPrefix
  205. ).to.have.been.calledWith(
  206. this.token,
  207. '#prefix',
  208. 'readAndWrite',
  209. this.user._id,
  210. { projectId: this.project._id, action: 'continue' }
  211. )
  212. })
  213. })
  214. describe('when project owner in link-sharing-warning split test', function () {
  215. beforeEach(function () {
  216. this.SplitTestHandler.promises.getAssignmentForUser.callsFake(
  217. async (userId, test) => {
  218. if (test === 'link-sharing-warning') {
  219. return { variant: 'active' }
  220. }
  221. }
  222. )
  223. })
  224. it('tells the ui to show the link-sharing-warning variant', async function () {
  225. this.req.params = { token: this.token }
  226. this.req.body = { tokenHashPrefix: '#prefix' }
  227. await this.TokenAccessController.grantTokenAccessReadAndWrite(
  228. this.req,
  229. {
  230. json: content => {
  231. expect(content).to.deep.equal({
  232. requireAccept: {
  233. linkSharingChanges: true,
  234. projectName: this.project.name,
  235. },
  236. })
  237. },
  238. }
  239. )
  240. })
  241. describe('normal case', function () {
  242. beforeEach(function (done) {
  243. this.req.params = { token: this.token }
  244. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  245. this.res.callback = done
  246. this.TokenAccessController.grantTokenAccessReadAndWrite(
  247. this.req,
  248. this.res,
  249. done
  250. )
  251. })
  252. it('adds the user as a read and write invited member', function () {
  253. expect(
  254. this.CollaboratorsHandler.promises.addUserIdToProject
  255. ).to.have.been.calledWith(
  256. this.project._id,
  257. undefined,
  258. this.user._id,
  259. PrivilegeLevels.READ_AND_WRITE
  260. )
  261. })
  262. it('writes a project audit log', function () {
  263. expect(
  264. this.ProjectAuditLogHandler.promises.addEntry
  265. ).to.have.been.calledWith(
  266. this.project._id,
  267. 'accept-via-link-sharing',
  268. this.user._id,
  269. this.req.ip,
  270. { privileges: 'readAndWrite' }
  271. )
  272. })
  273. it('records a project-joined event for the user', function () {
  274. expect(
  275. this.AnalyticsManager.recordEventForUserInBackground
  276. ).to.have.been.calledWith(this.user._id, 'project-joined', {
  277. mode: 'read-write',
  278. projectId: this.project._id.toString(),
  279. })
  280. })
  281. it('emits a project membership changed event', function () {
  282. expect(
  283. this.EditorRealTimeController.emitToRoom
  284. ).to.have.been.calledWith(
  285. this.project._id,
  286. 'project:membership:changed',
  287. { members: true, invites: true }
  288. )
  289. })
  290. it('checks token hash', function () {
  291. expect(
  292. this.TokenAccessHandler.checkTokenHashPrefix
  293. ).to.have.been.calledWith(
  294. this.token,
  295. '#prefix',
  296. 'readAndWrite',
  297. this.user._id,
  298. { projectId: this.project._id, action: 'continue' }
  299. )
  300. })
  301. })
  302. describe('when the project owner is in the link-sharing-enforcement split test', function () {
  303. beforeEach(function () {
  304. this.SplitTestHandler.promises.getAssignmentForUser.callsFake(
  305. async (userId, test) => {
  306. if (test === 'link-sharing-warning') {
  307. return { variant: 'active' }
  308. } else if (test === 'link-sharing-enforcement') {
  309. return { variant: 'active' }
  310. }
  311. }
  312. )
  313. })
  314. describe('normal case (edit slot available)', function () {
  315. beforeEach(function (done) {
  316. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  317. true
  318. )
  319. this.req.params = { token: this.token }
  320. this.req.body = {
  321. confirmedByUser: true,
  322. tokenHashPrefix: '#prefix',
  323. }
  324. this.res.callback = done
  325. this.TokenAccessController.grantTokenAccessReadAndWrite(
  326. this.req,
  327. this.res,
  328. done
  329. )
  330. })
  331. it('adds the user as a read and write invited member', function () {
  332. expect(
  333. this.CollaboratorsHandler.promises.addUserIdToProject
  334. ).to.have.been.calledWith(
  335. this.project._id,
  336. undefined,
  337. this.user._id,
  338. PrivilegeLevels.READ_AND_WRITE
  339. )
  340. })
  341. it('writes a project audit log', function () {
  342. expect(
  343. this.ProjectAuditLogHandler.promises.addEntry
  344. ).to.have.been.calledWith(
  345. this.project._id,
  346. 'accept-via-link-sharing',
  347. this.user._id,
  348. this.req.ip,
  349. { privileges: 'readAndWrite' }
  350. )
  351. })
  352. it('records a project-joined event for the user', function () {
  353. expect(
  354. this.AnalyticsManager.recordEventForUserInBackground
  355. ).to.have.been.calledWith(this.user._id, 'project-joined', {
  356. mode: 'read-write',
  357. projectId: this.project._id.toString(),
  358. })
  359. })
  360. it('emits a project membership changed event', function () {
  361. expect(
  362. this.EditorRealTimeController.emitToRoom
  363. ).to.have.been.calledWith(
  364. this.project._id,
  365. 'project:membership:changed',
  366. { members: true, invites: true }
  367. )
  368. })
  369. it('checks token hash', function () {
  370. expect(
  371. this.TokenAccessHandler.checkTokenHashPrefix
  372. ).to.have.been.calledWith(
  373. this.token,
  374. '#prefix',
  375. 'readAndWrite',
  376. this.user._id,
  377. { projectId: this.project._id, action: 'continue' }
  378. )
  379. })
  380. })
  381. describe('when there are no edit collaborator slots available', function () {
  382. beforeEach(function (done) {
  383. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  384. false
  385. )
  386. this.req.params = { token: this.token }
  387. this.req.body = {
  388. confirmedByUser: true,
  389. tokenHashPrefix: '#prefix',
  390. }
  391. this.res.callback = done
  392. this.TokenAccessController.grantTokenAccessReadAndWrite(
  393. this.req,
  394. this.res,
  395. done
  396. )
  397. })
  398. it('adds the user as a read only invited member instead (pendingEditor)', function () {
  399. expect(
  400. this.CollaboratorsHandler.promises.addUserIdToProject
  401. ).to.have.been.calledWith(
  402. this.project._id,
  403. undefined,
  404. this.user._id,
  405. PrivilegeLevels.READ_ONLY,
  406. { pendingEditor: true }
  407. )
  408. })
  409. it('writes a project audit log', function () {
  410. expect(
  411. this.ProjectAuditLogHandler.promises.addEntry
  412. ).to.have.been.calledWith(
  413. this.project._id,
  414. 'accept-via-link-sharing',
  415. this.user._id,
  416. this.req.ip,
  417. { privileges: 'readOnly', pendingEditor: true }
  418. )
  419. })
  420. it('records a project-joined event for the user', function () {
  421. expect(
  422. this.AnalyticsManager.recordEventForUserInBackground
  423. ).to.have.been.calledWith(this.user._id, 'project-joined', {
  424. mode: 'read-only',
  425. projectId: this.project._id.toString(),
  426. pendingEditor: true,
  427. })
  428. })
  429. it('emits a project membership changed event', function () {
  430. expect(
  431. this.EditorRealTimeController.emitToRoom
  432. ).to.have.been.calledWith(
  433. this.project._id,
  434. 'project:membership:changed',
  435. { members: true, invites: true }
  436. )
  437. })
  438. it('checks token hash', function () {
  439. expect(
  440. this.TokenAccessHandler.checkTokenHashPrefix
  441. ).to.have.been.calledWith(
  442. this.token,
  443. '#prefix',
  444. 'readAndWrite',
  445. this.user._id,
  446. { projectId: this.project._id, action: 'continue' }
  447. )
  448. })
  449. })
  450. })
  451. })
  452. describe('when the access was already granted', function () {
  453. beforeEach(function (done) {
  454. this.project.tokenAccessReadAndWrite_refs.push(this.user._id)
  455. this.req.params = { token: this.token }
  456. this.req.body = { confirmedByUser: true }
  457. this.res.callback = done
  458. this.TokenAccessController.grantTokenAccessReadAndWrite(
  459. this.req,
  460. this.res,
  461. done
  462. )
  463. })
  464. it("doesn't write a project audit log", function () {
  465. expect(this.ProjectAuditLogHandler.promises.addEntry).to.not.have.been
  466. .called
  467. })
  468. it('checks token hash', function () {
  469. expect(
  470. this.TokenAccessHandler.checkTokenHashPrefix
  471. ).to.have.been.calledWith(
  472. this.token,
  473. undefined,
  474. 'readAndWrite',
  475. this.user._id,
  476. { projectId: this.project._id, action: 'continue' }
  477. )
  478. })
  479. })
  480. describe('hash prefix missing in request', function () {
  481. beforeEach(function (done) {
  482. this.req.params = { token: this.token }
  483. this.req.body = { confirmedByUser: true }
  484. this.res.callback = done
  485. this.TokenAccessController.grantTokenAccessReadAndWrite(
  486. this.req,
  487. this.res,
  488. done
  489. )
  490. })
  491. it('grants read and write access', function () {
  492. expect(
  493. this.TokenAccessHandler.promises.addReadAndWriteUserToProject
  494. ).to.have.been.calledWith(this.user._id, this.project._id)
  495. })
  496. it('checks the hash prefix', function () {
  497. expect(
  498. this.TokenAccessHandler.checkTokenHashPrefix
  499. ).to.have.been.calledWith(
  500. this.token,
  501. undefined,
  502. 'readAndWrite',
  503. this.user._id,
  504. { projectId: this.project._id, action: 'continue' }
  505. )
  506. })
  507. })
  508. describe('user is owner of project', function () {
  509. beforeEach(function (done) {
  510. this.AuthorizationManager.promises.getPrivilegeLevelForProject.returns(
  511. PrivilegeLevels.OWNER
  512. )
  513. this.req.params = { token: this.token }
  514. this.req.body = {}
  515. this.res.callback = done
  516. this.TokenAccessController.grantTokenAccessReadAndWrite(
  517. this.req,
  518. this.res,
  519. done
  520. )
  521. })
  522. it('checks token hash and includes log data', function () {
  523. expect(
  524. this.TokenAccessHandler.checkTokenHashPrefix
  525. ).to.have.been.calledWith(
  526. this.token,
  527. undefined,
  528. 'readAndWrite',
  529. this.user._id,
  530. {
  531. projectId: this.project._id,
  532. action: 'user already has higher or same privilege',
  533. }
  534. )
  535. })
  536. })
  537. describe('when user is not logged in', function () {
  538. beforeEach(function () {
  539. this.SessionManager.getLoggedInUserId.returns(null)
  540. this.req.params = { token: this.token }
  541. this.req.body = { tokenHashPrefix: '#prefix' }
  542. })
  543. describe('ANONYMOUS_READ_AND_WRITE_ENABLED is undefined', function () {
  544. beforeEach(function (done) {
  545. this.res.callback = done
  546. this.TokenAccessController.grantTokenAccessReadAndWrite(
  547. this.req,
  548. this.res,
  549. done
  550. )
  551. })
  552. it('redirects to restricted', function () {
  553. expect(this.res.json).to.have.been.calledWith({
  554. redirect: '/restricted',
  555. anonWriteAccessDenied: true,
  556. })
  557. })
  558. it('checks the hash prefix and includes log data', function () {
  559. expect(
  560. this.TokenAccessHandler.checkTokenHashPrefix
  561. ).to.have.been.calledWith(
  562. this.token,
  563. '#prefix',
  564. 'readAndWrite',
  565. null,
  566. {
  567. action: 'denied anonymous read-and-write token access',
  568. }
  569. )
  570. })
  571. it('saves redirect URL with URL fragment', function () {
  572. expect(
  573. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  574. ).to.equal('/#prefix')
  575. })
  576. })
  577. describe('ANONYMOUS_READ_AND_WRITE_ENABLED is true', function () {
  578. beforeEach(function (done) {
  579. this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
  580. this.res.callback = done
  581. this.TokenAccessController.grantTokenAccessReadAndWrite(
  582. this.req,
  583. this.res,
  584. done
  585. )
  586. })
  587. it('redirects to project', function () {
  588. expect(this.res.json).to.have.been.calledWith({
  589. redirect: `/project/${this.project._id}`,
  590. grantAnonymousAccess: 'readAndWrite',
  591. })
  592. })
  593. it('checks the hash prefix and includes log data', function () {
  594. expect(
  595. this.TokenAccessHandler.checkTokenHashPrefix
  596. ).to.have.been.calledWith(
  597. this.token,
  598. '#prefix',
  599. 'readAndWrite',
  600. null,
  601. {
  602. projectId: this.project._id,
  603. action: 'granting read-write anonymous access',
  604. }
  605. )
  606. })
  607. })
  608. })
  609. describe('when Overleaf SaaS', function () {
  610. beforeEach(function () {
  611. this.Settings.overleaf = {}
  612. })
  613. describe('when token is for v1 project', function () {
  614. beforeEach(function (done) {
  615. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  616. this.TokenAccessHandler.promises.getV1DocInfo.resolves({
  617. exists: true,
  618. has_owner: true,
  619. })
  620. this.req.params = { token: this.token }
  621. this.req.body = { tokenHashPrefix: '#prefix' }
  622. this.res.callback = done
  623. this.TokenAccessController.grantTokenAccessReadAndWrite(
  624. this.req,
  625. this.res,
  626. done
  627. )
  628. })
  629. it('returns v1 import data', function () {
  630. expect(this.res.json).to.have.been.calledWith({
  631. v1Import: {
  632. status: 'canDownloadZip',
  633. projectId: this.token,
  634. hasOwner: true,
  635. name: 'Untitled',
  636. brandInfo: undefined,
  637. },
  638. })
  639. })
  640. it('checks the hash prefix and includes log data', function () {
  641. expect(
  642. this.TokenAccessHandler.checkTokenHashPrefix
  643. ).to.have.been.calledWith(
  644. this.token,
  645. '#prefix',
  646. 'readAndWrite',
  647. this.user._id,
  648. {
  649. action: 'import v1',
  650. }
  651. )
  652. })
  653. })
  654. describe('when token is not for a v1 or v2 project', function () {
  655. beforeEach(function (done) {
  656. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  657. this.TokenAccessHandler.promises.getV1DocInfo.resolves({
  658. exists: false,
  659. })
  660. this.req.params = { token: this.token }
  661. this.req.body = { tokenHashPrefix: '#prefix' }
  662. this.res.callback = done
  663. this.TokenAccessController.grantTokenAccessReadAndWrite(
  664. this.req,
  665. this.res,
  666. done
  667. )
  668. })
  669. it('returns 404', function () {
  670. expect(this.res.sendStatus).to.have.been.calledWith(404)
  671. })
  672. it('checks the hash prefix and includes log data', function () {
  673. expect(
  674. this.TokenAccessHandler.checkTokenHashPrefix
  675. ).to.have.been.calledWith(
  676. this.token,
  677. '#prefix',
  678. 'readAndWrite',
  679. this.user._id,
  680. {
  681. action: '404',
  682. }
  683. )
  684. })
  685. })
  686. })
  687. describe('not Overleaf SaaS', function () {
  688. beforeEach(function () {
  689. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  690. this.req.params = { token: this.token }
  691. this.req.body = { tokenHashPrefix: '#prefix' }
  692. })
  693. it('passes Errors.NotFoundError to next when project not found and still checks token hash', function (done) {
  694. this.TokenAccessController.grantTokenAccessReadAndWrite(
  695. this.req,
  696. this.res,
  697. args => {
  698. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  699. expect(
  700. this.TokenAccessHandler.checkTokenHashPrefix
  701. ).to.have.been.calledWith(
  702. this.token,
  703. '#prefix',
  704. 'readAndWrite',
  705. this.user._id,
  706. {
  707. action: '404',
  708. }
  709. )
  710. done()
  711. }
  712. )
  713. })
  714. })
  715. describe('when user is admin', function () {
  716. const admin = { _id: new ObjectId(), isAdmin: true }
  717. beforeEach(function () {
  718. this.SessionManager.getLoggedInUserId.returns(admin._id)
  719. this.SessionManager.getSessionUser.returns(admin)
  720. this.AdminAuthorizationHelper.canRedirectToAdminDomain.returns(true)
  721. this.req.params = { token: this.token }
  722. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  723. })
  724. it('redirects if project owner is non-admin', function () {
  725. this.UserGetter.promises.getUserConfirmedEmails = sinon
  726. .stub()
  727. .resolves([{ email: 'test@not-overleaf.com' }])
  728. this.res.callback = () => {
  729. expect(this.res.json).to.have.been.calledWith({
  730. redirect: `${this.Settings.adminUrl}/#prefix`,
  731. })
  732. }
  733. this.TokenAccessController.grantTokenAccessReadAndWrite(
  734. this.req,
  735. this.res
  736. )
  737. })
  738. it('grants access if project owner is an internal staff', function () {
  739. const internalStaff = { _id: new ObjectId(), isAdmin: true }
  740. const projectFromInternalStaff = {
  741. _id: new ObjectId(),
  742. name: 'test',
  743. tokenAccessReadAndWrite_refs: [],
  744. tokenAccessReadOnly_refs: [],
  745. owner_ref: internalStaff._id,
  746. }
  747. this.UserGetter.promises.getUser = sinon.stub().resolves(internalStaff)
  748. this.UserGetter.promises.getUserConfirmedEmails = sinon
  749. .stub()
  750. .resolves([{ email: 'test@overleaf.com' }])
  751. this.TokenAccessHandler.promises.getProjectByToken = sinon
  752. .stub()
  753. .resolves(projectFromInternalStaff)
  754. this.res.callback = () => {
  755. expect(
  756. this.TokenAccessHandler.promises.addReadAndWriteUserToProject
  757. ).to.have.been.calledWith(admin._id, projectFromInternalStaff._id)
  758. }
  759. this.TokenAccessController.grantTokenAccessReadAndWrite(
  760. this.req,
  761. this.res
  762. )
  763. })
  764. })
  765. it('passes Errors.NotFoundError to next when token access is not enabled but still checks token hash', function (done) {
  766. this.TokenAccessHandler.tokenAccessEnabledForProject.returns(false)
  767. this.req.params = { token: this.token }
  768. this.req.body = { tokenHashPrefix: '#prefix' }
  769. this.TokenAccessController.grantTokenAccessReadAndWrite(
  770. this.req,
  771. this.res,
  772. args => {
  773. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  774. expect(
  775. this.TokenAccessHandler.checkTokenHashPrefix
  776. ).to.have.been.calledWith(
  777. this.token,
  778. '#prefix',
  779. 'readAndWrite',
  780. this.user._id,
  781. {
  782. projectId: this.project._id,
  783. action: 'token access not enabled',
  784. }
  785. )
  786. done()
  787. }
  788. )
  789. })
  790. it('returns 400 when not using a read write token', function () {
  791. this.TokenAccessHandler.isReadAndWriteToken.returns(false)
  792. this.req.params = { token: this.token }
  793. this.req.body = { tokenHashPrefix: '#prefix' }
  794. this.TokenAccessController.grantTokenAccessReadAndWrite(
  795. this.req,
  796. this.res
  797. )
  798. expect(this.res.sendStatus).to.have.been.calledWith(400)
  799. })
  800. })
  801. describe('grantTokenAccessReadOnly', function () {
  802. describe('normal case', function () {
  803. beforeEach(function (done) {
  804. this.req.params = { token: this.token }
  805. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  806. this.res.callback = done
  807. this.TokenAccessController.grantTokenAccessReadOnly(
  808. this.req,
  809. this.res,
  810. done
  811. )
  812. })
  813. it('grants read-only access', function () {
  814. expect(
  815. this.TokenAccessHandler.promises.addReadOnlyUserToProject
  816. ).to.have.been.calledWith(this.user._id, this.project._id)
  817. })
  818. it('writes a project audit log', function () {
  819. expect(
  820. this.ProjectAuditLogHandler.promises.addEntry
  821. ).to.have.been.calledWith(
  822. this.project._id,
  823. 'join-via-token',
  824. this.user._id,
  825. this.req.ip,
  826. { privileges: 'readOnly' }
  827. )
  828. })
  829. it('checks if hash prefix matches', function () {
  830. expect(
  831. this.TokenAccessHandler.checkTokenHashPrefix
  832. ).to.have.been.calledWith(
  833. this.token,
  834. '#prefix',
  835. 'readOnly',
  836. this.user._id,
  837. { projectId: this.project._id, action: 'continue' }
  838. )
  839. })
  840. })
  841. describe('when the access was already granted', function () {
  842. beforeEach(function (done) {
  843. this.project.tokenAccessReadOnly_refs.push(this.user._id)
  844. this.req.params = { token: this.token }
  845. this.req.body = { confirmedByUser: true }
  846. this.res.callback = done
  847. this.TokenAccessController.grantTokenAccessReadOnly(
  848. this.req,
  849. this.res,
  850. done
  851. )
  852. })
  853. it("doesn't write a project audit log", function () {
  854. expect(this.ProjectAuditLogHandler.promises.addEntry).to.not.have.been
  855. .called
  856. })
  857. it('still checks if hash prefix matches', function () {
  858. expect(
  859. this.TokenAccessHandler.checkTokenHashPrefix
  860. ).to.have.been.calledWith(
  861. this.token,
  862. undefined,
  863. 'readOnly',
  864. this.user._id,
  865. { projectId: this.project._id, action: 'continue' }
  866. )
  867. })
  868. })
  869. it('returns 400 when not using a read only token', function () {
  870. this.TokenAccessHandler.isReadOnlyToken.returns(false)
  871. this.req.params = { token: this.token }
  872. this.req.body = { tokenHashPrefix: '#prefix' }
  873. this.TokenAccessController.grantTokenAccessReadOnly(this.req, this.res)
  874. expect(this.res.sendStatus).to.have.been.calledWith(400)
  875. })
  876. describe('anonymous users', function () {
  877. beforeEach(function (done) {
  878. this.req.params = { token: this.token }
  879. this.SessionManager.getLoggedInUserId.returns(null)
  880. this.res.callback = done
  881. this.TokenAccessController.grantTokenAccessReadOnly(
  882. this.req,
  883. this.res,
  884. done
  885. )
  886. })
  887. it('allows anonymous users and checks the token hash', function () {
  888. expect(this.res.json).to.have.been.calledWith({
  889. redirect: `/project/${this.project._id}`,
  890. grantAnonymousAccess: 'readOnly',
  891. })
  892. expect(
  893. this.TokenAccessHandler.checkTokenHashPrefix
  894. ).to.have.been.calledWith(this.token, undefined, 'readOnly', null, {
  895. projectId: this.project._id,
  896. action: 'granting read-only anonymous access',
  897. })
  898. })
  899. })
  900. describe('user is owner of project', function () {
  901. beforeEach(function (done) {
  902. this.AuthorizationManager.promises.getPrivilegeLevelForProject.returns(
  903. PrivilegeLevels.OWNER
  904. )
  905. this.req.params = { token: this.token }
  906. this.req.body = {}
  907. this.res.callback = done
  908. this.TokenAccessController.grantTokenAccessReadOnly(
  909. this.req,
  910. this.res,
  911. done
  912. )
  913. })
  914. it('checks token hash and includes log data', function () {
  915. expect(
  916. this.TokenAccessHandler.checkTokenHashPrefix
  917. ).to.have.been.calledWith(
  918. this.token,
  919. undefined,
  920. 'readOnly',
  921. this.user._id,
  922. {
  923. projectId: this.project._id,
  924. action: 'user already has higher or same privilege',
  925. }
  926. )
  927. })
  928. })
  929. it('passes Errors.NotFoundError to next when token access is not enabled but still checks token hash', function (done) {
  930. this.TokenAccessHandler.tokenAccessEnabledForProject.returns(false)
  931. this.req.params = { token: this.token }
  932. this.req.body = { tokenHashPrefix: '#prefix' }
  933. this.TokenAccessController.grantTokenAccessReadOnly(
  934. this.req,
  935. this.res,
  936. args => {
  937. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  938. expect(
  939. this.TokenAccessHandler.checkTokenHashPrefix
  940. ).to.have.been.calledWith(
  941. this.token,
  942. '#prefix',
  943. 'readOnly',
  944. this.user._id,
  945. {
  946. projectId: this.project._id,
  947. action: 'token access not enabled',
  948. }
  949. )
  950. done()
  951. }
  952. )
  953. })
  954. })
  955. describe('ensureUserCanUseSharingUpdatesConsentPage', function () {
  956. beforeEach(function () {
  957. this.req.params = { Project_id: this.project._id }
  958. })
  959. describe('when not in link sharing changes test', function () {
  960. beforeEach(function (done) {
  961. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  962. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  963. this.req,
  964. this.res,
  965. done
  966. )
  967. })
  968. it('redirects to the project/editor', function () {
  969. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  970. this.req,
  971. this.res,
  972. `/project/${this.project._id}`
  973. )
  974. })
  975. })
  976. describe('when link sharing changes test active', function () {
  977. beforeEach(function () {
  978. this.SplitTestHandler.promises.getAssignmentForUser.resolves({
  979. variant: 'active',
  980. })
  981. })
  982. describe('when user is not an invited editor and is a read write token member', function () {
  983. beforeEach(function (done) {
  984. this.CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject.resolves(
  985. false
  986. )
  987. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember.resolves(
  988. true
  989. )
  990. this.next.callsFake(() => done())
  991. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  992. this.req,
  993. this.res,
  994. this.next
  995. )
  996. })
  997. it('calls next', function () {
  998. expect(
  999. this.CollaboratorsGetter.promises
  1000. .isUserInvitedReadWriteMemberOfProject
  1001. ).to.have.been.calledWith(this.user._id, this.project._id)
  1002. expect(
  1003. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember
  1004. ).to.have.been.calledWith(this.user._id, this.project._id)
  1005. expect(this.next).to.have.been.calledOnce
  1006. expect(this.next.firstCall.args[0]).to.not.exist
  1007. })
  1008. })
  1009. describe('when user is already an invited editor', function () {
  1010. beforeEach(function (done) {
  1011. this.CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject.resolves(
  1012. true
  1013. )
  1014. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  1015. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  1016. this.req,
  1017. this.res,
  1018. done
  1019. )
  1020. })
  1021. it('redirects to the project/editor', function () {
  1022. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  1023. this.req,
  1024. this.res,
  1025. `/project/${this.project._id}`
  1026. )
  1027. })
  1028. })
  1029. describe('when user not a read write token member', function () {
  1030. beforeEach(function (done) {
  1031. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember.resolves(
  1032. false
  1033. )
  1034. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  1035. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  1036. this.req,
  1037. this.res,
  1038. done
  1039. )
  1040. })
  1041. it('redirects to the project/editor', function () {
  1042. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  1043. this.req,
  1044. this.res,
  1045. `/project/${this.project._id}`
  1046. )
  1047. })
  1048. })
  1049. })
  1050. })
  1051. describe('moveReadWriteToCollaborators', function () {
  1052. beforeEach(function () {
  1053. this.req.params = { Project_id: this.project._id }
  1054. })
  1055. describe('read only invited viewer gaining edit access via link sharing', function () {
  1056. beforeEach(function (done) {
  1057. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  1058. true
  1059. )
  1060. this.res.callback = done
  1061. this.TokenAccessController.moveReadWriteToCollaborators(
  1062. this.req,
  1063. this.res,
  1064. done
  1065. )
  1066. })
  1067. it('sets the privilege level to read and write for the invited viewer', function () {
  1068. expect(
  1069. this.CollaboratorsHandler.promises.setCollaboratorPrivilegeLevel
  1070. ).to.have.been.calledWith(
  1071. this.project._id,
  1072. this.user._id,
  1073. PrivilegeLevels.READ_AND_WRITE
  1074. )
  1075. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1076. })
  1077. })
  1078. describe('previously joined token access user moving to named collaborator', function () {
  1079. beforeEach(function (done) {
  1080. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  1081. false
  1082. )
  1083. this.res.callback = done
  1084. this.TokenAccessController.moveReadWriteToCollaborators(
  1085. this.req,
  1086. this.res,
  1087. done
  1088. )
  1089. })
  1090. it('sets the privilege level to read and write for the invited viewer', function () {
  1091. expect(
  1092. this.TokenAccessHandler.promises.removeReadAndWriteUserFromProject
  1093. ).to.have.been.calledWith(this.user._id, this.project._id)
  1094. expect(
  1095. this.CollaboratorsHandler.promises.addUserIdToProject
  1096. ).to.have.been.calledWith(
  1097. this.project._id,
  1098. undefined,
  1099. this.user._id,
  1100. PrivilegeLevels.READ_AND_WRITE
  1101. )
  1102. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1103. })
  1104. })
  1105. describe('when link-sharing-enforcement test is active', function () {
  1106. beforeEach(function () {
  1107. this.SplitTestHandler.promises.getAssignmentForUser.resolves({
  1108. variant: 'active',
  1109. })
  1110. })
  1111. describe('when there are collaborator slots available', function () {
  1112. beforeEach(function () {
  1113. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  1114. true
  1115. )
  1116. })
  1117. describe('previously joined token access user moving to named collaborator', function () {
  1118. beforeEach(function (done) {
  1119. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  1120. false
  1121. )
  1122. this.res.callback = done
  1123. this.TokenAccessController.moveReadWriteToCollaborators(
  1124. this.req,
  1125. this.res,
  1126. done
  1127. )
  1128. })
  1129. it('sets the privilege level to read and write for the invited viewer', function () {
  1130. expect(
  1131. this.TokenAccessHandler.promises.removeReadAndWriteUserFromProject
  1132. ).to.have.been.calledWith(this.user._id, this.project._id)
  1133. expect(
  1134. this.CollaboratorsHandler.promises.addUserIdToProject
  1135. ).to.have.been.calledWith(
  1136. this.project._id,
  1137. undefined,
  1138. this.user._id,
  1139. PrivilegeLevels.READ_AND_WRITE
  1140. )
  1141. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1142. })
  1143. })
  1144. })
  1145. describe('when there are no edit collaborator slots available', function () {
  1146. beforeEach(function () {
  1147. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  1148. false
  1149. )
  1150. })
  1151. describe('previously joined token access user moving to named collaborator', function () {
  1152. beforeEach(function (done) {
  1153. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  1154. false
  1155. )
  1156. this.res.callback = done
  1157. this.TokenAccessController.moveReadWriteToCollaborators(
  1158. this.req,
  1159. this.res,
  1160. done
  1161. )
  1162. })
  1163. it('sets the privilege level to read only for the invited viewer (pendingEditor)', function () {
  1164. expect(
  1165. this.TokenAccessHandler.promises.removeReadAndWriteUserFromProject
  1166. ).to.have.been.calledWith(this.user._id, this.project._id)
  1167. expect(
  1168. this.CollaboratorsHandler.promises.addUserIdToProject
  1169. ).to.have.been.calledWith(
  1170. this.project._id,
  1171. undefined,
  1172. this.user._id,
  1173. PrivilegeLevels.READ_ONLY,
  1174. { pendingEditor: true }
  1175. )
  1176. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1177. })
  1178. })
  1179. })
  1180. })
  1181. })
  1182. describe('moveReadWriteToReadOnly', function () {
  1183. beforeEach(function () {
  1184. this.req.params = { Project_id: this.project._id }
  1185. })
  1186. describe('previously joined token access user moving to anonymous viewer', function () {
  1187. beforeEach(function (done) {
  1188. this.res.callback = done
  1189. this.TokenAccessController.moveReadWriteToReadOnly(
  1190. this.req,
  1191. this.res,
  1192. done
  1193. )
  1194. })
  1195. it('removes them from read write token access refs and adds them to read only token access refs', function () {
  1196. expect(
  1197. this.TokenAccessHandler.promises.moveReadAndWriteUserToReadOnly
  1198. ).to.have.been.calledWith(this.user._id, this.project._id)
  1199. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1200. })
  1201. it('writes a project audit log', function () {
  1202. expect(
  1203. this.ProjectAuditLogHandler.promises.addEntry
  1204. ).to.have.been.calledWith(
  1205. this.project._id,
  1206. 'readonly-via-sharing-updates',
  1207. this.user._id,
  1208. this.req.ip
  1209. )
  1210. })
  1211. })
  1212. })
  1213. })