Skip to main content

Lack of data validation - Special Characters

Need

Implementation of proper data validation to prevent the entry of special characters in fields.

Context

  • Usage of Scala for building scalable and high-performance applications
  • Usage of play.api.mvc for handling HTTP requests in Play Framework

Description

Non compliant code

import play.api.mvc._

class ApplicationController extends Controller {
def saveData = Action { request =>
val body: AnyContent = request.body
val textBody: Option[String] = body.asText

// This is where the vulnerability lies. We directly save the
// user input without any validation.
textBody.map { text =>
Database.save(text)
Ok("Data saved successfully")
}.getOrElse {
BadRequest("Expecting text/plain request body")
}
}
}

In the above code, the saveData method in the ApplicationController class is responsible for saving user input to the database. The user input is extracted from the request body as plain text and directly saved to the database without any validation.

This lack of validation makes the system vulnerable to different types of attacks. For example, if a user enters special characters that form a malicious script (like a SQL injection attack or a Cross-Site Scripting (XSS) attack), the system will save and potentially execute that script, leading to serious security breaches.

The vulnerability lies in the line Database.save(text). Here, text is the user input that is directly saved to the database without any checks or validation. This is a typical example of lack of data validation vulnerability.

Steps

  • Implement data validation to ensure that special characters are not entered in the text input.
  • Use a regular expression or a library function to check for the presence of special characters.
  • If special characters are found, reject the input and return an appropriate error message.
  • Consider using a whitelist approach, where you define a set of allowed characters and reject any input that contains characters outside of this set.
  • Sanitize the input by removing or escaping any special characters before saving it to the database.
  • Ensure that the validation and sanitization logic is applied consistently across all relevant fields and endpoints in the application.

Compliant code

import play.api.mvc._
import scala.util.matching.Regex

class ApplicationController extends Controller {
def saveData = Action { request =>
val body: AnyContent = request.body
val textBody: Option[String] = body.asText

// Define a regular expression for allowed characters
val allowedChars: Regex = "^[a-zA-Z0-9]*$".r

textBody.map { text =>
// Check if the text contains only allowed characters
allowedChars.findFirstIn(text) match {
case Some(_) =>
// If the text is valid, save it to the database
Database.save(text)
Ok("Data saved successfully")
case None =>
// If the text is not valid, return an error message
BadRequest("Invalid input. Only alphanumeric characters are allowed.")
}
}.getOrElse {
BadRequest("Expecting text/plain request body")
}
}
}

The updated code includes data validation to ensure that special characters are not entered in the text input. This is done using a regular expression that matches only alphanumeric characters (i.e., letters and numbers).

When a request is received, the text body of the request is extracted and checked against the regular expression. If the text contains only allowed characters, it is saved to the database and a success message is returned. If the text contains any disallowed characters, the input is rejected and an error message is returned.

This approach is known as a whitelist approach, where a set of allowed characters is defined and any input that contains characters outside of this set is rejected. This is generally more secure than a blacklist approach, where a set of disallowed characters is defined, as it is impossible to predict all possible malicious inputs.

By validating and sanitizing the input in this way, we can prevent special characters from being entered in the text input, thereby fixing the vulnerability.

References