Skip to content
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

Update my naive db sample.java #178

Closed
wants to merge 1 commit into from
Closed

Update my naive db sample.java #178

wants to merge 1 commit into from

Conversation

mobbeitan
Copy link
Owner

No description provided.

@@ -12,7 +12,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db");
String user = request.getParameter("username");

String query = "SELECT * FROM users WHERE username2 = '" + user + "';";
String query = "SELECT * FROM users WHERE username = '" + user + "';";
Copy link

@github-actions github-actions bot Jan 17, 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 by Mobb is ready

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

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


Learn more and fine tune the fix

@mobbeitan mobbeitan closed this Jan 17, 2024
@mobbeitan mobbeitan deleted the mobbeitan-patch-2 branch January 17, 2024 17:38
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.

1 participant