|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, PostgreSQL Global Development Group |
| 3 | + * See the LICENSE file in the project root for more information. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.postgresql.xml; |
| 7 | + |
| 8 | +import org.xml.sax.SAXException; |
| 9 | +import org.xml.sax.XMLReader; |
| 10 | +import org.xml.sax.helpers.XMLReaderFactory; |
| 11 | + |
| 12 | +import javax.xml.XMLConstants; |
| 13 | +import javax.xml.parsers.DocumentBuilder; |
| 14 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 15 | +import javax.xml.parsers.ParserConfigurationException; |
| 16 | +import javax.xml.stream.XMLInputFactory; |
| 17 | +import javax.xml.stream.XMLOutputFactory; |
| 18 | +import javax.xml.transform.TransformerFactory; |
| 19 | +import javax.xml.transform.sax.SAXTransformerFactory; |
| 20 | + |
| 21 | +/** |
| 22 | + * Default implementation of PGXmlFactoryFactory that configures each factory per OWASP recommendations. |
| 23 | + * |
| 24 | + * @see <a href="https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html">https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html</a> |
| 25 | + */ |
| 26 | +public class DefaultPGXmlFactoryFactory implements PGXmlFactoryFactory { |
| 27 | + public static final DefaultPGXmlFactoryFactory INSTANCE = new DefaultPGXmlFactoryFactory(); |
| 28 | + |
| 29 | + private DefaultPGXmlFactoryFactory() { |
| 30 | + } |
| 31 | + |
| 32 | + private DocumentBuilderFactory getDocumentBuilderFactory() { |
| 33 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 34 | + setFactoryProperties(factory); |
| 35 | + factory.setXIncludeAware(false); |
| 36 | + factory.setExpandEntityReferences(false); |
| 37 | + return factory; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { |
| 42 | + DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder(); |
| 43 | + builder.setEntityResolver(EmptyStringEntityResolver.INSTANCE); |
| 44 | + builder.setErrorHandler(NullErrorHandler.INSTANCE); |
| 45 | + return builder; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TransformerFactory newTransformerFactory() { |
| 50 | + TransformerFactory factory = TransformerFactory.newInstance(); |
| 51 | + setFactoryProperties(factory); |
| 52 | + return factory; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public SAXTransformerFactory newSAXTransformerFactory() { |
| 57 | + SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); |
| 58 | + setFactoryProperties(factory); |
| 59 | + return factory; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public XMLInputFactory newXMLInputFactory() { |
| 64 | + XMLInputFactory factory = XMLInputFactory.newInstance(); |
| 65 | + setPropertyQuietly(factory, XMLInputFactory.SUPPORT_DTD, false); |
| 66 | + setPropertyQuietly(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
| 67 | + return factory; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public XMLOutputFactory newXMLOutputFactory() { |
| 72 | + XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
| 73 | + return factory; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public XMLReader createXMLReader() throws SAXException { |
| 78 | + XMLReader factory = XMLReaderFactory.createXMLReader(); |
| 79 | + setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", true); |
| 80 | + setFeatureQuietly(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 81 | + setFeatureQuietly(factory, "http://xml.org/sax/features/external-general-entities", false); |
| 82 | + setFeatureQuietly(factory, "http://xml.org/sax/features/external-parameter-entities", false); |
| 83 | + factory.setErrorHandler(NullErrorHandler.INSTANCE); |
| 84 | + return factory; |
| 85 | + } |
| 86 | + |
| 87 | + private static void setFeatureQuietly(Object factory, String name, boolean value) { |
| 88 | + try { |
| 89 | + if (factory instanceof DocumentBuilderFactory) { |
| 90 | + ((DocumentBuilderFactory) factory).setFeature(name, value); |
| 91 | + } else if (factory instanceof TransformerFactory) { |
| 92 | + ((TransformerFactory) factory).setFeature(name, value); |
| 93 | + } else if (factory instanceof XMLReader) { |
| 94 | + ((XMLReader) factory).setFeature(name, value); |
| 95 | + } else { |
| 96 | + throw new Error("Invalid factory class: " + factory.getClass()); |
| 97 | + } |
| 98 | + return; |
| 99 | + } catch (Exception ignore) { |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static void setAttributeQuietly(Object factory, String name, Object value) { |
| 104 | + try { |
| 105 | + if (factory instanceof DocumentBuilderFactory) { |
| 106 | + ((DocumentBuilderFactory) factory).setAttribute(name, value); |
| 107 | + } else if (factory instanceof TransformerFactory) { |
| 108 | + ((TransformerFactory) factory).setAttribute(name, value); |
| 109 | + } else { |
| 110 | + throw new Error("Invalid factory class: " + factory.getClass()); |
| 111 | + } |
| 112 | + } catch (Exception ignore) { |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void setFactoryProperties(Object factory) { |
| 117 | + setFeatureQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 118 | + setFeatureQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", true); |
| 119 | + setFeatureQuietly(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 120 | + setFeatureQuietly(factory, "http://xml.org/sax/features/external-general-entities", false); |
| 121 | + setFeatureQuietly(factory, "http://xml.org/sax/features/external-parameter-entities", false); |
| 122 | + // Values from XMLConstants inlined for JDK 1.6 compatibility |
| 123 | + setAttributeQuietly(factory, "http://javax.xml.XMLConstants/property/accessExternalDTD", ""); |
| 124 | + setAttributeQuietly(factory, "http://javax.xml.XMLConstants/property/accessExternalSchema", ""); |
| 125 | + setAttributeQuietly(factory, "http://javax.xml.XMLConstants/property/accessExternalStylesheet", ""); |
| 126 | + } |
| 127 | + |
| 128 | + private static void setPropertyQuietly(Object factory, String name, Object value) { |
| 129 | + try { |
| 130 | + if (factory instanceof XMLReader) { |
| 131 | + ((XMLReader) factory).setProperty(name, value); |
| 132 | + } else if (factory instanceof XMLInputFactory) { |
| 133 | + ((XMLInputFactory) factory).setProperty(name, value); |
| 134 | + } else { |
| 135 | + throw new Error("Invalid factory class: " + factory.getClass()); |
| 136 | + } |
| 137 | + } catch (Exception ignore) { |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments