-
-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for union based sql injection (#444)
* Add first version of unit tests for union based sql injection vulnerability * Added tests for the UnionBasedSQLInjction Co-authored-by: Paul Weber <[email protected]>
- Loading branch information
1 parent
ecfe0a0
commit 571108b
Showing
2 changed files
with
126 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...sasanlabs/service/vulnerability/sqlInjection/UnionBasedSQLInjectionVulnerabilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.sasanlabs.service.vulnerability.sqlInjection; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.PreparedStatementSetter; | ||
import org.springframework.jdbc.core.ResultSetExtractor; | ||
|
||
class UnionBasedSQLInjectionVulnerabilityTest { | ||
|
||
private UnionBasedSQLInjectionVulnerability unionBasedSQLInjectionVulnerability; | ||
private JdbcTemplate template; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
template = Mockito.mock(JdbcTemplate.class); | ||
|
||
// mock database | ||
doReturn(null) | ||
.when(template) | ||
.query(anyString(), (ResultSetExtractor<? extends Object>) any()); | ||
doReturn(null) | ||
.when(template) | ||
.query( | ||
anyString(), | ||
(PreparedStatementSetter) any(), | ||
(ResultSetExtractor<? extends Object>) any()); | ||
|
||
unionBasedSQLInjectionVulnerability = new UnionBasedSQLInjectionVulnerability(template); | ||
} | ||
|
||
@Test | ||
void getCarInformationLevel1_ExpectParamInjected() throws IOException { | ||
// Act | ||
final Map<String, String> params = | ||
Collections.singletonMap("id", "1 UNION SELECT * FROM cars;"); | ||
unionBasedSQLInjectionVulnerability.getCarInformationLevel1(params); | ||
|
||
// Assert | ||
verify(template) | ||
.query( | ||
eq("select * from cars where id=1 UNION SELECT * FROM cars;"), | ||
(ResultSetExtractor<? extends Object>) any()); | ||
} | ||
|
||
@Test | ||
void getCarInformationLevel2_ExpectParamInjected() throws IOException { | ||
// Act | ||
final Map<String, String> params = | ||
Collections.singletonMap("id", "1' UNION SELECT * FROM cars; --"); | ||
unionBasedSQLInjectionVulnerability.getCarInformationLevel2(params); | ||
|
||
// Assert | ||
verify(template) | ||
.query( | ||
eq("select * from cars where id='1' UNION SELECT * FROM cars; --'"), | ||
(ResultSetExtractor<? extends Object>) any()); | ||
} | ||
|
||
@Test | ||
void getCarInformationLevel3_ExpectParamEscaped() throws IOException { | ||
// Act | ||
final Map<String, String> params = | ||
Collections.singletonMap("id", "1' UNION SELECT * FROM cars; --"); | ||
unionBasedSQLInjectionVulnerability.getCarInformationLevel3(params); | ||
|
||
// Assert | ||
verify(template) | ||
.query( | ||
eq("select * from cars where id='1 UNION SELECT * FROM cars; --'"), | ||
(ResultSetExtractor<? extends Object>) any()); | ||
} | ||
|
||
@Test | ||
void getCarInformationLevel4_ExpecParamEscaped() throws IOException { | ||
// Act | ||
final Map<String, String> params = | ||
Collections.singletonMap("id", "1' UNION SELECT * FROM cars; --"); | ||
unionBasedSQLInjectionVulnerability.getCarInformationLevel4(params); | ||
|
||
// Assert | ||
verify(template) | ||
.query( | ||
eq("select * from cars where id=?"), | ||
(PreparedStatementSetter) any(), | ||
(ResultSetExtractor<? extends Object>) any()); | ||
} | ||
} |