cbnz
cbnz

Reputation: 674

How can I make Tomcat accept unescaped brackets in URLs?

I've never used Tomcat before, but I've recently inherited a JSP project and now I need to get it running. I've managed to install Tomcat 8.0 locally in Eclipse and everything works fine. I've also installed Tomcat 8.0 on an Ubuntu VPS. The app is running fine, except for a small issue with how it handles URLs.

The client-side application produces URLs with unescaped square and curly brackets in parameters, like this:

GET /saveItems.json?items=[{%22json%22:%22here%22}]

As much as I would like to change the client-side app, I can't. I just need to get this backend running.

My local copy of the application handles this fine. On the server, however, I get this error:

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:286)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1504)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1460)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

I've been looking for a setting that might affect this, without any luck. What am I missing here?

Upvotes: 13

Views: 16665

Answers (4)

Leroy
Leroy

Reputation: 620

I was experiencing a similar problem, but using Spring boot too, the following solution resolved my issue:

@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            connector.setProperty("relaxedQueryChars", "|{}[]");
        }
    });
return factory;
}

Credited to Matthias-lohr - https://stackoverflow.com/questions/46251131/invalid-character-found-in-the-request-target-in-spring-boot

Upvotes: 4

Latif
Latif

Reputation: 191

I make this workable by editing $CATALINA_HOME\conf\server.xml

Old Value: <Connector ... protocol="HTTP/1.1"... />

New Value: <Connector ... protocol="HTTP/1.1"... relaxedQueryChars='[]|{}^&#x5c;&#x60;&quot;&lt;&gt;' />

Upvotes: 18

pravin patil
pravin patil

Reputation: 21

I get this error when I try to GET URL with some special character.

java.lang.IllegalArgumentException: Invalid character found in the request target.

By changing the connector parameter in server.xml, we are able to parse a special character in Tomcat version 9.

Connector port="80" protocol="HTTP/1.1" relaxedQueryChars="\ { } |" 

Upvotes: 2

Beck Yang
Beck Yang

Reputation: 3024

Tomcat 8.0.39/8.5.9/7.0.73 add additional checks for valid characters to the HTTP request line parsing. According to this requirement, version 8.0.42/8.5.12/7.0.76 make some changes to allow some invalid characters.
{, } and | are allowed if the character present in value of system property tomcat.util.http.parser.HttpParser.requestTargetAllow.

You can add system property tomcat.util.http.parser.HttpParser.requestTargetAllow={} to prevent this error. One of the soltion is editing $CATALINA_HOME\conf\catalina.properties.

Upvotes: 8

Related Questions