Skip to content

handling unicode characters in test editor #2546

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -68,6 +68,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.akto.util.enums.GlobalEnums.YamlTemplateSource;

Expand Down Expand Up @@ -134,6 +136,7 @@ private String getInfoKeyMissing(Info info){
public String saveTestEditorFile() {
TestConfig testConfig;
try {
this.content = (escapeUnicodeLiterals(content));
ObjectMapper mapper = new ObjectMapper(YAMLFactory.builder()
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.disable(YAMLGenerator.Feature.SPLIT_LINES)
Expand Down Expand Up @@ -212,7 +215,7 @@ public String saveTestEditorFile() {
config.replace("id", finalTestId);
infoMap.put("name", finalTestName);

this.content = mapper.writeValueAsString(config);
this.content = unescapeUnicodeLiterals(mapper.writeValueAsString(config));
testConfig = TestConfigYamlParser.parseTemplate(content);
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -262,6 +265,22 @@ public String saveTestEditorFile() {
return SUCCESS.toUpperCase();
}

private static String escapeUnicodeLiterals(String yamlContent) {
Pattern pattern = Pattern.compile("(?<!\\\\)\\\\u([0-9a-fA-F]{4})");
Matcher matcher = pattern.matcher(yamlContent);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String unicode = matcher.group(1);
matcher.appendReplacement(sb, Matcher.quoteReplacement("__AKTO_ESC__u" + unicode));
}
matcher.appendTail(sb);
return sb.toString();
}

private static String unescapeUnicodeLiterals(String yaml) {
return yaml.replaceAll("__AKTO_ESC__u([0-9a-fA-F]{4})", "\\\\u$1");
}

public static int compareVersions(String v1, String v2) {
try {
String[] parts1 = v1.split("\\.");
Expand Down
Loading