Skip to content

Commit f214209

Browse files
committed
Use default bcrypt log rounds, check json payload before updating user fields
1 parent f3629d3 commit f214209

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

lib/pbench/server/api/resources/users_api.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class RegisterUser(Resource):
1919
def __init__(self, config, logger):
2020
self.server_config = config
2121
self.logger = logger
22-
self.bcrypt_log_rounds = int(
23-
self.server_config.get("pbench-server", "bycrypt_log_rounds")
24-
)
2522
try:
2623
self.token_expire_duration = self.server_config.get(
2724
"pbench-server", "token_expiration_duration"
@@ -145,7 +142,6 @@ def post(self):
145142

146143
try:
147144
user = User(
148-
bcrypt_log_rounds=self.bcrypt_log_rounds,
149145
username=username,
150146
password=password,
151147
first_name=first_name,
@@ -440,15 +436,31 @@ def put(self, username):
440436
}
441437
"""
442438
post_data = request.get_json()
439+
if not post_data:
440+
self.logger.warning("Invalid json object: {}", request.url)
441+
abort(400, message="Invalid json object in request")
442+
443443
try:
444444
user, verified = self.auth.verify_user(username)
445445
except Exception:
446446
self.logger.exception("Exception occurred while verifying the user")
447447
abort(500, message="INTERNAL ERROR")
448448

449449
# TODO: Check if the user has the right privileges
450-
if verified or user.is_admin():
450+
if verified:
451451
try:
452+
# Log if the user payload contain fields that are either non-updatabale or
453+
# are not present in the user db.
454+
non_updatable = list(
455+
set(post_data.keys()) - set(User.__table__.columns.keys())
456+
)
457+
if "registered_on" in post_data.keys():
458+
non_updatable.append("registered_on")
459+
if non_updatable:
460+
self.logger.warning(
461+
"User trying to update fields that are either non-updatable or does not present in the user database. Fields: {}",
462+
non_updatable,
463+
)
452464
# We will update the user object with the keys and values provided in the request payload.
453465
# THe keys need to match with the column names in the user model. However, if any key in
454466
# the payload does not match with the column name we just skip that field.

lib/pbench/server/database/models/users.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ class User(Database.Base):
1414
username = Column(String(255), unique=True, nullable=False)
1515
first_name = Column(String(255), unique=False, nullable=False)
1616
last_name = Column(String(255), unique=False, nullable=False)
17-
password = Column(LargeBinary(500), nullable=False)
17+
password = Column(LargeBinary(128), nullable=False)
1818
registered_on = Column(DateTime, nullable=False)
19-
bcrypt_log_rounds = Column(Integer, nullable=False)
2019
email = Column(String(255), unique=True, nullable=False)
2120
auth_tokens = relationship("ActiveTokens", backref="users")
2221

23-
def __init__(self, bcrypt_log_rounds, **kwargs):
22+
def __init__(self, **kwargs):
2423
super().__init__(**kwargs)
2524
self.username = kwargs.get("username")
2625
self.first_name = kwargs.get("first_name")
2726
self.last_name = kwargs.get("last_name")
28-
self.bcrypt_log_rounds = bcrypt_log_rounds
29-
self.password = generate_password_hash(
30-
kwargs.get("password"), bcrypt_log_rounds
31-
)
27+
self.password = generate_password_hash(kwargs.get("password"))
3228
self.email = kwargs.get("email")
3329
self.registered_on = datetime.datetime.now()
3430

@@ -75,11 +71,9 @@ def update(self, **kwargs):
7571
self.auth_tokens.append(value)
7672
Database.db_session.add(value)
7773
elif key == "password":
78-
setattr(
79-
self, key, generate_password_hash(value, self.bcrypt_log_rounds)
80-
)
81-
# Prevent update on "registered_on" and "bcrypt_log_rounds" fields
82-
elif key in ["registered_on", "bcrypt_log_rounds"]:
74+
setattr(self, key, generate_password_hash(value))
75+
# Prevent update on "registered_on" field
76+
elif key == "registered_on":
8377
continue
8478
else:
8579
setattr(self, key, value)

lib/pbench/test/unit/server/test_user_auth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def test_registration_email_validity(client, server_config):
8787
def test_registration_with_registered_user(client, server_config):
8888
""" Test registration with already registered email"""
8989
user = User(
90-
int(server_config.get("pbench-server", "bycrypt_log_rounds")),
9190
9291
password="12345",
9392
username="user",

server/lib/config/pbench-server-default.cfg

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ mailto=%(admin-email)s
2929
mailfrom=%(user)s@%(host)s
3030
commit_id=unknown
3131

32-
# bycrypt log round number for hashing passwords
33-
bycrypt_log_rounds = 5
3432
# Token expiration duration in minutes, can be overridden in the main config file, defaults to 10 mins
3533
token_expiration_duration = 10
3634

0 commit comments

Comments
 (0)