Skip to main content

Insecure exceptions - NullPointerException


Ensure secure exception handling to prevent unexpected behavior


  • Usage of Ruby for building dynamic and object-oriented applications
  • Usage of the User dependency for user-related functionality


Non compliant code

def fetch_user_data(user_id)
user = User.find(user_id)
user_data =
puts "An error occurred"

In the provided Ruby on Rails code snippet, the fetch_user_data method is used to fetch the data of a user with a specific user_id. The User.find(user_id) is used to find a user with the provided user_id and is used to fetch the user's data.

The vulnerability lies in the rescue block. Here, we are catching all types of exceptions, including the NullPointerException. The NullPointerException is thrown when we try to access a method or property of a nil object. In this case, if User.find(user_id) does not find a user with the provided user_id, it will return nil, and when we try to access, it will throw a NullPointerException.

The problem with catching all exceptions, especially NullPointerException, is that it can hide bugs in our code and make debugging difficult. In this case, if a NullPointerException is thrown, we will not know about it, because we are catching it and just printing "An error occurred". This can lead to unexpected behavior in our system.


  • Remove the catch-all rescue block and replace it with specific exception handling.
  • Identify the specific exception that can be raised in the code (e.g., ActiveRecord::RecordNotFound).
  • Use a specific rescue block to handle the identified exception.
  • Handle the exception appropriately, such as logging the error, displaying a user-friendly message, or taking corrective actions.
  • Consider using Rails' built-in exception handling mechanisms, such as rescue_from, to handle exceptions globally.
  • Test the code to ensure that the exception handling works as expected.

Compliant code

def fetch_user_data(user_id)
user = User.find(user_id)
user_data =
rescue ActiveRecord::RecordNotFound => e
Rails.logger.error "User with ID #{user_id} not found: #{e.message}"

The updated code now handles the specific exception ActiveRecord::RecordNotFound which is raised when a record is not found in the database. This is a more secure way of handling exceptions as it doesn't expose other potential exceptions that could be exploited.

In the begin block, we attempt to find the user with the given user_id and access its data. If the user does not exist in the database, User.find(user_id) will raise an ActiveRecord::RecordNotFound exception.

We then rescue this specific exception. The => e syntax allows us to access the exception object, which we can use to log a detailed error message. This is useful for debugging purposes and doesn't expose any sensitive information.

If the exception is raised, we log the error message and return nil. This ensures that the method doesn't return any unexpected or potentially harmful values.

This approach is more secure than a catch-all rescue block, as it only handles a specific exception and doesn't swallow other potential errors that could indicate a problem with the system. It also provides more detailed logging for debugging purposes.

Remember to always handle exceptions in a way that doesn't expose sensitive information or system internals, and to test your code thoroughly to ensure that it behaves as expected in all scenarios.