File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed
Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change 11package com .acme .search ;
22
3+ import java .sql .PreparedStatement ;
34import org .springframework .beans .factory .annotation .Autowired ;
45import org .springframework .stereotype .Service ;
56
@@ -21,9 +22,10 @@ String doSearch(final String searchTerm) throws SQLException {
2122 // connect to the federal database
2223 Connection conn = fedConnectionLoader .getConnection ();
2324 // search the forecasts table for entries with the given query
24- String query = "SELECT * FROM forecasts WHERE entry_desc LIKE '%" + searchTerm + "%'" ;
25- Statement stmt = conn .createStatement ();
26- ResultSet rs = stmt .executeQuery (query );
25+ String query = "SELECT * FROM forecasts WHERE entry_desc LIKE ?" ;
26+ PreparedStatement stmt = conn .prepareStatement (query );
27+ stmt .setString (1 , "%" + searchTerm + "%" );
28+ ResultSet rs = stmt .execute ();
2729 List <String > ids = new ArrayList <>();
2830 while (rs .next ()) {
2931 String id = rs .getString ("entry_id" );
Original file line number Diff line number Diff line change 55import jakarta .ws .rs .QueryParam ;
66
77import java .sql .Connection ;
8+ import java .sql .PreparedStatement ;
89import java .sql .SQLException ;
910import java .sql .Statement ;
1011
1112@ Path ("/unsafe-sql-injection" )
1213public class SQLInjectionVuln {
1314 @ GET
1415 public String lookupResource (Connection connection , @ QueryParam ("resource" ) final String resource ) throws SQLException {
15- Statement statement = connection .createStatement ();
16- statement .executeQuery ("select * from users where name = '" + resource + "'" );
16+ PreparedStatement statement = connection .prepareStatement ("select * from users where name = ?" );
17+ statement .setString (1 , resource );
18+ statement .execute ();
1719 return "ok" ;
1820 }
1921}
You can’t perform that action at this time.
0 commit comments