Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-1958 61db8afdd -> 2d4b2c1bb
LOG4J2-1959 Disable DTD processing in XML configuration files Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/594e573f Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/594e573f Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/594e573f Branch: refs/heads/LOG4J2-1958 Commit: 594e573f5d8d82dae498abe6cdffbe3d1d89a039 Parents: a5a9588 Author: Mikael Ståldal <mik...@staldal.nu> Authored: Wed Jun 28 16:37:18 2017 +0200 Committer: Mikael Ståldal <mik...@staldal.nu> Committed: Wed Jun 28 16:37:18 2017 +0200 ---------------------------------------------------------------------- .../log4j/core/config/xml/XmlConfiguration.java | 11 ++++++ .../config/xml/XmlConigurationSecurity.java | 36 ++++++++++++++++++++ .../test/resources/XmlConfigurationSecurity.xml | 34 ++++++++++++++++++ src/changes/changes.xml | 3 ++ src/site/xdoc/manual/configuration.xml.vm | 5 +++ 5 files changed, 89 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/594e573f/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java index 51f31d4..6007461 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java @@ -190,12 +190,23 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu static DocumentBuilder newDocumentBuilder(final boolean xIncludeAware) throws ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); + + disableDtdProcessing(factory); + if (xIncludeAware) { enableXInclude(factory); } return factory.newDocumentBuilder(); } + private static void disableDtdProcessing(DocumentBuilderFactory factory) throws ParserConfigurationException { + factory.setValidating(false); + factory.setExpandEntityReferences(false); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + } + /** * Enables XInclude for the given DocumentBuilderFactory * http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/594e573f/log4j-core/src/test/java/org/apache/logging/log4j/core/config/xml/XmlConigurationSecurity.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/xml/XmlConigurationSecurity.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/xml/XmlConigurationSecurity.java new file mode 100644 index 0000000..4d45315 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/xml/XmlConigurationSecurity.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.config.xml; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertNotNull; + +public class XmlConigurationSecurity { + + @Test(timeout = 5000L) + public void xmlSecurity() throws IOException { + LoggerContext context = Configurator.initialize("XmlConfigurationSecurity", "XmlConfigurationSecurity.xml"); + assertNotNull(context.getConfiguration().getAppender("list")); + } + +} + http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/594e573f/log4j-core/src/test/resources/XmlConfigurationSecurity.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/XmlConfigurationSecurity.xml b/log4j-core/src/test/resources/XmlConfigurationSecurity.xml new file mode 100644 index 0000000..558c43f --- /dev/null +++ b/log4j-core/src/test/resources/XmlConfigurationSecurity.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<!DOCTYPE Configuration [ + <!ENTITY % dtd SYSTEM "http://localhost:12345/evil1.dtd"> +%dtd;]> +<Configuration status="OFF" name="XmlConfigurationSecurity"> + <Appenders> + <List name="list"> + <PatternLayout pattern="%m%n"/> + </List> + </Appenders> + + <Loggers> + <Root level="info"> + <AppenderRef ref="list"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/594e573f/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d272730..75bb8ce 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -31,6 +31,9 @@ - "remove" - Removed --> <release version="2.9.0" date="2017-MM-DD" description="GA Release 2.9.0"> + <action issue="LOG4J2-1959" dev="mikes" type="update"> + Disable DTD processing in XML configuration files. + </action> <action issue="LOG4J2-1766" dev="ggregory" type="add" due-to="Pierrick HYMBERT"> Temporary compress directory during rollover (#88). </action> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/594e573f/src/site/xdoc/manual/configuration.xml.vm ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/configuration.xml.vm b/src/site/xdoc/manual/configuration.xml.vm index 4569086..b5f7af2 100644 --- a/src/site/xdoc/manual/configuration.xml.vm +++ b/src/site/xdoc/manual/configuration.xml.vm @@ -358,6 +358,11 @@ public class Bar { <a name="ConfigurationSyntax"/> <subsection name="Configuration Syntax"> <p> + As of version 2.9, for security reasons, Log4j does not process DTDs in XML files. + If you want to split the configuration in multiple files, use <a href="#XInclude">XInclude</a> or + <a href="#CompositeConfiguration">Composite Configuration</a>. + </p> + <p> As the previous examples have shown as well as those to follow, Log4j allows you to easily redefine logging behavior without needing to modify your application. It is possible to disable logging for certain parts of the application, log only when specific criteria are met such