AuthorizationMiddlewareTests.js 19 KB

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