Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] SQL injection test for AccountRequestsDbIT #12788

Merged
merged 12 commits into from
Mar 4, 2024
44 changes: 43 additions & 1 deletion src/it/java/teammates/it/storage/sqlapi/AccountRequestsDbIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.storage.sqlapi.AccountRequestsDb;
import teammates.storage.sqlentity.AccountRequest;

/**
* SUT: {@link CoursesDb}.
* SUT: {@link AccountRequestsDb}.
*/
public class AccountRequestsDbIT extends BaseTestCaseWithSqlDatabaseAccess {

Expand Down Expand Up @@ -88,4 +89,45 @@ public void testUpdateAccountRequest() throws Exception {
accountRequest.getEmail(), accountRequest.getInstitute());
verifyEquals(accountRequest, actual);
}

@Test
public void testSqlInjectionInEmailField() throws Exception {
______TS("SQL Injection test in email field");

// Attempt to use SQL commands in email field
String email = "name'; DROP TABLE AccountRequest; [email protected]";
weiquu marked this conversation as resolved.
Show resolved Hide resolved
AccountRequest accountRequest = new AccountRequest(email, "name", "institute");

// The regex check should fail and throw an exception
assertThrows(InvalidParametersException.class,
() -> accountRequestDb.createAccountRequest(accountRequest));
}

@Test
public void testSqlInjectionInNameField() throws Exception {
______TS("SQL Injection test in name field");

// Attempt to use SQL commands in name field
String name = "name'; SELECT * FROM AccountRequest; --";
AccountRequest accountRequest = new AccountRequest("[email protected]", name, "institute");

// The system should treat the input as a plain text string
accountRequestDb.createAccountRequest(accountRequest);
AccountRequest actual = accountRequestDb.getAccountRequest(accountRequest.getEmail(), accountRequest.getInstitute());
assertEquals(name, actual.getName());
}

@Test
public void testSqlInjectionInInstituteField() throws Exception {
______TS("SQL Injection test in institute field");

// Attempt to use SQL commands in institute field
String institute = "institute'; DROP TABLE AccountRequest; --";
AccountRequest accountRequest = new AccountRequest("[email protected]", "name", institute);

// The system should treat the input as a plain text string
accountRequestDb.createAccountRequest(accountRequest);
AccountRequest actual = accountRequestDb.getAccountRequest(accountRequest.getEmail(), institute);
assertEquals(institute, actual.getInstitute());
}
}
Loading