AuthorizationTests.mjs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 Features from '../../../app/src/infrastructure/Features.js'
  7. import expectErrorResponse from './helpers/expectErrorResponse.mjs'
  8. function tryReadAccess(user, projectId, test, callback) {
  9. async.series(
  10. [
  11. cb =>
  12. user.request.get(`/project/${projectId}`, (error, response, body) => {
  13. if (error != null) {
  14. return cb(error)
  15. }
  16. test(response, body)
  17. cb()
  18. }),
  19. cb =>
  20. user.request.get(
  21. `/project/${projectId}/download/zip`,
  22. (error, response, body) => {
  23. if (error != null) {
  24. return cb(error)
  25. }
  26. test(response, body)
  27. cb()
  28. }
  29. ),
  30. ],
  31. callback
  32. )
  33. }
  34. function tryRenameProjectAccess(user, projectId, test, callback) {
  35. user.request.post(
  36. {
  37. uri: `/project/${projectId}/settings`,
  38. json: {
  39. name: 'new name',
  40. },
  41. },
  42. (error, response, body) => {
  43. if (error != null) {
  44. return callback(error)
  45. }
  46. test(response, body)
  47. callback()
  48. }
  49. )
  50. }
  51. function trySettingsWriteAccess(user, projectId, test, callback) {
  52. async.series(
  53. [
  54. cb =>
  55. user.request.post(
  56. {
  57. uri: `/project/${projectId}/settings`,
  58. json: {
  59. compiler: 'latex',
  60. },
  61. },
  62. (error, response, body) => {
  63. if (error != null) {
  64. return cb(error)
  65. }
  66. test(response, body)
  67. cb()
  68. }
  69. ),
  70. ],
  71. callback
  72. )
  73. }
  74. function tryProjectAdminAccess(user, projectId, test, callback) {
  75. async.series(
  76. [
  77. cb =>
  78. user.request.post(
  79. {
  80. uri: `/project/${projectId}/rename`,
  81. json: {
  82. newProjectName: 'new-name',
  83. },
  84. },
  85. (error, response, body) => {
  86. if (error != null) {
  87. return cb(error)
  88. }
  89. test(response, body)
  90. cb()
  91. }
  92. ),
  93. cb =>
  94. user.request.post(
  95. {
  96. uri: `/project/${projectId}/settings/admin`,
  97. json: {
  98. publicAccessLevel: 'private',
  99. },
  100. },
  101. (error, response, body) => {
  102. if (error != null) {
  103. return cb(error)
  104. }
  105. test(response, body)
  106. cb()
  107. }
  108. ),
  109. ],
  110. callback
  111. )
  112. }
  113. function tryAdminAccess(user, test, callback) {
  114. async.series(
  115. [
  116. cb =>
  117. user.request.get(
  118. {
  119. uri: '/admin',
  120. },
  121. (error, response, body) => {
  122. if (error != null) {
  123. return cb(error)
  124. }
  125. test(response, body)
  126. cb()
  127. }
  128. ),
  129. cb => {
  130. if (!Features.hasFeature('saas')) {
  131. return cb()
  132. }
  133. user.request.get(
  134. {
  135. uri: `/admin/user/${user._id}`,
  136. },
  137. (error, response, body) => {
  138. if (error != null) {
  139. return cb(error)
  140. }
  141. test(response, body)
  142. cb()
  143. }
  144. )
  145. },
  146. ],
  147. callback
  148. )
  149. }
  150. function tryContentAccess(user, projectId, test, callback) {
  151. // The real-time service calls this end point to determine the user's
  152. // permissions.
  153. let userId
  154. if (user.id != null) {
  155. userId = user.id
  156. } else {
  157. userId = 'anonymous-user'
  158. }
  159. request.post(
  160. {
  161. url: `/project/${projectId}/join`,
  162. auth: {
  163. user: settings.apis.web.user,
  164. pass: settings.apis.web.pass,
  165. sendImmediately: true,
  166. },
  167. json: { userId },
  168. jar: false,
  169. },
  170. (error, response, body) => {
  171. if (error != null) {
  172. return callback(error)
  173. }
  174. test(response, body)
  175. callback()
  176. }
  177. )
  178. }
  179. function expectAdminAccess(user, callback) {
  180. tryAdminAccess(
  181. user,
  182. response => expect(response.statusCode).to.be.oneOf([200, 204]),
  183. callback
  184. )
  185. }
  186. function expectRedirectedAdminAccess(user, callback) {
  187. tryAdminAccess(
  188. user,
  189. response => {
  190. expect(response.statusCode).to.equal(302)
  191. expect(response.headers.location).to.equal(
  192. settings.adminUrl + response.request.uri.pathname
  193. )
  194. },
  195. callback
  196. )
  197. }
  198. function expectReadAccess(user, projectId, callback) {
  199. async.series(
  200. [
  201. cb =>
  202. tryReadAccess(
  203. user,
  204. projectId,
  205. (response, body) =>
  206. expect(response.statusCode).to.be.oneOf([200, 204]),
  207. cb
  208. ),
  209. cb =>
  210. tryContentAccess(
  211. user,
  212. projectId,
  213. (response, body) =>
  214. expect(body.privilegeLevel).to.be.oneOf([
  215. 'owner',
  216. 'readAndWrite',
  217. 'readOnly',
  218. ]),
  219. cb
  220. ),
  221. ],
  222. callback
  223. )
  224. }
  225. function expectContentWriteAccess(user, projectId, callback) {
  226. tryContentAccess(
  227. user,
  228. projectId,
  229. (response, body) =>
  230. expect(body.privilegeLevel).to.be.oneOf(['owner', 'readAndWrite']),
  231. callback
  232. )
  233. }
  234. function expectRenameProjectAccess(user, projectId, callback) {
  235. tryRenameProjectAccess(
  236. user,
  237. projectId,
  238. (response, body) => {
  239. expect(response.statusCode).to.be.oneOf([200, 204])
  240. },
  241. callback
  242. )
  243. }
  244. function expectSettingsWriteAccess(user, projectId, callback) {
  245. trySettingsWriteAccess(
  246. user,
  247. projectId,
  248. (response, body) => expect(response.statusCode).to.be.oneOf([200, 204]),
  249. callback
  250. )
  251. }
  252. function expectProjectAdminAccess(user, projectId, callback) {
  253. tryProjectAdminAccess(
  254. user,
  255. projectId,
  256. (response, body) => expect(response.statusCode).to.be.oneOf([200, 204]),
  257. callback
  258. )
  259. }
  260. function expectNoReadAccess(user, projectId, callback) {
  261. async.series(
  262. [
  263. cb =>
  264. tryReadAccess(user, projectId, expectErrorResponse.restricted.html, cb),
  265. cb =>
  266. tryContentAccess(
  267. user,
  268. projectId,
  269. (response, body) => {
  270. expect(response.statusCode).to.equal(403)
  271. expect(body).to.equal('Forbidden')
  272. },
  273. cb
  274. ),
  275. ],
  276. callback
  277. )
  278. }
  279. function expectNoContentWriteAccess(user, projectId, callback) {
  280. tryContentAccess(
  281. user,
  282. projectId,
  283. (response, body) =>
  284. expect(body.privilegeLevel).to.be.oneOf([undefined, null, 'readOnly']),
  285. callback
  286. )
  287. }
  288. function expectNoSettingsWriteAccess(user, projectId, callback) {
  289. trySettingsWriteAccess(
  290. user,
  291. projectId,
  292. expectErrorResponse.restricted.json,
  293. callback
  294. )
  295. }
  296. function expectNoRenameProjectAccess(user, projectId, callback) {
  297. tryRenameProjectAccess(
  298. user,
  299. projectId,
  300. expectErrorResponse.restricted.json,
  301. callback
  302. )
  303. }
  304. function expectNoProjectAdminAccess(user, projectId, callback) {
  305. tryProjectAdminAccess(
  306. user,
  307. projectId,
  308. (response, body) => {
  309. expect(response.statusCode).to.equal(403)
  310. },
  311. callback
  312. )
  313. }
  314. function expectNoAnonymousProjectAdminAccess(user, projectId, callback) {
  315. tryProjectAdminAccess(
  316. user,
  317. projectId,
  318. expectErrorResponse.requireLogin.json,
  319. callback
  320. )
  321. }
  322. function expectChatAccess(user, projectId, callback) {
  323. user.request.get(
  324. {
  325. url: `/project/${projectId}/messages`,
  326. json: true,
  327. },
  328. (error, response) => {
  329. if (error != null) {
  330. return callback(error)
  331. }
  332. expect(response.statusCode).to.equal(200)
  333. callback()
  334. }
  335. )
  336. }
  337. function expectNoChatAccess(user, projectId, callback) {
  338. user.request.get(
  339. {
  340. url: `/project/${projectId}/messages`,
  341. json: true,
  342. },
  343. (error, response) => {
  344. if (error != null) {
  345. return callback(error)
  346. }
  347. expect(response.statusCode).to.equal(403)
  348. callback()
  349. }
  350. )
  351. }
  352. describe('Authorization', function () {
  353. beforeEach(function (done) {
  354. this.timeout(90000)
  355. this.owner = new User()
  356. this.other1 = new User()
  357. this.other2 = new User()
  358. this.anon = new User()
  359. this.site_admin = new User({ email: 'admin@example.com' })
  360. async.parallel(
  361. [
  362. cb => this.owner.login(cb),
  363. cb => this.other1.login(cb),
  364. cb => this.other2.login(cb),
  365. cb => this.anon.getCsrfToken(cb),
  366. cb => {
  367. this.site_admin.ensureUserExists(err => {
  368. if (err) return cb(err)
  369. this.site_admin.ensureAdmin(err => {
  370. if (err != null) {
  371. return cb(err)
  372. }
  373. return this.site_admin.login(cb)
  374. })
  375. })
  376. },
  377. ],
  378. done
  379. )
  380. })
  381. describe('private project', function () {
  382. beforeEach(function (done) {
  383. this.owner.createProject('private-project', (error, projectId) => {
  384. if (error != null) {
  385. return done(error)
  386. }
  387. this.projectId = projectId
  388. done()
  389. })
  390. })
  391. it('should allow the owner read access to it', function (done) {
  392. expectReadAccess(this.owner, this.projectId, done)
  393. })
  394. it('should allow the owner write access to its content', function (done) {
  395. expectContentWriteAccess(this.owner, this.projectId, done)
  396. })
  397. it('should allow the owner write access to its settings', function (done) {
  398. expectSettingsWriteAccess(this.owner, this.projectId, done)
  399. })
  400. it('should allow the owner to rename the project', function (done) {
  401. expectRenameProjectAccess(this.owner, this.projectId, done)
  402. })
  403. it('should allow the owner project admin access to it', function (done) {
  404. expectProjectAdminAccess(this.owner, this.projectId, done)
  405. })
  406. it('should allow the owner user chat messages access', function (done) {
  407. expectChatAccess(this.owner, this.projectId, done)
  408. })
  409. it('should not allow another user read access to the project', function (done) {
  410. expectNoReadAccess(this.other1, this.projectId, done)
  411. })
  412. it('should not allow another user write access to its content', function (done) {
  413. expectNoContentWriteAccess(this.other1, this.projectId, done)
  414. })
  415. it('should not allow another user write access to its settings', function (done) {
  416. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  417. })
  418. it('should not allow another user to rename the project', function (done) {
  419. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  420. })
  421. it('should not allow another user project admin access to it', function (done) {
  422. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  423. })
  424. it('should not allow another user chat messages access', function (done) {
  425. expectNoChatAccess(this.other1, this.projectId, done)
  426. })
  427. it('should not allow anonymous user read access to it', function (done) {
  428. expectNoReadAccess(this.anon, this.projectId, done)
  429. })
  430. it('should not allow anonymous user write access to its content', function (done) {
  431. expectNoContentWriteAccess(this.anon, this.projectId, done)
  432. })
  433. it('should not allow anonymous user write access to its settings', function (done) {
  434. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  435. })
  436. it('should not allow anonymous user to rename the project', function (done) {
  437. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  438. })
  439. it('should not allow anonymous user project admin access to it', function (done) {
  440. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  441. })
  442. it('should not allow anonymous user chat messages access', function (done) {
  443. expectNoChatAccess(this.anon, this.projectId, done)
  444. })
  445. describe('with admin privilege available', function () {
  446. beforeEach(function () {
  447. settings.adminPrivilegeAvailable = true
  448. })
  449. it('should allow site admin users read access to it', function (done) {
  450. expectReadAccess(this.site_admin, this.projectId, done)
  451. })
  452. it('should allow site admin users write access to its content', function (done) {
  453. expectContentWriteAccess(this.site_admin, this.projectId, done)
  454. })
  455. it('should allow site admin users write access to its settings', function (done) {
  456. expectSettingsWriteAccess(this.site_admin, this.projectId, done)
  457. })
  458. it('should allow site admin users to rename the project', function (done) {
  459. expectRenameProjectAccess(this.site_admin, this.projectId, done)
  460. })
  461. it('should allow site admin users project admin access to it', function (done) {
  462. expectProjectAdminAccess(this.site_admin, this.projectId, done)
  463. })
  464. it('should allow site admin users site admin access to site admin endpoints', function (done) {
  465. expectAdminAccess(this.site_admin, done)
  466. })
  467. })
  468. describe('with admin privilege unavailable', function () {
  469. beforeEach(function () {
  470. settings.adminPrivilegeAvailable = false
  471. })
  472. afterEach(function () {
  473. settings.adminPrivilegeAvailable = true
  474. })
  475. it('should not allow site admin users read access to it', function (done) {
  476. expectNoReadAccess(this.site_admin, this.projectId, done)
  477. })
  478. it('should not allow site admin users write access to its content', function (done) {
  479. expectNoContentWriteAccess(this.site_admin, this.projectId, done)
  480. })
  481. it('should not allow site admin users write access to its settings', function (done) {
  482. expectNoSettingsWriteAccess(this.site_admin, this.projectId, done)
  483. })
  484. it('should not allow site admin users to rename the project', function (done) {
  485. expectNoRenameProjectAccess(this.site_admin, this.projectId, done)
  486. })
  487. it('should not allow site admin users project admin access to it', function (done) {
  488. expectNoProjectAdminAccess(this.site_admin, this.projectId, done)
  489. })
  490. it('should redirect site admin users when accessing site admin endpoints', function (done) {
  491. expectRedirectedAdminAccess(this.site_admin, done)
  492. })
  493. })
  494. })
  495. describe('shared project', function () {
  496. beforeEach(function (done) {
  497. this.rw_user = this.other1
  498. this.ro_user = this.other2
  499. this.owner.createProject('private-project', (error, projectId) => {
  500. if (error != null) {
  501. return done(error)
  502. }
  503. this.projectId = projectId
  504. this.owner.addUserToProject(
  505. this.projectId,
  506. this.ro_user,
  507. 'readOnly',
  508. error => {
  509. if (error != null) {
  510. return done(error)
  511. }
  512. this.owner.addUserToProject(
  513. this.projectId,
  514. this.rw_user,
  515. 'readAndWrite',
  516. error => {
  517. if (error != null) {
  518. return done(error)
  519. }
  520. done()
  521. }
  522. )
  523. }
  524. )
  525. })
  526. })
  527. it('should allow the read-only user read access to it', function (done) {
  528. expectReadAccess(this.ro_user, this.projectId, done)
  529. })
  530. it('should allow the read-only user chat messages access', function (done) {
  531. expectChatAccess(this.ro_user, this.projectId, done)
  532. })
  533. it('should not allow the read-only user write access to its content', function (done) {
  534. expectNoContentWriteAccess(this.ro_user, this.projectId, done)
  535. })
  536. it('should not allow the read-only user write access to its settings', function (done) {
  537. expectNoSettingsWriteAccess(this.ro_user, this.projectId, done)
  538. })
  539. it('should not allow the read-only user to rename the project', function (done) {
  540. expectNoRenameProjectAccess(this.ro_user, this.projectId, done)
  541. })
  542. it('should not allow the read-only user project admin access to it', function (done) {
  543. expectNoProjectAdminAccess(this.ro_user, this.projectId, done)
  544. })
  545. it('should allow the read-write user read access to it', function (done) {
  546. expectReadAccess(this.rw_user, this.projectId, done)
  547. })
  548. it('should allow the read-write user write access to its content', function (done) {
  549. expectContentWriteAccess(this.rw_user, this.projectId, done)
  550. })
  551. it('should allow the read-write user write access to its settings', function (done) {
  552. expectSettingsWriteAccess(this.rw_user, this.projectId, done)
  553. })
  554. it('should not allow the read-write user to rename the project', function (done) {
  555. expectNoRenameProjectAccess(this.rw_user, this.projectId, done)
  556. })
  557. it('should not allow the read-write user project admin access to it', function (done) {
  558. expectNoProjectAdminAccess(this.rw_user, this.projectId, done)
  559. })
  560. it('should allow the read-write user chat messages access', function (done) {
  561. expectChatAccess(this.rw_user, this.projectId, done)
  562. })
  563. })
  564. describe('public read-write project', function () {
  565. /**
  566. * Note: this is a test for the legacy "public access" feature.
  567. * See documentation comment in `Authorization/PublicAccessLevels`
  568. * */
  569. beforeEach(function (done) {
  570. this.owner.createProject('public-rw-project', (error, projectId) => {
  571. if (error != null) {
  572. return done(error)
  573. }
  574. this.projectId = projectId
  575. this.owner.makePublic(this.projectId, 'readAndWrite', done)
  576. })
  577. })
  578. it('should allow a user read access to it', function (done) {
  579. expectReadAccess(this.other1, this.projectId, done)
  580. })
  581. it('should allow a user write access to its content', function (done) {
  582. expectContentWriteAccess(this.other1, this.projectId, done)
  583. })
  584. it('should allow a user chat messages access', function (done) {
  585. expectChatAccess(this.other1, this.projectId, done)
  586. })
  587. it('should not allow a user write access to its settings', function (done) {
  588. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  589. })
  590. it('should not allow a user to rename the project', function (done) {
  591. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  592. })
  593. it('should not allow a user project admin access to it', function (done) {
  594. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  595. })
  596. it('should allow an anonymous user read access to it', function (done) {
  597. expectReadAccess(this.anon, this.projectId, done)
  598. })
  599. it('should allow an anonymous user write access to its content', function (done) {
  600. expectContentWriteAccess(this.anon, this.projectId, done)
  601. })
  602. it('should allow an anonymous user chat messages access', function (done) {
  603. // chat access for anonymous users is a CE/SP-only feature, although currently broken
  604. // https://github.com/overleaf/internal/issues/10944
  605. if (Features.hasFeature('saas')) {
  606. this.skip()
  607. }
  608. expectChatAccess(this.anon, this.projectId, done)
  609. })
  610. it('should not allow an anonymous user write access to its settings', function (done) {
  611. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  612. })
  613. it('should not allow an anonymous user to rename the project', function (done) {
  614. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  615. })
  616. it('should not allow an anonymous user project admin access to it', function (done) {
  617. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  618. })
  619. })
  620. describe('public read-only project', function () {
  621. /**
  622. * Note: this is a test for the legacy "public access" feature.
  623. * See documentation comment in `Authorization/PublicAccessLevels`
  624. * */
  625. beforeEach(function (done) {
  626. this.owner.createProject('public-ro-project', (error, projectId) => {
  627. if (error != null) {
  628. return done(error)
  629. }
  630. this.projectId = projectId
  631. this.owner.makePublic(this.projectId, 'readOnly', done)
  632. })
  633. })
  634. it('should allow a user read access to it', function (done) {
  635. expectReadAccess(this.other1, this.projectId, done)
  636. })
  637. it('should not allow a user write access to its content', function (done) {
  638. expectNoContentWriteAccess(this.other1, this.projectId, done)
  639. })
  640. it('should not allow a user write access to its settings', function (done) {
  641. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  642. })
  643. it('should not allow a user to rename the project', function (done) {
  644. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  645. })
  646. it('should not allow a user project admin access to it', function (done) {
  647. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  648. })
  649. // NOTE: legacy readOnly access does not count as 'restricted' in the new model
  650. it('should allow a user chat messages access', function (done) {
  651. expectChatAccess(this.other1, this.projectId, done)
  652. })
  653. it('should allow an anonymous user read access to it', function (done) {
  654. expectReadAccess(this.anon, this.projectId, done)
  655. })
  656. it('should not allow an anonymous user write access to its content', function (done) {
  657. expectNoContentWriteAccess(this.anon, this.projectId, done)
  658. })
  659. it('should not allow an anonymous user write access to its settings', function (done) {
  660. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  661. })
  662. it('should not allow an anonymous user to rename the project', function (done) {
  663. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  664. })
  665. it('should not allow an anonymous user project admin access to it', function (done) {
  666. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  667. })
  668. it('should not allow an anonymous user chat messages access', function (done) {
  669. expectNoChatAccess(this.anon, this.projectId, done)
  670. })
  671. })
  672. })