Skip to content

Commit 3143b48

Browse files
committed
fed connection established
1 parent d09898f commit 3143b48

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.acme.search;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.sql.Connection;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@Service
14+
public class FederalConnection {
15+
16+
@Autowired
17+
private FederalConnectionLoader fedConnectionLoader;
18+
19+
// connect to the federal database and search the forecasts table for entries with the given query
20+
String doSearch(final String searchTerm) throws SQLException {
21+
// connect to the federal database
22+
Connection conn = fedConnectionLoader.getConnection();
23+
// 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);
27+
List<String> ids = new ArrayList<>();
28+
while(rs.next()) {
29+
String id = rs.getString("entry_id");
30+
ids.add(id);
31+
}
32+
rs.close();
33+
stmt.close();
34+
conn.close();
35+
return String.join(",", ids);
36+
}
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.acme.search;
2+
3+
import java.sql.Connection;
4+
5+
public interface FederalConnectionLoader {
6+
Connection getConnection();
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.acme.search;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
7+
@Controller
8+
public final class SearchController {
9+
10+
private SearchService searchService;
11+
12+
@GetMapping("/search/federal")
13+
public String searchFederal(@RequestParam String q) {
14+
return searchService.searchFederal(q);
15+
}
16+
17+
/** Change the code given. */
18+
@GetMapping("/search/federify")
19+
public String createFedSearchToken(@RequestParam String searchCode) {
20+
return "<html><body>FEDSEARCH:" + searchCode.toUpperCase().trim() + "</body></html>";
21+
}
22+
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.acme.search;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.sql.SQLException;
7+
import java.util.logging.Logger;
8+
9+
@Service
10+
final class SearchService {
11+
12+
@Autowired
13+
private FederalConnection fedConnection;
14+
15+
String searchFederal(final String query) {
16+
log.info("Searching federal for query: " + '"' + query + '"');
17+
String fedQuery = "FEDSEARCH:" + query.toUpperCase().trim();
18+
19+
// stop sqli that was reported by the security team
20+
if(fedQuery.contains("DROP") || fedQuery.contains("UNION") || fedQuery.contains("DELETE") || fedQuery.contains("1=1")) {
21+
throw new IllegalArgumentException("Commands detected");
22+
}
23+
try {
24+
return fedConnection.doSearch(fedQuery);
25+
} catch (SQLException e) {
26+
log.info("Error searching federal for query: " + '"' + query + '"');
27+
return "error";
28+
}
29+
}
30+
31+
private static final Logger log = Logger.getAnonymousLogger();
32+
}

0 commit comments

Comments
 (0)