26

My code is working on tomcat 8 version 8.0.33 but on 8.5.4 i get : An invalid domain [.mydomain] was specified for this cookie.

I have found that Rfc6265CookieProcessor is introduced in tomcat 8 latest versions.

It says on official doc that this can be reverted to LegacyCookieProcessor in context.xml but i don't know how.

Please let me know how to do this.

Thanks

Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37

6 Answers6

31

You can try in context.xml

<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />

reference: https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html

linzkl
  • 326
  • 3
  • 2
15

Case 1: You are using Standalone Tomcat & have access to change files in tomcat server

Please follow answer by @linzkl

Case 2: You are using Standalone Tomcat but you don't have access to change files in tomcat server

Create a new file called context.xml under src/main/webapp/META-INF folder in your application & paste the content given below

<?xml version="1.0" encoding="UTF-8"?> 
<Context>
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
  <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
  <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> 
  <CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>

When you deploy your application in Standalone Tomcat, the context.xml file you placed under META-INF folder will override the context.xml file given in tomcat/conf/context.xml

Note: If you are following this solution, you have to do it for every single application because META-INF/context.xml is application specific

Case 3: You are using Embedded Tomcat

Create a new bean for WebServerFactoryCustomizer

@Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {

        @Override
        void customize(TomcatServletWebServerFactory tomcatServletWebServerFactory) {
            tomcatServletWebServerFactory.addContextCustomizers(new TomcatContextCustomizer() {
                @Override
                public void customize(Context context) {
                    context.setCookieProcessor(new LegacyCookieProcessor());
                }
            });
        }
    };
}
IamVickyAV
  • 1,495
  • 1
  • 17
  • 15
  • I'm trying the same with Case 2, but this doesn't seems to be working for me. Though the Case 1 is working fine. Does solution provided in Case 2 worked for anynone? Tomcat Version: 8.5.69 Spring webmvc – Lavish Nov 22 '22 at 10:31
11

Enabling the LegacyCookieProcessor which is used in previous versions of Tomcat has solved the problem in my application. As linzkl mentioned this is explained in Apache's website https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html.

The reason is that the new version of Tomcat does not understand the . (dot) in front of the domain name of the Cookie being used.

Also, make sure to check this post when you are using Internet Explorer. Apparently, it's very likely to break.

You can find context.xml in the following path.

tomcat8/conf/context.xml

<?xml version="1.0" encoding="UTF-8”?>
<!-- The contents of this file will be loaded for each web application —>
<Context>
<!-- Default set of monitored resources. If one of these changes, the    -->
<!-- web application will be reloaded.                                   -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor"/>
</Context>
smos
  • 148
  • 1
  • 6
4

The problem is still with Tomcat9. Same process need to follow for Tomcat 9 to set the class.

Add the class in context.xml file.

If you are using eclipse to run the application, need to set in the context.xml file in the server folder. Refer the below screenshot for more reference.

enter image description here

Hope this helps someone.

Atul
  • 3,043
  • 27
  • 39
3

SameSite issue in tomcat version < 8.5.47 has resolved

In Tomcat 8.5.47 and bellow (Tomcat 8 versions), setting CookieProcessor tag to enable same site (as given bellow) in context.xml does not work due to a bug in Tomcat.

<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" sameSiteCookies="none" />

If you find in this situation where it is not a easy thing to upgrade tomcat immediately (which I faced recently), or if you find any other case where you just need custom processing in cookies; You can write your own CookieProcessor class to get around.

Please find a custom CookieProcessor implementation and details of it's deployment steps here.

In my case I wrote a custom CookieProcessor based on LegacyCookieProcessor source code that allows tomcat 8.5.47 to enable SameSite attribute in cookies.

1

As mentioned by @atul, this issue persists in Tomcat 9. It will most likely persist moving forward with all future versions of Tomcat, since this is the new standard.

Using the legacy cookie processor (by adding the line above to the context.xml file) is working well for us. However, the true 'fix' is to adjust how your cookie is formed in the first place. This will need to be done in your application, not in Tomcat.

The new cookie processor does not allow the domain to start with a . (dot). Adjusting your cookie (if possible) to start with a value other than that will fix this problem without reverting to the old, legacy cookie processor.

Also, it should be obvious, but I didn't see it mentioned above: after updating the context.xml file, you need to restart the Tomcat service for the change to take effect.

Cheers!

PatrickV
  • 11
  • 2