Skip to content

Commit 58e1e12

Browse files
authored
Simplify XML FSP (#731)
* Simplify XML FSP * Simplify XML FSP
1 parent b5052c9 commit 58e1e12

File tree

4 files changed

+14
-27
lines changed

4 files changed

+14
-27
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
5353
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Apache RAT plugin console warnings.</action>
5454
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix site XML to use version 2.0.0 XML schema.</action>
5555
<action type="fix" dev="ggregory" due-to="Michael Hausegger">Removed unreachable threshold verification code in src/main/java/org/apache/commons/text/similarity #730.</action>
56-
<action type="fix" dev="ggregory" due-to="김민재, Gary Gregory">Enable secure processing for the XML parser in XmlStringLookup #729.</action>
56+
<action type="fix" dev="ggregory" due-to="김민재, Gary Gregory, Piotr Karwasz">Enable secure processing for the XML parser in XmlStringLookup in case the underlying JAXP implementation doesn't #729.</action>
5757
<!-- ADD -->
5858
<action type="add" dev="ggregory" due-to="Piotr P. Karwasz, Gary Gregory">Add experimental CycloneDX VEX file #683.</action>
5959
<action type="add" dev="ggregory" due-to="LorgeN, Gary Gregory" issue="TEXT-235">Add Damerau-Levenshtein distance #687.</action>

src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,15 +1718,14 @@ public StringLookup xmlStringLookup(final Map<String, Boolean> factoryFeatures)
17181718
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
17191719
* </ul>
17201720
* <p>
1721-
* Secure processing is enabled by default and can be overridden with the system property {@code "XmlStringLookup.secure"} set to {@code false}. The secure
1722-
* boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}.
1721+
* Secure processing is enabled by default and can be overridden with this constructor.
17231722
* </p>
17241723
* <p>
17251724
* Using a {@link StringLookup} from the {@link StringLookupFactory} fenced by the current directory ({@code Paths.get("")}):
17261725
* </p>
17271726
*
17281727
* <pre>
1729-
* StringLookupFactory.INSTANCE.xmlStringLookup(map, Pathe.get("")).lookup("com/domain/document.xml:/path/to/node");
1728+
* StringLookupFactory.INSTANCE.xmlStringLookup(map, Path.get("")).lookup("com/domain/document.xml:/path/to/node");
17301729
* </pre>
17311730
* <p>
17321731
* To use a {@link StringLookup} fenced by the current directory, use:

src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javax.xml.xpath.XPathFactory;
3131

3232
import org.apache.commons.lang3.StringUtils;
33-
import org.apache.commons.lang3.SystemProperties;
3433
import org.w3c.dom.Document;
3534

3635
/**
@@ -42,8 +41,7 @@
4241
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
4342
* </ul>
4443
* <p>
45-
* Secure processing is enabled by default and can be overridden with the system property {@code "XmlStringLookup.secure"} set to {@code false}. The secure
46-
* boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}.
44+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
4745
* </p>
4846
*
4947
* @since 1.5
@@ -72,14 +70,13 @@ final class XmlStringLookup extends AbstractPathFencedLookup {
7270
}
7371

7472
/**
75-
* Defines the singleton for this class with secure processing enabled.
73+
* Defines the singleton for this class with secure processing enabled by default.
74+
* <p>
75+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
76+
* </p>
7677
*/
7778
static final XmlStringLookup INSTANCE = new XmlStringLookup(DEFAULT_XML_FEATURES, DEFAULT_XPATH_FEATURES, (Path[]) null);
7879

79-
private static boolean isSecure() {
80-
return SystemProperties.getBoolean(XmlStringLookup.class, "secure", () -> true);
81-
}
82-
8380
/**
8481
* Defines XPath factory features.
8582
*/
@@ -113,8 +110,7 @@ private static boolean isSecure() {
113110
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
114111
* </ul>
115112
* <p>
116-
* Secure processing is enabled by default. The secure boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}. The secure
117-
* value in the key overrides instance settings given in the constructor.
113+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
118114
* </p>
119115
*
120116
* @param key the key to be looked up, may be null.
@@ -130,22 +126,19 @@ public String lookup(final String key) {
130126
if (keyLen != KEY_PARTS_LEN) {
131127
throw IllegalArgumentExceptions.format("Bad XML key format '%s'; the expected format is 'DocumentPath:XPath'.", key);
132128
}
133-
final boolean secure = isSecure();
134129
final String documentPath = keys[0];
135130
final String xpath = StringUtils.substringAfterLast(key, SPLIT_CH);
136131
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
137132
try {
138133
for (final Entry<String, Boolean> p : xmlFactoryFeatures.entrySet()) {
139134
dbFactory.setFeature(p.getKey(), p.getValue());
140135
}
141-
dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secure);
142136
try (InputStream inputStream = Files.newInputStream(getPath(documentPath))) {
143137
final Document doc = dbFactory.newDocumentBuilder().parse(inputStream);
144138
final XPathFactory xpFactory = XPathFactory.newInstance();
145139
for (final Entry<String, Boolean> p : xPathFactoryFeatures.entrySet()) {
146140
xpFactory.setFeature(p.getKey(), p.getValue());
147141
}
148-
xpFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secure);
149142
return xpFactory.newXPath().evaluate(xpath, doc);
150143
}
151144
} catch (final Exception e) {

src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ void testExternalEntityOff() {
6969
}
7070

7171
@Test
72-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
7372
void testExternalEntityOn() {
7473
final String key = DOC_DIR + "document-entity-ref.xml:/document/content";
7574
assertEquals(DATA, new XmlStringLookup(EMPTY_MAP, EMPTY_MAP).apply(key).trim());
@@ -79,16 +78,14 @@ void testExternalEntityOn() {
7978
@Test
8079
void testInterpolatorExternalDtdOff() {
8180
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
82-
assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${xml:" + DOC_DIR
83-
+ "document-external-dtd.xml:/document/content}"));
81+
assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${xml:" + DOC_DIR + "document-external-dtd.xml:/document/content}"));
8482
}
8583

8684
@Test
87-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
85+
@SetSystemProperty(key = "javax.xml.accessExternalDTD", value = "file")
8886
void testInterpolatorExternalDtdOn() {
8987
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
90-
assertEquals("This is an external entity.",
91-
stringSubstitutor.replace("${xml:" + DOC_DIR + "document-external-dtd.xml:/document/content}").trim());
88+
assertEquals("This is an external entity.", stringSubstitutor.replace("${xml:" + DOC_DIR + "document-external-dtd.xml:/document/content}").trim());
9289
}
9390

9491
@Test
@@ -98,7 +95,7 @@ void testInterpolatorExternalEntityOff() {
9895
}
9996

10097
@Test
101-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
98+
@SetSystemProperty(key = "javax.xml.accessExternalDTD", value = "file")
10299
void testInterpolatorExternalEntityOffOverride() {
103100
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
104101
assertEquals(DATA, stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}").trim());
@@ -111,11 +108,9 @@ void testInterpolatorExternalEntityOn() {
111108
}
112109

113110
@Test
114-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "true")
115111
void testInterpolatorExternalEntityOnOverride() {
116112
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
117-
assertThrows(IllegalArgumentException.class,
118-
() -> stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}"));
113+
assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}"));
119114
}
120115

121116
@Test

0 commit comments

Comments
 (0)