XMLInputFactory setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,"") gives Unrecognized property - spring-boot

Environment: Java 8 / SpringBoot v1.3.3.RELEASE
This is an old Java code base. Recently sonar was introduced and my task is to fix Critical/Blocker level security vulnerabilities.
At this code sonar recommends to "Disable access to external entities in XML parsing".
XMLInputFactory factory = XMLInputFactory.newInstance();
As per the sonar recommendation I added following properties to the factory,
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
However, when I run the unit tests it gives my following error,
java.lang.IllegalArgumentException: Unrecognized property 'http://javax.xml.XMLConstants/property/accessExternalDTD'
But it doesn't give me the any such error when I set the same properties for the DocumentBuilderFactory as follows,
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
I did a bit of a research and found This Java bug. But it's fixed and also it's related to TransformerFactory. Couldn't find any such bug related to XmlInputFactory though.
Any workaround or fix is much appreciated

I have applied the solution mentioned here and configured the XMLInputFactory object this way:
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
OWASP XML External Entity Prevention

Related

QFJ upgrade issue from 2.1.1 to 2.2.0 due to XMLConstants.ACCESS_EXTERNAL_DTD

I am trying to upgrade the QFJ version 2.2.0 from 2.1.1 facing below issue.
java.lang.IllegalArgumentException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(Unknown Source)
I have analysed this is because of QFJ DataDictionary.java's load method using below code.
private void load(InputStream inputStream) throws ConfigError {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
...}
Reason of error is not clearly understood by me(please try to explain) but found that Xerces, xercesImpl jar causing this error.
we are using these jar in some our project code that is why it is needed.
Can anyone try to explain why we have placed these lines? and we are placing then it should be under try and catch block. Is there any way to fix it. Without doing source code changes provide by QFJ.
Answer from QFJ team really appreciated.

JDBC DatabaseMetaData method not implemented by JDBC(T4SQLMX) driver

I am setting up a Spring-boot application to connect to HP NonStop Tandem's SQL/MX. First I achieved this connection by hard-coding the jdbc parameters like dataSource, URL, etc in the service section of the application and it worked (I was able to access tables by executing query).
Now I am trying to remove the hard coded part and have my database related info in application.properties file, but now I am getting the following error
org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver; nested exception is java.lang.AbstractMethodError: Method com/tandem/t4jdbc/SQLMXConnection.isValid(I)Z is abstract
Can someone help me understand the root cause? The same driver jar is being used when hard-coding the datasource details and it worked but not working when having the data source properties in application.properties and needs an upgrade to the jar.
I encountered the same exception when using Spring Data JPA in a Spring Boot application, the JTDS driver and the Hikari connection pool. In my case I discovered that the following fixed the problem:
Examining the class com.zaxxer.hikari.pool.PoolBase, the following can be observed:
this.isUseJdbc4Validation = config.getConnectionTestQuery() == null;
Thus JDBC 4 validation will not be attempted if there is a connection test query configured. In a Spring Boot application, this can be accomplished like this:
spring.datasource.hikari.connection-test-query=select 1;
Regretfully I do not have any experience with the T4SQLMX driver but nevertheless hope this can be of some use.
I recently fought through the same issue, for me I was using a JDBC type 3 driver; but my spring implementation only supported a type 4 driver, thus when the method you linked above was attempted to be called, it caused the error.
I suggest you look for a type 4 driver for your particular database and see if that resolves your issue.

How to use DistributedMapCacheClient service as a Property in nifi custom Processor?

I am trying to use DistributedMapCacheClient service as a Property in my nifi custom Processor. Everything was fine but when I am going to configure the the Processor in Nifi, I am getting the following error message.
"No controller service types found that are applicable for this property."
This is how I have configured the Property in my code.
public static final PropertyDescriptor DISTRIBUTED_CACHE_SERVICE = new PropertyDescriptor.Builder()
.name("Distributed Cache Service")
.description("")
.required(false)
.identifiesControllerService(DistributedMapCacheClient.class)
.build();
Please let me know how can I use DistributedMapCacheClient service as a Property in my processor.
I was missing below dependencies in my pom file. After I added those, my custom processor was working fine. I Found the answer in this link.
Added the scope as provided for the dependency, nifi-distributed-cache-client-service-api.
Added the dependency for nifi-standard-services-api-nar (type = nar)

How to obtain JMSServerControl object

I'm using an out of the box Wildfly 9 application server started with the
standalone-full configuration.
I want to create persistent topics at runtime, so the internet told me I need
an instance of JMSServerControl.
But I don't know how to get one.
I read: https://developer.jboss.org/thread/166207
, but that didn't really help me.
The doc says I can find the resource at jms.server
but when I use:
#Resource(mappedName = "jms.server")
private JMSServerControl control;
it isn't found.
I'm new to Wildfly, HornetQ and Dependency Injection so I'm a bit at a loss here.
Try doing an explicit JNDI lookup. Something along the lines of
Context context = new InitialContext();
JMSServerControl control = (JMSServerControl)context.lookup("jms.server");

XSLT ClassCastException in WebSphere when Spring tries to create an AnnotationMethodHandlerAdapter

When starting WebSphere, I get this exception:
Could not instantiate bean class [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]:
Constructor threw exception; nested exception is java.lang.ClassCastException:
com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl incompatible with
javax.xml.transform.TransformerFactory
Caused by: java.lang.ClassCastException: com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl
incompatible with javax.xml.transform.TransformerFactory
at javax.xml.transform.TransformerFactory.newInstance(Unknown Source)
at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.<init>(AbstractXmlHttpMessageConverter.java:47)
at org.springframework.http.converter.xml.SourceHttpMessageConverter.<init>(SourceHttpMessageConverter.java:45)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.<init>(AnnotationMethodHandlerAdapter.java:197)
This doesn't seem have any impact on any beans in my applicationContext.xml but it's still odd. For me, this looks as if IBM classes are leaking into my application.
How can I fix this? I already set the option "Access to internal server classes" to "Restrict".
It was indeed a class-loading issue, however this cannot be solved by changing class-loader settings.
The problem was that the xml-apis and javax.xml jars were being imported over some maven dependencies.
Since we already set the class loader policies for the application to PARENT_LAST, the javax.xml.transform.TransformerFactory was being loaded from the WebApp-Class loader from our jar files.
However its implementation 'com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl' was coming from the server class loader, this one was linked to the javax.xml.transform.TransformerFactory provided by the JDK/JRE.
Since the classes were loaded from different sources a ClassCastException was thrown.
Removing all dependencies to xml-apis / xerces / javax.xml jars solved the problem.
Since these APIs are now part of the JDK they no longer need to be imported.
... and if you wonder why I know so much about this issue: I work together with Aaron. ;)
I can't speak for Restrict as I have no personal experience with it,But I think the problem is more to do with IBM Class Loader. The class you are referring to is part of IBM Java implementation of TransformerFactory, I think you can try one of the following to solve this issue on hand
Either change the server class loader policy to PARENT_LAST (This way class loader will find the class from application's local class path, before going to up the chain all the way to java run time)
The other option would be look at the jaxp.properties file, I think it is located in (was_root\java\jre\lib), I only read about this option never actually used it
Why do you say IBM classes are leaking into your application?
The TransformerFactory is asked to create a newInstance. It follows a sequence of steps to determine which TransformerFactory to use. If none of the config is specified, it simply chooses to use the default factory.
Here is the javadoc for TransformerFactory:
http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()
What is the OS ? Is that AIX?
http://www.ibm.com/developerworks/java/jdk/aix/j664/sdkguide.aix64.html
Looking at this doc (link above) for AIX it tells me that this is the default Impl:
javax.xml.transform.TransformerFactory
Selects the XSLT processor. Possible values are:
com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl
Use the XL TXE-J compiler. This value is the default.
Post back additional information so that we can try and troubleshoot this.
HTH
Manglu

Resources