|
6 | 6 | import java.io.File; |
7 | 7 |
|
8 | 8 | import org.apache.commons.io.FileUtils; |
| 9 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
9 | 10 | import org.junit.BeforeClass; |
10 | 11 | import org.junit.Test; |
11 | 12 | import org.xml.sax.SAXParseException; |
|
15 | 16 |
|
16 | 17 | public class ER7SerializerTest { |
17 | 18 | private static ER7Serializer serializer; |
18 | | - |
| 19 | + // Strict parser with strict validation: XML input is parsed by HAPI (the XXE sink). |
| 20 | + private static ER7Serializer strictValidatingSerializer; |
| 21 | + |
19 | 22 | @BeforeClass |
20 | 23 | public static void setupClass() throws Exception { |
21 | 24 | SerializerProperties serializerProperties = new SerializerProperties(new HL7v2SerializationProperties(), new HL7v2DeserializationProperties(), null); |
22 | 25 | serializer = new ER7Serializer(serializerProperties); |
| 26 | + |
| 27 | + HL7v2SerializationProperties strictValidatingProperties = new HL7v2SerializationProperties(); |
| 28 | + strictValidatingProperties.setUseStrictParser(true); |
| 29 | + strictValidatingProperties.setUseStrictValidation(true); |
| 30 | + strictValidatingSerializer = new ER7Serializer(new SerializerProperties(strictValidatingProperties, new HL7v2DeserializationProperties(), null)); |
23 | 31 | } |
24 | | - |
| 32 | + |
25 | 33 | @Test |
26 | 34 | public void testFromXMLWithExternalDTD() throws Exception { |
27 | 35 | String xml = FileUtils.readFileToString(new File("tests/test-xxe-hl7-example.xml"), "UTF-8"); |
28 | | - |
| 36 | + |
29 | 37 | boolean exceptionThrown = false; |
30 | 38 | try { |
31 | 39 | serializer.fromXML(xml); |
32 | 40 | } catch (MessageSerializerException e) { |
33 | 41 | exceptionThrown = true; |
34 | | - |
| 42 | + |
35 | 43 | // See https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#jaxp-documentbuilderfactory-saxparserfactory-and-dom4j |
36 | 44 | assertTrue(e.getCause() instanceof SAXParseException); |
37 | 45 | } |
38 | | - |
| 46 | + |
39 | 47 | assertTrue(exceptionThrown); |
40 | 48 | } |
41 | 49 |
|
42 | 50 | @Test |
43 | 51 | public void testValidFromXMLWithExternalDTD() throws Exception { |
44 | 52 | String xml = FileUtils.readFileToString(new File("tests/test-xxe-hl7-example-valid.xml"), "UTF-8"); |
45 | | - |
| 53 | + |
46 | 54 | boolean exceptionThrown = false; |
47 | 55 | try { |
48 | 56 | serializer.fromXML(xml); |
49 | 57 | } catch (MessageSerializerException e) { |
50 | 58 | exceptionThrown = true; |
51 | | - |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + assertFalse(exceptionThrown); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testToXmlStrictValidatingRejectsExternalDTD() throws Exception { |
| 67 | + // A DOCTYPE-bearing message on the strict-parser toXML path is the unauthenticated MLLP XXE |
| 68 | + // vector (HAPI 2.3 resolved external entities). It must be rejected rather than have its |
| 69 | + // external entity resolved. Note: this asserts the intended behavior; the discriminating |
| 70 | + // before/after proof that the override (not incidental parser behavior) closes the XXE is the |
| 71 | + // live MLLP reproduction documented in the PR. |
| 72 | + String xml = FileUtils.readFileToString(new File("tests/test-xxe-hl7-strict-mllp.xml"), "UTF-8"); |
| 73 | + |
| 74 | + boolean exceptionThrown = false; |
| 75 | + try { |
| 76 | + strictValidatingSerializer.toXML(xml); |
| 77 | + } catch (MessageSerializerException e) { |
| 78 | + exceptionThrown = true; |
| 79 | + |
| 80 | + // The rejection must be the DOCTYPE being disallowed, not some incidental parse failure. |
| 81 | + Throwable rootCause = ExceptionUtils.getRootCause(e); |
| 82 | + assertTrue(rootCause instanceof SAXParseException); |
| 83 | + assertTrue(rootCause.getMessage().contains("DOCTYPE")); |
| 84 | + } |
| 85 | + |
| 86 | + assertTrue(exceptionThrown); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testToXmlStrictValidatingAllowsValidXml() throws Exception { |
| 91 | + // The same message without a DOCTYPE is legitimate HL7 v2.x XML and must still round-trip, so |
| 92 | + // the hardening does not break the strict parser's XML support. |
| 93 | + String xml = FileUtils.readFileToString(new File("tests/test-xxe-hl7-strict-mllp-valid.xml"), "UTF-8"); |
| 94 | + |
| 95 | + boolean exceptionThrown = false; |
| 96 | + try { |
| 97 | + strictValidatingSerializer.toXML(xml); |
| 98 | + } catch (MessageSerializerException e) { |
| 99 | + exceptionThrown = true; |
52 | 100 | } |
53 | | - |
| 101 | + |
54 | 102 | assertFalse(exceptionThrown); |
55 | 103 | } |
56 | 104 | } |
0 commit comments