Skip to main content

Technical information leak - Errors

Need

Prevention of technical information leaks through error messages

Context

  • Usage of Scala 2.11 for developing scalable and high-performance applications
  • Usage of play.api.mvc for handling HTTP requests and building web applications in Play Framework
  • Usage of play.api.libs.json.JsValue for handling JSON data in Play Framework

Description

Non compliant code

import play.api.mvc._

object ApplicationController extends Controller {
def index = Action { request =>
try {
val data = request.body.asJson.get
val processedData = processData(data)
Ok(processedData)
} catch {
case e: Exception => InternalServerError(e.getMessage)
}
}

def processData(data: JsValue): String = {
// Process data...
}
}

In the code above, we have a Play Framework controller named ApplicationController. It has an action method index which processes incoming requests.

The vulnerability lies within the index action method. The request.body.asJson.get will throw an exception if the request body is not a valid JSON. This exception is caught and the error message is sent back to the client as a response with HTTP status 500 (InternalServerError).

This is a technical information leak vulnerability because it exposes internal server error details to the client. Attackers can use this information to understand the internals of the application and exploit other potential vulnerabilities. For example, they could send massive requests or massive characters in the body of the application to cause the server to throw errors, and then analyze these error messages.

Moreover, if the processData method has any unhandled exceptions, these will also be caught and their error messages will be exposed to the client.

To fix this vulnerability, we should not expose the detailed error message to the client. Instead, we should log the error message for debugging purposes and send a generic error message to the client.

Steps

  • Implement proper error handling and logging mechanism to handle exceptions and prevent sensitive information leakage.
  • Avoid exposing detailed error messages to the client in production environment.
  • Use appropriate error response codes (e.g., 400 Bad Request, 500 Internal Server Error) to indicate the nature of the error without revealing sensitive information.
  • Implement rate limiting or request throttling mechanisms to prevent abuse of the application by sending massive requests.
  • Validate and sanitize user input to prevent injection attacks and unexpected behavior.
  • Implement proper input validation and limit the size of request bodies to prevent denial-of-service attacks.
  • Regularly update and patch the Play framework and its dependencies to ensure the latest security fixes are applied.
  • Perform security testing and code reviews to identify and fix any potential vulnerabilities in the application.

Compliant code

import play.api.mvc._
import play.api.Logger
import scala.util.{Try, Success, Failure}

object ApplicationController extends Controller {
def index = Action { request =>
val data = Try(request.body.asJson.get)
data match {
case Success(json) => processData(json) match {
case Success(processedData) => Ok(processedData)
case Failure(e) =>
Logger.error("Processing error", e)
InternalServerError("An error occurred while processing your request.")
}
case Failure(e) =>
Logger.error("Bad request", e)
BadRequest("Invalid request.")
}
}

def processData(data: JsValue): Try[String] = {
// Process data...
}
}

The updated code includes several improvements to address the identified vulnerabilities:

  1. Error Handling and Logging: The Try and match constructs are used to handle potential exceptions when processing the request. If an exception occurs, it is logged using the Logger.error method, and an appropriate HTTP response is returned. The actual error message is not exposed to the client, preventing information leakage.

  2. Input Validation: The asJson.get method is wrapped in a Try construct to handle potential exceptions if the request body cannot be parsed as JSON. If the parsing fails, a BadRequest response is returned.

  3. Rate Limiting and Request Throttling: This is not directly addressed in the code, as it would typically be handled at a higher level (e.g., by a reverse proxy or a dedicated rate limiting service). However, by properly handling errors and not exposing detailed error messages, the application is less likely to reveal information that could be used to abuse the system.

  4. Security Updates and Patches: This is also not directly addressed in the code, but it is a crucial part of maintaining a secure application. Regularly updating the Play framework and its dependencies will ensure that the latest security fixes are applied.

  5. Security Testing and Code Reviews: While not visible in the code, these practices are essential for identifying and fixing potential vulnerabilities. Regular code reviews and security testing should be part of the development process.

References