Unverified Account Parameter in API Endpoints

Introduction

Welcome to the very first lesson of the "Broken Access Control" course! In this lesson, we will explore the critical topic of unverified account parameters in API endpoints. This vulnerability is a common entry point for attackers seeking unauthorized access to sensitive data.

By understanding how these vulnerabilities occur and learning how to secure your code, you'll be taking a significant step toward building more secure web applications. Let's dive in! 🚀

Understanding Unauthorized Access via Parameter Manipulation

When building API endpoints that handle user data, it's crucial to implement proper access controls. Without proper verification, attackers can manipulate request parameters to access data belonging to other users. This is particularly dangerous when dealing with account information, as it can lead to unauthorized access to user data.

In this lesson, we'll focus on how unverified parameters can lead to unauthorized access vulnerabilities and the importance of securing these parameters to protect your application.

Vulnerable Code Example

Let's take a look at a code snippet that demonstrates a vulnerable API endpoint using unverified parameters. This example will help us understand the risks associated with such vulnerabilities.

TypeScript
router.get('/accountInfo', async (req, res) => {
  const userId = req.query.id;
  if (!userId) {
    return res.status(400).json({ error: "Missing user ID parameter" });
  }
  
  const user = await User.findByPk(userId);
  if (!user) {
    return res.status(404).json({ error: "User not found" });
  }
  
  res.json(user);
});

In this code, the userId parameter is taken directly from the query string and used to fetch user data without any verification. This lack of validation allows an attacker to manipulate the id parameter to access any user's account details, leading to unauthorized data access.

Exploiting the Vulnerability

An attacker can easily exploit this vulnerability by manipulating the URL parameters. Here is an example of how this can be done using a simple curl request:

curl "http://localhost/api/accountInfo?id=1"

By sending this request, an attacker can access the account information of the user with id=1, which could be an admin or any other user. This demonstrates how easily unverified parameters can be exploited to gain unauthorized access to sensitive data.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal