Skip to content

Commit c85cd73

Browse files
joegallogmjehovich
authored andcommitted
Add XPath to XmlUtils (elastic#134923)
1 parent 6938ed0 commit c85cd73

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

build-tools-internal/src/main/resources/forbidden/jdk-signatures.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,9 @@ javax.xml.validation.SchemaFactory#newInstance(java.lang.String, java.lang.Strin
128128

129129
@defaultMessage Validator should not be used directly. Use XmlUtils#getHardenedValidator() instead
130130
javax.xml.validation.Schema#newValidator()
131+
132+
@defaultMessage XPathFactory should not be used directly. Use XmlUtils#getHardenedXPath() instead
133+
javax.xml.xpath.XPathFactory#newDefaultInstance()
134+
javax.xml.xpath.XPathFactory#newInstance()
135+
javax.xml.xpath.XPathFactory#newInstance(java.lang.String)
136+
javax.xml.xpath.XPathFactory#newInstance(java.lang.String, java.lang.String, java.lang.ClassLoader)

libs/core/src/main/java/org/elasticsearch/core/XmlUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import javax.xml.validation.Schema;
2929
import javax.xml.validation.SchemaFactory;
3030
import javax.xml.validation.Validator;
31+
import javax.xml.xpath.XPath;
32+
import javax.xml.xpath.XPathFactory;
33+
import javax.xml.xpath.XPathFactoryConfigurationException;
3134

3235
public class XmlUtils {
3336

@@ -126,16 +129,24 @@ public static Validator getHardenedValidator(Schema schema) throws SAXNotSupport
126129
public static SAXParserFactory getHardenedSaxParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException,
127130
ParserConfigurationException {
128131
var saxParserFactory = SAXParserFactory.newInstance();
129-
130132
saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
131133
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
132134
saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
133135
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
134136
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
135-
136137
return saxParserFactory;
137138
}
138139

140+
/**
141+
* Constructs an XPath configured to be secure
142+
*/
143+
@SuppressForbidden(reason = "This is the only allowed way to construct an XPath")
144+
public static XPath getHardenedXPath() throws XPathFactoryConfigurationException {
145+
XPathFactory xPathFactory = XPathFactory.newInstance();
146+
xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
147+
return xPathFactory.newXPath();
148+
}
149+
139150
private static class ErrorHandler implements org.xml.sax.ErrorHandler {
140151
/**
141152
* Enabling schema validation with `setValidating(true)` in our

x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import javax.xml.parsers.DocumentBuilderFactory;
4343
import javax.xml.xpath.XPath;
4444
import javax.xml.xpath.XPathConstants;
45-
import javax.xml.xpath.XPathFactory;
4645

4746
import static org.hamcrest.Matchers.containsInAnyOrder;
4847
import static org.hamcrest.Matchers.containsString;
@@ -170,8 +169,7 @@ public void testCustomAttributesInIdpInitiatedSso() throws Exception {
170169
Document document = builder.parse(new InputSource(new StringReader(samlResponse)));
171170

172171
// Create XPath evaluator
173-
XPathFactory xPathFactory = XPathFactory.newInstance();
174-
XPath xpath = xPathFactory.newXPath();
172+
XPath xpath = XmlUtils.getHardenedXPath();
175173

176174
// Validate SAML Response structure
177175
Element responseElement = (Element) xpath.evaluate("//*[local-name()='Response']", document, XPathConstants.NODE);

0 commit comments

Comments
 (0)