Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
[SECURITY-1699]
Browse files Browse the repository at this point in the history
  • Loading branch information
fedepell authored and daniel-beck committed Apr 3, 2020
1 parent f4e7853 commit f0ef84c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public Document convert(T report) throws CoverageException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private Document readXMLtoDocumentWithoutXSD(File file) throws ParserConfigurati
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);

DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();

return builder.parse(file);
Expand All @@ -203,10 +207,12 @@ private TransformerFactory newSecureTransformerFactory() {
TransformerFactory transformerFactory = new TransformerFactoryImpl();
try {
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
transformerFactory.setAttribute("http://saxon.sf.net/feature/parserFeature?uri=http://apache.org/xml/features/disallow-doctype-decl", true);
transformerFactory.setAttribute("http://saxon.sf.net/feature/parserFeature?uri=http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
transformerFactory.setAttribute("http://saxon.sf.net/feature/parserFeature?uri=http://xml.org/sax/features/external-general-entities", false);
transformerFactory.setAttribute("http://saxon.sf.net/feature/parserFeature?uri=http://xml.org/sax/features/external-parameter-entities", false);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
}
return transformerFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import java.nio.file.Path;
import java.util.Objects;

import io.jenkins.plugins.coverage.adapter.util.XMLUtils;
import java.io.File;
import org.w3c.dom.Document;

public class CoverageCornerCaseTest {

@Rule
Expand Down Expand Up @@ -64,4 +68,16 @@ public void testIfFoundEmptyReport() throws Exception {
}


@Test
public void testPreventXXE() throws Exception {
/*
Test for SECURITY-1699: if external entities are executed an exception will be thrown
as an invalid external entity (unknown protocol foobar) is defined in the supplied XML
test file
*/
Document d;
File file = new File(getClass().getResource("sec1699.xml").toURI());
d = XMLUtils.getInstance().readXMLtoDocument(file);
}

}
22 changes: 22 additions & 0 deletions src/test/resources/io/jenkins/plugins/coverage/sec1699.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" ?>
<!DOCTYPE test [
<!ENTITY xxe SYSTEM "foobar://this.will.except">
]>
<coverage branch-rate="0.363636877551020408" line-rate="0.7036379769299024" timestamp="1571654451" version="gcovr 3.3">
<sources>
<source>fedepell</source>
</sources>
<packages>
<package branch-rate="0.7142857142857143" complexity="0.0" line-rate="0.9207547169811321" name="...fedepell">
<classes>
<class branch-rate="0.36" complexity="0.0" filename="ooops.c" line-rate="0.48484848484848486" name="ooops.c">
<methods/>
<lines>
<line branch="false" hits="5" number="1"/>
&xxe;
</lines>
</class>
</classes>
</package>
</packages>
</coverage>

0 comments on commit f0ef84c

Please # to comment.