Skip to content

Commit d9dad57

Browse files
JoeWang-Javabchristi-git
authored andcommitted
8356294: Enhance Path Factories
Reviewed-by: ahgross, rriggs, rhalade, lancea, naoto
1 parent 8145cfa commit d9dad57

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/**
4242
* @author Rajiv Mordani
4343
* @author Edwin Goei
44-
* @LastModified: May 2025
44+
* @LastModified: June 2025
4545
*/
4646
public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
4747
/** These are DocumentBuilderFactory attributes not DOM attributes */
@@ -59,11 +59,24 @@ public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
5959
XMLSecurityManager fSecurityManager;
6060
XMLSecurityPropertyManager fSecurityPropertyMgr;
6161

62+
/**
63+
* Creates a new {@code DocumentBuilderFactory} instance.
64+
*/
6265
public DocumentBuilderFactoryImpl() {
66+
this(null, null);
67+
}
68+
69+
/**
70+
* Creates a new {@code DocumentBuilderFactory} instance with a {@code XMLSecurityManager}
71+
* and {@code XMLSecurityPropertyManager}.
72+
* @param xsm the {@code XMLSecurityManager}
73+
* @param xspm the {@code XMLSecurityPropertyManager}
74+
*/
75+
public DocumentBuilderFactoryImpl(XMLSecurityManager xsm, XMLSecurityPropertyManager xspm) {
6376
JdkXmlConfig config = JdkXmlConfig.getInstance(false);
6477
// security (property) managers updated with current system properties
65-
fSecurityManager = config.getXMLSecurityManager(true);
66-
fSecurityPropertyMgr = config.getXMLSecurityPropertyManager(true);
78+
fSecurityManager = (xsm == null) ? config.getXMLSecurityManager(true) : xsm;
79+
fSecurityPropertyMgr = (xspm == null) ? config.getXMLSecurityPropertyManager(true) : xspm;
6780
}
6881

6982
/**

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* @author Ramesh Mandava
3737
*
38-
* @LastModified: May 2025
38+
* @LastModified: June 2025
3939
*/
4040
public class XPathFactoryImpl extends XPathFactory {
4141

@@ -72,6 +72,7 @@ public class XPathFactoryImpl extends XPathFactory {
7272
* The XML security manager
7373
*/
7474
private XMLSecurityManager _xmlSecMgr;
75+
private XMLSecurityPropertyManager _xmlSecPropMgr;
7576

7677
/**
7778
* javax.xml.xpath.XPathFactory implementation.
@@ -80,6 +81,7 @@ public XPathFactoryImpl() {
8081
JdkXmlConfig config = JdkXmlConfig.getInstance(false);
8182
_xmlSecMgr = config.getXMLSecurityManager(true);
8283
_featureManager = config.getXMLFeatures(true);
84+
_xmlSecPropMgr = config.getXMLSecurityPropertyManager(true);
8385
}
8486

8587
/**
@@ -129,7 +131,7 @@ public boolean isObjectModelSupported(String objectModel) {
129131
*/
130132
public javax.xml.xpath.XPath newXPath() {
131133
return new XPathImpl(xPathVariableResolver, xPathFunctionResolver,
132-
!_isNotSecureProcessing, _featureManager, _xmlSecMgr);
134+
!_isNotSecureProcessing, _featureManager, _xmlSecMgr, _xmlSecPropMgr);
133135
}
134136

135137
/**
@@ -183,6 +185,7 @@ public void setFeature(String name, boolean value)
183185
if (value && _featureManager != null) {
184186
_featureManager.setFeature(JdkXmlFeatures.XmlFeature.ENABLE_EXTENSION_FUNCTION,
185187
JdkProperty.State.FSP, false);
188+
_xmlSecMgr.setSecureProcessing(value);
186189
}
187190

188191
// all done processing feature
@@ -338,8 +341,7 @@ public void setProperty(String name, String value) {
338341
throw new NullPointerException(fmsg);
339342
}
340343

341-
if (_xmlSecMgr != null &&
342-
_xmlSecMgr.setLimit(name, JdkProperty.State.APIPROPERTY, value)) {
344+
if (JdkXmlUtils.setProperty(_xmlSecMgr, _xmlSecPropMgr, name, value)) {
343345
return;
344346
}
345347

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
@@ -36,6 +36,7 @@
3636
import jdk.xml.internal.JdkXmlConfig;
3737
import jdk.xml.internal.JdkXmlFeatures;
3838
import jdk.xml.internal.XMLSecurityManager;
39+
import jdk.xml.internal.XMLSecurityPropertyManager;
3940
import org.w3c.dom.Document;
4041
import org.xml.sax.InputSource;
4142

@@ -50,7 +51,7 @@
5051
* New methods: evaluateExpression
5152
* Refactored to share code with XPathExpressionImpl.
5253
*
53-
* @LastModified: May 2025
54+
* @LastModified: June 2025
5455
*/
5556
public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath {
5657

@@ -62,19 +63,21 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath {
6263
XPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr) {
6364
this(vr, fr, false,
6465
JdkXmlConfig.getInstance(false).getXMLFeatures(false),
65-
JdkXmlConfig.getInstance(false).getXMLSecurityManager(false));
66+
JdkXmlConfig.getInstance(false).getXMLSecurityManager(false),
67+
JdkXmlConfig.getInstance(false).getXMLSecurityPropertyManager(false));
6668
}
6769

6870
XPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr,
6971
boolean featureSecureProcessing, JdkXmlFeatures featureManager,
70-
XMLSecurityManager xmlSecMgr) {
72+
XMLSecurityManager xmlSecMgr, XMLSecurityPropertyManager xmlSecPropMgr) {
7173
this.origVariableResolver = this.variableResolver = vr;
7274
this.origFunctionResolver = this.functionResolver = fr;
7375
this.featureSecureProcessing = featureSecureProcessing;
7476
this.featureManager = featureManager;
7577
overrideDefaultParser = featureManager.getFeature(
7678
JdkXmlFeatures.XmlFeature.JDK_OVERRIDE_PARSER);
7779
this.xmlSecMgr = xmlSecMgr;
80+
this.xmlSecPropMgr = xmlSecPropMgr;
7881
}
7982

