Created
March 10, 2015 21:53
-
-
Save asudhakar02/45e2e6fd8bcdfb4bc3b2 to your computer and use it in GitHub Desktop.
Secure SAXParserFactory that prevents XXE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features | |
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
import org.xml.sax.SAXNotRecognizedException; // catching unknown features | |
import org.xml.sax.SAXNotSupportedException; // catching known but unsupported features | |
import org.xml.sax.XMLReader; | |
... | |
SAXParserFactory spf = SAXParserFactory.newInstance(); | |
SAXParser saxParser = spf.newSAXParser(); | |
XMLReader reader = saxParser.getXMLReader(); | |
try { | |
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities | |
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities | |
// Using the SAXParserFactory's setFeature | |
spf.setFeature("http://xml.org/sax/features/external-general-entities", false); | |
// Using the XMLReader's setFeature | |
reader.setFeature("http://xml.org/sax/features/external-general-entities", false); | |
// Xerces 2 only - http://xerces.apache.org/xerces-j/features.html#external-general-entities | |
spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); | |
// remaining parser logic | |
... | |
} catch (ParserConfigurationException e) { | |
// Tried an unsupported feature. | |
} catch (SAXNotRecognizedException e) { | |
// Tried an unknown feature. | |
} catch (SAXNotSupportedException e) { | |
// Tried a feature known to the parser but unsupported. | |
} catch ... { | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two comments, if you can't spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); because this means your parser throws an error if a DTD is included in the document (which might be there from legacy documents).
So we found we had to disable the external loading using spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
This feature gets ignored though by xerces if you set the setValidating on SAXParserFactory to true, so make sure that is set to false or xerces will always try to load the DTD and external DTDs.