Skip to content

Commit

Permalink
#1258 now rejects non-sec requests with params
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-flores committed Jul 31, 2024
1 parent 53c9fb1 commit 403b3bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/controller/org.controller/org.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ async function updateOrg (req, res, next) {
result = await orgRepo.aggregate(agt)
result = result.length > 0 ? result[0] : null

if (!isSec) {
result = { last_active: result.last_active }
}

const responseMessage = {
message: shortName + ' organization was successfully updated.',
updated: result
Expand Down
6 changes: 4 additions & 2 deletions src/middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ async function validateOrg (req, res, next) {

const isSec = await orgRepo.isSecretariat(org)
if (!isSec) {
if (!(org === reqOrg)) {
if (org !== reqOrg) {
logger.info({ uuid: req.ctx.uuid, message: org + ' is not a ' + CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT + ' or the same as ' + reqOrg + ' and is not allowed to make these changes.' })
return res.status(401).json(error.unauthorized())
return res.status(403).json(error.secretariatOnly())
} else if (Object.keys(req.query).length > 0) {
return res.status(403).json(error.secretariatOnly())
}
}

Expand Down
27 changes: 20 additions & 7 deletions test/integration-tests/org/putOrgTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('Testing org put endpoint', () => {
await chai.request(app)
.put('/api/org/win_5')
.set({ ...constants.nonSecretariatUserHeaders })
.query(params)
.send()
.then((res, err) => {
// Assert that that the last_active field was updated under 2 seconds ago
Expand All @@ -75,8 +74,9 @@ describe('Testing org put endpoint', () => {
expect(withinTwoSeconds).to.be.true
// Assert no other fields were changed
expect(res).to.have.status(200)
expect(res.body.updated.name).to.equal(cnaParams.name)
expect(res.body.updated.policies.id_quota).to.equal(cnaParams.id_quota)
expect(res.body.updated.active_roles).to.be.undefined
expect(res.body.updated.name).to.be.undefined
expect(res.body.updated.policies).to.be.undefined
expect(err).to.be.undefined
})
})
Expand All @@ -88,10 +88,23 @@ describe('Testing org put endpoint', () => {
.set({ ...constants.nonSecretariatUserHeaders })
.send()
.then((res, err) => {
expect(res).to.have.status(401)
expect(res).to.have.status(403)
expect(err).to.be.undefined
expect(res.body).to.haveOwnProperty('error')
expect(res.body.error).to.equal('SECRETARIAT_ONLY')
})
})
it('Fails update to fields made by a non-secretariat org to itself', async () => {
await chai.request(app)
.put('/api/org/win_5')
.set({ ...constants.nonSecretariatUserHeaders })
.query(params)
.send()
.then((res, err) => {
expect(res).to.have.status(403)
expect(err).to.be.undefined
expect(res.body).to.haveOwnProperty('error')
expect(res.body.error).to.equal('UNAUTHORIZED')
expect(res.body.error).to.equal('SECRETARIAT_ONLY')
})
})
it('Fails update made by a non-secretariat org to a secretariat', async () => {
Expand All @@ -100,10 +113,10 @@ describe('Testing org put endpoint', () => {
.set({ ...constants.nonSecretariatUserHeaders })
.send()
.then((res, err) => {
expect(res).to.have.status(401)
expect(res).to.have.status(403)
expect(err).to.be.undefined
expect(res.body).to.haveOwnProperty('error')
expect(res.body.error).to.equal('UNAUTHORIZED')
expect(res.body.error).to.equal('SECRETARIAT_ONLY')
})
})
})
Expand Down

0 comments on commit 403b3bf

Please sign in to comment.