8083

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
@@ -31,6 +31,7 @@
3131
import com.sun.org.apache.xpath.internal.objects.XObject;
3232
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
3333
import java.io.IOException;
34+
import javax.xml.XMLConstants;
3435
import javax.xml.namespace.QName;
3536
import javax.xml.parsers.DocumentBuilderFactory;
3637
import javax.xml.parsers.ParserConfigurationException;
@@ -44,6 +45,7 @@
4445
import jdk.xml.internal.JdkXmlFeatures;
4546
import jdk.xml.internal.JdkXmlUtils;
4647
import jdk.xml.internal.XMLSecurityManager;
48+
import jdk.xml.internal.XMLSecurityPropertyManager;
4749
import org.w3c.dom.Document;
4850
import org.w3c.dom.Node;
4951
import org.w3c.dom.traversal.NodeIterator;
@@ -54,7 +56,7 @@
5456
* This class contains several utility methods used by XPathImpl and
5557
* XPathExpressionImpl
5658
*
57-
* @LastModified: Apr 2025
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
@@ -128,7 +131,12 @@ Document getDocument(InputSource source)
128131
//
129132
// so we really have to create a fresh DocumentBuilder every time we need one
130133
// - KK
131-
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(overrideDefaultParser);
134+
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(
135+
overrideDefaultParser, xmlSecMgr, xmlSecPropMgr);
136+
if (xmlSecMgr != null && xmlSecMgr.isSecureProcessingSet()) {
137+
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,
138+
xmlSecMgr.isSecureProcessing());
139+
}
132140
return dbf.newDocumentBuilder().parse(source);
133141
} catch (ParserConfigurationException | SAXException | IOException e) {
134142
throw new XPathExpressionException (e);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,20 @@ public static Document getDOMDocument() {
445445
* @return a DocumentBuilderFactory instance.
446446
*/
447447
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {
448+
return getDOMFactory(overrideDefaultParser, null, null);
449+
}
450+
451+
/**
452+
* {@return a DocumentBuilderFactory instance}
453+
*
454+
* @param overrideDefaultParser a flag indicating whether the system-default
455+
* implementation may be overridden. If the system property of the
456+
* DOM factory ID is set, override is always allowed.
457+
* @param xsm XMLSecurityManager
458+
* @param xspm XMLSecurityPropertyManager
459+
*/
460+
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser,
461+
XMLSecurityManager xsm, XMLSecurityPropertyManager xspm) {
448462
boolean override = overrideDefaultParser;
449463
String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);
450464

@@ -453,7 +467,7 @@ public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser
453467
}
454468
DocumentBuilderFactory dbf
455469
= !override
456-
? new DocumentBuilderFactoryImpl()
470+
? new DocumentBuilderFactoryImpl(xsm, xspm)
457471
: DocumentBuilderFactory.newInstance();
458472
dbf.setNamespaceAware(true);
459473
// 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
@@ -244,6 +244,12 @@ public static enum Processor {
244244
*/
245245
boolean secureProcessing;
246246

247+
/**
248+
* Flag indicating the secure processing is set explicitly through factories'
249+
* setFeature method and then the setSecureProcessing method
250+
*/
251+
boolean secureProcessingSet;
252+
247253
/**
248254
* States that determine if properties are set explicitly
249255
*/
@@ -340,6 +346,7 @@ private NotFoundAction toActionType(String resolve) {
340346
* Setting FEATURE_SECURE_PROCESSING explicitly
341347
*/
342348
public void setSecureProcessing(boolean secure) {
349+
secureProcessingSet = true;
343350
secureProcessing = secure;
344351
for (Limit limit : Limit.values()) {
345352
if (secure) {
@@ -358,6 +365,15 @@ public boolean isSecureProcessing() {
358365
return secureProcessing;
359366
}
360367

368+
/**
369+
* Returns the state indicating whether the Secure Processing is set explicitly,
370+
* via factories' setFeature and then this class' setSecureProcessing method.
371+
* @return the state indicating whether the Secure Processing is set explicitly
372+
*/
373+
public boolean isSecureProcessingSet() {
374+
return secureProcessingSet;
375+
}
376+
361377
/**
362378
* Finds a limit's new name with the given property name.
363379
* @param propertyName the property name specified

0 commit comments

Comments
 (0)