Skip to content

Update SQLInjectionExample.java #100

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

Closed
wants to merge 1 commit into from
Closed

Conversation

antonychiu2
Copy link
Owner

No description provided.

@@ -11,8 +11,8 @@ 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") + "';";
Statement stmt = con.createStatement();
String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "';";
Copy link

@github-actions github-actions bot Oct 29, 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

This change fixes a high severity (🚩) SQL Injection issue reported by Snyk.

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

Use parameterized queries or prepared statements to sanitize user input and prevent manipulation of the SQL query.

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

Copy link

image 1 fix is ready to be committed

SQL Injection - 1

@antonychiu2 antonychiu2 deleted the antonychiu2-patch-38 branch April 16, 2025 14:35
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