Skip to content

Commit

Permalink
Merge pull request #671 from akto-api-security/hotfix/concat_vars
Browse files Browse the repository at this point in the history
replace multiple differents vars
  • Loading branch information
avneesh-akto authored Oct 17, 2023
2 parents 41c9992 + 6736ee9 commit 0f3505d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,25 @@ public static String resolveExpression(Map<String, Object> varMap, String expres

Pattern pattern = Pattern.compile("\\$\\{[^}]*\\}");
Matcher matcher = pattern.matcher(expression);
if (matcher.find()) {
while (matcher.find()) {
try {
String match = matcher.group(0);
match = match.substring(2, match.length());
match = match.substring(0, match.length() - 1);
Object val = getValue(varMap, match);
String valString = val.toString();
expression = expression.replaceAll("(\\$\\{[^}]*\\})", valString);
expression = expression.replaceFirst("(\\$\\{[^}]*\\})", valString);
} catch (Exception e) {
return expression;
}
}

Object val = getValue(varMap, expression);
if (val == null) {
return expression;
} else {
Object val = getValue(varMap, expression);
if (val == null) {
return expression;
} else {
return val.toString();
}
return val.toString();
}
return expression;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.akto.test_editor.execution;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;

public class TestVariableResolver {

@Test
public void testResolveExpression() {
Map<String, Object> varMap = new HashMap<>();
varMap.put("var1", "user1");
varMap.put("var2", "user2");
String result = VariableResolver.resolveExpression(varMap, "${var1}");
assertEquals("user1", result);
result = VariableResolver.resolveExpression(varMap, "var1");
assertEquals("user1", result);

result = VariableResolver.resolveExpression(varMap, "${var1}!!!");
assertEquals("user1!!!", result);
result = VariableResolver.resolveExpression(varMap, "var1!!!");
assertEquals("var1!!!", result);

result = VariableResolver.resolveExpression(varMap, "${var1}${var2}");
assertEquals("user1user2", result);
result = VariableResolver.resolveExpression(varMap, "var1${var2}");
assertEquals("var1user2", result);

result = VariableResolver.resolveExpression(varMap, "${var1}&${var2}");
assertEquals("user1&user2", result);
result = VariableResolver.resolveExpression(varMap, "var1&${var2}");
assertEquals("var1&user2", result);

result = VariableResolver.resolveExpression(varMap, "${var3}");
assertEquals("${var3}", result);

result = VariableResolver.resolveExpression(varMap, "${var1}${var1}");
assertEquals("user1user1", result);

result = VariableResolver.resolveExpression(varMap, "akto");
assertEquals("akto", result);
}

}

0 comments on commit 0f3505d

Please sign in to comment.