Skip to content

Commit dcb2781

Browse files
committed
Prevent XML xxe on HL7 parsing
Signed-off-by: Mitch Gaffigan <mitch.gaffigan@comcast.net>
1 parent 302ebab commit dcb2781

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎server/src/main/java/com/mirth/connect/plugins/datatypes/hl7v2/ER7Serializer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public String toXML(String source) throws MessageSerializerException {
199199
Message message = null;
200200
source = source.trim();
201201

202-
if (source.length() > 0 && source.charAt(0) == '<') {
202+
if (serializationProperties.isAllowXml() && source.length() > 0 && source.charAt(0) == '<') {
203203
if (serializationProperties.isUseStrictValidation()) {
204204
// If the message is XML and strict validation is needed, we'll need to create a message to be encoded.
205205
message = serializationXmlParser.parse(source);

‎server/src/main/java/com/mirth/connect/plugins/datatypes/hl7v2/HL7v2SerializationProperties.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class HL7v2SerializationProperties extends SerializationProperties {
2323
private boolean handleSubcomponents = true;
2424
private boolean useStrictParser = false;
2525
private boolean useStrictValidation = false;
26+
private boolean allowXml = false;
2627
private boolean stripNamespaces = false;
2728
private String segmentDelimiter = "\\r";
2829
private boolean convertLineBreaks = true;
@@ -35,6 +36,7 @@ public Map<String, DataTypePropertyDescriptor> getPropertyDescriptors() {
3536
properties.put("handleSubcomponents", new DataTypePropertyDescriptor(handleSubcomponents, "Parse Subcomponents", "Parse subcomponents (applies to Non-Strict Parser only).", PropertyEditorType.BOOLEAN));
3637
properties.put("useStrictParser", new DataTypePropertyDescriptor(useStrictParser, "Use Strict Parser", "Parse messages based upon strict HL7 specifications.", PropertyEditorType.BOOLEAN));
3738
properties.put("useStrictValidation", new DataTypePropertyDescriptor(useStrictValidation, "Validate in Strict Parser", "Validate messages using HL7 specifications (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
39+
properties.put("allowXml", new DataTypePropertyDescriptor(allowXml, "Allow XML Messages", "Allow input that appears to be XML-formatted to be parsed as HL7-XML instead of being rejected as invalid ER7 (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
3840
properties.put("stripNamespaces", new DataTypePropertyDescriptor(stripNamespaces, "Strip Namespaces", "Strips namespace definitions from the transformed XML message (applies to Strict Parser only).", PropertyEditorType.BOOLEAN));
3941
properties.put("segmentDelimiter", new DataTypePropertyDescriptor(segmentDelimiter, "Segment Delimiter", "This is the input delimiter character(s) expected to occur after each segment.", PropertyEditorType.STRING));
4042
properties.put("convertLineBreaks", new DataTypePropertyDescriptor(convertLineBreaks, "Convert Line Breaks", "Convert all styles of line breaks (CRLF, CR, LF) in the raw message to the segment delimiter. ", PropertyEditorType.BOOLEAN));
@@ -61,6 +63,10 @@ public void setProperties(Map<String, Object> properties) {
6163
this.useStrictValidation = (Boolean) properties.get("useStrictValidation");
6264
}
6365

66+
if (properties.get("allowXml") != null) {
67+
this.allowXml = (Boolean) properties.get("allowXml");
68+
}
69+
6470
if (properties.get("stripNamespaces") != null) {
6571
this.stripNamespaces = (Boolean) properties.get("stripNamespaces");
6672
}
@@ -107,6 +113,14 @@ public void setUseStrictValidation(boolean useStrictValidation) {
107113
this.useStrictValidation = useStrictValidation;
108114
}
109115

116+
public boolean isAllowXml() {
117+
return allowXml;
118+
}
119+
120+
public void setAllowXml(boolean allowXml) {
121+
this.allowXml = allowXml;
122+
}
123+
110124
public boolean isStripNamespaces() {
111125
return stripNamespaces;
112126
}
@@ -152,6 +166,7 @@ public Map<String, Object> getPurgedProperties() {
152166
purgedProperties.put("handleRepetitions", handleRepetitions);
153167
purgedProperties.put("handleSubcomponents", handleSubcomponents);
154168
purgedProperties.put("useStrictParser", useStrictParser);
169+
purgedProperties.put("allowXml", allowXml);
155170
purgedProperties.put("stripNamespaces", stripNamespaces);
156171
purgedProperties.put("convertLineBreaks", convertLineBreaks);
157172
return purgedProperties;

‎server/src/test/java/com/mirth/connect/plugins/datatypes/hl7v2/ER7SerializerTest.java‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mirth.connect.plugins.datatypes.hl7v2;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
56

@@ -15,11 +16,22 @@
1516

1617
public class ER7SerializerTest {
1718
private static ER7Serializer serializer;
18-
19+
private static ER7Serializer strictSerializer;
20+
private static ER7Serializer strictSerializerAllowXml;
21+
1922
@BeforeClass
2023
public static void setupClass() throws Exception {
2124
SerializerProperties serializerProperties = new SerializerProperties(new HL7v2SerializationProperties(), new HL7v2DeserializationProperties(), null);
2225
serializer = new ER7Serializer(serializerProperties);
26+
27+
HL7v2SerializationProperties strictProperties = new HL7v2SerializationProperties();
28+
strictProperties.setUseStrictParser(true);
29+
strictSerializer = new ER7Serializer(new SerializerProperties(strictProperties, new HL7v2DeserializationProperties(), null));
30+
31+
HL7v2SerializationProperties strictPropertiesAllowXml = new HL7v2SerializationProperties();
32+
strictPropertiesAllowXml.setUseStrictParser(true);
33+
strictPropertiesAllowXml.setAllowXml(true);
34+
strictSerializerAllowXml = new ER7Serializer(new SerializerProperties(strictPropertiesAllowXml, new HL7v2DeserializationProperties(), null));
2335
}
2436

2537
@Test
@@ -53,4 +65,27 @@ public void testValidFromXMLWithExternalDTD() throws Exception {
5365

5466
assertFalse(exceptionThrown);
5567
}
68+
69+
@Test
70+
public void testToXmlWithStrictParserRejectsXmlInputByDefault() throws Exception {
71+
String xmlDisguisedAsHl7 = "<foo><bar>notreallyhl7</bar></foo>";
72+
73+
boolean exceptionThrown = false;
74+
try {
75+
strictSerializer.toXML(xmlDisguisedAsHl7);
76+
} catch (MessageSerializerException e) {
77+
exceptionThrown = true;
78+
}
79+
80+
assertTrue(exceptionThrown);
81+
}
82+
83+
@Test
84+
public void testToXmlWithStrictParserAllowsXmlInputWhenOptedIn() throws Exception {
85+
String xmlDisguisedAsHl7 = "<foo><bar>notreallyhl7</bar></foo>";
86+
87+
String result = strictSerializerAllowXml.toXML(xmlDisguisedAsHl7);
88+
89+
assertEquals(xmlDisguisedAsHl7, result);
90+
}
5691
}

0 commit comments

Comments
 (0)