Skip to content

Commit 770db93

Browse files
Aleksei VoitylovRealCLanger
authored andcommitted
8356294: Enhance Path Factories
Reviewed-by: abakhtin, fferrari Backport-of: 65fb43fdae221be75f2657697b96ddf996ebca44
1 parent 05d9cf5 commit 770db93

File tree

6 files changed

+77
-12
lines changed

6 files changed

+77
-12
lines changed

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/DocumentBuilderFactoryImpl.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.sun.org.apache.xerces.internal.parsers.DOMParser;
2424
import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
25-
import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
2625
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
2726
import java.util.HashMap;
2827
import java.util.Map;
@@ -32,14 +31,15 @@
3231
import javax.xml.parsers.ParserConfigurationException;
3332
import javax.xml.validation.Schema;
3433
import jdk.xml.internal.JdkProperty;
34+
import jdk.xml.internal.XMLSecurityManager;
3535
import org.xml.sax.SAXException;
3636
import org.xml.sax.SAXNotRecognizedException;
3737
import org.xml.sax.SAXNotSupportedException;
3838

3939
/**
4040
* @author Rajiv Mordani
4141
* @author Edwin Goei
42-
* @LastModified: May 2021
42+
* @LastModified: June 2025
4343
*/
4444
public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
4545
/** These are DocumentBuilderFactory attributes not DOM attributes */
@@ -54,8 +54,26 @@ public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
5454
private boolean fSecureProcess = true;
5555

5656
// used to verify attributes
57-
XMLSecurityManager fSecurityManager = new XMLSecurityManager(true);
58-
XMLSecurityPropertyManager fSecurityPropertyMgr = new XMLSecurityPropertyManager();
57+
XMLSecurityManager fSecurityManager;
58+
XMLSecurityPropertyManager fSecurityPropertyMgr;
59+
60+
/**
61+
* Creates a new {@code DocumentBuilderFactory} instance.
62+
*/
63+
public DocumentBuilderFactoryImpl() {
64+
this(null, null);
65+
}
66+
67+
/**
68+
* Creates a new {@code DocumentBuilderFactory} instance with a {@code XMLSecurityManager}
69+
* and {@code XMLSecurityPropertyManager}.
70+
* @param xsm the {@code XMLSecurityManager}
71+
* @param xspm the {@code XMLSecurityPropertyManager}
72+
*/
73+
public DocumentBuilderFactoryImpl(XMLSecurityManager xsm, XMLSecurityPropertyManager xspm) {
74+
fSecurityManager = (xsm == null) ? new XMLSecurityManager(true) : xsm;
75+
fSecurityPropertyMgr = (xspm == null) ? new XMLSecurityPropertyManager() : xspm;
76+
}
5977

