Skip to main content

Use of an insecure channel

Need

Secure transmission of sensitive information

Context

  • Usage of Java 8 for developing applications with enhanced features and performance improvements.
  • Usage of javax.servlet-api for Java Servlet development

Description

Non compliant code

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class InsecureChannelServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sensitiveData = request.getParameter("sensitiveData");
PrintWriter out = response.getWriter();
out.println(sensitiveData);
}
}

This servlet handles POST requests and directly writes the received sensitive data to the response without any encryption or secure communication protocol.

The doPost method retrieves a parameter named "sensitiveData" from the request. This data could be anything sensitive like a password, credit card number, or personal identification number (PIN).

The sensitive data is then written directly to the response with out.println(sensitiveData);. This means that the data is sent back to the client over the network in plain text. If an attacker is able to intercept the network traffic (for example, if the client is using an insecure Wi-Fi network), they would be able to see the sensitive data in plain text.

This is a serious security vulnerability as it exposes sensitive user data to potential eavesdropping or Man-in-the-Middle (MitM) attacks. The data should be encrypted before being sent over the network, or a secure communication protocol like HTTPS should be used.

Steps

  • Implement secure communication protocols such as HTTPS.
  • Enable SSL/TLS encryption for the channel.
  • Use secure communication libraries or frameworks.
  • Ensure that sensitive data is not transmitted in plain text.
  • Implement proper authentication and authorization mechanisms.
  • Regularly update and patch the system to address any security vulnerabilities.

Compliant code

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;

public class SecureChannelServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sensitiveData = request.getParameter("sensitiveData");

// Create a URL object with the target server
URL url = new URL("https://secure.example.com");
// Open a connection to the server
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

// Enable input and output streams
conn.setDoInput(true);
conn.setDoOutput(true);

PrintWriter out = new PrintWriter(conn.getOutputStream());
out.println(sensitiveData);
out.close();
}
}

The above code fixes the vulnerability by implementing a secure communication protocol, HTTPS, using the javax.net.ssl.HttpsURLConnection class.

The HttpsURLConnection class is a part of javax.net.ssl package which provides classes for the secure socket package. This package allows secure socket communication between the client and the server, where the client's data is encrypted.

In the doPost method, we first get the sensitive data from the request. Then, we create a URL object with the target server's URL. We open a connection to the server using url.openConnection(), which returns a HttpsURLConnection object.

We then enable input and output streams by calling conn.setDoInput(true) and conn.setDoOutput(true). This allows us to read from and write to the connection.

Finally, we create a PrintWriter with the connection's output stream and write the sensitive data to it. This data is sent to the server over a secure HTTPS connection, ensuring that it is not transmitted in plain text.

References