Skip to main content

Sensitive information sent insecurely

Need

Secure transmission of sensitive information

Context

  • Usage of Scala for building scalable and high-performance applications
  • Usage of play.api.mvc for handling HTTP requests and responses in Play Framework
  • Usage of global execution context for Scala concurrent programming

Description

Non compliant code

import play.api.mvc._
import scala.concurrent.ExecutionContext.Implicits.global

class Application extends Controller {

def sendSensitiveInfo = Action.async { implicit request =>
val sensitiveData = "This is a sensitive information"

// Sends sensitive information insecurely
Future.successful(Ok(sensitiveData))
}
}

The above code represents a typical Scala backend application using the Play framework. The sendSensitiveInfo action method is used to send sensitive information to the client.

The vulnerability lies in the fact that the sensitive data is being sent over an insecure channel. The Ok(sensitiveData) method sends the sensitive information as a plain text response to the client. This means that if an attacker is able to intercept the network traffic, they would be able to read the sensitive information directly, as it is not encrypted or secured in any way.

This kind of vulnerability is a serious security risk, as it could potentially expose sensitive user data, such as passwords, credit card numbers, or personal identification information. It is therefore crucial to ensure that sensitive data is always sent over a secure channel, such as HTTPS, and that it is properly encrypted before being sent.

Steps

  • Step 1: Identify the sensitive information that needs to be protected.
  • Step 2: Use a secure communication channel or method to send the sensitive information.
  • Step 3: Encrypt the sensitive information before sending it.
  • Step 4: Implement secure protocols such as HTTPS to ensure confidentiality and integrity of the communication.
  • Step 5: Use appropriate authentication and authorization mechanisms to restrict access to the sensitive information.
  • Step 6: Regularly update and patch the system to address any security vulnerabilities.

Compliant code

import play.api.mvc._
import play.api.libs.ws._
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import java.util.Base64
import scala.concurrent.ExecutionContext.Implicits.global

class Application(ws: WSClient) extends Controller {

def sendSensitiveInfo = Action.async { implicit request =>
val sensitiveData = "This is a sensitive information"
val encryptedData = encrypt(sensitiveData, "secretKey")

// Sends sensitive information securely
ws.url("https://secure-endpoint.com")
.withHttpHeaders("Content-Type" -> "application/json")
.post(encryptedData)
.map(response => Ok(response.body))
}

def encrypt(strToEncrypt: String, secret: String) : String = {
val cipher: Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
val secretKey: SecretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "AES")
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
val encryptedString: Array[Byte] = cipher.doFinal(strToEncrypt.getBytes("UTF-8"))
Base64.getEncoder.encodeToString(encryptedString)
}
}

The updated code now encrypts the sensitive data before sending it. The encryption is done using AES (Advanced Encryption Standard) with a secret key. The encrypted data is then sent over a secure HTTPS connection to a secure endpoint.

The encrypt function takes the sensitive data and a secret key as parameters. It uses the AES encryption algorithm in ECB mode with PKCS5 padding. The secret key is used to initialize the cipher for encryption. The sensitive data is then encrypted and the encrypted data is encoded into a string using Base64 encoding.

In the sendSensitiveInfo function, the sensitive data is first encrypted. Then, a POST request is made to a secure endpoint over HTTPS. The encrypted data is included in the body of the POST request. The response from the server is then returned.

This way, the sensitive data is protected during transmission over the network. The data is encrypted, so even if it is intercepted, it cannot be read without the secret key. The use of HTTPS ensures that the data is sent over a secure channel.

References