Skip to main content

Lack of data validation - URL

Need

Implementation of proper data validation for URL parameters

Context

  • Usage of TypeScript for statically typed JavaScript development
  • Usage of Express for building web applications and APIs

Description

Non compliant code

import express from 'express';

const app = express();

app.get('/documents', (req, res) => {
const { date } = req.query;
// Fetch documents from the database based on the provided date
// ...

res.json(documents);
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

The vulnerability in the given code is the lack of data validation for the "date" parameter in the URL. This means that any user on the Internet can obtain information on users who have sent their documents through the portal by simply sending a specific date as a parameter in the URL.

In the code, the Express framework is used to create a server. The server listens for GET requests to the '/documents' endpoint. When a request is received, the code extracts the value of the "date" parameter from the request query using req.query.date.

The problem lies in the fact that this value is not validated or checked for proper permissions. It is directly used to fetch documents from the database based on the provided date. This means that any user can manipulate the URL and retrieve documents from any date, potentially accessing sensitive information about other users.

To fix this vulnerability, it is recommended to implement proper data validation and permission checks. This can be done by verifying the user's authentication and authorization before allowing access to user information. Additionally, input validation should be performed to ensure that the provided date is in the correct format and within the allowed range.

Steps

  • Implement data validation on the 'date' parameter to ensure it is in the correct format and within acceptable range.
  • Check if the user has the appropriate permissions to access user information before fetching documents from the database.
  • Consider using authentication and authorization mechanisms to control access to user information.
  • Sanitize the input to prevent any potential SQL injection or other types of attacks.
  • Implement logging and monitoring to detect any unauthorized access attempts or suspicious activities.
  • Regularly update and patch the server and dependencies to protect against known vulnerabilities.

Compliant code

import express from 'express';

const app = express();

app.get('/documents', (req, res) => {
const { date } = req.query;

// Validate the date parameter
if (!isValidDate(date)) {
return res.status(400).json({ error: 'Invalid date format' });
}

// Check user permissions
if (!hasAccess(req.user)) {
return res.status(403).json({ error: 'Unauthorized access' });
}

// Fetch documents from the database based on the provided date
// ...

res.json(documents);
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

function isValidDate(date: string): boolean {
// Implement date validation logic here
// Return true if the date is valid, false otherwise
}

function hasAccess(user: any): boolean {
// Implement user permission logic here
// Return true if the user has access, false otherwise
}

The fixed code addresses the vulnerability by implementing data validation and permission checks before providing access to user information. Here's an explanation of the changes made:

  1. The code imports the Express framework and creates an instance of the app.

  2. The app defines a route for handling GET requests to '/documents'.

  3. Inside the route handler, the code retrieves the 'date' parameter from the request query.

  4. The code then validates the 'date' parameter using the 'isValidDate' function. If the date is invalid, a 400 Bad Request response is sent with an error message.

  5. The code checks the user's permissions using the 'hasAccess' function. If the user does not have the necessary permissions, a 403 Forbidden response is sent with an error message.

  6. If the date is valid and the user has the required permissions, the code proceeds to fetch the documents from the database based on the provided date.

  7. Finally, the code sends a JSON response with the fetched documents.

  8. The app listens on port 3000 for incoming requests.

  9. The code includes two helper functions, 'isValidDate' and 'hasAccess', which need to be implemented to provide the necessary validation and permission logic. These functions are left undefined in the code snippet.

By validating the date parameter and checking user permissions, the fixed code ensures that only authorized users can access user information and prevents unauthorized access to the platform's users' data.

References