PageRenderTime 80ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/JaxpParserBehaviourTest.java

https://bitbucket.org/jwalton/xml-parser-sanity-check
Java | 150 lines | 89 code | 24 blank | 37 comment | 0 complexity | 11e26702655e9074536329a142c4602b MD5 | raw file
  1. import com.atlassian.security.xml.UntrustedXmlParserFactory;
  2. import org.junit.Test;
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.Node;
  5. import org.xml.sax.InputSource;
  6. import org.xml.sax.SAXParseException;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import java.io.IOException;
  11. import java.io.StringReader;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. public class JaxpParserBehaviourTest
  15. {
  16. // static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException
  17. // {
  18. //// System.setProperty("entityExpansionLimit", "0");
  19. //
  20. // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  21. //
  22. //// dbf.setExpandEntityReferences(false);
  23. //
  24. // // This will turn any attempt to use a DTD into failure
  25. //// dbf.setAttribute("http://apache.org/xml/features/disallow-doctype-decl", true);
  26. //
  27. // // Only necessary for bundled non-JDK Xerces
  28. // dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
  29. //
  30. // dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
  31. // dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  32. //
  33. // dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  34. //
  35. // DocumentBuilder db = dbf.newDocumentBuilder();
  36. //// db.setEntityResolver(new EntityResolver()
  37. //// {
  38. //// @Override
  39. //// public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
  40. //// {
  41. //// throw new IOException();
  42. //// }
  43. //// });
  44. // return db;
  45. // }
  46. static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException
  47. {
  48. return UntrustedXmlParserFactory.newDocumentBuilder();
  49. }
  50. @Test(expected = IllegalArgumentException.class)
  51. public void askingForUnknownAttributesFails()
  52. {
  53. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  54. dbf.setAttribute("no-attribute-with-this-identifier", false);
  55. }
  56. @Test
  57. public void parseDocumentExpandsAmpersand() throws Exception
  58. {
  59. Document d = createDocumentBuilder().parse(new InputSource(new StringReader(SampleXmlDocuments.AMPERSAND_DOCUMENT)));
  60. Node n = d.getDocumentElement().getChildNodes().item(0);
  61. assertEquals("&", n.getTextContent());
  62. }
  63. @Test(expected = SAXParseException.class, timeout = 1000)
  64. public void parseBillionLaughsDoesNotExhaustMemory() throws Exception
  65. {
  66. createDocumentBuilder().parse(new InputSource(new StringReader(SampleXmlDocuments.BILLION_LAUGHS)));
  67. }
  68. @Test
  69. public void externalEntityIsNotIncludedInDom() throws Exception
  70. {
  71. Document d = createDocumentBuilder().parse(new InputSource(new StringReader(SampleXmlDocuments.externalResourceEntity())));
  72. assertEquals(0, d.getDocumentElement().getChildNodes().getLength());
  73. // If we didn't expandEntityReferences...
  74. // Node n = d.getDocumentElement().getChildNodes().item(0);
  75. //
  76. // assertFalse(n instanceof Text);
  77. // assertTrue(n instanceof EntityReference);
  78. }
  79. @Test
  80. public void externalEntityIsNotRead() throws Exception
  81. {
  82. HttpAttemptDetector detector = new HttpAttemptDetector();
  83. new Thread(detector).start();
  84. createDocumentBuilder().parse(new InputSource(
  85. new StringReader(SampleXmlDocuments.externalResourceEntity(detector.getUrl()))));
  86. assertFalse(detector.wasAttempted());
  87. }
  88. @Test
  89. public void externalParameterEntityIsNotRead() throws Exception
  90. {
  91. HttpAttemptDetector detector = new HttpAttemptDetector();
  92. new Thread(detector).start();
  93. try
  94. {
  95. createDocumentBuilder().parse(new InputSource(
  96. new StringReader(SampleXmlDocuments.externalParameterEntity(detector.getUrl()))));
  97. }
  98. catch (SAXParseException spe)
  99. {
  100. // Don't care
  101. }
  102. catch (IOException e)
  103. {
  104. // Don't care
  105. }
  106. assertFalse(detector.wasAttempted());
  107. }
  108. @Test
  109. public void dtdUriPointsToFile() throws Exception
  110. {
  111. Document d = createDocumentBuilder().parse(new InputSource(new StringReader(SampleXmlDocuments.EXTERNAL_DTD)));
  112. assertEquals("root", d.getDocumentElement().getTagName());
  113. assertEquals(0, d.getDocumentElement().getChildNodes().getLength());
  114. }
  115. @Test
  116. public void dtdUriPointsToUrl() throws Exception
  117. {
  118. HttpAttemptDetector detector = new HttpAttemptDetector();
  119. new Thread(detector).start();
  120. String s = SampleXmlDocuments.externalUrlDtd(detector.getUrl());
  121. Document d = createDocumentBuilder().parse(new InputSource(new StringReader(s)));
  122. assertEquals("root", d.getDocumentElement().getTagName());
  123. assertEquals(0, d.getDocumentElement().getChildNodes().getLength());
  124. assertFalse("I don't want to see HTTP connection attempts", detector.wasAttempted());
  125. }
  126. }