logo

Database

Scala Tainted Sql Injection

Description

Detects potential SQL injection vulnerabilities in Scala code where untrusted input data could be concatenated into SQL queries. This vulnerability occurs when user-controlled data is directly incorporated into SQL statements without proper sanitization, allowing potential manipulation of the query structure.

Weakness:

146 - SQL injection

Category: Unexpected Injection

Detection Strategy

    • Code must import java.sql package

    • SQL execution methods like executeQuery, execute or prepareStatement are called

    • The SQL query string (first argument) contains string concatenation with variables

    • The concatenated variables are not properly sanitized or escaped

Vulnerable code example

import java.sql.DriverManager

object SqlInjectionExample {
  def unsafeQuery(name: String) = {
    val conn = DriverManager.getConnection("jdbc:mysql://localhost:8080")
    val stmt = conn.createStatement()
    // Vulnerable: Direct string concatenation allows SQL injection
    val sql = "SELECT * FROM users WHERE name = " + name + ";"...

✅ Secure code example

import java.sql.DriverManager

object SqlInjectionExample {
  def safeQuery(name: String) = {
    val conn = DriverManager.getConnection("jdbc:mysql://localhost:8080")
    // SAFE: Using prepared statement with parameterized query
    val pstmt = conn.prepareStatement("SELECT * FROM users WHERE name = ?")
    pstmt.setString(1, name)...