Excessive Data Exposure

Excessive data exposure in API security refers to the unintentional or unauthorized release of sensitive information through an API. This can occur when an API endpoint returns too much information or fails to properly validate or filter the information returned in a response.

Importance

APIs play a critical role in many applications and systems and are often used to access and exchange sensitive information, such as personal data, financial information and confidential business data. If this information is not properly protected it can lead to data breaches, loss of trust and serious consequences for individuals, organizations and entire industries.

Overview of the Risk

Excessive data exposure can result in a range of risks including:
Data breaches: Unauthorized access to sensitive information can occur if an API endpoint returns too much information or fails to properly validate the information returned in a response.
Loss of trust: Excessive data exposure can damage the reputation of an organization and lead to a loss of trust among its customers, partners and stakeholders.
Compliance violations: Certain industries, such as finance and healthcare, are subject to strict regulations regarding the protection of sensitive information. Excessive data exposure can result in non-compliance with these regulations and legal consequences.
It is essential for organizations to prioritize security of their APIs and to understand the risks-associated with excessive data exposure.

Scenario

Here’s a scenario that demonstrates excessive data exposure:
A PHP-based API has been created to allow a mobile app to access information from a database. The API has endpoints for accessing user information, order information and product information.
One of the endpoints is designed to return user information to the app, including the user’s name, address and payment information. However the code for this endpoint has been implemented as follows:

// Connect to database
$conn = mysqli_connect("localhost", "user", "password", "database");

// Get user information from database
$sql = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Return user information to API client
echo json_encode($user);

The problem with this code is that it is returning all of the user’s information in the response including their password and payment information. This information could be intercepted by a malicious hacker and used for identity theft or fraud.
The code should be changed to only return the essential information. such the user’s name and address and to store any sensitive information, like passwords and payment details in a secure location.

For example:

// Connect to database
$conn = mysqli_connect("localhost", "user", "password", "database");

// Get user information from database
$sql = "SELECT name, address FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Return user information to API client
echo json_encode($user);

This updated code only returns the user’s name and address. Which are not sensitive & do not expose any confidential information to potential hackers.

Some Examples

Exposing Sensitive Information

// Get order information from database
$sql = "SELECT * FROM orders WHERE id = 1";
$result = mysqli_query($conn, $sql);
$order = mysqli_fetch_assoc($result);

// Return order information to API client
echo json_encode($order);

In this example; the code is returning sensitive information such as the customer’s name, address and payment information in the response. This information could be used for malicious purposes if it falls into the wrong hands.

Exposing Confidential Business Data

// Get financial information from database
$sql = "SELECT * FROM financials";
$result = mysqli_query($conn, $sql);
$financials = mysqli_fetch_assoc($result);

// Return financial information to API client
echo json_encode($financials);

In this example; the code is returning confidential business data such as sales figures, profit margins and expense reports in the response. This information could be used by competitors or used to harm the business if it falls into the wrong hands.

Exposing Debug Information

// Get user information from database
$sql = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Return user information and debug information to API client
$response = array(
    'user' => $user,
    'debug' => mysqli_error($conn)
);
echo json_encode($response);

In this example; the code is returning debug information in the response, which could expose sensitive information about the system (database credentials and configuration details).

Exposing Passwords

// Connect to database
$conn = mysqli_connect("localhost", "user", "password", "database");

// Get user information from database
$sql = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Return user information to API client
echo json_encode($user);

In this example the code is returning the user’s password in the response, which is a major security vulnerability.

Exposing Tokens

// Get user information from database
$sql = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Generate token for user
$token = bin2hex(random_bytes(32));

// Store token in database
$sql = "UPDATE users SET token = '$token' WHERE id = 1";
mysqli_query($conn, $sql);

// Return user information and token to API client
$response = array(
    'user' => $user,
    'token' => $token
);
echo json_encode($response);

In this example the code is returning the user’s token in the response, which could be intercepted by a malicious hacker and used to gain unauthorized access.

It is important to remember that any information-returned by an API can be accessed by any client that has the API-endpoints. It is therefore crucial to ensure that only necessary and non_sensitive information is returned and that any sensitive information is securely stored and protected.

How to Fix Excessive Data Exposure

Best practices for filtering the Information returned by an API

To prevent EDE, it’s important to only return the information that’s essential for the client to perform it’s intended action. This can be achieved by filtering the information returned by the API as shown in the following PHP code:

<?php
$user_id = $_GET['user_id'];
$query = "SELECT username, email FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

echo json_encode($user);
?>

Code; Only the username and email are returned in response to the API request instead of the entire user record. This limits the amount of sensitive information that’s exposed to potential attackers.

Encrypting Sensitive information stored in a database

To protect sensitive_information stored in a database. It’s important to encrypt it before storing it. This can be achieved using encryption algorithms, Such as AES as shown in the following PHP code;

<?php
$password = $_POST['password'];
$encrypted_password = openssl_encrypt($password, 'AES-256-CBC', $secret_key);
$query = "INSERT INTO users (password) VALUES ('$encrypted_password')";
mysqli_query($conn, $query);
?>

In this code; Password is encrypted using the AES-256-CBC algorithm and a secret key before it’s stored in the database. This ensures that even if an attacker gains access to the database, they will not be able to retrieve the original, unencrypted password.

Properly validating input from an API request

To prevent malicious actors from exploiting vulnerabilities in an API it’s crucial to properly validate input from API requests. This can be achieved using input validation functions such as ‘filter_var()’ as shown in the following PHP code;

<?php
$user_id = filter_var($_GET['user_id'], FILTER_VALIDATE_INT);
$query = "SELECT username, email FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

echo json_encode($user);
?>

In this code;filter_var()‘ function is used to validate the ‘user_id‘ parameter from the API request ensuring that it’s valid integer. This helps to prevent malicious actors from exploiting vulnerabilities in the API by passing in invalid or malicious input.

Implementing Access Controls for API Endpoints

To ensure that only authorized users have access to sensitive information returned by an API, it’s important to implement access-controls for API endpoints. This can be achieved using access tokens or API keys, as shown in the following PHP code:

<?php
$access_token = $_GET['access_token'];
if (!validate_access_token($access_token)) {
    http_response_code(401);
    exit;
}

$user_id = $_GET['user_id'];
if (!is_numeric($user_id)) {
    http_response_code(400);
    exit;
}

$query = "SELECT username, email FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) == 0) {
    http_response_code(404);
    exit;
}

$row = mysqli_fetch_assoc($result);
echo json_encode($row);
?>

Here, we are checking if the user_id parameter is a valid number and if not, returning 400 Bad Request response. Check if the user with the given ID exists in the database and if not, return 404 Not Found response. Additionally, we have added a function validate_access_token() to check the access token passed in the request. The function could perform various checks (token is valid), has not expired and belongs to the user making the request.

Summary

Excessive Data Exposure in API security can lead to serious consequences, such as the compromise of sensitive information. Such exposure includes returning sensitive-information in API responses, storing sensitive-information in clear-text and failing to properly-validate input from API requests. To prevent & mitigate excessive data exposure, it is important to implement best practices such as filtering information returned by APIs. Encrypting sensitive information, properly validating API input, implementing access controls, and regularly monitoring and testing API security. By adopting these practices, organizations can better protect themselves and their users from data breaches and other security risks.

References

OWASP API Security Project