Skip to content

Conversation

richard-dennehy
Copy link
Contributor

@richard-dennehy richard-dennehy commented Aug 27, 2025

Create/move a set of utility functions that wrap the various java XML classes, providing secure default settings (e.g. prevent XXE), and enforce that these are used.

Provides secured versions of:

  • DocumentBuilderFactory
  • DocumentBuilder
  • SAXParserFactory
  • SchemaFactory
  • Validator
  • Transformer

I've tested integrating with #130337 in 665ce15 and the tests pass

@richard-dennehy richard-dennehy added >refactoring :Security/Security Security issues without another label Team:Security Meta label for security team labels Aug 27, 2025
@richard-dennehy richard-dennehy changed the title move hardened XML factories to core Introduce hardened XML utilities in core Aug 28, 2025
Comment on lines +109 to +111
javax.xml.parsers.DocumentBuilderFactory#newDefaultNSInstance()
javax.xml.parsers.DocumentBuilderFactory#newNSInstance()
javax.xml.parsers.DocumentBuilderFactory#newNSInstance(java.lang.String, java.lang.ClassLoader)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming we want to block all of these as well

* Returns a DocumentBuilderFactory pre-configured to be secure
*/
@SuppressForbidden(reason = "This is the only allowed way to construct a DocumentBuilder")
public static DocumentBuilderFactory getHardenedBuilderFactory() throws ParserConfigurationException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to provide a DocumentBuilder that doesn't validate schema as some existing usages of this API don't perform schema validation

Comment on lines +94 to +96
tfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
tfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
tfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, we don't actually need to configure ACCESS_EXTERNAL_DTD or ACCESS_EXTERNAL_STYLESHEET when FEATURE_SECURE_PROCESSING is true; as per JAXP Properties for External Access Restrictions:

Explicitly turning on Feature for Secure Processing (FSP) through the API, for example, factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true), disables all external connections.

Copy link
Contributor

@slobodanadamovic slobodanadamovic Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This is probably for historical reasons and old JDKs. If no harm, I think we should keep it.

ParserConfigurationException {
var saxParserFactory = SAXParserFactory.newInstance();

saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, this is the only security feature we actually need to set

@slobodanadamovic slobodanadamovic self-requested a review August 28, 2025 10:49
Copy link
Contributor

@slobodanadamovic slobodanadamovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@richard-dennehy richard-dennehy marked this pull request as ready for review August 29, 2025 08:34
@richard-dennehy richard-dennehy requested review from a team as code owners August 29, 2025 08:34
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

@elasticsearchmachine elasticsearchmachine added the serverless-linked Added by automation, don't add manually label Aug 29, 2025
@richard-dennehy richard-dennehy merged commit bbea507 into elastic:main Aug 29, 2025
40 checks passed
JeremyDahlgren pushed a commit to JeremyDahlgren/elasticsearch that referenced this pull request Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>refactoring :Security/Security Security issues without another label serverless-linked Added by automation, don't add manually Team:Security Meta label for security team v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants