Raw
Raw

Reputation: 675

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

java.lang.IllegalArgumentException: 
Invalid character found in the request target. 
The valid characters are defined in RFC 7230 and RFC 3986

This exception is caused by passing Chinese words in Get URLs. How can I resolve this issue in tomcat ?

We don't want to reduce the Tomcat version and ask our customer to encode the parameters.

Is there a way to fix this issue by changing configurations in tomcat ?

Upvotes: 21

Views: 96951

Answers (10)

Harshal Ringe
Harshal Ringe

Reputation: 61

For Spring Boot 3.2.0, adding the below properties in the application.properties file solved my issue

server.tomcat.relaxed-path-chars=<,>,[,\,],^,`,{,|,},:
server.tomcat.relaxed-query-chars=<,>,[,\,],^,`,{,|,},:

Upvotes: 1

Husen Mansurov
Husen Mansurov

Reputation: 11

Try to change your server.xml file with this code:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"  relaxedQueryChars="[,]()^`"/>

Upvotes: 1

Juan B. Rodriguez
Juan B. Rodriguez

Reputation: 24

Editing your catalina.properties and defining this:

server.tomcat.relaxed-path-chars=|,{,},[,]

Upvotes: 1

Warley Ferreira Dias
Warley Ferreira Dias

Reputation: 19

I faced the same problem, but using GET to pass the data, and the data contained the "|" character. When using POST, TOMCAT did not present the same error, but I can not give you the explanation. Hope this be useful to someone.

Upvotes: 0

Sandeep Jain
Sandeep Jain

Reputation: 1262

Since spring boot 2.2.0 it is possible to configure Tomcat so, that special characters are allowed in URLs.

Just sum the ones you wanna allow in the application.properties

In your case that is

server.tomcat.relaxed-query-chars=|
all configurable chars are `, {, }, [, ], ^, <, > and |

In application.yml that would be

server:
  tomcat:
    relaxed-query-chars: ['{','}']

Upvotes: 5

Tanvir Singh
Tanvir Singh

Reputation: 21

If you are trying to send JSON to request target then try encoding it first

In case you are using Postman for testing microservices then go to

https://onlinejsontools.com/url-encode-json

copy the encoded JSON and paste it in the place of value. This solved my problem

Upvotes: 1

Kumar Shanoo
Kumar Shanoo

Reputation: 135

Newer version of tomcat will not allow certain special character. In case you are using Spring Boot you can pass a property to allow your special character by tomcat.

 server:
  tomcat:
    relaxed-query-chars: ['{','}']

Upvotes: 1

Du-Lacoste
Du-Lacoste

Reputation: 12757

I encountered the same error when sending location of a file in a AJAX GET request.

Error:

java.lang.IllegalArgumentException: 
Invalid character found in the request target. 
The valid characters are defined in RFC 7230 and RFC 3986

Since the location had characters which are not recognized. I.e. "C:///" etc, the error was thrown.

The use of encodeURIComponent helped me fix the issue since it encodes the component.

When you pass the Chinese characters make sure you add those inside "encodeURIComponent" method. In my case:

 $.ajax({
        type: "GET",
        url: 'removeFile?removeFileName=' + encodeURIComponent("C:///YO/Ed/PO/")
        data: {},
        dataType: 'json',

Upvotes: 1

Mark Thomas
Mark Thomas

Reputation: 16615

There is no way to fix this in Tomcat. The requests are not specification compliant so Tomcat will not permit them. There is a long history of security issues around different components in a system reacting differently to such URLs. Usually in the form of header and/or request injection. As a result, Tomcat's URL parsing has been tightened up and it is extremely unlikely it will be relaxed.

httpd is heading in the same direction for the same reasons.

The best long term option is to point out to the clients that the requests they are sending are not specification compliant and that they need to fix them (by using appropriate %nn encoding). On the Tomcat side, make sure Tomcat is using UTF-8. That is the default on newer versions. You might need to explicitly set it on older releases.

Upvotes: 13

Zachary Craig
Zachary Craig

Reputation: 2220

It looks like Tomcat, out of the box, will not allow these characters, as you've seen, taking a glance at this question seems to imply you can change this behavior editing your catalina.properties and defining the requestTargetAllow parameter, like the following:

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}

Unfortunately, it looks like this is just a long list of characters you want to allow, so if you want all chinese characters to be there, you're going to have a pretty long list, per Wikpedia:

In China, which uses simplified Chinese characters, the Xiàndài Hànyǔ Chángyòng Zìbiǎo (现代汉语常用字表, Chart of Common Characters of Modern Chinese) lists 2,500 common characters and 1,000 less-than-common characters, while the Xiàndài Hànyǔ Tōngyòng Zìbiǎo (现代汉语通用字表, Chart of Generally Utilized Characters of Modern Chinese) lists 7,000 characters, including the 3,500 characters already listed above.

Upvotes: 8

Related Questions