forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from markgcera/Add-JsonInjectionParser
Add JsonInjectionParser class and its test classes.
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
src/main/java/seedu/address/logic/parser/JsonInjectionParser.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,35 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* This class handles the sanitisation of user input to prevent any possible JSON injections. | ||
*/ | ||
public class JsonInjectionParser { | ||
// List of possible JSON commands. | ||
private final String quote = "\'"; | ||
private final String hexEscape = "\\x"; | ||
private final String octalEscape = "\\0"; | ||
private final String comma = ","; | ||
private final String openParentheses = "("; | ||
private final String closedParentheses = ")"; | ||
private final String openCurlyBrackets = "{"; | ||
private final String closedCurlyBrackets = "}"; | ||
private String[] wordsToSanitise = | ||
new String[]{quote, hexEscape, octalEscape, comma, openParentheses, closedParentheses, | ||
openCurlyBrackets, closedCurlyBrackets}; | ||
|
||
/** | ||
* This method parses the user input. | ||
* @param args The String input from the user. | ||
* @throws ParseException The exception with the message ot the user to not include characters that could | ||
* form a JSON command. | ||
*/ | ||
public void parse(String args) throws ParseException { | ||
if (Arrays.stream(wordsToSanitise).anyMatch(args::contains)) { | ||
throw new ParseException("Please do not input JSON like content"); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/test/java/seedu/address/logic/parser/JsonInjectionParserTest.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,34 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
public class JsonInjectionParserTest { | ||
private static final JsonInjectionParser parser = new JsonInjectionParser(); | ||
|
||
@Test | ||
public void parse_singleInvalidInput_throwsException() { | ||
String testString = "This is a te'st input"; | ||
Exception exception = assertThrows(ParseException.class, () -> { | ||
parser.parse(testString); | ||
}); | ||
String expectedMessage = "Please do not input JSON like content"; | ||
String actualMessage = exception.getMessage(); | ||
assertTrue(actualMessage.contains(expectedMessage)); | ||
} | ||
|
||
@Test | ||
public void parse_multipleInvalidInputs_throwsException() { | ||
String testString = "This \\x0 is a te'st inp:ut()"; | ||
Exception exception = assertThrows(ParseException.class, () -> { | ||
parser.parse(testString); | ||
}); | ||
String expectedMessage = "Please do not input JSON like content"; | ||
String actualMessage = exception.getMessage(); | ||
assertTrue(actualMessage.contains(expectedMessage)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/seedu/address/logic/parser/UnflagCommandParserTest.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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.UnflagCommand; | ||
|
||
public class UnflagCommandParserTest { | ||
private UnflagCommandParser parser = new UnflagCommandParser(); | ||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
UnflagCommand.MESSAGE_USAGE)); | ||
} | ||
} |