Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set SameSite attribute?

I have a problem with setting SameSite attribute in Cookie. I wanted to set this attribute, but neither javax.servlet.http.Cookie nor java.net.HttpCookie provide method to deal with it. Therefore, I have an idea to create a response javax.servlet.Filter that catch "Set-Cookie" header and add "SameSite=Strict" attribute.

response.setHeader("Set-Cookie", response.getHeader("Set-Cookie") + "; SameSite=strict");

It works ok but problem appeares when I have more than one "Set-Cookie" header in one response. javax.servlet.http.HttpServletResponse does not provide method to remove or overwrite more than one heder with the same name (iterating over them and using setHeader() doesn't work because it always sets the last one). Do you have any idea how to set SameSite attribute to cookie or how to overwrite headers in response filter?

Thanks in advance.

like image 691
mwyrzyk Avatar asked Jun 03 '18 17:06

mwyrzyk


People also ask

How do I set the same site attribute?

Filter that catch "Set-Cookie" header and add "SameSite=Strict" attribute. response. setHeader("Set-Cookie", response. getHeader("Set-Cookie") + "; SameSite=strict");

What is the SameSite attribute?

SameSite cookie attribute is used by browsers to identify how first- and Third-Party Cookies should be handled. Browsers can either allow or block such cookies depending on attribute and scenario.

How do I set the SameSite attribute in Web XML?

If you wanna add the SameSite option to the cookies in your application, you can configure the Tomcat Cookie Processor (the CookieProcessor ) in the META-INF/context. xml . NOTE: This configuration may fail in older versions of Tomcat. Apparently, these options work well if you use, at least, Tomcat 8.5.

How do I set the SameSite cookie attribute to none?

A New Model for Cookie Security and Transparency Developers must use a new cookie setting, SameSite=None , to designate cookies for cross-site access. When the SameSite=None attribute is present, an additional Secure attribute must be used so cross-site cookies can only be accessed over HTTPS connections.


2 Answers

It turns out that using setHeader() method remove all previous headers with the same name so I just create simple for loop in doFilter() method. It adds SameSite=Strict attribute to every cookie that is set.

boolean firstHeader = true;
for (String header : cookiesHeaders) {
    if (firstHeader) {
        httpResponse.setHeader("Set-Cookie",
                String.format("%s; %s", header, "SameSite=Strict"));
        firstHeader = false;
        continue;
    }
    httpResponse.addHeader("Set-Cookie",
            String.format("%s; %s", header, "SameSite=Strict"));
}
like image 193
mwyrzyk Avatar answered Sep 25 '22 17:09

mwyrzyk


In etc/apache2/httpd.conf

Header edit Set-Cookie ^(.*)$ $1;SameSite=Strict


works for me.....

like image 25
user2677034 Avatar answered Sep 23 '22 17:09

user2677034