From fee1b41e9840aa2eda59a31a6955f330756f6032 Mon Sep 17 00:00:00 2001 From: Eitan Worcel <96389636+mobbeitan@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:36:08 -0400 Subject: [PATCH 1/2] Update my naive sql.java --- my naive sql.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/my naive sql.java b/my naive sql.java index 78e1515..e1c4304 100644 --- a/my naive sql.java +++ b/my naive sql.java @@ -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 = '" + user + "';"; + Statement stmt = con.createStatement(); + + stmt.executeQuery(query); } catch (Exception e) { throw new ServletException(e); } From 03a683d64d9dec6cccdaf68b1364a478ffef28c1 Mon Sep 17 00:00:00 2001 From: Mobb autofixer Date: Tue, 2 Apr 2024 15:38:45 +0000 Subject: [PATCH 2/2] SQL Injection fix by mobb-f490099d-c647-4b4d-80ea-fa2e775ff3d1 --- my naive sql.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/my naive sql.java b/my naive sql.java index e1c4304..eadb446 100644 --- 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 @@ 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 = '" + 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); }