org.wildfly.common.xml.SAXParserFactoryUtil Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
The newest version!
/*
* Copyright 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.common.xml;
import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
import static org.wildfly.common.xml.FactoryConstants.APACHE_DISALLOW_DOCTYPE_DECL;
import static org.wildfly.common.xml.FactoryConstants.XML_EXTERNAL_GENERAL_ENTITIES;
import static org.wildfly.common.xml.FactoryConstants.XML_EXTERNAL_PARAMETER_ENTITIES;
import static org.wildfly.common.xml.Log.XML_FACTORY_LOGGER;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.wildfly.common.annotation.NotNull;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Factory provides {@link SAXParserFactory} with secure defaults set. Properties not supported generate a warning, but the
* factory process creation will continue and return a result.
* Settings based on recommendations of
* Sonarcloud RSPEC-2755 and
* OWASP XML
* External Entity Prevention Cheatsheet.
*
*
* - {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} is set to true.
* - {@link org.wildfly.common.xml.FactoryConstants#APACHE_DISALLOW_DOCTYPE_DECL} is set to true.
* - {@link org.wildfly.common.xml.FactoryConstants#XML_EXTERNAL_GENERAL_ENTITIES} is set to false.
* - {@link org.wildfly.common.xml.FactoryConstants#XML_EXTERNAL_PARAMETER_ENTITIES} is set to false.
*
*
* @author Boris Unckel
* @since 1.6.0.Final
*/
public final class SAXParserFactoryUtil {
/*
* Prevent recurring log messages (per classloader).
*/
private static final AtomicBoolean TO_BE_LOGGED = new AtomicBoolean(true);
@NotNull
public static SAXParserFactory create() {
final SAXParserFactory instance = SAXParserFactory.newInstance();
final boolean toBeLogged = TO_BE_LOGGED.compareAndSet(true, false);
try {
instance.setFeature(FEATURE_SECURE_PROCESSING, true);
} catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
if (toBeLogged) {
XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, FEATURE_SECURE_PROCESSING,
instance.getClass().getCanonicalName());
}
}
try {
instance.setFeature(APACHE_DISALLOW_DOCTYPE_DECL, true);
} catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
if (toBeLogged) {
XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, APACHE_DISALLOW_DOCTYPE_DECL,
instance.getClass().getCanonicalName());
}
}
try {
instance.setFeature(XML_EXTERNAL_GENERAL_ENTITIES, false);
} catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
if (toBeLogged) {
XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, XML_EXTERNAL_GENERAL_ENTITIES,
instance.getClass().getCanonicalName());
}
}
try {
instance.setFeature(XML_EXTERNAL_PARAMETER_ENTITIES, false);
} catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
if (toBeLogged) {
XML_FACTORY_LOGGER.xmlFactoryPropertyNotSupported(e, XML_EXTERNAL_PARAMETER_ENTITIES,
instance.getClass().getCanonicalName());
}
}
return instance;
}
/**
* No instance.
*/
private SAXParserFactoryUtil() {
throw new IllegalStateException("No instance");
}
}