Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public ResponseEntity<String> getCarInformationLevel1(
@RequestParam Map<String, String> queryParams) {
String id = queryParams.get(Constants.ID);
BodyBuilder bodyBuilder = ResponseEntity.status(HttpStatus.OK);

// Use parameterized query to avoid SQL injection and select only needed column(s)
return applicationJdbcTemplate.query(
"select * from cars where id=" + id,
"SELECT id FROM cars WHERE id = ?",
new Object[] {id},
(rs) -> {
if (rs.next()) {
return bodyBuilder.body(CAR_IS_PRESENT_RESPONSE);
Expand All @@ -72,9 +75,11 @@ public ResponseEntity<String> getCarInformationLevel2(
@RequestParam Map<String, String> queryParams) {
String id = queryParams.get(Constants.ID);
BodyBuilder bodyBuilder = ResponseEntity.status(HttpStatus.OK);
bodyBuilder.body(ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);

// Parameterized query (even if originally wrapped with quotes)
return applicationJdbcTemplate.query(
"select * from cars where id='" + id + "'",
"SELECT id FROM cars WHERE id = ?",
new Object[] {id},
(rs) -> {
if (rs.next()) {
return bodyBuilder.body(CAR_IS_PRESENT_RESPONSE);
Expand All @@ -92,9 +97,10 @@ public ResponseEntity<String> getCarInformationLevel3(
@RequestParam Map<String, String> queryParams) {
String id = queryParams.get(Constants.ID);
BodyBuilder bodyBuilder = ResponseEntity.status(HttpStatus.OK);
bodyBuilder.body(ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);

// Prepared statement with parameter; select specific column(s) instead of '*'
return applicationJdbcTemplate.query(
(conn) -> conn.prepareStatement("select * from cars where id=?"),
(conn) -> conn.prepareStatement("SELECT id FROM cars WHERE id = ?"),
(prepareStatement) -> {
prepareStatement.setString(1, id);
},
Expand Down