This repository was archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Improve security of and make optional certain user API parameters #70
Open
hulbert
wants to merge
3
commits into
bucketsio:master
Choose a base branch
from
hulbert:user-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| express = require 'express' | ||
| async = require 'async' | ||
| crypto = require 'crypto' | ||
| _ = require 'underscore' | ||
| {isArray} = require 'util' | ||
|
|
||
| mailer = require '../../lib/mailer' | ||
| config = require '../../config' | ||
|
|
@@ -75,6 +77,7 @@ module.exports = app = express() | |
| @apiParam {String} name Full name of the user. | ||
| @apiParam {String} email Email address of the user. | ||
| @apiParam {String} password Password for the user. Must be between 6-20 characters and include a number. | ||
| @apiParam {Array} [roles] Array of roles the user is granted | ||
|
|
||
| @apiPermission administrator | ||
| ### | ||
|
|
@@ -93,13 +96,19 @@ app.route('/users') | |
| return res.status(401).end() unless req.user?.hasRole ['administrator'] | ||
| return req.status(400).end() unless typeof req?.body is 'object' | ||
|
|
||
| newUser = new User req.body | ||
| data = _.pick req.body, 'name', 'email', 'password' | ||
| newUser = new User data | ||
|
|
||
| # optional | ||
| if req.body.roles? and isArray(req.body.roles) | ||
| newUser.roles = req.body.roles | ||
|
|
||
| newUser.save (err) -> | ||
| return res.status(400).send err if err | ||
| res.status(200).send newUser | ||
|
|
||
| .get (req, res) -> | ||
| # TODO shouldn't we filter passwords and such from here? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Users should already have their passwordDigest filtered out (it has a |
||
| User.find {}, (err, users) -> | ||
| res.status(200).send users | ||
|
|
||
|
|
@@ -125,6 +134,7 @@ app.route('/users') | |
| @apiParam {String} id User ID (sent in URL) | ||
| @apiParam {String} [name] The full name of the user. | ||
| @apiParam {String} [email] The user’s email address. | ||
| @apiParam {Array} [roles] Array of roles the user has | ||
|
|
||
| @apiPermission administrator | ||
|
|
||
|
|
@@ -166,23 +176,27 @@ app.route('/users/:userID') | |
| res.status(200).end() | ||
|
|
||
| .put (req, res) -> | ||
| return res.status(401).end() unless req.user?.hasRole ['administrator'] or req.user?._id is req.params.userID | ||
| adminEditing = req.user?.hasRole ['administrator'] | ||
| return res.status(401).end() unless adminEditing or req.user?._id is req.params.userID | ||
|
|
||
| User.findById req.params.userID, 'passwordDigest', (err, user) -> | ||
| return res.status(400).end() if err or not user | ||
|
|
||
| {password, passwordconfirm, oldpassword} = req.body | ||
| delete req.body.password # Only add back after checking below | ||
| if password and passwordconfirm and oldpassword | ||
| if password isnt passwordconfirm | ||
| user.invalidate 'passwordconfirm', 'Your new password and confirmation don’t match.' | ||
| else if not user.authenticate oldpassword | ||
| user.invalidate 'oldpassword', 'The provided password is incorrect.' | ||
| else | ||
| # All good | ||
| req.body.password = password | ||
| user.password = password | ||
|
|
||
| user.set(req.body).save (err, user) -> | ||
| user.set _.pick req.body, 'name', 'email' | ||
|
|
||
| if adminEditing and req.body.roles? and isArray(req.body.roles) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test (or two) for "An admin can update user roles w/PUT, but a non-administrator can't (even if it’s their own profile)" |
||
| user.roles = req.body.roles | ||
|
|
||
| user.save (err, user) -> | ||
| return res.status(400).send err if err | ||
| res.status(200).send user | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of bringing in a custom util class above, you could use
_.isArrayhereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
util#isArrayis part of node core http://nodejs.org/api/util.html#util_util_isarray_objectThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry! Thought it was coming from the bucketUtil class from another PR — that's fine :)
On Mon, Sep 29, 2014 at 6:10 PM, Scott Hulbert notifications@github.com
wrote: