| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- const sinon = require('sinon')
- const { expect } = require('chai')
- const SandboxedModule = require('sandboxed-module')
- const HttpErrors = require('@overleaf/o-error/http')
- const Errors = require('../../../../app/src/Features/Errors/Errors.js')
- const MODULE_PATH =
- '../../../../app/src/Features/Authorization/AuthorizationMiddleware.js'
- describe('AuthorizationMiddleware', function() {
- beforeEach(function() {
- this.userId = 'user-id-123'
- this.project_id = 'project-id-123'
- this.token = 'some-token'
- this.AuthenticationController = {
- getLoggedInUserId: sinon.stub().returns(this.userId),
- isUserLoggedIn: sinon.stub().returns(true)
- }
- this.AuthorizationManager = {}
- this.TokenAccessHandler = {
- getRequestToken: sinon.stub().returns(this.token)
- }
- this.ObjectId = {
- isValid: sinon
- .stub()
- .withArgs(this.project_id)
- .returns(true)
- }
- this.AuthorizationManager = {}
- this.AuthorizationMiddleware = SandboxedModule.require(MODULE_PATH, {
- globals: {
- console: console
- },
- requires: {
- './AuthorizationManager': this.AuthorizationManager,
- 'logger-sharelatex': { log() {} },
- mongojs: {
- ObjectId: this.ObjectId
- },
- '@overleaf/o-error/http': HttpErrors,
- '../Errors/Errors': Errors,
- '../Authentication/AuthenticationController': this
- .AuthenticationController,
- '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler
- }
- })
- this.req = {}
- this.res = {}
- this.next = sinon.stub()
- })
- describe('_getUserId', function() {
- beforeEach(function() {
- this.req = {}
- })
- it('should get the user from session', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns('1234')
- this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
- expect(err).to.not.exist
- expect(userId).to.equal('1234')
- done()
- })
- })
- it('should get oauth_user from request', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns(null)
- this.req.oauth_user = { _id: '5678' }
- this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
- expect(err).to.not.exist
- expect(userId).to.equal('5678')
- done()
- })
- })
- it('should fall back to null', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns(null)
- this.req.oauth_user = undefined
- this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
- expect(err).to.not.exist
- expect(userId).to.equal(null)
- done()
- })
- })
- })
- const METHODS_TO_TEST = {
- ensureUserCanReadProject: 'canUserReadProject',
- ensureUserCanWriteProjectSettings: 'canUserWriteProjectSettings',
- ensureUserCanWriteProjectContent: 'canUserWriteProjectContent'
- }
- Object.entries(METHODS_TO_TEST).forEach(
- ([middlewareMethod, managerMethod]) => {
- describe(middlewareMethod, function() {
- beforeEach(function() {
- this.req.params = { project_id: this.project_id }
- this.AuthorizationManager[managerMethod] = sinon.stub()
- this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
- })
- describe('with missing project_id', function() {
- beforeEach(function() {
- this.req.params = {}
- })
- it('should return an error to next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.calledWith(new Error()).should.equal(true)
- })
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(this.userId)
- })
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthorizationManager[managerMethod]
- .withArgs(this.userId, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthorizationManager[managerMethod]
- .withArgs(this.userId, this.project_id, this.token)
- .yields(null, false)
- })
- it('should raise a 403', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager[managerMethod]
- .withArgs(null, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager[managerMethod]
- .withArgs(null, this.project_id, this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with malformed project id', function() {
- beforeEach(function() {
- this.req.params = { project_id: 'blah' }
- this.ObjectId.isValid = sinon.stub().returns(false)
- })
- it('should return a not found error', function(done) {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- error => {
- error.should.be.instanceof(Errors.NotFoundError)
- return done()
- }
- )
- })
- })
- })
- }
- )
- describe('ensureUserCanAdminProject', function() {
- beforeEach(function() {
- this.req.params = { project_id: this.project_id }
- this.AuthorizationManager.canUserAdminProject = sinon.stub()
- this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
- })
- describe('with missing project_id', function() {
- beforeEach(function() {
- this.req.params = {}
- })
- it('should return an error to next', function() {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- this.next
- )
- this.next.calledWith(new Error()).should.equal(true)
- })
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(this.userId)
- })
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserAdminProject
- .withArgs(this.userId, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserAdminProject
- .withArgs(this.userId, this.project_id, this.token)
- .yields(null, false)
- })
- it('should raise a 403', function(done) {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- err => {
- expect(err).to.be.an.instanceof(HttpErrors.ForbiddenError)
- done()
- }
- )
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserAdminProject
- .withArgs(null, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserAdminProject
- .withArgs(null, this.project_id, this.token)
- .yields(null, false)
- })
- it('should raise a 403', function(done) {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- err => {
- expect(err).to.be.an.instanceof(HttpErrors.ForbiddenError)
- done()
- }
- )
- })
- })
- })
- describe('with malformed project id', function() {
- beforeEach(function() {
- this.req.params = { project_id: 'blah' }
- this.ObjectId.isValid = sinon.stub().returns(false)
- })
- it('should return a not found error', function(done) {
- this.AuthorizationMiddleware.ensureUserCanAdminProject(
- this.req,
- this.res,
- error => {
- error.should.be.instanceof(Errors.NotFoundError)
- return done()
- }
- )
- })
- })
- })
- describe('ensureUserIsSiteAdmin', function() {
- beforeEach(function() {
- this.AuthorizationManager.isUserSiteAdmin = sinon.stub()
- this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(this.userId)
- })
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthorizationManager.isUserSiteAdmin
- .withArgs(this.userId)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthorizationManager.isUserSiteAdmin
- .withArgs(this.userId)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- })
- describe('ensureUserCanReadMultipleProjects', function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject = sinon.stub()
- this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
- this.req.query = { project_ids: 'project1,project2' }
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(this.userId)
- })
- describe('when user has permission to access all projects', function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.userId, 'project1', this.token)
- .yields(null, true)
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.userId, 'project2', this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission to access one of the projects", function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.userId, 'project1', this.token)
- .yields(null, true)
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.userId, 'project2', this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- describe('when user has permission to access all projects', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project1', this.token)
- .yields(null, true)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project2', this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission to access one of the projects", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project1', this.token)
- .yields(null, true)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project2', this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- })
- })
- })
|