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

[Fix] fix switch js #15487

Merged
merged 3 commits into from
Feb 5, 2024
Merged
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 @@ -23,6 +23,7 @@
import org.apache.commons.collections4.MapUtils;

import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -33,6 +34,7 @@
import lombok.extern.slf4j.Slf4j;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

@Slf4j
public class SwitchTaskUtils {
Expand All @@ -41,6 +43,15 @@ public class SwitchTaskUtils {
private static final ScriptEngine engine;
private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*";

private static final Set<String> blackKeySet = Sets.newHashSet(
"java",
"invoke",
"new",
"eval",
"function",
"import",
"\\\\");

static {
manager = new ScriptEngineManager();
engine = manager.getEngineByName("js");
Expand Down Expand Up @@ -83,6 +94,12 @@ public static String generateContentWithTaskParams(String condition, Map<String,
content = content.replace("${" + paramName + "}", value);
}

for (String blackKey : blackKeySet) {
if (content.contains(blackKey)) {
throw new IllegalArgumentException("condition is not valid, please check it. condition: " + condition);
}
}

// if not replace any params, throw exception to avoid illegal condition
if (originContent.equals(content)) {
throw new IllegalArgumentException("condition is not valid, please check it. condition: " + condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,19 @@ public void testIllegalCondition() {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
SwitchTaskUtils.generateContentWithTaskParams(content, globalParams, varParams);
});

String cmd = "bash /tmp/shell";
String cmdContent = "java.lang.Runtime.getRuntime().exec(\"${cmd}\")";
globalParams.put("cmd", new Property("cmd", Direct.IN, DataType.VARCHAR, cmd));
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
SwitchTaskUtils.generateContentWithTaskParams(cmdContent, globalParams, varParams);
});

String contentWithUnicode =
"\\\\u006a\\\\u0061\\\\u0076\\\\u0061\\\\u002e\\\\u006c\\\\u0061\\\\u006e\\\\u0067\\\\u002e\\\\u0052\\\\u0075\\\\u006e\\\\u0074\\\\u0069\\\\u006d\\\\u0065.getRuntime().exec(\\\"open -a Calculator.app\\";
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
SwitchTaskUtils.generateContentWithTaskParams(contentWithUnicode, globalParams, varParams);
});

}
}
Loading