Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ public ResponseEntity<String> doesCarInformationExistsLevel3(
htmlTemplate = "LEVEL_1/SQLInjection_Level1")
public ResponseEntity<String> doesCarInformationExistsLevel4(
@RequestParam Map<String, String> queryParams) {
final String id = queryParams.get(Constants.ID).replaceAll("'", "");
final String id = queryParams.get(Constants.ID);
BodyBuilder bodyBuilder = ResponseEntity.status(HttpStatus.OK);
bodyBuilder.body(ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
try {
ResponseEntity<String> response =
applicationJdbcTemplate.query(
(conn) ->
conn.prepareStatement(
"select * from cars where id='" + id + "'"),
(ps) -> {},
(conn) -> conn.prepareStatement("select * from cars where id=?"),
(prepareStatement) -> {
prepareStatement.setString(1, id);
},
(rs) -> {
if (rs.next()) {
CarInformation carInformation = new CarInformation();
Expand All @@ -225,7 +225,8 @@ public ResponseEntity<String> doesCarInformationExistsLevel4(
} catch (JsonProcessingException e) {
LOGGER.error("Following error occurred", e);
return bodyBuilder.body(
GENERIC_EXCEPTION_RESPONSE_FUNCTION.apply(e));
ErrorBasedSQLInjectionVulnerability
.CAR_IS_NOT_PRESENT_RESPONSE);
}
} else {
return bodyBuilder.body(
Expand All @@ -237,7 +238,8 @@ public ResponseEntity<String> doesCarInformationExistsLevel4(
return response;
} catch (Exception ex) {
LOGGER.error("Following error occurred", ex);
return bodyBuilder.body(GENERIC_EXCEPTION_RESPONSE_FUNCTION.apply(ex));
return bodyBuilder.body(
ErrorBasedSQLInjectionVulnerability.CAR_IS_NOT_PRESENT_RESPONSE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.sasanlabs.vulnerability.utils.Constants;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -85,10 +88,10 @@ void doesCarInformationExistsLevel3_ExpectParamEscaped() throws IOException {
}

@Test
void doesCarInformationExistsLevel4_ExpectValidResponse() {
void doesCarInformationExistsLevel4_ExpectValidResponse() throws Exception {
// Arrange
Map<String, String> queryParams = new HashMap<>();
queryParams.put(Constants.ID, "1'");
queryParams.put(Constants.ID, "#");

// Mock the response entity
ResponseEntity<String> mockResponseEntity =
Expand All @@ -107,11 +110,28 @@ void doesCarInformationExistsLevel4_ExpectValidResponse() {
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("Sample response", response.getBody());

ArgumentCaptor<PreparedStatementCreator> creatorCaptor =
ArgumentCaptor.forClass(PreparedStatementCreator.class);
ArgumentCaptor<PreparedStatementSetter> setterCaptor =
ArgumentCaptor.forClass(PreparedStatementSetter.class);
verify(template)
.query(
Mockito.any(PreparedStatementCreator.class),
Mockito.any(PreparedStatementSetter.class),
creatorCaptor.capture(),
setterCaptor.capture(),
Mockito.any(ResultSetExtractor.class));

Connection connection = Mockito.mock(Connection.class);
PreparedStatement preparedStatement = Mockito.mock(PreparedStatement.class);
doReturn(preparedStatement)
.when(connection)
.prepareStatement("select * from cars where id=?");

creatorCaptor.getValue().createPreparedStatement(connection);
setterCaptor.getValue().setValues(preparedStatement);

verify(connection).prepareStatement("select * from cars where id=?");
verify(preparedStatement).setString(1, "#");
}

@Test
Expand Down