-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from cboard-org/revert-138-revert-137-136-fix…
…-user-update-vulnerability Fix vulnerability with updateUser route
- Loading branch information
Showing
6 changed files
with
180 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -798,7 +798,7 @@ paths: | |
in: body | ||
required: true | ||
schema: | ||
$ref: "#/definitions/User" | ||
$ref: "#/definitions/UserUpdate" | ||
responses: | ||
"200": | ||
description: Success | ||
|
@@ -943,6 +943,23 @@ definitions: | |
locale: | ||
type: string | ||
enum: &APP_LANGS ['ar-SA','bn-BD','cs-CZ','da-DK','de-DE','el-GR','en-US','en-GB','es-ES','fi-FI','fr-FR','he-IL','hi-IN','hu-HU','id-ID','it-IT','ja-JP','km-KH','ko-KR','ne-NP','nl-NL','no-NO','pl-PL','pt-BR','pt-PT','ro-RO','ru-RU','si-LK','sk-SK','sv-SE','th-TH','tr-TR','uk-UA','vi-VN','zh-CN','zu-ZA'] | ||
UserUpdate: | ||
type: object | ||
properties: | ||
name: | ||
type: string | ||
example: Alice | ||
email: | ||
type: string | ||
format: email | ||
example: [email protected] | ||
birthdate: | ||
type: string | ||
format: date | ||
example: 2001-10-17 | ||
locale: | ||
type: string | ||
enum: *APP_LANGS | ||
Translate: | ||
type: object | ||
required: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,25 @@ process.env.NODE_ENV = 'test'; | |
const request = require('supertest'); | ||
const chai = require('chai'); | ||
var assert = chai.assert; | ||
const expect = chai.expect; | ||
|
||
const uuid = require('uuid'); | ||
|
||
const server = require('../../app'); | ||
const helper = require('../helper'); | ||
const { copy } = require('../../app'); | ||
|
||
const nev = require('../../api/mail/index'); | ||
const User = require('../../api/models/User'); | ||
|
||
//Parent block | ||
describe('User API calls', function () { | ||
let authToken; | ||
let url; | ||
let userid; | ||
|
||
before(async function (done) { | ||
helper.deleteMochaUser(server).then((token) => { | ||
authToken = token; | ||
done(); | ||
}); | ||
before(async function() { | ||
await helper.deleteMochaUser(); | ||
}); | ||
|
||
it('it should to create a new temporary user', function (done) { | ||
|
@@ -126,20 +127,68 @@ describe('User API calls', function () { | |
}); | ||
}); | ||
|
||
it('it should update a specific user', function (done) { | ||
request(server) | ||
.put('/user/' + userid) | ||
.send({ role: 'admin' }) | ||
.set('Authorization', 'Bearer ' + authToken) | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200) | ||
.end(function (err, res) { | ||
if (err) done(err); | ||
const updateUser = res.body; | ||
updateUser.should.to.have.property('role').to.equal('admin'); | ||
done(); | ||
describe('PUT /user/:userId', function() { | ||
it('only allows an admin user to update another user', async function() { | ||
const admin = await helper.prepareUser(server, { | ||
role: 'admin', | ||
email: helper.generateEmail(), | ||
}); | ||
|
||
const user = await helper.prepareUser(server, { | ||
role: 'user', | ||
email: helper.generateEmail(), | ||
}); | ||
|
||
// Try to update another user as a regular user. | ||
// This should fail. | ||
await request(server) | ||
.put(`/user/${admin.userId}`) | ||
.set('Authorization', `Bearer ${user.token}`) | ||
.expect({ | ||
message: 'Only admins can update another user.', | ||
}) | ||
.expect(403); | ||
|
||
// Try to update another user as an admin user. | ||
// This should succeed. | ||
await request(server) | ||
.put(`/user/${user.userId}`) | ||
.set('Authorization', `Bearer ${admin.token}`) | ||
.expect(200); | ||
}); | ||
|
||
it('only allows updating a subset of fields', async function() { | ||
const user = await helper.prepareUser(server, { | ||
role: 'user', | ||
email: helper.generateEmail(), | ||
}); | ||
|
||
const update = { | ||
// Updateable. | ||
email: '[email protected]', | ||
name: 'Alice', | ||
birthdate: '2001-10-17', | ||
locale: 'klingon', | ||
|
||
// Not updateable. | ||
role: 'foobar', | ||
password: uuid.v4(), | ||
}; | ||
|
||
const res = await request(server) | ||
.put(`/user/${user.userId}`) | ||
.send(update) | ||
.set('Authorization', `Bearer ${user.token}`) | ||
.expect(200); | ||
|
||
expect(res.body.email).to.equal(update.email); | ||
expect(res.body.name).to.equal(update.name); | ||
expect(res.body.birthdate).to.contain(update.birthdate); | ||
expect(res.body.locale).to.equal(update.locale); | ||
|
||
expect(res.body.role).to.equal('user'); | ||
expect(res.body.password).not.to.equal(update.password); | ||
}); | ||
}); | ||
|
||
it('it should Destroys user session and authentication token', function (done) { | ||
|
@@ -221,18 +270,21 @@ describe('User API calls', function () { | |
}); | ||
}); | ||
|
||
it('it should delete a user', function (done) { | ||
request(server) | ||
.del('/user/' + helper.userForgotPassword.userid) | ||
.set('Authorization', 'Bearer ' + authToken) | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200) | ||
.end(function (err, res) { | ||
if (err) done(err); | ||
const userDeleted = res.body; | ||
userDeleted.should.to.have.any.keys('name', 'email', 'locale'); | ||
done(); | ||
}); | ||
it('it should delete a user', async function() { | ||
const admin = await helper.prepareUser(server, { | ||
role: 'admin', | ||
email: helper.generateEmail(), | ||
}); | ||
|
||
const targetUserId = helper.userForgotPassword.userid; | ||
|
||
expect(await User.exists({ _id: targetUserId })).to.equal(true); | ||
|
||
const res = await request(server) | ||
.del(`/user/${targetUserId}`) | ||
.set('Authorization', `Bearer ${admin.token}`) | ||
.expect(200); | ||
|
||
expect(await User.exists({ _id: targetUserId })).to.equal(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters