Class FactorySupport
Every create* method returns a factory pre-configured to resist
common XML attack vectors (XXE, billion laughs, external resource
resolution). Overloads accepting flags let callers relax specific defaults
when they legitimately need DOCTYPE support or external resource resolution.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentBuilderFactoryCreates a new hardenedDocumentBuilderFactory.static DocumentBuilderFactorycreateDocumentBuilderFactory(boolean allowDocTypeDeclaration) Creates a new hardenedDocumentBuilderFactory.static SAXParserFactoryCreates a new hardenedSAXParserFactory.static SAXParserFactorycreateSaxParserFactory(boolean allowDocTypeDeclaration) Creates a new hardenedSAXParserFactory.static SchemaFactorycreateSchemaFactory(String schemaLanguage) Creates a new hardenedSchemaFactoryfor the requested schema language.static TransformerFactorycreateTransformerFactory(boolean allowDocTypeDeclaration, boolean allowExternalResources) Creates a new hardenedTransformerFactory.static XMLInputFactoryCreates a new hardenedXMLInputFactoryfor StAX parsing.static XMLInputFactorycreateXMLInputFactory(boolean allowDocTypeDeclaration) Creates a new hardenedXMLInputFactoryfor StAX parsing.static XPathFactoryCreates a new hardenedXPathFactory.
-
Constructor Details
-
FactorySupport
public FactorySupport()
-
-
Method Details
-
createDocumentBuilderFactory
public static DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationExceptionCreates a new hardenedDocumentBuilderFactory.Equivalent to
createDocumentBuilderFactory(false): DOCTYPE declarations are rejected andXMLConstants.FEATURE_SECURE_PROCESSINGis enabled. PasstruetocreateDocumentBuilderFactory(boolean)if DOCTYPE support is required.Note: prior to Groovy 6.0.0 this method returned a bare JDK factory with no hardening applied. Callers that previously parsed DOCTYPE-bearing documents through the returned factory must switch to
createDocumentBuilderFactory(true).- Returns:
- a newly created, hardened document builder factory
- Throws:
ParserConfigurationException- if the factory cannot be created
-
createDocumentBuilderFactory
public static DocumentBuilderFactory createDocumentBuilderFactory(boolean allowDocTypeDeclaration) throws ParserConfigurationException Creates a new hardenedDocumentBuilderFactory.The returned factory has
XMLConstants.FEATURE_SECURE_PROCESSINGenabled, the Apachedisallow-doctype-declfeature toggled according to theallowDocTypeDeclarationflag, XInclude disabled, and entity reference expansion disabled.- Parameters:
allowDocTypeDeclaration- whetherDOCTYPEdeclarations are allowed in parsed documents (defaults should befalsefor untrusted input)- Returns:
- a newly created, hardened document builder factory
- Throws:
ParserConfigurationException- if the factory cannot be created- Since:
- 6.0.0
-
createSaxParserFactory
Creates a new hardenedSAXParserFactory.Equivalent to
createSaxParserFactory(false): DOCTYPE declarations are rejected andXMLConstants.FEATURE_SECURE_PROCESSINGis enabled. PasstruetocreateSaxParserFactory(boolean)if DOCTYPE support is required.Note: prior to Groovy 6.0.0 this method returned a bare JDK factory with no hardening applied. Callers that previously parsed DOCTYPE-bearing documents through the returned factory must switch to
createSaxParserFactory(true).- Returns:
- a newly created, hardened SAX parser factory
- Throws:
ParserConfigurationException- if the factory cannot be created
-
createSaxParserFactory
public static SAXParserFactory createSaxParserFactory(boolean allowDocTypeDeclaration) throws ParserConfigurationException Creates a new hardenedSAXParserFactory.The returned factory has
XMLConstants.FEATURE_SECURE_PROCESSINGenabled and the Apachedisallow-doctype-declfeature toggled according to theallowDocTypeDeclarationflag.- Parameters:
allowDocTypeDeclaration- whetherDOCTYPEdeclarations are allowed in parsed documents (defaults should befalsefor untrusted input)- Returns:
- a newly created, hardened SAX parser factory
- Throws:
ParserConfigurationException- if the factory cannot be created- Since:
- 6.0.0
-
createXMLInputFactory
Creates a new hardenedXMLInputFactoryfor StAX parsing.Equivalent to
createXMLInputFactory(false): DTD support and external entity resolution are disabled.- Returns:
- a newly created, hardened StAX input factory
- Since:
- 6.0.0
-
createXMLInputFactory
Creates a new hardenedXMLInputFactoryfor StAX parsing.The returned factory disables external entity resolution unconditionally and toggles
XMLInputFactory.SUPPORT_DTDaccording to theallowDocTypeDeclarationflag.- Parameters:
allowDocTypeDeclaration- whetherDOCTYPEdeclarations are allowed in parsed documents (defaults should befalsefor untrusted input)- Returns:
- a newly created, hardened StAX input factory
- Since:
- 6.0.0
-
createTransformerFactory
public static TransformerFactory createTransformerFactory(boolean allowDocTypeDeclaration, boolean allowExternalResources) Creates a new hardenedTransformerFactory.The returned factory has
XMLConstants.FEATURE_SECURE_PROCESSINGenabled and the Apachedisallow-doctype-declfeature toggled according to theallowDocTypeDeclarationflag. Access to external DTDs and stylesheets is blocked unlessallowExternalResourcesistrue.- Parameters:
allowDocTypeDeclaration- whetherDOCTYPEdeclarations are allowed in transformed documentsallowExternalResources- whether<xsl:import>/<xsl:include>may resolve external DTDs or stylesheets- Returns:
- a newly created, hardened transformer factory
- Since:
- 6.0.0
-
createSchemaFactory
Creates a new hardenedSchemaFactoryfor the requested schema language.The returned factory has
XMLConstants.FEATURE_SECURE_PROCESSINGenabled. External schema resolution is left at JDK defaults so that legitimate<xs:import>/<xs:include>usages continue to work; callers needing stricter behaviour can setXMLConstants.ACCESS_EXTERNAL_SCHEMAorXMLConstants.ACCESS_EXTERNAL_DTDon the returned factory.- Parameters:
schemaLanguage- the schema language URI (seeXMLConstants)- Returns:
- a newly created, hardened schema factory
- Since:
- 6.0.0
-
createXPathFactory
Creates a new hardenedXPathFactory.The returned factory has
XMLConstants.FEATURE_SECURE_PROCESSINGenabled.- Returns:
- a newly created, hardened XPath factory
- Since:
- 6.0.0
-