Broken Access Control

Insecure Direct Object Reference in Python

Vulnerability: Insecure Direct Object Reference

Vulnerable Code:

pythonCopy code@app.route('/profile/<user_id>')
def profile(user_id):
    user = db.get_user(user_id)
    return render_template('profile.html', user=user)

Reason for vulnerability: Directly accessing user data by user_id without verifying that the current user has permission to view that data.

Fixed Code:

pythonCopy code@app.route('/profile/<user_id>')
@login_required
def profile(user_id):
    if user_id != current_user.id:
        abort(403)
    user = db.get_user(user_id)
    return render_template('profile.html', user=user)

Reason for fix: Adding access control checks ensures that users can only access their own data, preventing unauthorized access.


Vulnerable Code:

Reason for vulnerability: No access control check, allowing any user to access any user data.

Fixed Code:

Reason for fix: Check if the current user is authorized to access the requested user data.


Reason for vulnerability: The code uses user-input to directly access a database record without proper authorization. Fixed Code 1 (PHP):

Reason for fix: The code checks if the user is authenticated and authorized to access the requested user data. Vulnerable Code 2 (Java):

Reason for vulnerability: The code uses user-input to directly access a user object without proper authorization. Fixed Code 2 (Java):

Reason for fix: The code checks if the user is authenticated and authorized to access the requested user data.


Vulnerable Code

Reason for Vulnerability:

This endpoint doesn't check if the requesting user has permission to access the profile for the given userId, allowing any authenticated user to access any profile.

Fixed Code:

Reason for Fix:

The fixed code checks if the authenticated user has permission to access the requested profile before returning it, preventing unauthorized access.

Java Example

Vulnerable Code:

Reason for Vulnerability:

This servlet allows downloading any file by specifying its name, potentially exposing sensitive files.

Fixed Code:

Reason for Fix:

The fixed code checks if the requested file is in the allowed list and uses canonical path checking to prevent path traversal, ensuring only authorized files can be downloaded.

Python Example

Vulnerable Code:

Reason for Vulnerability:

This endpoint allows any user to access any note by its ID without checking ownership or permissions.

Fixed Code:

Reason for Fix:

The fixed code adds authentication and checks if the requesting user is the owner of the note before returning it, preventing unauthorized access.


Open Redirect

Example 1: Java

Vulnerable Code:

Reason for vulnerability: No validation of the URL, allowing open redirect attacks.

Fixed Code:

Reason for fix: Validate the URL before redirecting to ensure it is an allowed destination.

Example 2: Python

Vulnerable Code:

Reason for vulnerability: No validation of the URL, allowing open redirect attacks.

Fixed Code:

Reason for fix: Validate the URL before redirecting to ensure it is an allowed destination.


Java Example

Vulnerable Code:

Reason for Vulnerability:

This code allows redirection to any URL specified in the parameter, which can be exploited for phishing attacks.

Fixed Code:

Reason for Fix:

The fixed code validates the redirect URL against a whitelist of allowed domains, preventing redirection to potentially malicious sites.

Java Example

Vulnerable Code:

Reason for Vulnerability:

This servlet allows redirection to any URL after successful login, which can be exploited for phishing attacks.

Fixed Code:

Reason for Fix:

The fixed code validates that the redirect URL is a relative path within the same domain, preventing open redirects to external sites.

Python Example

Vulnerable Code:

Reason for Vulnerability:

This Flask route allows redirection to any URL specified in the query parameter, which can be exploited for phishing attacks.

Fixed Code:

Reason for Fix:

The fixed code validates that the redirect URL is within the same domain as the application, preventing open redirects to external sites.

Last updated