Insecure encryption algorithm - SHA1 - TypeScript

Insecure encryption algorithm - SHA1 - TypeScript

Need

Upgrade to a secure encryption algorithm

Context

  • Usage of Node.js v14.0.0 for server-side JavaScript development
  • Usage of Express for building web applications and APIs
  • Usage of crypto for cryptographic operations

Description

Non compliant code

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

const app = express();

app.get('/login', (req, res) => {
  const username = req.query.username;
  const password = req.query.password;

  // Insecure encryption using SHA1
  const hash = crypto.createHash('sha1').update(password).digest('hex');

  // Check if the hashed password matches the stored password
  if (hash === '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8') {
    res.send('Login successful');
  } else {
    res.send('Login failed');
  }
});

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

The vulnerability in the provided code is the use of an insecure encryption algorithm, specifically SHA1.

In the code, the password provided by the user is hashed using the SHA1 algorithm. However, SHA1 is considered to be insecure for cryptographic purposes due to its vulnerability to collision attacks. This means that an attacker could potentially reverse engineer the hashed password and obtain the original password.

Using an insecure encryption algorithm like SHA1 puts sensitive information, such as user passwords, at risk. Attackers can exploit this vulnerability to gain unauthorized access to user accounts or obtain other sensitive information.

To address this vulnerability, it is recommended to use algorithms that are considered cryptographically secure, such as SHA256 or bcrypt, which are designed to withstand various attacks and provide better protection for sensitive data.

Steps

  • Replace the insecure encryption algorithm SHA1 with a cryptographically secure algorithm such as SHA256 or bcrypt.
  • Update the code to use the chosen secure algorithm for hashing the password.
  • Ensure that the stored password is also hashed using the same secure algorithm.
  • Update the code to compare the hashed password with the stored hashed password using a secure comparison method.
  • Test the updated code to ensure it functions correctly.

Compliant code

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

const app = express();

app.get('/login', (req, res) => {
  const username = req.query.username;
  const password = req.query.password;

  // Secure encryption using SHA256
  const hash = crypto.createHash('sha256').update(password).digest('hex');

  // Check if the hashed password matches the stored password
  if (hash === '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8') {
    res.send('Login successful');
  } else {
    res.send('Login failed');
  }
});

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

The fixed code addresses the vulnerability by using a secure encryption algorithm, specifically SHA256, instead of the insecure SHA1 algorithm.

Here's a breakdown of the changes made:

1. Importing the necessary modules:
- The code imports the express module to create the server.
- The code also imports the crypto module to access cryptographic functions.

2. Creating the Express server:
- The code creates an instance of the Express server using express() and assigns it to the app variable.

3. Handling the login route:
- The code defines a route handler for the /login endpoint using app.get().
- Inside the route handler, it retrieves the username and password from the request query parameters.

4. Secure encryption using SHA256:
- The code uses crypto.createHash() to create a hash object with the SHA256 algorithm.
- It then updates the hash object with the password using update(password).
- Finally, it generates the hash value in hexadecimal format using digest('hex') and assigns it to the hash variable.

5. Checking the hashed password:
- The code compares the generated hash with a stored hash value.
- If the hashes match, it sends the response 'Login successful'.
- Otherwise, it sends the response 'Login failed'.

6. Starting the server:
- The code starts the server on port 3000 using app.listen().
- It also logs a message to the console indicating that the server is running.

By using the secure SHA256 algorithm for encryption, the fixed code ensures that sensitive information is protected and reduces the risk of unauthorized access.

References