Skip to content

Commit ebcab81

Browse files
authored
Escape regex replacement patterns in environment variables (#157)
Fixes: #156
1 parent 9452c3b commit ebcab81

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9+
### Fixed
10+
- [Java] Escape regex replacement patterns in environment variables ([#156](https://github.com/cucumber/ci-environment/issues/156), [#157](https://github.com/cucumber/ci-environment/pull/157))
11+
912
### Added
1013
- [Go] added ci-environment implementation in Go
1114

java/src/main/java/io/cucumber/cienvironment/VariableExpression.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.regex.Matcher;
55
import java.util.regex.Pattern;
66

7+
import static java.util.regex.Matcher.quoteReplacement;
8+
79
final class VariableExpression {
810
private static final Pattern variablePattern = Pattern.compile("\\$\\{(.*?)(?:(?<!\\\\)/(.*)/(.*))?}");
911

@@ -23,7 +25,7 @@ static String evaluate(String expression, Map<String, String> env) {
2325
}
2426
String pattern = variableMatcher.group(2);
2527
if (pattern == null) {
26-
variableMatcher.appendReplacement(sb, value);
28+
variableMatcher.appendReplacement(sb, quoteReplacement(value));
2729
} else {
2830
Matcher matcher = Pattern.compile(pattern.replace("\\/", "/")).matcher(value);
2931
if (!matcher.matches()) {
@@ -34,7 +36,7 @@ static String evaluate(String expression, Map<String, String> env) {
3436
String group = matcher.group(i + 1);
3537
replacement = replacement.replace("\\" + (i + 1), group);
3638
}
37-
variableMatcher.appendReplacement(sb, replacement);
39+
variableMatcher.appendReplacement(sb, quoteReplacement(replacement));
3840
}
3941
}
4042
variableMatcher.appendTail(sb);

java/src/test/java/io/cucumber/cienvironment/VariableExpressionTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public void it_gets_a_value_without_replacement() {
2424
}});
2525
assertEquals("some_value", result);
2626
}
27+
@Test
28+
public void it_escapes_a_value_without_replacement() {
29+
String expression = "${SOME_VAR}";
30+
String result = evaluate(expression, new HashMap<String, String>() {{
31+
put("SOME_VAR", "${SOME_VAR}");
32+
}});
33+
assertEquals("${SOME_VAR}", result);
34+
}
2735

2836
@Test
2937
public void it_captures_a_group() {
@@ -52,4 +60,13 @@ public void it_evaluates_a_complex_expression() {
5260
}});
5361
assertEquals("hello-amazing-beautiful-gorgeous-world", result);
5462
}
63+
@Test
64+
public void it_escapes_a_complex_expression() {
65+
String expression = "hello-${VAR1}-${VAR2/(.*) (.*)/\\2-\\1}-world";
66+
String result = evaluate(expression, new HashMap<String, String>() {{
67+
put("VAR1", "${VAR1}");
68+
put("VAR2", "${VAR2a} ${VAR2b}");
69+
}});
70+
assertEquals("hello-${VAR1}-${VAR2b}-${VAR2a}-world", result);
71+
}
5572
}

0 commit comments

Comments
 (0)