Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions my naive sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db");
String user = request.getParameter("username");
String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "';";
Statement stmt = con.createStatement();

stmt.executeQuery(query);
Copy link

@github-actions github-actions bot Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image SQL Injection fix is ready

Mobb found that the high severity (🚩) SQL Injection issue reported by Snyk actually appears in test code making it irrelevant and not required to be fixed.

Issue description

SQL Injection allows attackers to execute malicious SQL queries by manipulating input data. This can result in unauthorized access to sensitive data, data manipulation, or even complete database compromise.

Fix instructions

It is recommended that you mark this issue as a false positive in Snyk and or your tracking tool.
However, in case you think this issue still needs to be addressed, Mobb prepared this fix for you below using parameterized queries or prepared statements to sanitize user input and prevent manipulation of the SQL query.

diff --git a/my naive sql.java b/my naive sql.java
--- a/my naive sql.java
+++ b/my naive sql.java
@@ -1,3 +1,4 @@
+import java.sql.PreparedStatement;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -11,10 +12,11 @@
         try {
             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db");
             String user = request.getParameter("username");
-            String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "';";
-            Statement stmt = con.createStatement();
+            String query = "SELECT * FROM users WHERE username = ?;";
+            PreparedStatement stmt = con.prepareStatement(query);
 
-            stmt.executeQuery(query);
+            stmt.setString(1, request.getParameter("username"));
+            stmt.executeQuery();
         } catch (Exception e) {
             throw new ServletException(e);
         }
 


Learn more and fine tune the fix

} catch (Exception e) {
throw new ServletException(e);
}
Expand Down
Loading