Introduction

Welcome to the fourth lesson of the "Broken Access Control" course! In this lesson, we will explore the concept of privilege escalation, a critical aspect of broken access control vulnerabilities.

By understanding how attackers can exploit these vulnerabilities to gain unauthorized access to higher-level privileges, you'll be better equipped to secure your applications. Let's dive in and learn how to prevent privilege escalation! 🚀

Understanding Privilege Escalation

Privilege escalation occurs when an attacker gains elevated access to resources that are normally protected from an application or user. There are two main types: vertical and horizontal privilege escalation. Vertical privilege escalation involves gaining higher-level privileges, such as administrative rights, while horizontal privilege escalation involves accessing resources of another user with similar privileges.

For example, if a regular user can change their role to an admin by manipulating a request, that's vertical privilege escalation. On the other hand, if a user can view another user's private data without permission, that's horizontal privilege escalation. Understanding these concepts is vital for securing web applications against unauthorized access.

Let's look at a vulnerable code example to see how these vulnerabilities can manifest in real applications.

Vulnerable Code Example

Let's examine a code snippet that demonstrates a vulnerability allowing privilege escalation. This example shows how a lack of proper validation can lead to unauthorized role changes.

In this code, any user can update their profile, including the role field, without any checks. An attacker could send a payload to change their role to admin, leading to privilege escalation. This vulnerability arises from the absence of field filtering and role validation. To understand the severity of this issue, let's see how an attacker might exploit this vulnerability.

Exploiting the Vulnerability

To understand the impact of the vulnerability, let's see how it can be exploited. An attacker could use a simple script to change their role to admin.

This command sends a request to update the user's role to admin. Without proper validation, the application accepts this change, granting the attacker unauthorized admin access. This highlights the importance of securing code against such exploits. Let's start fixing these vulnerabilities by implementing proper authentication middleware.

JWT Authentication

Since we're using JWT authentication, as a first step, we can eliminate the need for ID parameters in our routes and instead utilize the user information stored in the token. Let's first look at our authentication middleware that we used before:

We apply this middleware to protect all user routes like so:

This line ensures that any request to the /api/user endpoint must be authenticated, allowing us to securely access user information and perform operations based on the authenticated user's identity.

Now we can update our profile endpoint to use the authenticated user's information:

The authenticateToken middleware verifies the JWT token and makes the user's information available through req.user, providing a secure way to identify the requesting user without relying on URL parameters.

Now that we have proper authentication in place, let's implement identity verification for our user updates.

Authentication and Identity Verification

With JWT authentication in place, we can now secure our update endpoint by using the user's identity from the token:

This modification ensures that users can only update their own profiles, as the ID comes from their authenticated token. However, we still need to restrict what fields they can update to prevent privilege escalation.

Restricting Field Updates

To prevent privilege escalation, we need to restrict non-admin users from updating sensitive fields like role:

This code allows non-admin users to update only basic fields, preventing unauthorized role changes.

Now, let's implement special handling for admin updates to maintain necessary flexibility for system administrators.

Allowing Admin Updates

For administrative users, we can allow full access to update all fields:

Admins have the authority to update any field, ensuring flexibility while maintaining security. This completes our secure implementation of user profile updates.

Conclusion and Next Steps

In this lesson, we explored the concept of privilege escalation and how it can be exploited through vulnerabilities in web applications. We examined a vulnerable code example and demonstrated how it can be exploited. By implementing secure coding practices, such as JWT authentication, role-based access control (RBAC), and proper validation, we can prevent privilege escalation and protect our applications.

As you move on to the practice exercises, apply what you've learned to identify and fix privilege escalation vulnerabilities. This hands-on experience will reinforce your understanding and prepare you for more advanced security challenges. Keep exploring and stay secure! 🔒

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