6078
/**
6179
* Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathFactoryImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.sun.org.apache.xpath.internal.jaxp;
2222

2323
import com.sun.org.apache.xalan.internal.res.XSLMessages;
24+
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
2425
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
2526
import javax.xml.XMLConstants;
2627
import javax.xml.xpath.XPathFactory;
@@ -37,7 +38,7 @@
3738
*
3839
* @author Ramesh Mandava
3940
*
40-
* @LastModified: Jan 2022
41+
* @LastModified: June 2025
4142
*/
4243
public class XPathFactoryImpl extends XPathFactory {
4344

@@ -74,6 +75,7 @@ public class XPathFactoryImpl extends XPathFactory {
7475
* The XML security manager
7576
*/
7677
private XMLSecurityManager _xmlSecMgr;
78+
private XMLSecurityPropertyManager _xmlSecPropMgr;
7779

7880
/**
7981
* javax.xml.xpath.XPathFactory implementation.
@@ -86,6 +88,7 @@ public XPathFactoryImpl() {
8688
}
8789
_featureManager = new JdkXmlFeatures(!_isNotSecureProcessing);
8890
_xmlSecMgr = new XMLSecurityManager(true);
91+
_xmlSecPropMgr = new XMLSecurityPropertyManager();
8992
}
9093

9194
/**
@@ -135,7 +138,7 @@ public boolean isObjectModelSupported(String objectModel) {
135138
*/
136139
public javax.xml.xpath.XPath newXPath() {
137140
return new XPathImpl(xPathVariableResolver, xPathFunctionResolver,
138-
!_isNotSecureProcessing, _featureManager, _xmlSecMgr);
141+
!_isNotSecureProcessing, _featureManager, _xmlSecMgr, _xmlSecPropMgr);
139142
}
140143

141144
/**
@@ -189,6 +192,7 @@ public void setFeature(String name, boolean value)
189192
if (value && _featureManager != null) {
190193
_featureManager.setFeature(JdkXmlFeatures.XmlFeature.ENABLE_EXTENSION_FUNCTION,
191194
JdkProperty.State.FSP, false);
195+
_xmlSecMgr.setSecureProcessing(value);
192196
}
193197

194198
// all done processing feature

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.sun.org.apache.xpath.internal.jaxp;
2222

23+
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
2324
import com.sun.org.apache.xpath.internal.*;
2425
import com.sun.org.apache.xpath.internal.objects.XObject;
2526
import javax.xml.namespace.NamespaceContext;
@@ -47,7 +48,7 @@
4748
* New methods: evaluateExpression
4849
* Refactored to share code with XPathExpressionImpl.
4950
*
50-
* @LastModified: Jan 2022
51+
* @LastModified: June 2025
5152
*/
5253
public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath {
5354

@@ -57,19 +58,21 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath {
5758
private NamespaceContext namespaceContext=null;
5859

5960
XPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr) {
60-
this(vr, fr, false, new JdkXmlFeatures(false), new XMLSecurityManager(true));
61+
this(vr, fr, false, new JdkXmlFeatures(false), new XMLSecurityManager(true),
62+
new XMLSecurityPropertyManager());
6163
}
6264

6365
XPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr,
6466
boolean featureSecureProcessing, JdkXmlFeatures featureManager,
65-
XMLSecurityManager xmlSecMgr) {
67+
XMLSecurityManager xmlSecMgr, XMLSecurityPropertyManager xmlSecPropMgr) {
6668
this.origVariableResolver = this.variableResolver = vr;
6769
this.origFunctionResolver = this.functionResolver = fr;
6870
this.featureSecureProcessing = featureSecureProcessing;
6971
this.featureManager = featureManager;
7072
overrideDefaultParser = featureManager.getFeature(
7173
JdkXmlFeatures.XmlFeature.JDK_OVERRIDE_PARSER);
7274
this.xmlSecMgr = xmlSecMgr;
75+
this.xmlSecPropMgr = xmlSecPropMgr;
7376
}
7477

7578

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImplUtil.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import com.sun.org.apache.xpath.internal.axes.LocPathIterator;
3131
import com.sun.org.apache.xpath.internal.objects.XObject;
3232
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
33+
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
3334
import java.io.IOException;
35+
import javax.xml.XMLConstants;
3436
import javax.xml.namespace.QName;
3537
import javax.xml.parsers.DocumentBuilderFactory;
3638
import javax.xml.parsers.ParserConfigurationException;
@@ -54,7 +56,7 @@
5456
* This class contains several utility methods used by XPathImpl and
5557
* XPathExpressionImpl
5658
*
57-
* @LastModified: Jan 2022
59+
* @LastModified: June 2025
5860
*/
5961
class XPathImplUtil {
6062
XPathFunctionResolver functionResolver;
@@ -67,6 +69,7 @@ class XPathImplUtil {
6769
boolean featureSecureProcessing = false;
6870
JdkXmlFeatures featureManager;
6971
XMLSecurityManager xmlSecMgr;
72+
XMLSecurityPropertyManager xmlSecPropMgr;
7073

7174
/**
7275
* Evaluate an XPath context using the internal XPath engine
@@ -129,7 +132,12 @@ Document getDocument(InputSource source)
129132
//
130133
// so we really have to create a fresh DocumentBuilder every time we need one
131134
// - KK
132-
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(overrideDefaultParser);
135+
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(
136+
overrideDefaultParser, xmlSecMgr, xmlSecPropMgr);
137+
if (xmlSecMgr != null && xmlSecMgr.isSecureProcessingSet()) {
138+
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,
139+
xmlSecMgr.isSecureProcessing());
140+
}
133141
return dbf.newDocumentBuilder().parse(source);
134142
} catch (ParserConfigurationException | SAXException | IOException e) {
135143
throw new XPathExpressionException (e);

src/java.xml/share/classes/jdk/xml/internal/JdkXmlUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
3030
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
3131
import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
32+
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
3233
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
3334
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
3435
import javax.xml.XMLConstants;
@@ -312,6 +313,21 @@ public static Document getDOMDocument() {
312313
*/
313314
@SuppressWarnings("removal")
314315
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {
316+
return getDOMFactory(overrideDefaultParser, null, null);
317+
}
318+
319+
/**
320+
* {@return a DocumentBuilderFactory instance}
321+
*
322+
* @param overrideDefaultParser a flag indicating whether the system-default
323+
* implementation may be overridden. If the system property of the
324+
* DOM factory ID is set, override is always allowed.
325+
* @param xsm XMLSecurityManager
326+
* @param xspm XMLSecurityPropertyManager
327+
*/
328+
@SuppressWarnings("removal")
329+
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser,
330+
XMLSecurityManager xsm, XMLSecurityPropertyManager xspm) {
315331
boolean override = overrideDefaultParser;
316332
String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);
317333

@@ -320,7 +336,7 @@ public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser
320336
}
321337
DocumentBuilderFactory dbf
322338
= !override
323-
? new DocumentBuilderFactoryImpl()
339+
? new DocumentBuilderFactoryImpl(xsm, xspm)
324340
: DocumentBuilderFactory.newInstance();
325341
dbf.setNamespaceAware(true);
326342
// false is the default setting. This step here is for compatibility

src/java.xml/share/classes/jdk/xml/internal/XMLSecurityManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ public static enum Processor {
191191
*/
192192
boolean secureProcessing;
193193

194+
/**
195+
* Flag indicating the secure processing is set explicitly through factories'
196+
* setFeature method and then the setSecureProcessing method
197+
*/
198+
boolean secureProcessingSet;
199+
194200
/**
195201
* States that determine if properties are set explicitly
196202
*/
@@ -238,6 +244,7 @@ public XMLSecurityManager(boolean secureProcessing) {
238244
* Setting FEATURE_SECURE_PROCESSING explicitly
239245
*/
240246
public void setSecureProcessing(boolean secure) {
247+
secureProcessingSet = true;
241248
secureProcessing = secure;
242249
for (Limit limit : Limit.values()) {
243250
if (secure) {
@@ -256,6 +263,15 @@ public boolean isSecureProcessing() {
256263
return secureProcessing;
257264
}
258265

266+
/**
267+
* Returns the state indicating whether the Secure Processing is set explicitly,
268+
* via factories' setFeature and then this class' setSecureProcessing method.
269+
* @return the state indicating whether the Secure Processing is set explicitly
270+
*/
271+
public boolean isSecureProcessingSet() {
272+
return secureProcessingSet;
273+
}
274+
259275
/**
260276
* Finds a limit's new name with the given property name.
261277
* @param propertyName the property name specified

0 commit comments

Comments
 (0)