Skip to content

Commit

Permalink
[#34] Make setFeature calls optional
Browse files Browse the repository at this point in the history
IMHO a missing `setFeature()` method or support for disabling external
entities should not cause a parsing error, just warnings.

Signed-off-by: Piotr P. Karwasz <piotr.github@karwasz.org>
  • Loading branch information
ppkarwasz committed Sep 30, 2022
1 parent fb382fd commit 48f892b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/org/apache/log4j/xml/DOMConfigurator.java
Expand Up @@ -788,6 +788,16 @@ public String toString() {
doConfigure(action, repository);
}

private final void setFeature(final DocumentBuilderFactory dbf, final String feature, final boolean value) {
try {
dbf.setFeature(feature, value);
} catch (Exception e) {
LogLog.warn("Failed to set DocumentBuilderFactory feature " + feature + ".", e);
} catch (AbstractMethodError e) {
LogLog.warn("Failed to set DocumentBuilderFactory feature " + feature + ". Missing DocumentBuilderFactory.setFeature() method?", e);
}
}

private final void doConfigure(final ParseAction action, final LoggerRepository repository)
throws FactoryConfigurationError {
DocumentBuilderFactory dbf = null;
Expand All @@ -806,8 +816,8 @@ private final void doConfigure(final ParseAction action, final LoggerRepository
try {
dbf.setValidating(true);
// prevent XXE attacks
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
setFeature(dbf, "http://xml.org/sax/features/external-general-entities", false);
setFeature(dbf, "http://xml.org/sax/features/external-parameter-entities", false);

DocumentBuilder docBuilder = dbf.newDocumentBuilder();

Expand All @@ -821,9 +831,6 @@ private final void doConfigure(final ParseAction action, final LoggerRepository
Thread.currentThread().interrupt();
}
LogLog.error("Could not parse " + action.toString() + ".", e);
} catch (AbstractMethodError e) {
LogLog.error("Failed to parse XML file. Missing DocumentBuilderFactory.setFeature() method?", e);
throw e;
}
}

Expand Down

0 comments on commit 48f892b

Please sign in to comment.