Skip to content

Commit

Permalink
fix: handle unsupported external DTD and schema properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mholthausen committed Oct 11, 2024
1 parent 382bad9 commit 0450f3c
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ public static Document getDocumentFromString(String xml) throws IOException {
InputSource source = new InputSource(new StringReader(xml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// limit resolution of external entities, see https://rules.sonarsource.com/c/type/Vulnerability/RSPEC-2755
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
try {
// limit resolution of external entities, see https://rules.sonarsource.com/c/type/Vulnerability/RSPEC-2755
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (IllegalArgumentException e) {
log.error("External DTD/Schema access properties not supported:"
+ e.getMessage());
}

DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(source);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new IOException("Could not parse input body " +
"as XML: " + e.getMessage());
} catch (IllegalArgumentException | ParserConfigurationException
| SAXException | IOException e) {
throw new IOException("Could not parse input body as XML: "
+ e.getMessage());
}
return document;
}
Expand Down

0 comments on commit 0450f3c

Please sign in to comment.