AuthorizationMiddlewareTests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const SandboxedModule = require('sandboxed-module')
  4. const HttpErrors = require('@overleaf/o-error/http')
  5. const Errors = require('../../../../app/src/Features/Errors/Errors.js')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/Authorization/AuthorizationMiddleware.js'
  8. describe('AuthorizationMiddleware', function() {
  9. beforeEach(function() {
  10. this.userId = 'user-id-123'
  11. this.project_id = 'project-id-123'
  12. this.token = 'some-token'
  13. this.AuthenticationController = {
  14. getLoggedInUserId: sinon.stub().returns(this.userId),
  15. isUserLoggedIn: sinon.stub().returns(true)
  16. }
  17. this.AuthorizationManager = {}
  18. this.TokenAccessHandler = {
  19. getRequestToken: sinon.stub().returns(this.token)
  20. }
  21. this.ObjectId = {
  22. isValid: sinon
  23. .stub()
  24. .withArgs(this.project_id)
  25. .returns(true)
  26. }
  27. this.AuthorizationManager = {}
  28. this.AuthorizationMiddleware = SandboxedModule.require(MODULE_PATH, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. './AuthorizationManager': this.AuthorizationManager,
  34. 'logger-sharelatex': { log() {} },
  35. mongojs: {
  36. ObjectId: this.ObjectId
  37. },
  38. '@overleaf/o-error/http': HttpErrors,
  39. '../Errors/Errors': Errors,
  40. '../Authentication/AuthenticationController': this
  41. .AuthenticationController,
  42. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler
  43. }
  44. })
  45. this.req = {}
  46. this.res = {}
  47. this.next = sinon.stub()
  48. })
  49. describe('_getUserId', function() {
  50. beforeEach(function() {
  51. this.req = {}
  52. })
  53. it('should get the user from session', function(done) {
  54. this.AuthenticationController.getLoggedInUserId = sinon
  55. .stub()
  56. .returns('1234')
  57. this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
  58. expect(err).to.not.exist
  59. expect(userId).to.equal('1234')
  60. done()
  61. })
  62. })
  63. it('should get oauth_user from request', function(done) {
  64. this.AuthenticationController.getLoggedInUserId = sinon
  65. .stub()
  66. .returns(null)
  67. this.req.oauth_user = { _id: '5678' }
  68. this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
  69. expect(err).to.not.exist
  70. expect(userId).to.equal('5678')
  71. done()
  72. })
  73. })
  74. it('should fall back to null', function(done) {
  75. this.AuthenticationController.getLoggedInUserId = sinon
  76. .stub()
  77. .returns(null)
  78. this.req.oauth_user = undefined
  79. this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
  80. expect(err).to.not.exist
  81. expect(userId).to.equal(null)
  82. done()
  83. })
  84. })
  85. })
  86. const METHODS_TO_TEST = {
  87. ensureUserCanReadProject: 'canUserReadProject',
  88. ensureUserCanWriteProjectSettings: 'canUserWriteProjectSettings',
  89. ensureUserCanWriteProjectContent: 'canUserWriteProjectContent'
  90. }
  91. Object.entries(METHODS_TO_TEST).forEach(
  92. ([middlewareMethod, managerMethod]) => {
  93. describe(middlewareMethod, function() {
  94. beforeEach(function() {
  95. this.req.params = { project_id: this.project_id }
  96. this.AuthorizationManager[managerMethod] = sinon.stub()
  97. this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
  98. })
  99. describe('with missing project_id', function() {
  100. beforeEach(function() {
  101. this.req.params = {}
  102. })
  103. it('should return an error to next', function() {
  104. this.AuthorizationMiddleware[middlewareMethod](
  105. this.req,
  106. this.res,
  107. this.next
  108. )
  109. this.next
  110. .calledWith(sinon.match.instanceOf(Error))
  111. .should.equal(true)
  112. })
  113. })
  114. describe('with logged in user', function() {
  115. beforeEach(function() {
  116. this.AuthenticationController.getLoggedInUserId.returns(this.userId)
  117. })
  118. describe('when user has permission', function() {
  119. beforeEach(function() {
  120. this.AuthorizationManager[managerMethod]
  121. .withArgs(this.userId, this.project_id, this.token)
  122. .yields(null, true)
  123. })
  124. it('should return next', function() {
  125. this.AuthorizationMiddleware[middlewareMethod](
  126. this.req,
  127. this.res,
  128. this.next
  129. )
  130. this.next.called.should.equal(true)
  131. })
  132. })
  133. describe("when user doesn't have permission", function() {
  134. beforeEach(function() {
  135. this.AuthorizationManager[managerMethod]
  136. .withArgs(this.userId, this.project_id, this.token)
  137. .yields(null, false)
  138. })
  139. it('should raise a 403', function() {
  140. this.AuthorizationMiddleware[middlewareMethod](
  141. this.req,
  142. this.res,
  143. this.next
  144. )
  145. this.next.called.should.equal(false)
  146. this.AuthorizationMiddleware.redirectToRestricted
  147. .calledWith(this.req, this.res, this.next)
  148. .should.equal(true)
  149. })
  150. })
  151. })
  152. describe('with anonymous user', function() {
  153. describe('when user has permission', function() {
  154. beforeEach(function() {
  155. this.AuthenticationController.getLoggedInUserId.returns(null)
  156. this.AuthorizationManager[managerMethod]
  157. .withArgs(null, this.project_id, this.token)
  158. .yields(null, true)
  159. })
  160. it('should return next', function() {
  161. this.AuthorizationMiddleware[middlewareMethod](
  162. this.req,
  163. this.res,
  164. this.next
  165. )
  166. this.next.called.should.equal(true)
  167. })
  168. })
  169. describe("when user doesn't have permission", function() {
  170. beforeEach(function() {
  171. this.AuthenticationController.getLoggedInUserId.returns(null)
  172. this.AuthorizationManager[managerMethod]
  173. .withArgs(null, this.project_id, this.token)
  174. .yields(null, false)
  175. })
  176. it('should redirect to redirectToRestricted', function() {
  177. this.AuthorizationMiddleware[middlewareMethod](
  178. this.req,
  179. this.res,
  180. this.next
  181. )
  182. this.next.called.should.equal(false)
  183. this.AuthorizationMiddleware.redirectToRestricted
  184. .calledWith(this.req, this.res, this.next)
  185. .should.equal(true)
  186. })
  187. })
  188. })
  189. describe('with malformed project id', function() {
  190. beforeEach(function() {
  191. this.req.params = { project_id: 'blah' }
  192. this.ObjectId.isValid = sinon.stub().returns(false)
  193. })
  194. it('should return a not found error', function(done) {
  195. this.AuthorizationMiddleware[middlewareMethod](
  196. this.req,
  197. this.res,
  198. error => {
  199. error.should.be.instanceof(Errors.NotFoundError)
  200. return done()
  201. }
  202. )
  203. })
  204. })
  205. })
  206. }
  207. )
  208. describe('ensureUserCanAdminProject', function() {
  209. beforeEach(function() {
  210. this.req.params = { project_id: this.project_id }
  211. this.AuthorizationManager.canUserAdminProject = sinon.stub()
  212. this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
  213. })
  214. describe('with missing project_id', function() {
  215. beforeEach(function() {
  216. this.req.params = {}
  217. })
  218. it('should return an error to next', function() {
  219. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  220. this.req,
  221. this.res,
  222. this.next
  223. )
  224. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  225. })
  226. })
  227. describe('with logged in user', function() {
  228. beforeEach(function() {
  229. this.AuthenticationController.getLoggedInUserId.returns(this.userId)
  230. })
  231. describe('when user has permission', function() {
  232. beforeEach(function() {
  233. this.AuthorizationManager.canUserAdminProject
  234. .withArgs(this.userId, this.project_id, this.token)
  235. .yields(null, true)
  236. })
  237. it('should return next', function() {
  238. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  239. this.req,
  240. this.res,
  241. this.next
  242. )
  243. this.next.called.should.equal(true)
  244. })
  245. })
  246. describe("when user doesn't have permission", function() {
  247. beforeEach(function() {
  248. this.AuthorizationManager.canUserAdminProject
  249. .withArgs(this.userId, this.project_id, this.token)
  250. .yields(null, false)
  251. })
  252. it('should raise a 403', function(done) {
  253. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  254. this.req,
  255. this.res,
  256. err => {
  257. expect(err).to.be.an.instanceof(HttpErrors.ForbiddenError)
  258. done()
  259. }
  260. )
  261. })
  262. })
  263. })
  264. describe('with anonymous user', function() {
  265. describe('when user has permission', function() {
  266. beforeEach(function() {
  267. this.AuthenticationController.getLoggedInUserId.returns(null)
  268. this.AuthorizationManager.canUserAdminProject
  269. .withArgs(null, this.project_id, this.token)
  270. .yields(null, true)
  271. })
  272. it('should return next', function() {
  273. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  274. this.req,
  275. this.res,
  276. this.next
  277. )
  278. this.next.called.should.equal(true)
  279. })
  280. })
  281. describe("when user doesn't have permission", function() {
  282. beforeEach(function() {
  283. this.AuthenticationController.getLoggedInUserId.returns(null)
  284. this.AuthorizationManager.canUserAdminProject
  285. .withArgs(null, this.project_id, this.token)
  286. .yields(null, false)
  287. })
  288. it('should raise a 403', function(done) {
  289. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  290. this.req,
  291. this.res,
  292. err => {
  293. expect(err).to.be.an.instanceof(HttpErrors.ForbiddenError)
  294. done()
  295. }
  296. )
  297. })
  298. })
  299. })
  300. describe('with malformed project id', function() {
  301. beforeEach(function() {
  302. this.req.params = { project_id: 'blah' }
  303. this.ObjectId.isValid = sinon.stub().returns(false)
  304. })
  305. it('should return a not found error', function(done) {
  306. this.AuthorizationMiddleware.ensureUserCanAdminProject(
  307. this.req,
  308. this.res,
  309. error => {
  310. error.should.be.instanceof(Errors.NotFoundError)
  311. return done()
  312. }
  313. )
  314. })
  315. })
  316. })
  317. describe('ensureUserIsSiteAdmin', function() {
  318. beforeEach(function() {
  319. this.AuthorizationManager.isUserSiteAdmin = sinon.stub()
  320. this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
  321. })
  322. describe('with logged in user', function() {
  323. beforeEach(function() {
  324. this.AuthenticationController.getLoggedInUserId.returns(this.userId)
  325. })
  326. describe('when user has permission', function() {
  327. beforeEach(function() {
  328. this.AuthorizationManager.isUserSiteAdmin
  329. .withArgs(this.userId)
  330. .yields(null, true)
  331. })
  332. it('should return next', function() {
  333. this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
  334. this.req,
  335. this.res,
  336. this.next
  337. )
  338. this.next.called.should.equal(true)
  339. })
  340. })
  341. describe("when user doesn't have permission", function() {
  342. beforeEach(function() {
  343. this.AuthorizationManager.isUserSiteAdmin
  344. .withArgs(this.userId)
  345. .yields(null, false)
  346. })
  347. it('should redirect to redirectToRestricted', function() {
  348. this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
  349. this.req,
  350. this.res,
  351. this.next
  352. )
  353. this.next.called.should.equal(false)
  354. this.AuthorizationMiddleware.redirectToRestricted
  355. .calledWith(this.req, this.res, this.next)
  356. .should.equal(true)
  357. })
  358. })
  359. })
  360. describe('with anonymous user', function() {
  361. describe('when user has permission', function() {
  362. beforeEach(function() {
  363. this.AuthenticationController.getLoggedInUserId.returns(null)
  364. this.AuthorizationManager.isUserSiteAdmin
  365. .withArgs(null)
  366. .yields(null, true)
  367. })
  368. it('should return next', function() {
  369. this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
  370. this.req,
  371. this.res,
  372. this.next
  373. )
  374. this.next.called.should.equal(true)
  375. })
  376. })
  377. describe("when user doesn't have permission", function() {
  378. beforeEach(function() {
  379. this.AuthenticationController.getLoggedInUserId.returns(null)
  380. this.AuthorizationManager.isUserSiteAdmin
  381. .withArgs(null)
  382. .yields(null, false)
  383. })
  384. it('should redirect to redirectToRestricted', function() {
  385. this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
  386. this.req,
  387. this.res,
  388. this.next
  389. )
  390. this.next.called.should.equal(false)
  391. this.AuthorizationMiddleware.redirectToRestricted
  392. .calledWith(this.req, this.res, this.next)
  393. .should.equal(true)
  394. })
  395. })
  396. })
  397. })
  398. describe('ensureUserCanReadMultipleProjects', function() {
  399. beforeEach(function() {
  400. this.AuthorizationManager.canUserReadProject = sinon.stub()
  401. this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
  402. this.req.query = { project_ids: 'project1,project2' }
  403. })
  404. describe('with logged in user', function() {
  405. beforeEach(function() {
  406. this.AuthenticationController.getLoggedInUserId.returns(this.userId)
  407. })
  408. describe('when user has permission to access all projects', function() {
  409. beforeEach(function() {
  410. this.AuthorizationManager.canUserReadProject
  411. .withArgs(this.userId, 'project1', this.token)
  412. .yields(null, true)
  413. this.AuthorizationManager.canUserReadProject
  414. .withArgs(this.userId, 'project2', this.token)
  415. .yields(null, true)
  416. })
  417. it('should return next', function() {
  418. this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
  419. this.req,
  420. this.res,
  421. this.next
  422. )
  423. this.next.called.should.equal(true)
  424. })
  425. })
  426. describe("when user doesn't have permission to access one of the projects", function() {
  427. beforeEach(function() {
  428. this.AuthorizationManager.canUserReadProject
  429. .withArgs(this.userId, 'project1', this.token)
  430. .yields(null, true)
  431. this.AuthorizationManager.canUserReadProject
  432. .withArgs(this.userId, 'project2', this.token)
  433. .yields(null, false)
  434. })
  435. it('should redirect to redirectToRestricted', function() {
  436. this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
  437. this.req,
  438. this.res,
  439. this.next
  440. )
  441. this.next.called.should.equal(false)
  442. this.AuthorizationMiddleware.redirectToRestricted
  443. .calledWith(this.req, this.res, this.next)
  444. .should.equal(true)
  445. })
  446. })
  447. })
  448. describe('with anonymous user', function() {
  449. describe('when user has permission', function() {
  450. describe('when user has permission to access all projects', function() {
  451. beforeEach(function() {
  452. this.AuthenticationController.getLoggedInUserId.returns(null)
  453. this.AuthorizationManager.canUserReadProject
  454. .withArgs(null, 'project1', this.token)
  455. .yields(null, true)
  456. this.AuthorizationManager.canUserReadProject
  457. .withArgs(null, 'project2', this.token)
  458. .yields(null, true)
  459. })
  460. it('should return next', function() {
  461. this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
  462. this.req,
  463. this.res,
  464. this.next
  465. )
  466. this.next.called.should.equal(true)
  467. })
  468. })
  469. describe("when user doesn't have permission to access one of the projects", function() {
  470. beforeEach(function() {
  471. this.AuthenticationController.getLoggedInUserId.returns(null)
  472. this.AuthorizationManager.canUserReadProject
  473. .withArgs(null, 'project1', this.token)
  474. .yields(null, true)
  475. this.AuthorizationManager.canUserReadProject
  476. .withArgs(null, 'project2', this.token)
  477. .yields(null, false)
  478. })
  479. it('should redirect to redirectToRestricted', function() {
  480. this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
  481. this.req,
  482. this.res,
  483. this.next
  484. )
  485. this.next.called.should.equal(false)
  486. this.AuthorizationMiddleware.redirectToRestricted
  487. .calledWith(this.req, this.res, this.next)
  488. .should.equal(true)
  489. })
  490. })
  491. })
  492. })
  493. })
  494. })