Skip to content

Commit c1403a0

Browse files
committed
Don't allow illegal characters in XmlAttrFilter
Fixes GHSA-h75v-3vvj-5mfj
1 parent 016e299 commit c1403a0

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.Map;
1010
import java.util.Objects;
11+
import java.util.regex.Pattern;
1112
import org.apache.commons.lang3.BooleanUtils;
1213
import org.apache.commons.lang3.StringEscapeUtils;
1314
import org.apache.commons.lang3.StringUtils;
@@ -37,6 +38,11 @@
3738
)
3839
public class XmlAttrFilter implements Filter {
3940

41+
// See https://html.spec.whatwg.org/#attribute-name-state Don't allow characters that would change the attribute name/value state
42+
private static final Pattern ILLEGAL_ATTRIBUTE_KEY_PATTERN = Pattern.compile(
43+
"[\\s/>=]"
44+
);
45+
4046
@Override
4147
public String getName() {
4248
return "xmlattr";
@@ -53,6 +59,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
5359
List<String> attrs = new ArrayList<>();
5460

5561
for (Map.Entry<String, Object> entry : dict.entrySet()) {
62+
if (ILLEGAL_ATTRIBUTE_KEY_PATTERN.matcher(entry.getKey()).find()) {
63+
throw new IllegalArgumentException(
64+
String.format("Invalid character in attribute name: %s", entry.getKey())
65+
);
66+
}
5667
attrs.add(
5768
new StringBuilder(entry.getKey())
5869
.append("=\"")

src/test/java/com/hubspot/jinjava/lib/filter/XmlAttrFilterTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import com.google.common.collect.ImmutableList;
56
import com.hubspot.jinjava.BaseJinjavaTest;
7+
import java.util.Collections;
68
import java.util.HashMap;
9+
import java.util.List;
710
import java.util.Map;
811
import org.jsoup.Jsoup;
912
import org.jsoup.nodes.Document;
@@ -27,4 +30,27 @@ public void testXmlAttr() {
2730
assertThat(dom.select("ul").attr("id")).isEqualTo("list-42");
2831
assertThat(dom.select("ul").attr("missing")).isEmpty();
2932
}
33+
34+
@Test
35+
public void itDoesNotAllowInvalidKeys() {
36+
List<String> invalidStrings = ImmutableList.of("\t", "\n", "\f", " ", "/", ">", "=");
37+
invalidStrings.forEach(invalidString ->
38+
assertThat(
39+
jinjava
40+
.renderForResult(
41+
String.format("{{ {'%s': 'foo'}|xmlattr }}", invalidString),
42+
Collections.emptyMap()
43+
)
44+
.getErrors()
45+
)
46+
.matches(templateErrors ->
47+
templateErrors.size() == 1 &&
48+
templateErrors
49+
.get(0)
50+
.getException()
51+
.getCause()
52+
.getCause() instanceof IllegalArgumentException
53+
)
54+
);
55+
}
3056
}

0 commit comments

Comments
 (0)