Securing a DOM Parser

Securing a DOM Parser

If your Java application is using DocumentBuilderFactory and DocumentBuilder classes for XML parsing, you can get a secured DocumentBuilderFactory implementation by calling a method from the XML Hardener Facade right before the XML Parser is instantiated.

The returned implementation HardenedDocumentBuilderFactory overrides the methods from DocumentBuilderFactory and secures the XML parser without further interaction from the application.

However, if there are more specific needs for a particular application, there are some additional features and attributes not available in the standard DocumentBuilderFactory and provided by HardenedDocumentBuilderFactory. They allow changes to the behavior of the XML Hardener.

To secure your DOM parser, proceed as follows:

  1. Import the following code to the place where your application code uses XML parsing:

    import com.sap.security.hardener.nw.facade.HardenerFacade;
  2. Locate code lines within the application, which instantiate the DocumentBuilderFactory. This would be a line similar to this:

  3. Add one more line that will secure your DocumentBuilderFactory

    dbFactory = HardenerFacade.secureDocumentBuilderFactory(dbFactory);

Upon calling the secureDocumentBuilderFactory method, the XML Hardener immediately secures the XML parser. As a result, the application will start to use the new secured HardenedDocumentBuilderFactory class.

The method with parameters DocumentBuilderFactory and String namespace is used for external configuration file. The second parameter identifies which exactly configuration file to be used.

public static DocumentBuilderFactory secureDocumentBuilderFactory(final DocumentBuilderFactory factory, final String callerNamespace)
        throws ParserConfigurationException

Immediate Effects of Calling the secureDocumentBuilderFactory() method

  • The parser will be set up to prevent XML bombs

  • The parser will set up an EntityResolver, which throws an exception if any external entity is used within a DTD

  • Validation will be disabled

  • Changing any settings (attributes, features or methods) of DocumentBuilderFactory is no longer possible

    An application may not alter any settings and an exception will be thrown if the application tries this.

New Features Provided by HardenedDocumentBuilderFactory

Depending on the application need, it may be sufficient simply to call secureDocumentBuilderFactory method from the HardenedDocumentBuilderFactoryFacade class. This way, the parser will be immediately secured and no more changes are required.

However, it may be necessary for the application to utilize the new set of features provided by the HardenedDocumentBuilderFactory.

Possible interactions with the class

General

  • HardenerConstants.FACTORYACCESS_FEATURE - If the value of the feature is set to true, the application will be granted full access to the DocumentBuilderFactory, and the normal restrictions will not be applicable. However, none of the initial settings during the securing are reverted

  • HardenerConstants.FACTORY_RESET_PARSER - If the value of the feature is set to true, the parser will be reset to the state right after securing it.

  • HardenerConstants.REMOVE_EXT_ENTITIES_FEATURE - any External Entity will be replaced with a blank entity instead of causing an exception

White List

  • HardenerConstants.USE_PSE_WHITELIST_FEATURE - Set the white list to be used by EntityResolver during DocumentBuilder instantiation

Mapper List

  • HardenerConstants.USE_PSE_REPLACEMENTMAP_FEATURE - Set the mapper list to be used by EntityResolver during DocumentBuilder instantiation

Learning Mode

  • HardenerConstants.USE_PSE_LEARNINGLIST_FEATURE - activates learning mode

Take Over Full Control of the XML parser

After securing DocumentBuilderFactory, you can still change some features and attributes. Changing a single feature or attribute may result in automatically changing several others. Features and attributes, which are considered related to the security, can only be changed with your explicit consent and awareness, and after changing the value of the XML Hardener feature HardenerConstants.FACTORYACCESS_FEATURE.

Reset Changes Done During Securing DocumentBuilderFactory

In case you want to undo the changes done to the XML parser after securing the DocumentBuilderFactory, execute the following code line:

dbFactory.setFeature (HardenerConstants.FACTORY_RESET_PARSER, true);

Identifying External Entities

External parsed entities refer to data that an XML processor has to parse. They are useful for creating common references that you can share between multiple documents. Any changes that are made to external entities are automatically updated in the documents they are referenced to.

There are two types of external entities: private and public.
  • Private external entities are identified by the keyword SYSTEM, and are intended for use by a single author or group of authors.

  • Public external entities are identified by the keyword PUBLIC and are intended for broad use.
    <!ENTITY name SYSTEM "URI">
    <!ENTITY name PUBLIC "public_ID" "URI">
    

The following entity is a private one and is identified by its name "c" and the URL to the entity definition, e.g. its system id http://www.xmlwriter.net/copyright.xml.

The following entity is a public one and is identified by its name "c", its public id -//W3C//TEXT copyright//EN and the URL to the entity's definition, e.g. its system id http://www.w3.org/xmlspec/copyright.xml.

In XML Hardener there are two classes used for entities identifiers depending on the EntityResolver used.

or for DOM Level 2 (http://www.w3.org/TR/2004/PR-DOM-Level-3-LS-20040205/load-save.html).

White Listing

White lists are used when you want to specify trusted URIs for external entities within the DTD in your application. All URIs added in the white list will be treated as secure and access to them will be allowed. It is up to you to define this list in your application.

By default, the feature HardenerConstants.USE_PSE_WHITELIST_FEATURE is set to true so there is no need to set it before the configuration of your white list.

White list can be configured from a configuration file or programmatically as in the following example:

Entity Mapping

In your application, you can specify a set of URIs, which are accepted within a DTD as external entities but will be mapped to a different URIs using a mapper list. For example, if the incoming URI http://www.xyz.com/my.dtd is mapped to C:/dtd/xyz_com_my.dtd, your application will accept the incoming DTD http://www.xyz.com/my.dtd and will replace it with the local version to which it is mapped C:/dtd/xyz_com_my.dtd. You can also add a list of Entity Resolvers to each entity mapping entry.

By default, the feature HardenerConstants.USE_PSE_REPLACEMENTMAP_FEATURE is set to false so, you have to set it to true before start configuring your mapper list.

Mapper list can be configured from a configuration file or programmatically as in the following example:

Entity Resolvers

Entity Resolvers are basically callbacks. If the XML parser finds an external entity during parsing of the document, the parser will check if an Entity Resolver has been specified and if so call it. The primary purpose of these Entity Resolvers is to make the application more robust against failures. For example, the external entity could point to a network URI - which will not work if the network is not available. However, the application should still process this document. Or the URI is a relative path which might also not work for the executing application, and again should not cause a failure.

All XML parsers are using entity resolvers to find external entities like DTDs or XSDs.HardenedDocumentBuilderFactory supports different entity resolvers. It depends on the use case which one to be used.

  • Entity Resolvers from Parsers

    The default entity resolvers do not check the external entities. The resolvers try to resolve the location of the entities directly. It means the parser will try to access everything entered as value using the URL class (all its supported features and protocols).

  • Default Entity Resolver from XML Hardener

    There are two default implementations for both interfaces org.xml.sax.EntityResolver and org.xml.sax.ext.EntityResolver2 in XML Hardener, correspondingly HardenedEntityResolver and HardenedEntityResolver2.

Hardened Entity Resolver diagram

Failed Parsing due to External Entities

There are different ways for the application to react to failed parsing due to External Entities. One option is to throw an exception and stop if the cause was an untrusted (unspecified) URI, or to add the URI to a list for a later evaluation by administrator.