|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.core; |
| 11 | + |
| 12 | +import org.elasticsearch.logging.LogManager; |
| 13 | +import org.elasticsearch.logging.Logger; |
| 14 | +import org.xml.sax.SAXException; |
| 15 | +import org.xml.sax.SAXNotRecognizedException; |
| 16 | +import org.xml.sax.SAXNotSupportedException; |
| 17 | +import org.xml.sax.SAXParseException; |
| 18 | + |
| 19 | +import javax.xml.XMLConstants; |
| 20 | +import javax.xml.parsers.DocumentBuilder; |
| 21 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 22 | +import javax.xml.parsers.ParserConfigurationException; |
| 23 | +import javax.xml.parsers.SAXParserFactory; |
| 24 | +import javax.xml.transform.Transformer; |
| 25 | +import javax.xml.transform.TransformerConfigurationException; |
| 26 | +import javax.xml.transform.TransformerException; |
| 27 | +import javax.xml.transform.TransformerFactory; |
| 28 | +import javax.xml.validation.Schema; |
| 29 | +import javax.xml.validation.SchemaFactory; |
| 30 | +import javax.xml.validation.Validator; |
| 31 | + |
| 32 | +public class XmlUtils { |
| 33 | + |
| 34 | + private static final Logger LOGGER = LogManager.getLogger(XmlUtils.class); |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructs a DocumentBuilder with all the necessary features for it to be secure |
| 38 | + * |
| 39 | + * @throws ParserConfigurationException if one of the features can't be set on the DocumentBuilderFactory |
| 40 | + */ |
| 41 | + public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws ParserConfigurationException { |
| 42 | + final DocumentBuilderFactory dbf = getHardenedBuilderFactory(); |
| 43 | + dbf.setNamespaceAware(true); |
| 44 | + // Ensure that Schema Validation is enabled for the factory |
| 45 | + dbf.setValidating(true); |
| 46 | + // This is required, otherwise schema validation causes signature invalidation |
| 47 | + dbf.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", false); |
| 48 | + // Make sure that URL schema namespaces are not resolved/downloaded from URLs we do not control |
| 49 | + dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar"); |
| 50 | + dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar"); |
| 51 | + // We ship our own xsd files for schema validation since we do not trust anyone else. |
| 52 | + dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaFiles); |
| 53 | + DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); |
| 54 | + documentBuilder.setErrorHandler(new ErrorHandler()); |
| 55 | + return documentBuilder; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns a DocumentBuilderFactory pre-configured to be secure |
| 60 | + */ |
| 61 | + @SuppressForbidden(reason = "This is the only allowed way to construct a DocumentBuilder") |
| 62 | + public static DocumentBuilderFactory getHardenedBuilderFactory() throws ParserConfigurationException { |
| 63 | + final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 64 | + // Disallow internal and external entity expansion |
| 65 | + dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 66 | + dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); |
| 67 | + dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); |
| 68 | + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 69 | + dbf.setFeature("http://xml.org/sax/features/validation", true); |
| 70 | + dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); |
| 71 | + dbf.setIgnoringComments(true); |
| 72 | + dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 73 | + dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
| 74 | + dbf.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true); |
| 75 | + // Ensure we do not resolve XIncludes. Defaults to false, but set it explicitly to be future-proof |
| 76 | + dbf.setXIncludeAware(false); |
| 77 | + // Ensure we do not expand entity reference nodes |
| 78 | + dbf.setExpandEntityReferences(false); |
| 79 | + // Further limit danger from denial of service attacks |
| 80 | + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 81 | + dbf.setAttribute("http://apache.org/xml/features/validation/schema", true); |
| 82 | + dbf.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", true); |
| 83 | + dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 84 | + |
| 85 | + return dbf; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Constructs a Transformer configured to be secure |
| 90 | + */ |
| 91 | + @SuppressForbidden(reason = "This is the only allowed way to construct a Transformer") |
| 92 | + public static Transformer getHardenedXMLTransformer() throws TransformerConfigurationException { |
| 93 | + final TransformerFactory tfactory = TransformerFactory.newInstance(); |
| 94 | + tfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 95 | + tfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 96 | + tfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); |
| 97 | + tfactory.setAttribute("indent-number", 2); |
| 98 | + Transformer transformer = tfactory.newTransformer(); |
| 99 | + transformer.setErrorListener(new ErrorListener()); |
| 100 | + return transformer; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns a SchemaFactory configured to be secure |
| 105 | + */ |
| 106 | + @SuppressForbidden(reason = "This is the only allowed way to construct a SchemaFactory") |
| 107 | + public static SchemaFactory getHardenedSchemaFactory() throws SAXNotSupportedException, SAXNotRecognizedException { |
| 108 | + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 109 | + schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 110 | + schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
| 111 | + return schemaFactory; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Constructs a Validator configured to be secure |
| 116 | + */ |
| 117 | + @SuppressForbidden(reason = "This is the only allowed way to construct a Validator") |
| 118 | + public static Validator getHardenedValidator(Schema schema) throws SAXNotSupportedException, SAXNotRecognizedException { |
| 119 | + Validator validator = schema.newValidator(); |
| 120 | + validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 121 | + validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
| 122 | + return validator; |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressForbidden(reason = "This is the only allowed way to construct a SAXParser") |
| 126 | + public static SAXParserFactory getHardenedSaxParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException, |
| 127 | + ParserConfigurationException { |
| 128 | + var saxParserFactory = SAXParserFactory.newInstance(); |
| 129 | + |
| 130 | + saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 131 | + saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); |
| 132 | + saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); |
| 133 | + saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 134 | + saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 135 | + |
| 136 | + return saxParserFactory; |
| 137 | + } |
| 138 | + |
| 139 | + private static class ErrorHandler implements org.xml.sax.ErrorHandler { |
| 140 | + /** |
| 141 | + * Enabling schema validation with `setValidating(true)` in our |
| 142 | + * DocumentBuilderFactory requires that we provide our own |
| 143 | + * ErrorHandler implementation |
| 144 | + * |
| 145 | + * @throws SAXException If the document we attempt to parse is not valid according to the specified schema. |
| 146 | + */ |
| 147 | + @Override |
| 148 | + public void warning(SAXParseException e) throws SAXException { |
| 149 | + LOGGER.debug("XML Parser error ", e); |
| 150 | + throw e; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void error(SAXParseException e) throws SAXException { |
| 155 | + LOGGER.debug("XML Parser error ", e); |
| 156 | + throw e; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void fatalError(SAXParseException e) throws SAXException { |
| 161 | + LOGGER.debug("XML Parser error ", e); |
| 162 | + throw e; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private static class ErrorListener implements javax.xml.transform.ErrorListener { |
| 167 | + |
| 168 | + @Override |
| 169 | + public void warning(TransformerException e) throws TransformerException { |
| 170 | + LOGGER.debug("XML transformation error", e); |
| 171 | + throw e; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void error(TransformerException e) throws TransformerException { |
| 176 | + LOGGER.debug("XML transformation error", e); |
| 177 | + throw e; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void fatalError(TransformerException e) throws TransformerException { |
| 182 | + LOGGER.debug("XML transformation error", e); |
| 183 | + throw e; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments