Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 2.04 KB

API1-Broken-Object-Level-Authorization.md

File metadata and controls

62 lines (46 loc) · 2.04 KB

Broken Object Level Authorization

The code below is vulnureable to Broken Object Level Authorization

// This code is for an API endpoint that allows users to access and update their profile information

// Read the user's input from the request

let userId = req.body.userId;
let username = req.body.username;
let email = req.body.email;

// Check if the user is authenticated

if (!req.isAuthenticated()) {
  res.status(401).json({
    error: "Unauthorized"
  });
  return;
}

// Check if the user has permission to update their own profile

if (userId !== req.user.id) {
  // Return an error if the user does not have permission
  res.status(403).json({
    error: "Forbidden"
  });
  return;
}

// Update the user's profile information in the database

User.findByIdAndUpdate(userId, {
  username: username,
  email: email
}, (err, user) => {
  if (err) {
    res.status(500).json({
      error: err
    });
    return;
  }

  // Return the updated profile information
  
  res.json({
    user: user
  });
});

Why it's vulnerable?

The code is checking if the user is authenticated and has permission to update their own profile before allowing the update to be made. However, the code is only checking the user's ID, which means that an attacker could potentially modify the user ID in the request and update another user's profile.

Impact?

This is an example of broken object level authorization, because the code is not properly verifying that the user has the appropriate permissions to update the specific object (in this case, the user's profile).

How to fix?

To avoid this type of vulnerability, it is important to implement robust and comprehensive authorization checks that verify the user's permissions for each specific object they are attempting to access or modify. In this case, the code could be updated to check the user's permissions for the specific user's profile they are attempting to update, rather than just checking the user ID. This would ensure that only users with the appropriate permissions can update a specific profile.