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
2 changes: 1 addition & 1 deletion src/main/java/SQLInjectionExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db");

String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "';";
String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "';";
Copy link

@github-actions github-actions bot Jan 22, 2024

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

Apply the following code change to fix SQL Injection issue detected by Fortify:

--- a/src/main/java/SQLInjectionExample.java
+++ b/src/main/java/SQLInjectionExample.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 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

Statement stmt = con.createStatement();

stmt.executeQuery(query);
Expand Down