Skip to content

Commit

Permalink
Disable External Entities in XmlParser (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasrosdal authored Dec 6, 2023
1 parent b8bae86 commit 2c7b0d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/xml/TagMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public TagMap(InputStream in) {

protected void init(InputStream in) {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
SAXParser parser = factory.newSAXParser();
parser.parse(new InputSource(in), new AttributeHandler((Map<String, XmlPeer>) this));
} catch (Exception e) {
throw new ExceptionConverter(e);
Expand Down
5 changes: 4 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public class XmlParser {

public XmlParser() {
try {
parser = SAXParserFactory.newInstance().newSAXParser();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
parser = factory.newSAXParser();
} catch (ParserConfigurationException | SAXException pce) {
throw new ExceptionConverter(pce);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ public static void main(String[] args) {
writer.setPageEvent(events);

// step 3: we create a parser and set the document handler
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
SAXParser parser = factory.newSAXParser();

// step 4: we parse the document
parser.parse("playRomeoJuliet.xml", new Events().getXmlHandler(document));
Expand Down

0 comments on commit 2c7b0d6

Please sign in to comment.