logo

Insecure generation of random numbers - Typescript


Need

Secure generation of random numbers


Context

  1. Usage of TypeScript for statically typed JavaScript development
  2. Usage of Express for building web applications and handling HTTP requests

Description

Insecure Code Example

import express from 'express';

const app = express();

app.get('/random', (req, res) => {
  const randomNumber = Math.random(); // Insecure random number generation
  res.send(`Random number: ${randomNumber}`);
});

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

The vulnerability in this code is the insecure generation of random numbers. The code uses the `Math.random()` function to generate a random number. However, the `Math.random()` function in JavaScript is not suitable for generating secure random numbers because it relies on a low-entropy source and its output can be easily predicted. Using insecure random number generation can have serious security implications. An attacker could potentially guess the sequence of random numbers being generated after a short time or predict the results using probabilistic methods. This can lead to the creation of new attack vectors, such as bypassing security measures that rely on unpredictable random numbers. To mitigate this vulnerability, it is recommended to use the most secure mechanisms offered by the language to generate random numbers. In JavaScript, this can be achieved by using the `crypto` module, which provides a secure random number generator.

Steps

  1. Replace Math.random() with a secure random number generation mechanism provided by the language or a trusted library.
  2. Research and choose a secure random number generation mechanism that suits your specific requirements.
  3. Implement the chosen secure random number generation mechanism in the code.
  4. Test the application to ensure that the secure random number generation is functioning correctly.
  5. Monitor for any potential issues or vulnerabilities related to random number generation in the future and apply necessary updates or patches.

Secure Code Example

import express from 'express';
import crypto from 'crypto';

const app = express();

app.get('/random', (req, res) => {
  const secureRandomNumber = crypto.randomBytes(4).readUInt32BE(0) / 4294967295; // Secure random number generation
  res.send(`Random number: ${secureRandomNumber}`);
});

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

The fixed code addresses the vulnerability by using a secure mechanism to generate random numbers. In the code, the `crypto` module from the Node.js standard library is imported. This module provides cryptographic functionality, including secure random number generation. The `app.get('/random')` route handler generates a secure random number using the `crypto.randomBytes()` function. This function generates a buffer of cryptographically secure random bytes. In this case, it generates 4 random bytes. To convert the random bytes into a usable number, the `readUInt32BE(0)` method is called on the buffer. This method reads an unsigned 32-bit integer from the buffer at the specified offset, which in this case is 0. To ensure that the generated number falls within the range of 0 to 1, the generated number is divided by `4294967295`, which is the maximum value of a 32-bit unsigned integer. Finally, the generated secure random number is sent as a response to the client. By using the `crypto.randomBytes()` function, which is a secure mechanism provided by the Node.js `crypto` module, the code ensures that the random numbers generated are not predictable and cannot be easily guessed by an attacker.


References

  • 034 - Insecure generation of random numbers

  • Last updated

    2023/09/18