Skip to content

Fix SQL Injection Vulnerability by Parameterizing the customerId in Scala's CustomerController. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zeropath-ai[bot]
Copy link

@zeropath-ai zeropath-ai bot commented Dec 23, 2024

Summary

"""

  • The Vulnerability Description: The application was vulnerable to SQL injection because it constructed SQL queries by directly concatenating the customerId input into the query without sanitization, allowing attackers to inject arbitrary SQL commands.
  • This Fix: The patched code now uses a parameterized query to securely include the customerId by using a placeholder :id, which prevents potential SQL injections.
  • The Cause of the Issue: The vulnerability was caused by the practice of directly appending user input (customerId) into the SQL query string, creating an entry point for malicious SQL injections.
  • The Patch Implementation: The patch converts the SQL query to a parameterized format with :id as a placeholder, then maps the customerId input to this placeholder using query.setParameter("id", customerId), ensuring safe execution of SQL commands.
    """

Vulnerability Details

  • Vulnerability Class: SQL Injection (SQLI)
  • Severity: 10.0
  • Affected File: app/controllers/CustomerController.scala
  • Vulnerable Lines: 108-110

Code Snippets

diff --git a/app/controllers/CustomerController.scala b/app/controllers/CustomerController.scala
index c5477dc..ddb1d6f 100644
--- a/app/controllers/CustomerController.scala
+++ b/app/controllers/CustomerController.scala
@@ -105,10 +105,11 @@ class CustomerController @Inject() (ws: WSClient, config: Configuration) extends
   // get /rawcustomers/{customerId}
   def getRawCustomer(customerId: String) = Action {
     if (null == customerId) throw new InvalidCustomerRequestException
-    val sqlQuery = "SELECT first_name, last_name FROM customer WHERE id = " + customerId
+    val sqlQuery = "SELECT first_name, last_name FROM customer WHERE id = :id"
     val rawSql = RawSqlBuilder.parse(sqlQuery).create
     val query = CustomerController.db.find(classOf[Customer])
     query.setRawSql(rawSql)
+    query.setParameter("id", customerId)
     val customer = query.findList.asScala
     if (null == customer || customer.isEmpty) throw new CustomerNotFoundException
     Ok(Json.toJson(customer))

How to Modify the Patch

You can modify this patch by using one of the two methods outlined below. We recommend using the @zeropath-ai bot for updating the code. If you encounter any bugs or issues with the patch, please report them here.

Ask @zeropath-ai!

To request modifications, please post a comment beginning with @zeropath-ai and specify the changes required.

@zeropath-ai will then implement the requested adjustments and commit them to the specified branch in this pull request. Our bot is capable of managing changes across multiple files and various development-related requests.

Manually Modify the Files

# Checkout created branch:
git checkout zvuln_fix_sql_injection_sqli_1734969450133559

# if vscode is installed run (or use your favorite editor / IDE):
code app/controllers/CustomerController.scala

# Add, commit, and push changes:
git add -A
git commit -m "Update generated patch with x, y, and z changes."
git push zvuln_fix_sql_injection_sqli_1734969450133559

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants