Skip to content

Commit

Permalink
fix(tsl): ${message} escaping incorrectly
Browse files Browse the repository at this point in the history
- Fixed ${message} escaping incorrectly.
- Added ${message_unescaped} for non-string usages
- Fixed EXECUTE attempting to execute DISPLAYING words too
  • Loading branch information
iGoodie committed Jun 22, 2020
1 parent 444cdad commit 0beaac2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Expand Up @@ -25,7 +25,7 @@ public ExecuteAction(List<String> words) throws TSLSyntaxError {
if (!actionWords.stream().allMatch(word -> word.startsWith("/")))
throw new TSLSyntaxError("Every command must start with '/' character");

this.commands = new LinkedList<>(words);
this.commands = new LinkedList<>(actionWords);
}

@Override
Expand Down
Expand Up @@ -47,6 +47,9 @@ public static String fromArgs(String expression, EventArguments args) {
if (expression.equals("message"))
return JSONUtils.escape(args.message);

if (expression.equals("message_unescaped"))
return args.message;

if (expression.equals("title"))
return args.rewardTitle;

Expand Down
Expand Up @@ -49,22 +49,12 @@ public static void forEach(JSONArray array, Consumer<JSONObject> consumer) {
public static String escape(String jsonString) {
StringBuilder escapedString = new StringBuilder();

boolean isEscaping = false;

for (char character : jsonString.toCharArray()) {
if (character == '\\' && !isEscaping) {
isEscaping = true;
escapedString.append(character);
continue;
}

if (character == '\'' || character == '\"' || character == '\\') {
if (!isEscaping)
escapedString.append("\\");
if (character == '\'' || character == '"' || character == '\\') {
escapedString.append("\\");
}

escapedString.append(character);
isEscaping = false;
}

return escapedString.toString();
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/programmer/igoodie/twitchspawn/UtilsTests.java
@@ -1,7 +1,10 @@
package net.programmer.igoodie.twitchspawn;

import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLParser;
import net.programmer.igoodie.twitchspawn.util.JSONUtils;
import net.programmer.igoodie.twitchspawn.util.PercentageRandomizer;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -80,4 +83,20 @@ public void pencentageBelow100Test() {
});
}

@Test
@DisplayName("should escape JSON strings successfully.")
public void jsonStringEscapistTest() throws JSONException {
// !drop " \" \ \\ \' \\' \\\
String original = "!drop \" \\\" \\ \\\\ \\' \\\\' \\\\\\";
String escaped = JSONUtils.escape(original);

String jsonString = String.format("{text:'%s'}", escaped);
System.out.println("Original : " + original);
System.out.println("Formatted: " + escaped);

JSONObject json = new JSONObject(jsonString);
System.out.println("Parsed: " + json);
System.out.println("Text : " + json.getString("text"));
}

}

0 comments on commit 0beaac2

Please sign in to comment.