Skip to content

Commit

Permalink
Blind sql injection vulnerabilities secure implementations (#477)
Browse files Browse the repository at this point in the history
* Add blindSQL secure implementation level 4

* Add blindSQL secure implementation level 5
  • Loading branch information
imertetsu authored Nov 11, 2024
1 parent 93ac7c6 commit 28348f4
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.sasanlabs.service.vulnerability.sqlInjection;

import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.sasanlabs.internal.utility.LevelConstants;
import org.sasanlabs.internal.utility.Variant;
import org.sasanlabs.internal.utility.annotations.AttackVector;
Expand Down Expand Up @@ -29,6 +31,7 @@
value = "BlindSQLInjectionVulnerability")
public class BlindSQLInjectionVulnerability {

@PersistenceContext private EntityManager entityManager;
private JdbcTemplate applicationJdbcTemplate;

static final String CAR_IS_PRESENT_RESPONSE = "{ \"isCarPresent\": true}";
Expand Down Expand Up @@ -106,4 +109,49 @@ public ResponseEntity<String> getCarInformationLevel3(
ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
});
}
// Input Validation - Ensure that the input data is valid and of the expected type.
@VulnerableAppRequestMapping(
value = LevelConstants.LEVEL_4,
variant = Variant.SECURE,
htmlTemplate = "LEVEL_1/SQLInjection_Level1")
public ResponseEntity<String> getCarInformationLevel4(
@RequestParam Map<String, String> queryParams) {
String id = queryParams.get(Constants.ID);

// Validate numeric ID
if (!id.matches("\\d+")) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid ID format.");
}

BodyBuilder bodyBuilder = ResponseEntity.status(HttpStatus.OK);
bodyBuilder.body(ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
return applicationJdbcTemplate.query(
"select * from cars where id=" + id,
(rs) -> {
if (rs.next()) {
return bodyBuilder.body(CAR_IS_PRESENT_RESPONSE);
}
return bodyBuilder.body(
ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
});
}

// Implementation Level 5 - Hibernate
@VulnerableAppRequestMapping(
value = LevelConstants.LEVEL_5,
variant = Variant.SECURE,
htmlTemplate = "LEVEL_1/SQLInjection_Level1")
public ResponseEntity<String> getCarInformationLevel5(
@RequestParam Map<String, String> queryParams) {
int id = Integer.parseInt(queryParams.get(Constants.ID));

CarInformation car = entityManager.find(CarInformation.class, id);

if (car != null) {
return ResponseEntity.ok(CAR_IS_PRESENT_RESPONSE);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
}
}
}

0 comments on commit 28348f4

Please sign in to comment.