Skip to content

Instantly share code, notes, and snippets.

@adnan-i
Last active December 29, 2017 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adnan-i/5d95fe9065b236c60b6139248fe1ee6c to your computer and use it in GitHub Desktop.
Save adnan-i/5d95fe9065b236c60b6139248fe1ee6c to your computer and use it in GitHub Desktop.
Mitigating Broken Access Control threat (HapiJS)
update(req, reply) {
return Promise.resolve()
.then(() => {
/*
* Even though this action is accessed from the "PUT /api/users/:id" route
* we cannot rely on reading the user id from the params. Instead we're using
* the user id from the session.
* This is to mitigate the Broken Access Control threat
* (https://www.owasp.org/index.php/Top_10-2017_A5-Broken_Access_Control)
*/
const where = {
id: req.auth.credentials.id,
};
/*
* In case admin is updating a user record we can take the user id from the params
*/
if (req.auth.credentials.isAdmin) {
where.id = req.params.id;
}
let emailChanged;
let phoneChanged;
return this.User.findOne({ where })
.then((user) => {
if (!user) throw Boom.notFound('Record not found');
user.set(req.payload);
emailChanged = user.changed('email');
return user.save();
})
.tap((user) => {
if (!emailChanged) return;
this.EmailVerificationService.sendVerificationEmail(user.email)
.catch((err) => {
this.logger.error(err);
});
})
.then((user) => {
return this.User.scope('public').findById(user.id);
});
})
.then(reply);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment