Skip to content

Commit 7a5b1f6

Browse files
authored
Merge pull request #88 from fugerit-org/87-xml-external-entity-xxe-injection-in-docfacade
XMLFactorySAX secure version #87
2 parents 45927fd + e1cd6fa commit 7a5b1f6

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- XMLFactorySAX.newInstanceSecure() disabling external entities <https://github.com/fugerit-org/fj-lib/issues/87>
13+
1014
### Changed
1115

1216
- Added 'ubuntu-24.04-arm' runner to compatibility workdlow

fj-core/src/main/java/org/fugerit/java/core/xml/sax/XMLFactorySAX.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,69 @@ public XMLValidator newXMLValidator(EntityResolver er) throws XMLException {
2929
public static SAXParser makeSAXParser(boolean val, boolean nsa) throws XMLException {
3030
return (newInstance(val, nsa).newSAXParser());
3131
}
32+
33+
public static SAXParser makeSAXParserSecure(boolean val, boolean nsa) throws XMLException {
34+
return (newInstanceSecure(val, nsa).newSAXParser());
35+
}
3236

3337
public SAXParser newSAXParser() throws XMLException {
3438
return SafeFunction.getEx( () -> this.factory.newSAXParser(), XMLException.CONVERT_FUN );
3539
}
3640

3741
public static XMLFactorySAX newInstance() throws XMLException {
3842
return newInstance(false, false);
39-
}
43+
}
44+
45+
public static XMLFactorySAX newInstanceSecure() throws XMLException {
46+
return newInstanceSecure(false);
47+
}
4048

4149
public static XMLFactorySAX newInstance(boolean validating) throws XMLException {
4250
return newInstance(validating, false);
4351
}
44-
52+
53+
public static XMLFactorySAX newInstanceSecure(boolean validating) throws XMLException {
54+
return newInstanceSecure(validating, false);
55+
}
56+
57+
public static XMLFactorySAX newInstanceSecure(boolean validating, boolean namespaceAware) throws XMLException {
58+
return newInstance( validating, namespaceAware, Boolean.TRUE );
59+
}
60+
4561
public static XMLFactorySAX newInstance(boolean validating, boolean namespaceAware) throws XMLException {
46-
return XMLException.get( () -> {
47-
SAXParserFactory saxFac = SAXParserFactory.newInstance();
48-
saxFac.setValidating(validating);
49-
saxFac.setNamespaceAware(namespaceAware);
50-
return new XMLFactorySAX(saxFac);
51-
} );
62+
return newInstance( validating, namespaceAware, Boolean.FALSE );
63+
}
64+
65+
/**
66+
* Creates a new XMLFactorySAX wrapping a javax.xml.parsers.SAXParserFactory
67+
*
68+
* if the secure flag is set, the external entities will be disabled :
69+
*
70+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
71+
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
72+
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
73+
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
74+
*
75+
* @param validating to set the XMLFactorySAX as validating
76+
* @param namespaceAware to set the XMLFactorySAX as namespaceAware
77+
* @param secure to set the XMLFactorySAX as secure (external entities disabled)
78+
* @return the new configured XMLFactorySAX
79+
* @throws XMLException in case any issue arise
80+
*/
81+
public static XMLFactorySAX newInstance(boolean validating, boolean namespaceAware, boolean secure) throws XMLException {
82+
return XMLException.get( () -> {
83+
SAXParserFactory factory = SAXParserFactory.newInstance();
84+
factory.setValidating(validating);
85+
factory.setNamespaceAware(namespaceAware);
86+
if ( secure ) {
87+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
88+
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
89+
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
90+
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
91+
factory.setXIncludeAware(false);
92+
}
93+
return new XMLFactorySAX( factory );
94+
} );
5295
}
5396

5497
public void setValidating(boolean val) {

fj-core/src/test/java/test/org/fugerit/java/core/xml/sax/TestXmlFactorySAX.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public void test1() throws XMLException {
2828
boolean ok = this.worker( XMLFactorySAX.newInstance() );
2929
Assert.assertTrue(ok);
3030
}
31+
32+
@Test
33+
public void testSecure() throws XMLException {
34+
Assert.assertNotNull( XMLFactorySAX.makeSAXParserSecure( true, true ) );
35+
Assert.assertTrue( this.worker( XMLFactorySAX.newInstanceSecure() ) );
36+
Assert.assertTrue( this.worker( XMLFactorySAX.newInstanceSecure( true ) ) );
37+
Assert.assertTrue( this.worker( XMLFactorySAX.newInstanceSecure( true, true ) ) );
38+
}
3139

3240
@Test
3341
public void test2() throws XMLException {

0 commit comments

Comments
 (0)