Skip to main content

Lack of data validation - Numbers

Need

Implementation of robust data validation for numbers

Context

  • Usage of Scala for functional and object-oriented programming in the JVM environment
  • Usage of play.api.mvc for handling HTTP requests and responses in Play Framework

Description

Non compliant code

import play.api.mvc._

object MyController extends Controller {
def processData = Action { request =>
val number = request.getQueryString("number").getOrElse("0")
Ok("Received number: " + number)
}
}

The above Scala code is using the Play framework to create a web application. It has a controller named MyController that has a method processData. This method is intended to receive a request and extract a query parameter named "number".

The vulnerability lies in the line:

val number = request.getQueryString("number").getOrElse("0")

Here, the application is taking user input directly through a query parameter and not performing any kind of data validation. This means that an attacker could potentially input any value, not just numbers, which could lead to unexpected behavior, errors, or even security vulnerabilities such as SQL Injection or Cross-Site Scripting (XSS) attacks, depending on how the number value is used later in the application.

This lack of data validation can break the current business logic. For instance, if the application expects this value to be a number and uses it for mathematical operations, providing a non-numeric value could cause runtime errors. Or if the application stores this value in a database, providing a specially crafted value could lead to SQL Injection attacks.

This code is vulnerable as it implicitly trusts user input, which is a common security anti-pattern. All user input should be considered untrusted by default and should be properly validated and sanitized before use.

Steps

  • Implement data validation for the 'number' parameter
  • Check if the 'number' parameter is a valid number
  • Handle invalid number inputs appropriately
  • Consider using a validation library or framework to simplify the validation process

Compliant code

import play.api.mvc._
import scala.util.Try

object MyController extends Controller {
def processData = Action { request =>
val number = request.getQueryString("number").getOrElse("0")
if (Try(number.toInt).isSuccess) {
Ok("Received number: " + number)
} else {
BadRequest("Invalid number input")
}
}
}

The updated code now includes a validation check for the 'number' parameter. The Try(number.toInt).isSuccess line attempts to convert the 'number' parameter to an integer. If the conversion is successful, it means that the 'number' parameter is a valid number and the function proceeds as normal.

However, if the conversion fails (which would happen if the 'number' parameter is not a valid number), the Try(number.toInt).isSuccess check will return false. In this case, the function will return a BadRequest response with the message "Invalid number input". This informs the user that their input was not valid and needs to be corrected.

This validation check ensures that the 'number' parameter is always a valid number, preventing any potential issues or vulnerabilities that could arise from invalid number inputs.

References