Skip to content

Refactored to use parameterized SQL APIs #24

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
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions src/main/java/com/acme/search/FederalConnection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.acme.search;

import java.sql.PreparedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -21,9 +22,10 @@ String doSearch(final String searchTerm) throws SQLException {
// connect to the federal database
Connection conn = fedConnectionLoader.getConnection();
// search the forecasts table for entries with the given query
String query = "SELECT * FROM forecasts WHERE entry_desc LIKE '%" + searchTerm + "%'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
String query = "SELECT * FROM forecasts WHERE entry_desc LIKE ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, "%" + searchTerm + "%");
ResultSet rs = stmt.executeQuery();
List<String> ids = new ArrayList<>();
while(rs.next()) {
String id = rs.getString("entry_id");
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/acme/sql/SQLInjectionVuln.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import jakarta.ws.rs.QueryParam;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

@Path("/unsafe-sql-injection")
public class SQLInjectionVuln {
@GET
public String lookupResource(Connection connection, @QueryParam("resource") final String resource) throws SQLException {
Statement statement = connection.createStatement();
statement.executeQuery("select * from users where name = '" + resource + "'");
PreparedStatement statement = connection.prepareStatement("select * from users where name = ?");
statement.setString(1, resource);
statement.executeQuery();
return "ok";
}
}