Skip to main content

Insecure functionality - Password management

Need

Secure password management

Context

  • Requirement of Ruby 2.0 or higher for running the application
  • Usage of Ruby on Rails for building web applications

Description

Non compliant code

class UsersController < ApplicationController
def change_password
@user = User.find(params[:id])
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]
if @user.save
redirect_to @user, notice: 'Password was successfully updated.'
else
render :edit
end
end
end

The code above is a controller action in a Ruby on Rails application that allows a user to change their password. The change_password method is vulnerable to an insecure direct object reference (IDOR) attack. This vulnerability exists because the application uses the params[:id] to look up the user in the database and directly changes the password of that user.

An attacker could exploit this vulnerability by sending a request to the change_password method with the id parameter set to the ID of another user. The application would then change the password of the other user, even though the attacker is not authenticated as that user.

This vulnerability is due to the application not properly checking if the current user is authorized to perform the password change operation on the target user. The application should ensure that the current user is either the target user or has the necessary administrative privileges to change another user's password.

Steps

  • Implement proper authentication and authorization mechanisms to ensure that only authorized users can access the change_password action.
  • Use strong and secure password hashing algorithms, such as bcrypt, to securely store user passwords.
  • Implement password complexity requirements, such as minimum length and the use of a combination of letters, numbers, and special characters.
  • Enforce password expiration and regular password updates to enhance security.
  • Implement password confirmation to ensure that the user enters the correct password when changing it.
  • Implement rate limiting or CAPTCHA mechanisms to prevent brute-force attacks on the password change functionality.
  • Log password change events to detect any suspicious activity or unauthorized password changes.
  • Regularly update the Ruby on Rails framework and its dependencies to benefit from security patches and fixes.
  • Conduct regular security audits and penetration testing to identify and address any potential vulnerabilities in the application.

Compliant code

class UsersController < ApplicationController
before_action :authenticate_user!
before_action :correct_user, only: [:change_password]

def change_password
@user = User.find(params[:id])
if @user.authenticate(params[:old_password])
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]
if @user.save
redirect_to @user, notice: 'Password was successfully updated.'
else
render :edit
end
else
flash[:error] = "Old password is incorrect"
render :edit
end
end

private

def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end

The updated code includes several security enhancements to address the identified vulnerability:

  1. before_action :authenticate_user! - This line ensures that a user is authenticated before they can access the change_password action. This prevents unauthorized users from changing the password of another user.

  2. before_action :correct_user, only: [:change_password] - This line ensures that only the correct user (the user who is currently logged in) can access the change_password action. The correct_user method checks if the current user is the same as the user whose ID is specified in the URL parameters. If not, the user is redirected to the root URL.

  3. if @user.authenticate(params[:old_password]) - This line checks if the old password entered by the user is correct before allowing the password to be changed. This prevents an attacker from changing the password if they somehow manage to hijack a user's session.

These changes ensure that only authenticated and authorized users can change their own passwords, and that the old password must be correctly entered before a new password can be set. This significantly reduces the risk of unauthorized password changes.

References