AuthorizationTests.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. const { expect } = require('chai')
  2. const async = require('async')
  3. const User = require('./helpers/User')
  4. const request = require('./helpers/request')
  5. const settings = require('@overleaf/settings')
  6. const Features = require('../../../app/src/infrastructure/Features')
  7. const expectErrorResponse = require('./helpers/expectErrorResponse')
  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. qs: { user_id: userId },
  163. auth: {
  164. user: settings.apis.web.user,
  165. pass: settings.apis.web.pass,
  166. sendImmediately: true,
  167. },
  168. json: true,
  169. jar: false,
  170. },
  171. (error, response, body) => {
  172. if (error != null) {
  173. return callback(error)
  174. }
  175. test(response, body)
  176. callback()
  177. }
  178. )
  179. }
  180. function expectAdminAccess(user, callback) {
  181. tryAdminAccess(
  182. user,
  183. response => expect(response.statusCode).to.be.oneOf([200, 204]),
  184. callback
  185. )
  186. }
  187. function expectRedirectedAdminAccess(user, callback) {
  188. tryAdminAccess(
  189. user,
  190. response => {
  191. expect(response.statusCode).to.equal(302)
  192. expect(response.headers.location).to.equal(
  193. settings.adminUrl + response.request.uri.pathname
  194. )
  195. },
  196. callback
  197. )
  198. }
  199. function expectReadAccess(user, projectId, callback) {
  200. async.series(
  201. [
  202. cb =>
  203. tryReadAccess(
  204. user,
  205. projectId,
  206. (response, body) =>
  207. expect(response.statusCode).to.be.oneOf([200, 204]),
  208. cb
  209. ),
  210. cb =>
  211. tryContentAccess(
  212. user,
  213. projectId,
  214. (response, body) =>
  215. expect(body.privilegeLevel).to.be.oneOf([
  216. 'owner',
  217. 'readAndWrite',
  218. 'readOnly',
  219. ]),
  220. cb
  221. ),
  222. ],
  223. callback
  224. )
  225. }
  226. function expectContentWriteAccess(user, projectId, callback) {
  227. tryContentAccess(
  228. user,
  229. projectId,
  230. (response, body) =>
  231. expect(body.privilegeLevel).to.be.oneOf(['owner', 'readAndWrite']),
  232. callback
  233. )
  234. }
  235. function expectRenameProjectAccess(user, projectId, callback) {
  236. tryRenameProjectAccess(
  237. user,
  238. projectId,
  239. (response, body) => {
  240. expect(response.statusCode).to.be.oneOf([200, 204])
  241. },
  242. callback
  243. )
  244. }
  245. function expectSettingsWriteAccess(user, projectId, callback) {
  246. trySettingsWriteAccess(
  247. user,
  248. projectId,
  249. (response, body) => expect(response.statusCode).to.be.oneOf([200, 204]),
  250. callback
  251. )
  252. }
  253. function expectProjectAdminAccess(user, projectId, callback) {
  254. tryProjectAdminAccess(
  255. user,
  256. projectId,
  257. (response, body) => expect(response.statusCode).to.be.oneOf([200, 204]),
  258. callback
  259. )
  260. }
  261. function expectNoReadAccess(user, projectId, callback) {
  262. async.series(
  263. [
  264. cb =>
  265. tryReadAccess(user, projectId, expectErrorResponse.restricted.html, cb),
  266. cb =>
  267. tryContentAccess(
  268. user,
  269. projectId,
  270. (response, body) => {
  271. expect(response.statusCode).to.equal(403)
  272. expect(body).to.equal('Forbidden')
  273. },
  274. cb
  275. ),
  276. ],
  277. callback
  278. )
  279. }
  280. function expectNoContentWriteAccess(user, projectId, callback) {
  281. tryContentAccess(
  282. user,
  283. projectId,
  284. (response, body) =>
  285. expect(body.privilegeLevel).to.be.oneOf([undefined, null, 'readOnly']),
  286. callback
  287. )
  288. }
  289. function expectNoSettingsWriteAccess(user, projectId, callback) {
  290. trySettingsWriteAccess(
  291. user,
  292. projectId,
  293. expectErrorResponse.restricted.json,
  294. callback
  295. )
  296. }
  297. function expectNoRenameProjectAccess(user, projectId, callback) {
  298. tryRenameProjectAccess(
  299. user,
  300. projectId,
  301. expectErrorResponse.restricted.json,
  302. callback
  303. )
  304. }
  305. function expectNoProjectAdminAccess(user, projectId, callback) {
  306. tryProjectAdminAccess(
  307. user,
  308. projectId,
  309. (response, body) => {
  310. expect(response.statusCode).to.equal(403)
  311. },
  312. callback
  313. )
  314. }
  315. function expectNoAnonymousProjectAdminAccess(user, projectId, callback) {
  316. tryProjectAdminAccess(
  317. user,
  318. projectId,
  319. expectErrorResponse.requireLogin.json,
  320. callback
  321. )
  322. }
  323. function expectChatAccess(user, projectId, callback) {
  324. user.request.get(
  325. {
  326. url: `/project/${projectId}/messages`,
  327. json: true,
  328. },
  329. (error, response) => {
  330. if (error != null) {
  331. return callback(error)
  332. }
  333. expect(response.statusCode).to.equal(200)
  334. callback()
  335. }
  336. )
  337. }
  338. function expectNoChatAccess(user, projectId, callback) {
  339. user.request.get(
  340. {
  341. url: `/project/${projectId}/messages`,
  342. json: true,
  343. },
  344. (error, response) => {
  345. if (error != null) {
  346. return callback(error)
  347. }
  348. expect(response.statusCode).to.equal(403)
  349. callback()
  350. }
  351. )
  352. }
  353. describe('Authorization', function () {
  354. beforeEach(function (done) {
  355. this.timeout(90000)
  356. this.owner = new User()
  357. this.other1 = new User()
  358. this.other2 = new User()
  359. this.anon = new User()
  360. this.site_admin = new User({ email: 'admin@example.com' })
  361. async.parallel(
  362. [
  363. cb => this.owner.login(cb),
  364. cb => this.other1.login(cb),
  365. cb => this.other2.login(cb),
  366. cb => this.anon.getCsrfToken(cb),
  367. cb => {
  368. this.site_admin.ensureUserExists(err => {
  369. if (err) return cb(err)
  370. this.site_admin.ensureAdmin(err => {
  371. if (err != null) {
  372. return cb(err)
  373. }
  374. return this.site_admin.login(cb)
  375. })
  376. })
  377. },
  378. ],
  379. done
  380. )
  381. })
  382. describe('private project', function () {
  383. beforeEach(function (done) {
  384. this.owner.createProject('private-project', (error, projectId) => {
  385. if (error != null) {
  386. return done(error)
  387. }
  388. this.projectId = projectId
  389. done()
  390. })
  391. })
  392. it('should allow the owner read access to it', function (done) {
  393. expectReadAccess(this.owner, this.projectId, done)
  394. })
  395. it('should allow the owner write access to its content', function (done) {
  396. expectContentWriteAccess(this.owner, this.projectId, done)
  397. })
  398. it('should allow the owner write access to its settings', function (done) {
  399. expectSettingsWriteAccess(this.owner, this.projectId, done)
  400. })
  401. it('should allow the owner to rename the project', function (done) {
  402. expectRenameProjectAccess(this.owner, this.projectId, done)
  403. })
  404. it('should allow the owner project admin access to it', function (done) {
  405. expectProjectAdminAccess(this.owner, this.projectId, done)
  406. })
  407. it('should allow the owner user chat messages access', function (done) {
  408. expectChatAccess(this.owner, this.projectId, done)
  409. })
  410. it('should not allow another user read access to the project', function (done) {
  411. expectNoReadAccess(this.other1, this.projectId, done)
  412. })
  413. it('should not allow another user write access to its content', function (done) {
  414. expectNoContentWriteAccess(this.other1, this.projectId, done)
  415. })
  416. it('should not allow another user write access to its settings', function (done) {
  417. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  418. })
  419. it('should not allow another user to rename the project', function (done) {
  420. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  421. })
  422. it('should not allow another user project admin access to it', function (done) {
  423. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  424. })
  425. it('should not allow another user chat messages access', function (done) {
  426. expectNoChatAccess(this.other1, this.projectId, done)
  427. })
  428. it('should not allow anonymous user read access to it', function (done) {
  429. expectNoReadAccess(this.anon, this.projectId, done)
  430. })
  431. it('should not allow anonymous user write access to its content', function (done) {
  432. expectNoContentWriteAccess(this.anon, this.projectId, done)
  433. })
  434. it('should not allow anonymous user write access to its settings', function (done) {
  435. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  436. })
  437. it('should not allow anonymous user to rename the project', function (done) {
  438. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  439. })
  440. it('should not allow anonymous user project admin access to it', function (done) {
  441. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  442. })
  443. it('should not allow anonymous user chat messages access', function (done) {
  444. expectNoChatAccess(this.anon, this.projectId, done)
  445. })
  446. describe('with admin privilege available', function () {
  447. beforeEach(function () {
  448. settings.adminPrivilegeAvailable = true
  449. })
  450. it('should allow site admin users read access to it', function (done) {
  451. expectReadAccess(this.site_admin, this.projectId, done)
  452. })
  453. it('should allow site admin users write access to its content', function (done) {
  454. expectContentWriteAccess(this.site_admin, this.projectId, done)
  455. })
  456. it('should allow site admin users write access to its settings', function (done) {
  457. expectSettingsWriteAccess(this.site_admin, this.projectId, done)
  458. })
  459. it('should allow site admin users to rename the project', function (done) {
  460. expectRenameProjectAccess(this.site_admin, this.projectId, done)
  461. })
  462. it('should allow site admin users project admin access to it', function (done) {
  463. expectProjectAdminAccess(this.site_admin, this.projectId, done)
  464. })
  465. it('should allow site admin users site admin access to site admin endpoints', function (done) {
  466. expectAdminAccess(this.site_admin, done)
  467. })
  468. })
  469. describe('with admin privilege unavailable', function () {
  470. beforeEach(function () {
  471. settings.adminPrivilegeAvailable = false
  472. })
  473. afterEach(function () {
  474. settings.adminPrivilegeAvailable = true
  475. })
  476. it('should not allow site admin users read access to it', function (done) {
  477. expectNoReadAccess(this.site_admin, this.projectId, done)
  478. })
  479. it('should not allow site admin users write access to its content', function (done) {
  480. expectNoContentWriteAccess(this.site_admin, this.projectId, done)
  481. })
  482. it('should not allow site admin users write access to its settings', function (done) {
  483. expectNoSettingsWriteAccess(this.site_admin, this.projectId, done)
  484. })
  485. it('should not allow site admin users to rename the project', function (done) {
  486. expectNoRenameProjectAccess(this.site_admin, this.projectId, done)
  487. })
  488. it('should not allow site admin users project admin access to it', function (done) {
  489. expectNoProjectAdminAccess(this.site_admin, this.projectId, done)
  490. })
  491. it('should redirect site admin users when accessing site admin endpoints', function (done) {
  492. expectRedirectedAdminAccess(this.site_admin, done)
  493. })
  494. })
  495. })
  496. describe('shared project', function () {
  497. beforeEach(function (done) {
  498. this.rw_user = this.other1
  499. this.ro_user = this.other2
  500. this.owner.createProject('private-project', (error, projectId) => {
  501. if (error != null) {
  502. return done(error)
  503. }
  504. this.projectId = projectId
  505. this.owner.addUserToProject(
  506. this.projectId,
  507. this.ro_user,
  508. 'readOnly',
  509. error => {
  510. if (error != null) {
  511. return done(error)
  512. }
  513. this.owner.addUserToProject(
  514. this.projectId,
  515. this.rw_user,
  516. 'readAndWrite',
  517. error => {
  518. if (error != null) {
  519. return done(error)
  520. }
  521. done()
  522. }
  523. )
  524. }
  525. )
  526. })
  527. })
  528. it('should allow the read-only user read access to it', function (done) {
  529. expectReadAccess(this.ro_user, this.projectId, done)
  530. })
  531. it('should allow the read-only user chat messages access', function (done) {
  532. expectChatAccess(this.ro_user, this.projectId, done)
  533. })
  534. it('should not allow the read-only user write access to its content', function (done) {
  535. expectNoContentWriteAccess(this.ro_user, this.projectId, done)
  536. })
  537. it('should not allow the read-only user write access to its settings', function (done) {
  538. expectNoSettingsWriteAccess(this.ro_user, this.projectId, done)
  539. })
  540. it('should not allow the read-only user to rename the project', function (done) {
  541. expectNoRenameProjectAccess(this.ro_user, this.projectId, done)
  542. })
  543. it('should not allow the read-only user project admin access to it', function (done) {
  544. expectNoProjectAdminAccess(this.ro_user, this.projectId, done)
  545. })
  546. it('should allow the read-write user read access to it', function (done) {
  547. expectReadAccess(this.rw_user, this.projectId, done)
  548. })
  549. it('should allow the read-write user write access to its content', function (done) {
  550. expectContentWriteAccess(this.rw_user, this.projectId, done)
  551. })
  552. it('should allow the read-write user write access to its settings', function (done) {
  553. expectSettingsWriteAccess(this.rw_user, this.projectId, done)
  554. })
  555. it('should not allow the read-write user to rename the project', function (done) {
  556. expectNoRenameProjectAccess(this.rw_user, this.projectId, done)
  557. })
  558. it('should not allow the read-write user project admin access to it', function (done) {
  559. expectNoProjectAdminAccess(this.rw_user, this.projectId, done)
  560. })
  561. it('should allow the read-write user chat messages access', function (done) {
  562. expectChatAccess(this.rw_user, this.projectId, done)
  563. })
  564. })
  565. describe('public read-write project', function () {
  566. /**
  567. * Note: this is a test for the legacy "public access" feature.
  568. * See documentation comment in `Authorization/PublicAccessLevels`
  569. * */
  570. beforeEach(function (done) {
  571. this.owner.createProject('public-rw-project', (error, projectId) => {
  572. if (error != null) {
  573. return done(error)
  574. }
  575. this.projectId = projectId
  576. this.owner.makePublic(this.projectId, 'readAndWrite', done)
  577. })
  578. })
  579. it('should allow a user read access to it', function (done) {
  580. expectReadAccess(this.other1, this.projectId, done)
  581. })
  582. it('should allow a user write access to its content', function (done) {
  583. expectContentWriteAccess(this.other1, this.projectId, done)
  584. })
  585. it('should allow a user chat messages access', function (done) {
  586. expectChatAccess(this.other1, this.projectId, done)
  587. })
  588. it('should not allow a user write access to its settings', function (done) {
  589. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  590. })
  591. it('should not allow a user to rename the project', function (done) {
  592. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  593. })
  594. it('should not allow a user project admin access to it', function (done) {
  595. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  596. })
  597. it('should allow an anonymous user read access to it', function (done) {
  598. expectReadAccess(this.anon, this.projectId, done)
  599. })
  600. it('should allow an anonymous user write access to its content', function (done) {
  601. expectContentWriteAccess(this.anon, this.projectId, done)
  602. })
  603. it('should allow an anonymous user chat messages access', function (done) {
  604. expectChatAccess(this.anon, this.projectId, done)
  605. })
  606. it('should not allow an anonymous user write access to its settings', function (done) {
  607. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  608. })
  609. it('should not allow an anonymous user to rename the project', function (done) {
  610. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  611. })
  612. it('should not allow an anonymous user project admin access to it', function (done) {
  613. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  614. })
  615. })
  616. describe('public read-only project', function () {
  617. /**
  618. * Note: this is a test for the legacy "public access" feature.
  619. * See documentation comment in `Authorization/PublicAccessLevels`
  620. * */
  621. beforeEach(function (done) {
  622. this.owner.createProject('public-ro-project', (error, projectId) => {
  623. if (error != null) {
  624. return done(error)
  625. }
  626. this.projectId = projectId
  627. this.owner.makePublic(this.projectId, 'readOnly', done)
  628. })
  629. })
  630. it('should allow a user read access to it', function (done) {
  631. expectReadAccess(this.other1, this.projectId, done)
  632. })
  633. it('should not allow a user write access to its content', function (done) {
  634. expectNoContentWriteAccess(this.other1, this.projectId, done)
  635. })
  636. it('should not allow a user write access to its settings', function (done) {
  637. expectNoSettingsWriteAccess(this.other1, this.projectId, done)
  638. })
  639. it('should not allow a user to rename the project', function (done) {
  640. expectNoRenameProjectAccess(this.other1, this.projectId, done)
  641. })
  642. it('should not allow a user project admin access to it', function (done) {
  643. expectNoProjectAdminAccess(this.other1, this.projectId, done)
  644. })
  645. // NOTE: legacy readOnly access does not count as 'restricted' in the new model
  646. it('should allow a user chat messages access', function (done) {
  647. expectChatAccess(this.other1, this.projectId, done)
  648. })
  649. it('should allow an anonymous user read access to it', function (done) {
  650. expectReadAccess(this.anon, this.projectId, done)
  651. })
  652. it('should not allow an anonymous user write access to its content', function (done) {
  653. expectNoContentWriteAccess(this.anon, this.projectId, done)
  654. })
  655. it('should not allow an anonymous user write access to its settings', function (done) {
  656. expectNoSettingsWriteAccess(this.anon, this.projectId, done)
  657. })
  658. it('should not allow an anonymous user to rename the project', function (done) {
  659. expectNoRenameProjectAccess(this.anon, this.projectId, done)
  660. })
  661. it('should not allow an anonymous user project admin access to it', function (done) {
  662. expectNoAnonymousProjectAdminAccess(this.anon, this.projectId, done)
  663. })
  664. it('should not allow an anonymous user chat messages access', function (done) {
  665. expectNoChatAccess(this.anon, this.projectId, done)
  666. })
  667. })
  668. })