Encoding Issue "The valid characters are defined in RFC 7230 and RFC 3986" in Tomcat 9

Viewed 967

I'm working in a very bing and old software that works with servlets and includes accents and other strange characters in the URL. The software was upgraded from JDK7 to JDK11 a few weeks ago and the server was upgraded from Tomcat6 to Tomcat9.

I've tried:

- Using URLEncoder in the java part and encodeURI in the javascript part and it works fine, but there are hundreds of places that need this change and it's complicated because it's easy to make a mistake.
- Use the encodeURI in the server.xml of Tomcat but it doesn't change anything. And I've added the relaxedQueryChars tag, but it doesn't accept accents.
- The last thing that I've tried is to add a filter, to try to encode the params of the request, in the web.xml but it doesn't work when the error appears and there's no option to change the order. And another problem is that the ServletRequest has no setParameter so if I can solve the order filter problem I have this one...

I'm getting this error:

Estado HTTP 400 – Bad Request 
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 

The code worked perfectly in Tomcat6 but with the upgrade we get this problem. I'm trying to get a server config solution to avoid using code encoding but I don't know what more can I try...

Thank you!

1 Answers

Finally we decided to install Tomcat8 instead of 9 because the codification change was introduced in Tomcat8.5.

It's a workaround and it doesn't solve the real codification problem. But, actually, it's risky to try to use the encode function in every part of the code because it may generate other kind of problems.

Related