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.

In this code, the id parameter is taken directly from the query string using @RequestParam and is 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.

Note: This is a classic example of Insecure Direct Object Reference (IDOR). The application trusts the user-supplied id without verifying if the authenticated user has the right to access that specific resource.

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:

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

Authentication Checks

The first line of defense is proper authentication. We need to ensure that only authenticated users can access account information. We'll use json web tokens (JWT) for authentication. A JWT is a compact, URL-safe means of representing claims between two parties. It consists of three parts: a header, a payload, and a signature. When a user logs in, they receive a JWT that they must include in subsequent requests to prove their identity.

The JWT_SECRET_KEY is a private key used to sign and verify tokens. It should be kept secure and never exposed to the public, as anyone with access to this key could forge valid tokens. In your application.yml, this is configured as:

Here's how we implement JWT verification in our controller:

Parameter Validation

After ensuring authentication, we must validate the parameter format and verify access rights. This step ensures that users can only access their own account information.

This code performs several crucial checks:

  1. Verifies that the JWT token is present and valid
  2. Ensures the id parameter is present and is a valid integer
Conclusion and Next Steps

In this lesson, we've explored the risks associated with unverified account parameters in API endpoints and learned how to secure our code using authentication, input validation, and proper authorization checks.

Important Note: In a real-world secure API, if a user is requesting their own info, you typically don't even ask for the id in the parameter. You simply fetch the ID from the JWT and use that. Including it in the parameter is redundant and creates a surface for attackers to test.

As you move on to the practice exercises, focus on applying these secure coding practices to reinforce your understanding. In the next lesson, we'll continue to build on this foundation by exploring other common vulnerabilities and their mitigations. Keep up the great work! 🌟

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