Package io.vertigo.core.util
Class XmlUtil
java.lang.Object
io.vertigo.core.util.XmlUtil
XML utility : XSD validation and XXE hardening.
- Author:
- pchretien
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentParses an XML document securely (OWASP strict mode) after removing a leading DOCTYPE declaration.static DocumentparseXmlSecure(String xml) Parses an XML document securely (OWASP strict mode) after removing a leading DOCTYPE declaration.static StringremoveDoctype(String xml) Removes a leading DOCTYPE declaration (including its internal subset) from an XML content, so that the document can be parsed with strict XXE settings (disallow-doctype-decl).static voidsecureXmlXXEByOwasp(DocumentBuilderFactory documentBuilderFactory) Secure XML parser with OWASP recommendations.static voidsecureXmlXXEByOwasp(SAXParserFactory factory) Secure XML parser with OWASP recommendations.static voidSecure XML parser with OWASP recommendations.static voidsecureXmlXXEByOwasp(SchemaFactory schemaFactory) Secure XML parser with OWASP recommendations.static voidsecureXmlXXEByOwaspAllowDoctype(DocumentBuilderFactory documentBuilderFactory) Fallback pour documents imposant un DOCTYPE (fichiers reçus non modifiables).static voidvalidateXmlByXsd(URL xml, URL xsd) Util to validate XML with a XSD.
-
Method Details
-
removeDoctype
Removes a leading DOCTYPE declaration (including its internal subset) from an XML content, so that the document can be parsed with strict XXE settings (disallow-doctype-decl).
The DOCTYPE keyword is matched case-insensitively. Only a DOCTYPE positioned right after an optional BOM, whitespaces and an XML declaration (<?xml ...?>) is removed : this is the only position where a DOCTYPE declaration is legal in XML. The input is returned unchanged when no DOCTYPE is found, when the DOCTYPE is not at this initial position, or when the declaration is not properly terminated (the XML parser will then report the underlying error). Nothing else is ever modified.- Parameters:
xml- XML content- Returns:
- the same content without its leading DOCTYPE declaration, or the input unchanged
- Throws:
NullPointerException- if xml is null
-
secureXmlXXEByOwasp
public static void secureXmlXXEByOwasp(SchemaFactory schemaFactory) throws SAXNotRecognizedException, SAXNotSupportedException Secure XML parser with OWASP recommendations.- Parameters:
schemaFactory- SchemaFactory- Throws:
SAXNotRecognizedExceptionSAXNotSupportedException
-
secureXmlXXEByOwasp
public static void secureXmlXXEByOwasp(SAXParserFactory factory) throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException Secure XML parser with OWASP recommendations.- Parameters:
schemaFactory- SchemaFactory- Throws:
ParserConfigurationExceptionSAXNotRecognizedExceptionSAXNotSupportedException
-
secureXmlXXEByOwasp
public static void secureXmlXXEByOwasp(TransformerFactory tf) throws TransformerConfigurationException Secure XML parser with OWASP recommendations.- Parameters:
tf- TransformerFactory- Throws:
TransformerConfigurationException
-
secureXmlXXEByOwasp
public static void secureXmlXXEByOwasp(DocumentBuilderFactory documentBuilderFactory) throws ParserConfigurationException Secure XML parser with OWASP recommendations. Documents containing a DOCTYPE declaration are rejected : seeremoveDoctype(String)/parseXmlSecure(String).- Parameters:
domFactory- DocumentBuilderFactory- Throws:
ParserConfigurationException
-
secureXmlXXEByOwaspAllowDoctype
public static void secureXmlXXEByOwaspAllowDoctype(DocumentBuilderFactory documentBuilderFactory) throws ParserConfigurationException Fallback pour documents imposant un DOCTYPE (fichiers reçus non modifiables). Risque résiduel documenté par l'OWASP : SSRF et DoS (billion laughs, décompression jar:). PréférersecureXmlXXEByOwasp(DocumentBuilderFactory)+removeDoctype(String)/parseXmlSecure(String)dès que possible. Note : expandEntityReferences=false laisse les EntityReference non expansées dans le DOM. Défense en profondeur : définir aussi sur le DocumentBuilder issu de cette factory un EntityResolver renvoyant une InputSource vide (builder.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader("")))).- Parameters:
documentBuilderFactory- DocumentBuilderFactory- Throws:
ParserConfigurationException
-
parseXmlSecure
Parses an XML document securely (OWASP strict mode) after removing a leading DOCTYPE declaration. Use it for documents that must be parseable while carrying a residual <!DOCTYPE> (e.g. generated XHTML).- Parameters:
xml- XML content- Returns:
- parsed Document (without any DOCTYPE)
- Throws:
NullPointerException- if xml is null
-
parseXmlSecure
Parses an XML document securely (OWASP strict mode) after removing a leading DOCTYPE declaration. The stream is fully read using UTF-8 : an encoding declared in the XML prolog is ignored.- Parameters:
xml- XML content (UTF-8)- Returns:
- parsed Document (without any DOCTYPE)
- Throws:
NullPointerException- if xml is null
-
validateXmlByXsd
Util to validate XML with a XSD.- Parameters:
xml- XML to validatexsd- XSD
-