Skip to content

Commit e8cf690

Browse files
committed
Add parameter "setTotalEntitySizeLimit" to XmlDecoder (#554)
- add tests
1 parent 9a73d3c commit e8cf690

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

metafacture-xml/src/main/java/org/metafacture/xml/XmlDecoder.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,34 @@
4848
public final class XmlDecoder extends DefaultObjectPipe<Reader, XmlReceiver> {
4949

5050
private static final String SAX_PROPERTY_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
51-
52-
private final XMLReader saxReader;
51+
private XMLReader saxReader;
52+
private final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
5353

5454
/**
55-
* Constructs an XmlDecoder by obtaining a new instance of an
55+
* Creates an instance of {@link XmlDecoder} by obtaining a new instance of an
5656
* {@link org.xml.sax.XMLReader}.
5757
*/
5858
public XmlDecoder() {
5959
try {
60-
final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
6160
parserFactory.setNamespaceAware(true);
61+
saxReader = parserFactory.newSAXParser().getXMLReader();
62+
}
63+
catch (final ParserConfigurationException | SAXException e) {
64+
throw new MetafactureException(e);
65+
}
66+
}
6267

68+
/**
69+
* Sets the total entity size limit for the XML parser.
70+
* See <a href="https://docs.oracle.com/en/java/javase/13/security/java-api-xml-processing-jaxp-security-guide.html#GUID-82F8C206-F2DF-4204-9544-F96155B1D258__TABLE_RQ1_3PY_HHB">java-api-xml-processing-jaxp-security-guide.html</a>
71+
*
72+
* Defaults to "50,000,000". Set to "0" to allow unlimited entities.
73+
*
74+
* @param size the size of the allowed entities. Set to "0" if entities should be unlimited.
75+
*/
76+
public void setTotalEntitySizeLimit(final String size) {
77+
try {
78+
System.setProperty("jdk.xml.totalEntitySizeLimit", size);
6379
saxReader = parserFactory.newSAXParser().getXMLReader();
6480
}
6581
catch (final ParserConfigurationException | SAXException e) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2024 Pascal Christoph (hbz)
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.xml;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.metafacture.framework.MetafactureException;
22+
23+
import java.io.IOException;
24+
import java.io.Reader;
25+
import java.io.StringReader;
26+
27+
/**
28+
* Tests for class {@link XmlDecoder}.
29+
*
30+
* @author Pascal Christoph (dr0i)
31+
*/
32+
public final class XmlDecoderTest {
33+
34+
private final String TEST_XML_WITH_TWO_ENTITIES = "<record>&gt;&gt;</record>";
35+
private XmlDecoder xmlDecoder;
36+
private final Reader reader = new StringReader(TEST_XML_WITH_TWO_ENTITIES);
37+
38+
@Before
39+
public void initSystemUnderTest() {
40+
xmlDecoder = new XmlDecoder();
41+
}
42+
43+
@Test
44+
public void issue554_default() {
45+
process(xmlDecoder);
46+
}
47+
48+
@Test(expected = MetafactureException.class)
49+
public void issue554_shouldFail() {
50+
xmlDecoder.setTotalEntitySizeLimit("1");
51+
process(xmlDecoder);
52+
}
53+
54+
@Test
55+
public void issue554_unlimitedEntities() {
56+
xmlDecoder.setTotalEntitySizeLimit("0");
57+
process(xmlDecoder);
58+
}
59+
60+
private void process(XmlDecoder xmlDecoder) {
61+
try {
62+
xmlDecoder.process(reader);
63+
reader.close();
64+
}
65+
catch (IOException e) {
66+
throw new RuntimeException(e);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)