@@ -11,95 +11,97 @@ class DocumentBuilderTests {
1111 public void unconfiguredParse (Socket sock ) throws Exception {
1212 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
1313 DocumentBuilder builder = factory .newDocumentBuilder ();
14- builder .parse (sock .getInputStream ()); //unsafe
14+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
1515 }
1616
1717 public void disableDTD (Socket sock ) throws Exception {
1818 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
1919 factory .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , true );
2020 DocumentBuilder builder = factory .newDocumentBuilder ();
21- builder .parse (sock .getInputStream ()); //safe
21+ builder .parse (sock .getInputStream ()); // safe
2222 }
2323
2424 public void enableSecurityFeature (Socket sock ) throws Exception {
2525 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
2626 factory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , true );
2727 DocumentBuilder builder = factory .newDocumentBuilder ();
28- builder .parse (sock .getInputStream ()); //unsafe -- secure-processing by itself is insufficient
28+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow -- secure-processing by itself is
29+ // insufficient
2930 }
3031
3132 public void enableSecurityFeature2 (Socket sock ) throws Exception {
3233 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
3334 factory .setFeature ("http://javax.xml.XMLConstants/feature/secure-processing" , true );
3435 DocumentBuilder builder = factory .newDocumentBuilder ();
35- builder .parse (sock .getInputStream ()); //unsafe -- secure-processing by itself is insufficient
36+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow -- secure-processing by itself is
37+ // insufficient
3638 }
3739
3840 public void enableDTD (Socket sock ) throws Exception {
3941 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
4042 factory .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , false );
4143 DocumentBuilder builder = factory .newDocumentBuilder ();
42- builder .parse (sock .getInputStream ()); //unsafe
44+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
4345 }
4446
4547 public void disableSecurityFeature (Socket sock ) throws Exception {
4648 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
4749 factory .setFeature ("http://javax.xml.XMLConstants/feature/secure-processing" , false );
4850 DocumentBuilder builder = factory .newDocumentBuilder ();
49- builder .parse (sock .getInputStream ()); //unsafe
51+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
5052 }
5153
5254 public void disableExternalEntities (Socket sock ) throws Exception {
5355 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
5456 factory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
5557 factory .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
5658 DocumentBuilder builder = factory .newDocumentBuilder ();
57- builder .parse (sock .getInputStream ()); //safe
59+ builder .parse (sock .getInputStream ()); // safe
5860 }
5961
6062 public void partialDisableExternalEntities (Socket sock ) throws Exception {
6163 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
6264 factory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
6365 DocumentBuilder builder = factory .newDocumentBuilder ();
64- builder .parse (sock .getInputStream ()); //unsafe
66+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
6567 }
6668
6769 public void partialDisableExternalEntities2 (Socket sock ) throws Exception {
6870 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
6971 factory .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
7072 DocumentBuilder builder = factory .newDocumentBuilder ();
71- builder .parse (sock .getInputStream ()); //unsafe
73+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
7274 }
7375
7476 public void misConfigureExternalEntities1 (Socket sock ) throws Exception {
7577 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
7678 factory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , true );
7779 factory .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
7880 DocumentBuilder builder = factory .newDocumentBuilder ();
79- builder .parse (sock .getInputStream ()); //unsafe
81+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
8082 }
8183
8284 public void misConfigureExternalEntities2 (Socket sock ) throws Exception {
8385 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
8486 factory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
8587 factory .setFeature ("http://xml.org/sax/features/external-general-entities" , true );
8688 DocumentBuilder builder = factory .newDocumentBuilder ();
87- builder .parse (sock .getInputStream ()); //unsafe
89+ builder .parse (sock .getInputStream ()); // $ hasTaintFlow
8890 }
8991
9092 public void taintedSAXInputSource1 (Socket sock ) throws Exception {
91- DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
92- DocumentBuilder builder = factory .newDocumentBuilder ();
93- SAXSource source = new SAXSource (new InputSource (sock .getInputStream ()));
94- builder .parse (source .getInputSource ()); //unsafe
93+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
94+ DocumentBuilder builder = factory .newDocumentBuilder ();
95+ SAXSource source = new SAXSource (new InputSource (sock .getInputStream ()));
96+ builder .parse (source .getInputSource ()); // $ hasTaintFlow
9597 }
9698
9799 public void taintedSAXInputSource2 (Socket sock ) throws Exception {
98- DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
99- DocumentBuilder builder = factory .newDocumentBuilder ();
100- StreamSource source = new StreamSource (sock .getInputStream ());
101- builder .parse (SAXSource .sourceToInputSource (source )); //unsafe
102- builder .parse (source .getInputStream ()); //unsafe
100+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
101+ DocumentBuilder builder = factory .newDocumentBuilder ();
102+ StreamSource source = new StreamSource (sock .getInputStream ());
103+ builder .parse (SAXSource .sourceToInputSource (source )); // $ hasTaintFlow
104+ builder .parse (source .getInputStream ()); // $ hasTaintFlow
103105 }
104106
105107 private static DocumentBuilderFactory getDocumentBuilderFactory () throws Exception {
@@ -112,21 +114,22 @@ private static DocumentBuilderFactory getDocumentBuilderFactory() throws Excepti
112114 return factory ;
113115 }
114116
115- private static final ThreadLocal <DocumentBuilder > XML_DOCUMENT_BUILDER = new ThreadLocal <DocumentBuilder >() {
116- @ Override
117- protected DocumentBuilder initialValue () {
118- try {
119- DocumentBuilderFactory factory = getDocumentBuilderFactory ();
120- return factory .newDocumentBuilder ();
121- } catch (Exception ex ) {
122- throw new RuntimeException (ex );
123- }
124- }
125- };
117+ private static final ThreadLocal <DocumentBuilder > XML_DOCUMENT_BUILDER =
118+ new ThreadLocal <DocumentBuilder >() {
119+ @ Override
120+ protected DocumentBuilder initialValue () {
121+ try {
122+ DocumentBuilderFactory factory = getDocumentBuilderFactory ();
123+ return factory .newDocumentBuilder ();
124+ } catch (Exception ex ) {
125+ throw new RuntimeException (ex );
126+ }
127+ }
128+ };
126129
127130 public void disableExternalEntities2 (Socket sock ) throws Exception {
128131 DocumentBuilder builder = XML_DOCUMENT_BUILDER .get ();
129- builder .parse (sock .getInputStream ()); //safe
132+ builder .parse (sock .getInputStream ()); // safe
130133 }
131134
132135}
0 commit comments