Preventing Privilege Escalation

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. This is a common and severe vulnerability that can give an attacker full control over an application or system.

There are two main types of privilege escalation:

  1. Vertical Privilege Escalation: This is when an attacker gains higher-level privileges. It's like a regular hotel guest getting a master keycard that opens every room.

    • Example: A regular user manipulates a request to change their role to admin.
    • Example: An attacker accesses an admin-only dashboard (e.g., /admin/dashboard) that lacks proper access control checks.
  2. Horizontal Privilege Escalation: This is when an attacker gains access to the resources of another user with the same level of privileges. It's like a hotel guest using their keycard to open the room next door.

    • Example: A user changes the id in a URL like /orders/123 to /orders/124 and successfully views another user's order.
    • Example: An attacker accesses another user's private messages or profile information.

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 vertical privilege escalation. This example shows how a lack of proper validation can lead to unauthorized role changes.

# VULNERABLE: no auth, no field whitelist; allows role escalation
@router.put("/users/{id}")
async def update_user(id: int, request: Request, db: AsyncSession = Depends(get_db)):
    data = await request.json()
    result = await db.execute(select(User).where(User.id == id))
    user = result.scalar_one_or_none()
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    # Blindly update all fields from the request payload
    for key, value in data.items():
        setattr(user, key, value)
        
    await db.commit()
    await db.refresh(user)
    return {"id": user.id, "username": user.username, "role": user.role.value}

The vulnerability lies in the for loop. It iterates through all key-value pairs in the JSON payload sent by the client and uses setattr to update the user object's attributes directly. This is known as mass assignment. Because there is no filter, an attacker can include sensitive fields like role in their request, and the server will blindly update them.

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