Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #6474: disable external dtd load by default #6476

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/pmd.xml
Expand Up @@ -96,13 +96,13 @@
</rule>
<rule ref="category/java/codestyle.xml/ClassNamingConventions">
<properties>
<!-- Definitions and XmlLoader.FeaturesForVerySecureJavaInstallations aren't utility classes.
<!-- Definitions and XmlLoader.LoadExternalDtdFeatureProvider aren't utility classes.
JavadocTokenTypes and TokenTypes aren't utility classes.
They are token definition classes. Also, they are part of the API. -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@Image='Definitions'
or @Image='JavadocTokenTypes' or @Image='TokenTypes'
or @Image='FeaturesForVerySecureJavaInstallations']"/>
or @Image='LoadExternalDtdFeatureProvider']"/>
</properties>
</rule>
<rule ref="category/java/codestyle.xml/ConfusingTernary">
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -2678,12 +2678,12 @@
</targetTests>
<excludedMethods>
<!--cause of https://github.com/checkstyle/checkstyle/issues/3605-->
<param>addFeaturesForVerySecureJavaInstallations</param>
<param>setFeaturesBySystemProperty</param>
</excludedMethods>
<avoidCallsTo>
<!--cause of https://github.com/checkstyle/checkstyle/issues/3605-->
<avoidCallsTo>
com.puppycrawl.tools.checkstyle.XmlLoader$FeaturesForVerySecureJavaInstallations
com.puppycrawl.tools.checkstyle.XmlLoader$LoadExternalDtdFeatureProvider
</avoidCallsTo>
</avoidCallsTo>
<coverageThreshold>100</coverageThreshold>
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/puppycrawl/tools/checkstyle/XmlLoader.java
Expand Up @@ -65,7 +65,7 @@ protected XmlLoader(Map<String, String> publicIdToResourceNameMap)
throws SAXException, ParserConfigurationException {
this.publicIdToResourceNameMap = new HashMap<>(publicIdToResourceNameMap);
final SAXParserFactory factory = SAXParserFactory.newInstance();
FeaturesForVerySecureJavaInstallations.addFeaturesForVerySecureJavaInstallations(factory);
LoadExternalDtdFeatureProvider.setFeaturesBySystemProperty(factory);
factory.setValidating(true);
parser = factory.newSAXParser().getXMLReader();
parser.setContentHandler(this);
Expand Down Expand Up @@ -113,7 +113,10 @@ public void error(SAXParseException exception) throws SAXException {
* Used for setting specific for secure java installations features to SAXParserFactory.
* Pulled out as a separate class in order to suppress Pitest mutations.
*/
public static final class FeaturesForVerySecureJavaInstallations {
public static final class LoadExternalDtdFeatureProvider {

/** System property name to enable external DTD load. */
public static final String ENABLE_EXTERNAL_DTD_LOAD = "checkstyle.enableExternalDtdLoad";

/** Feature that enables loading external DTD when loading XML files. */
private static final String LOAD_EXTERNAL_DTD =
Expand All @@ -123,20 +126,26 @@ public static final class FeaturesForVerySecureJavaInstallations {
"http://xml.org/sax/features/external-general-entities";

/** Stop instances being created. **/
private FeaturesForVerySecureJavaInstallations() {
private LoadExternalDtdFeatureProvider() {
}

/**
* Configures SAXParserFactory with features required
* for execution on very secured environments.
* to use external DTD file loading, this is not activated by default to no allow
* usage of schema files that checkstyle do not know
* it is even security problem to allow files from outside.
* @param factory factory to be configured with special features
* @throws SAXException if an error occurs
* @throws ParserConfigurationException if an error occurs
*/
public static void addFeaturesForVerySecureJavaInstallations(SAXParserFactory factory)
public static void setFeaturesBySystemProperty(SAXParserFactory factory)
throws SAXException, ParserConfigurationException {
factory.setFeature(LOAD_EXTERNAL_DTD, true);
factory.setFeature(EXTERNAL_GENERAL_ENTITIES, true);

final boolean enableExternalDtdLoad = Boolean.valueOf(
System.getProperty(ENABLE_EXTERNAL_DTD_LOAD, "false"));

factory.setFeature(LOAD_EXTERNAL_DTD, enableExternalDtdLoad);
factory.setFeature(EXTERNAL_GENERAL_ENTITIES, enableExternalDtdLoad);
}

}
Expand Down
Expand Up @@ -406,6 +406,9 @@ public void testExternalEntity() throws Exception {
final Properties props = new Properties();
props.setProperty("checkstyle.basedir", "basedir");

System.setProperty(
XmlLoader.LoadExternalDtdFeatureProvider.ENABLE_EXTERNAL_DTD_LOAD, "true");

final DefaultConfiguration config =
(DefaultConfiguration) loadConfiguration(
"InputConfigurationLoaderExternalEntity.xml", props);
Expand All @@ -421,6 +424,9 @@ public void testExternalEntitySubdirectory() throws Exception {
final Properties props = new Properties();
props.setProperty("checkstyle.basedir", "basedir");

System.setProperty(
XmlLoader.LoadExternalDtdFeatureProvider.ENABLE_EXTERNAL_DTD_LOAD, "true");

final DefaultConfiguration config =
(DefaultConfiguration) loadConfiguration(
"subdir/InputConfigurationLoaderExternalEntitySubDir.xml", props);
Expand All @@ -436,6 +442,9 @@ public void testExternalEntityFromUri() throws Exception {
final Properties props = new Properties();
props.setProperty("checkstyle.basedir", "basedir");

System.setProperty(
XmlLoader.LoadExternalDtdFeatureProvider.ENABLE_EXTERNAL_DTD_LOAD, "true");

final File file = new File(
getPath("subdir/InputConfigurationLoaderExternalEntitySubDir.xml"));
final DefaultConfiguration config =
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void testParserConfiguredSuccessfully() throws Exception {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertTrue("Constructor is not private", isUtilsClassHasPrivateConstructor(
XmlLoader.FeaturesForVerySecureJavaInstallations.class, true));
XmlLoader.LoadExternalDtdFeatureProvider.class, true));
}

@Test
Expand Down
11 changes: 11 additions & 0 deletions src/xdocs/config_reporting.xml
Expand Up @@ -69,5 +69,16 @@
to an empty string.
</p>
</section>

<section name="Enable External DTD load">
<p>
The property <code>checkstyle.enableExternalDtdLoad</code>
defines ability use custom DTD files inconfig and load them from some location.
The property type
is <a href="property_types.html#boolean">boolean</a> and defaults
to <code>false</code>.
</p>
</section>

</body>
</document>