Class XmlUtil

java.lang.Object
io.vertigo.core.util.XmlUtil

public final class XmlUtil extends Object
XML utility : XSD validation and XXE hardening.
Author:
pchretien
  • Method Details

    • removeDoctype

      public static String removeDoctype(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).
      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:
      SAXNotRecognizedException
      SAXNotSupportedException
    • secureXmlXXEByOwasp

      public static void secureXmlXXEByOwasp(SAXParserFactory factory) throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException
      Secure XML parser with OWASP recommendations.
      Parameters:
      schemaFactory - SchemaFactory
      Throws:
      ParserConfigurationException
      SAXNotRecognizedException
      SAXNotSupportedException
    • 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 : see removeDoctype(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érer secureXmlXXEByOwasp(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

      public static Document parseXmlSecure(String xml)
      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

      public static Document parseXmlSecure(InputStream xml)
      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

      public static void validateXmlByXsd(URL xml, URL xsd)
      Util to validate XML with a XSD.
      Parameters:
      xml - XML to validate
      xsd - XSD