Skip to content

Commit 3aecb04

Browse files
committed
Add XXE test for samuel paschuan's 7/17 vuln
Signed-off-by: Mitch Gaffigan <mitch.gaffigan@comcast.net>
1 parent 9359d9a commit 3aecb04

6 files changed

Lines changed: 368 additions & 0 deletions

File tree

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
<channel version="4.6.0">
2+
<id>d82ce8bd-37a0-4b16-8fc8-1593ab3c4648</id>
3+
<nextMetaDataId>2</nextMetaDataId>
4+
<name>HL7 XXE repro</name>
5+
<description></description>
6+
<revision>3</revision>
7+
<sourceConnector version="4.6.0">
8+
<metaDataId>0</metaDataId>
9+
<name>sourceConnector</name>
10+
<properties class="com.mirth.connect.connectors.vm.VmReceiverProperties" version="4.6.0">
11+
<pluginProperties/>
12+
<sourceConnectorProperties version="4.6.0">
13+
<responseVariable>None</responseVariable>
14+
<respondAfterProcessing>true</respondAfterProcessing>
15+
<processBatch>false</processBatch>
16+
<firstResponse>false</firstResponse>
17+
<processingThreads>1</processingThreads>
18+
<resourceIds class="linked-hash-map">
19+
<entry>
20+
<string>Default Resource</string>
21+
<string>[Default Resource]</string>
22+
</entry>
23+
</resourceIds>
24+
<queueBufferSize>1000</queueBufferSize>
25+
</sourceConnectorProperties>
26+
</properties>
27+
<transformer version="4.6.0">
28+
<elements/>
29+
<inboundDataType>HL7V2</inboundDataType>
30+
<outboundDataType>XML</outboundDataType>
31+
<inboundProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DataTypeProperties" version="4.6.0">
32+
<serializationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2SerializationProperties" version="4.6.0">
33+
<handleRepetitions>true</handleRepetitions>
34+
<handleSubcomponents>true</handleSubcomponents>
35+
<useStrictParser>true</useStrictParser>
36+
<useStrictValidation>true</useStrictValidation>
37+
<stripNamespaces>false</stripNamespaces>
38+
<segmentDelimiter>\r</segmentDelimiter>
39+
<convertLineBreaks>true</convertLineBreaks>
40+
</serializationProperties>
41+
<deserializationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DeserializationProperties" version="4.6.0">
42+
<useStrictParser>false</useStrictParser>
43+
<useStrictValidation>false</useStrictValidation>
44+
<segmentDelimiter>\r</segmentDelimiter>
45+
</deserializationProperties>
46+
<batchProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2BatchProperties" version="4.6.0">
47+
<splitType>MSH_Segment</splitType>
48+
<batchScript></batchScript>
49+
</batchProperties>
50+
<responseGenerationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2ResponseGenerationProperties" version="4.6.0">
51+
<segmentDelimiter>\r</segmentDelimiter>
52+
<successfulACKCode>AA</successfulACKCode>
53+
<successfulACKMessage></successfulACKMessage>
54+
<errorACKCode>AE</errorACKCode>
55+
<errorACKMessage>An Error Occurred Processing Message.</errorACKMessage>
56+
<rejectedACKCode>AR</rejectedACKCode>
57+
<rejectedACKMessage>Message Rejected.</rejectedACKMessage>
58+
<msh15ACKAccept>false</msh15ACKAccept>
59+
<dateFormat>yyyyMMddHHmmss.SSS</dateFormat>
60+
</responseGenerationProperties>
61+
<responseValidationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2ResponseValidationProperties" version="4.6.0">
62+
<successfulACKCode>AA,CA</successfulACKCode>
63+
<errorACKCode>AE,CE</errorACKCode>
64+
<rejectedACKCode>AR,CR</rejectedACKCode>
65+
<validateMessageControlId>true</validateMessageControlId>
66+
<originalMessageControlId>Destination_Encoded</originalMessageControlId>
67+
<originalIdMapVariable></originalIdMapVariable>
68+
</responseValidationProperties>
69+
</inboundProperties>
70+
<outboundProperties class="com.mirth.connect.plugins.datatypes.xml.XMLDataTypeProperties" version="4.6.0">
71+
<serializationProperties class="com.mirth.connect.plugins.datatypes.xml.XMLSerializationProperties" version="4.6.0">
72+
<stripNamespaces>false</stripNamespaces>
73+
</serializationProperties>
74+
<batchProperties class="com.mirth.connect.plugins.datatypes.xml.XMLBatchProperties" version="4.6.0">
75+
<splitType>Element_Name</splitType>
76+
<elementName></elementName>
77+
<level>1</level>
78+
<query></query>
79+
<batchScript></batchScript>
80+
</batchProperties>
81+
</outboundProperties>
82+
</transformer>
83+
<filter version="4.6.0">
84+
<elements/>
85+
</filter>
86+
<transportName>Channel Reader</transportName>
87+
<mode>SOURCE</mode>
88+
<enabled>true</enabled>
89+
<waitForPrevious>true</waitForPrevious>
90+
</sourceConnector>
91+
<destinationConnectors>
92+
<connector version="4.6.0">
93+
<metaDataId>1</metaDataId>
94+
<name>Destination 1</name>
95+
<properties class="com.mirth.connect.connectors.vm.VmDispatcherProperties" version="4.6.0">
96+
<pluginProperties/>
97+
<destinationConnectorProperties version="4.6.0">
98+
<queueEnabled>false</queueEnabled>
99+
<sendFirst>false</sendFirst>
100+
<retryIntervalMillis>10000</retryIntervalMillis>
101+
<regenerateTemplate>false</regenerateTemplate>
102+
<retryCount>0</retryCount>
103+
<rotate>false</rotate>
104+
<includeFilterTransformer>false</includeFilterTransformer>
105+
<threadCount>1</threadCount>
106+
<threadAssignmentVariable></threadAssignmentVariable>
107+
<validateResponse>false</validateResponse>
108+
<resourceIds class="linked-hash-map">
109+
<entry>
110+
<string>Default Resource</string>
111+
<string>[Default Resource]</string>
112+
</entry>
113+
</resourceIds>
114+
<queueBufferSize>1000</queueBufferSize>
115+
<reattachAttachments>true</reattachAttachments>
116+
</destinationConnectorProperties>
117+
<channelId>none</channelId>
118+
<channelTemplate>${message.encodedData}</channelTemplate>
119+
<mapVariables/>
120+
</properties>
121+
<transformer version="4.6.0">
122+
<elements/>
123+
<inboundDataType>XML</inboundDataType>
124+
<outboundDataType>XML</outboundDataType>
125+
<inboundProperties class="com.mirth.connect.plugins.datatypes.xml.XMLDataTypeProperties" version="4.6.0">
126+
<serializationProperties class="com.mirth.connect.plugins.datatypes.xml.XMLSerializationProperties" version="4.6.0">
127+
<stripNamespaces>false</stripNamespaces>
128+
</serializationProperties>
129+
<batchProperties class="com.mirth.connect.plugins.datatypes.xml.XMLBatchProperties" version="4.6.0">
130+
<splitType>Element_Name</splitType>
131+
<elementName></elementName>
132+
<level>1</level>
133+
<query></query>
134+
<batchScript></batchScript>
135+
</batchProperties>
136+
</inboundProperties>
137+
<outboundProperties class="com.mirth.connect.plugins.datatypes.xml.XMLDataTypeProperties" version="4.6.0">
138+
<serializationProperties class="com.mirth.connect.plugins.datatypes.xml.XMLSerializationProperties" version="4.6.0">
139+
<stripNamespaces>false</stripNamespaces>
140+
</serializationProperties>
141+
<batchProperties class="com.mirth.connect.plugins.datatypes.xml.XMLBatchProperties" version="4.6.0">
142+
<splitType>Element_Name</splitType>
143+
<elementName></elementName>
144+
<level>1</level>
145+
<query></query>
146+
<batchScript></batchScript>
147+
</batchProperties>
148+
</outboundProperties>
149+
</transformer>
150+
<responseTransformer version="4.6.0">
151+
<elements/>
152+
<inboundDataType>HL7V2</inboundDataType>
153+
<outboundDataType>XML</outboundDataType>
154+
<inboundProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DataTypeProperties" version="4.6.0">
155+
<serializationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2SerializationProperties" version="4.6.0">
156+
<handleRepetitions>true</handleRepetitions>
157+
<handleSubcomponents>true</handleSubcomponents>
158+
<useStrictParser>false</useStrictParser>
159+
<useStrictValidation>false</useStrictValidation>
160+
<stripNamespaces>false</stripNamespaces>
161+
<segmentDelimiter>\r</segmentDelimiter>
162+
<convertLineBreaks>true</convertLineBreaks>
163+
</serializationProperties>
164+
<deserializationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DeserializationProperties" version="4.6.0">
165+
<useStrictParser>false</useStrictParser>
166+
<useStrictValidation>false</useStrictValidation>
167+
<segmentDelimiter>\r</segmentDelimiter>
168+
</deserializationProperties>
169+
<batchProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2BatchProperties" version="4.6.0">
170+
<splitType>MSH_Segment</splitType>
171+
<batchScript></batchScript>
172+
</batchProperties>
173+
<responseGenerationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2ResponseGenerationProperties" version="4.6.0">
174+
<segmentDelimiter>\r</segmentDelimiter>
175+
<successfulACKCode>AA</successfulACKCode>
176+
<successfulACKMessage></successfulACKMessage>
177+
<errorACKCode>AE</errorACKCode>
178+
<errorACKMessage>An Error Occurred Processing Message.</errorACKMessage>
179+
<rejectedACKCode>AR</rejectedACKCode>
180+
<rejectedACKMessage>Message Rejected.</rejectedACKMessage>
181+
<msh15ACKAccept>false</msh15ACKAccept>
182+
<dateFormat>yyyyMMddHHmmss.SSS</dateFormat>
183+
</responseGenerationProperties>
184+
<responseValidationProperties class="com.mirth.connect.plugins.datatypes.hl7v2.HL7v2ResponseValidationProperties" version="4.6.0">
185+
<successfulACKCode>AA,CA</successfulACKCode>
186+
<errorACKCode>AE,CE</errorACKCode>
187+
<rejectedACKCode>AR,CR</rejectedACKCode>
188+
<validateMessageControlId>true</validateMessageControlId>
189+
<originalMessageControlId>Destination_Encoded</originalMessageControlId>
190+
<originalIdMapVariable></originalIdMapVariable>
191+
</responseValidationProperties>
192+
</inboundProperties>
193+
<outboundProperties class="com.mirth.connect.plugins.datatypes.xml.XMLDataTypeProperties" version="4.6.0">
194+
<serializationProperties class="com.mirth.connect.plugins.datatypes.xml.XMLSerializationProperties" version="4.6.0">
195+
<stripNamespaces>false</stripNamespaces>
196+
</serializationProperties>
197+
<batchProperties class="com.mirth.connect.plugins.datatypes.xml.XMLBatchProperties" version="4.6.0">
198+
<splitType>Element_Name</splitType>
199+
<elementName></elementName>
200+
<level>1</level>
201+
<query></query>
202+
<batchScript></batchScript>
203+
</batchProperties>
204+
</outboundProperties>
205+
</responseTransformer>
206+
<filter version="4.6.0">
207+
<elements/>
208+
</filter>
209+
<transportName>Channel Writer</transportName>
210+
<mode>DESTINATION</mode>
211+
<enabled>true</enabled>
212+
<waitForPrevious>true</waitForPrevious>
213+
</connector>
214+
</destinationConnectors>
215+
<preprocessingScript>// Modify the message variable below to pre process data
216+
return message;</preprocessingScript>
217+
<postprocessingScript>// This script executes once after a message has been processed
218+
// Responses returned from here will be stored as &quot;Postprocessor&quot; in the response map
219+
return;</postprocessingScript>
220+
<deployScript>// This script executes once when the channel is deployed
221+
// You only have access to the globalMap and globalChannelMap here to persist data
222+
return;</deployScript>
223+
<undeployScript>// This script executes once when the channel is undeployed
224+
// You only have access to the globalMap and globalChannelMap here to persist data
225+
return;</undeployScript>
226+
<properties version="4.6.0">
227+
<clearGlobalChannelMap>true</clearGlobalChannelMap>
228+
<messageStorageMode>DEVELOPMENT</messageStorageMode>
229+
<encryptData>false</encryptData>
230+
<encryptAttachments>false</encryptAttachments>
231+
<encryptCustomMetaData>false</encryptCustomMetaData>
232+
<removeContentOnCompletion>false</removeContentOnCompletion>
233+
<removeOnlyFilteredOnCompletion>false</removeOnlyFilteredOnCompletion>
234+
<removeAttachmentsOnCompletion>false</removeAttachmentsOnCompletion>
235+
<initialState>STARTED</initialState>
236+
<storeAttachments>true</storeAttachments>
237+
<metaDataColumns>
238+
<metaDataColumn>
239+
<name>SOURCE</name>
240+
<type>STRING</type>
241+
<mappingName>mirth_source</mappingName>
242+
</metaDataColumn>
243+
<metaDataColumn>
244+
<name>TYPE</name>
245+
<type>STRING</type>
246+
<mappingName>mirth_type</mappingName>
247+
</metaDataColumn>
248+
</metaDataColumns>
249+
<attachmentProperties version="4.6.0">
250+
<type>None</type>
251+
<properties/>
252+
</attachmentProperties>
253+
<resourceIds class="linked-hash-map">
254+
<entry>
255+
<string>Default Resource</string>
256+
<string>[Default Resource]</string>
257+
</entry>
258+
</resourceIds>
259+
</properties>
260+
<exportData>
261+
<metadata>
262+
<enabled>true</enabled>
263+
<lastModified>
264+
<time>1789825765822</time>
265+
<timezone>America/Chicago</timezone>
266+
</lastModified>
267+
<pruningSettings>
268+
<archiveEnabled>true</archiveEnabled>
269+
<pruneErroredMessages>false</pruneErroredMessages>
270+
</pruningSettings>
271+
<userId>1</userId>
272+
</metadata>
273+
</exportData>
274+
</channel>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE ACK [ <!ENTITY xxe SYSTEM "file:///etc/hostname"> ]>
3+
<ACK xmlns="urn:hl7-org:v2xml"><MSH><MSH.1>|</MSH.1><MSH.2>^~\&amp;</MSH.2><MSH.3><HD.1>&xxe;</HD.1></MSH.3><MSH.9><MSG.1>ACK</MSG.1></MSH.9><MSH.10>1</MSH.10><MSH.12><VID.1>2.4</VID.1></MSH.12></MSH><MSA><MSA.1>AA</MSA.1><MSA.2>1</MSA.2></MSA></ACK>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ERROR
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0"?>
2+
<ACK xmlns="urn:hl7-org:v2xml"><MSH><MSH.1>|</MSH.1><MSH.2>^~\&amp;</MSH.2><MSH.3><HD.1>asdf</HD.1></MSH.3><MSH.9><MSG.1>ACK</MSG.1></MSH.9><MSH.10>1</MSH.10><MSH.12><VID.1>2.4</VID.1></MSH.12></MSH><MSA><MSA.1>AA</MSA.1><MSA.2>1</MSA.2></MSA></ACK>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TRANSFORMED
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.mirth.connect.plugins.datatypes.hl7v2;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.File;
8+
9+
import org.apache.commons.io.FileUtils;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
13+
import com.mirth.connect.donkey.model.message.MessageSerializerException;
14+
import com.mirth.connect.model.datatype.SerializerProperties;
15+
16+
/**
17+
* XXE coverage for {@link ER7Serializer#fromXML(String)}. The lenient handler and the strict (HAPI)
18+
* parser are independent code paths; ci/tests/205-hl7-xxe covers the strict one end to end.
19+
*
20+
* Every payload declares a dummy entity holding " xmlns=". fromXML injects a namespace into the
21+
* first '>'-delimited chunk of the message when that chunk does not already contain one, and with a
22+
* DOCTYPE present the chunk is an entity declaration rather than the root element. The injection
23+
* then corrupts the declaration into malformed XML and the parser rejects the message for being
24+
* malformed instead of for its external entity, which makes an XXE test pass for the wrong reason.
25+
* The dummy entity satisfies the check so the payload reaches the parser intact.
26+
*/
27+
public class ER7SerializerXxeTest {
28+
29+
private static File secret;
30+
31+
@BeforeClass
32+
public static void writeSecret() throws Exception {
33+
secret = File.createTempFile("xxe", ".txt");
34+
secret.deleteOnExit();
35+
FileUtils.write(secret, "canary", UTF_8);
36+
}
37+
38+
@Test
39+
public void lenientParserDoesNotResolveExternalEntity() {
40+
assertExternalEntityNotResolved(false, false);
41+
}
42+
43+
@Test
44+
public void strictParserDoesNotResolveExternalEntity() {
45+
assertExternalEntityNotResolved(true, false);
46+
}
47+
48+
@Test
49+
public void strictValidatingParserDoesNotResolveExternalEntity() {
50+
assertExternalEntityNotResolved(true, true);
51+
}
52+
53+
/** Guards the payload shape, so the tests above exercise a message the parser really reads. */
54+
@Test
55+
public void strictParserAcceptsDoctypeWithoutExternalEntity() throws Exception {
56+
assertTrue(serializer(true, false).fromXML(message("", "asdf")).contains("asdf"));
57+
}
58+
59+
/** Dropping the entity and rejecting the message are both fine; leaking the file is not. */
60+
private static void assertExternalEntityNotResolved(boolean strictParser, boolean strictValidation) {
61+
String xml = message("<!ENTITY xxe SYSTEM \"" + secret.toURI() + "\">", "&xxe;");
62+
String outcome;
63+
64+
try {
65+
outcome = serializer(strictParser, strictValidation).fromXML(xml);
66+
} catch (MessageSerializerException e) {
67+
outcome = String.valueOf(e);
68+
}
69+
70+
assertFalse(outcome, outcome.contains("canary"));
71+
}
72+
73+
private static ER7Serializer serializer(boolean strictParser, boolean strictValidation) {
74+
HL7v2DeserializationProperties properties = new HL7v2DeserializationProperties();
75+
properties.setUseStrictParser(strictParser);
76+
properties.setUseStrictValidation(strictValidation);
77+
return new ER7Serializer(new SerializerProperties(new HL7v2SerializationProperties(), properties, null));
78+
}
79+
80+
private static String message(String entityDeclaration, String sendingApplication) {
81+
return "<?xml version=\"1.0\"?>\n<!DOCTYPE ACK [ <!ENTITY ns \" xmlns=\" > " + entityDeclaration + " ]>\n"
82+
+ "<ACK xmlns=\"urn:hl7-org:v2xml\"><MSH><MSH.1>|</MSH.1><MSH.2>^~\\&amp;</MSH.2>"
83+
+ "<MSH.3><HD.1>" + sendingApplication + "</HD.1></MSH.3><MSH.9><MSG.1>ACK</MSG.1></MSH.9>"
84+
+ "<MSH.10>1</MSH.10><MSH.12><VID.1>2.4</VID.1></MSH.12></MSH>"
85+
+ "<MSA><MSA.1>AA</MSA.1><MSA.2>1</MSA.2></MSA></ACK>";
86+
}
87+
}

0 commit comments

Comments
 (0)