Skip to content

Add support for omitting the SameSite attribute from the session cookie #44714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

filiphr
Copy link
Contributor

@filiphr filiphr commented Mar 14, 2025

The main reason for this pull request is to make it easier to disable setting the SameSite cookie in the Spring Session CookieSerializer. The default for the Spring Session is Lax. However, this does not work when using SAML with POST redirect, when the cookie is Lax, then the session cookie will not be sent when the redirect happens and thus the authentication will not work (see https://stackoverflow.com/questions/60068271/samesite-attribute-break-saml-flow/60096206#60096206 and https://www.linkedin.com/pulse/samesite-cookie-infinite-redirections-saml-digvijay-singh/).

The current workaround I have is to expose the following bean

@Bean
public DefaultCookieSerializerCustomizer disableSameSite() {
    return cookieSerializer -> cookieSerializer.setSameSite(null);
}

i.e. use the serializer to disable the cookie.

If this PR is accepted it would make it easier to configure this without the need to expose a bean by just doing

server.servlet.session.cookie.same-site=unset

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 14, 2025
@filiphr filiphr force-pushed the unset-same-site branch 3 times, most recently from eca72bd to 3d808be Compare March 14, 2025 15:22
/**
* Don't set the SameSite cookie attribute.
*/
UNSET("Unset"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may just be me, but UNSET makes me have to stop and think to understand what it does. I wonder if OMITTED, described as "The SameSite cookie attribute is omitted" would be clearer?

Also, could the attribute value be null? I wonder if that would simplify the logic for applying it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if OMITTED, described as "The SameSite cookie attribute is omitted" would be clearer?

You are fully right @wilkinsona. OMITTED is extremely clear. I'll look into it tomorrow.

Also, could the attribute value be null? I wonder if that would simplify the logic for applying it.

I'll check that tomorrow, I had an idea to use null, but not sure why I didn't do it in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember now why I picked UNSET, it's because the enum org.apache.tomcat.util.http.SameSiteCookies has it. In any case OMITTED is more clear so I'll go ahead and use that

filiphr added 3 commits March 20, 2025 11:02
Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
@filiphr
Copy link
Contributor Author

filiphr commented Mar 20, 2025

Thanks for the review @wilkinsona. I've changed the name and tried using null for the attribute value.

@wilkinsona wilkinsona changed the title Add support for unsetting the Session Cookie SameSite Add support for omitting the SameSite attribute from the session cookie Mar 20, 2025
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 20, 2025
@wilkinsona wilkinsona added this to the 3.5.x milestone Mar 20, 2025
@wilkinsona wilkinsona self-requested a review March 20, 2025 10:57
@wilkinsona wilkinsona self-assigned this Mar 20, 2025
@wilkinsona wilkinsona modified the milestones: 3.5.x, 3.5.0-M3 Mar 20, 2025
wilkinsona pushed a commit that referenced this pull request Mar 20, 2025
See gh-44714

Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
@wilkinsona
Copy link
Member

Thanks very much, @filiphr.

@filiphr filiphr deleted the unset-same-site branch March 20, 2025 13:17
@filiphr
Copy link
Contributor Author

filiphr commented Mar 20, 2025

Thanks for the quick integration @wilkinsona

@vpavic
Copy link
Contributor

vpavic commented Apr 17, 2025

@filiphr are you sure it wasn't possible to solve the issue you're having using this combination of properties:

server.servlet.session.cookie.same-site=none
server.servlet.session.cookie.secure=true

I distinctly remember one of the teams in organization I'm working with having what looks like the same issue, and we were able to solve it this way.

It's been a while since I looked at SameSite at depth, but resources like this one still mention Lax being the default if SameSite attribute is omitted. Any behavior different from that might be browser specific and only temporary - from that standpoint I'm not sure if adding omitted as an option is a wise thing.

Also see this StackOverflow answer: https://stackoverflow.com/a/72652458/3944191

@filiphr
Copy link
Contributor Author

filiphr commented Apr 17, 2025

I didn't try that combination @vpavic. I did try with:

server.servlet.session.cookie.same-site=none

However, that was not working. I was also reading and it was saying that if SameSite is omitted then it should be as Lax. However, when I was testing this in Chrome it was not working the same.

Also see this StackOverflow answer: https://stackoverflow.com/a/72652458/3944191

That's the StackOverflow answer I also referenced in my initial issue. That's where I found

@Bean
public DefaultCookieSerializerCustomizer disableSameSite() {
    return cookieSerializer -> cookieSerializer.setSameSite(null);
}

I was also comparing the Tomcat JSESSIONID cookie with the Spring Session SESSIONID cookie and the only difference was the Lax being there for the Spring Session cookie. When I was not using Spring Session, i.e. when I was using the default Tomcat implementation, then it worked properly. That's how I came to the conclusion that I need to omit the SameSite

@vpavic
Copy link
Contributor

vpavic commented Apr 17, 2025

From what I can see your issue description links to different SO question, but that's not so important as both SO questions we linked mention the same thing as the article about SameSite I also linked to in previous comment:

Cookies with SameSite=None must also specify Secure, meaning they require a secure context.

I personally wouldn't rely on unspecified behavior that might change in future without notice.

@filiphr
Copy link
Contributor Author

filiphr commented Apr 17, 2025

From what I can see your issue description links to different SO question, but that's not so important as both SO questions we linked mention the same thing as the article about SameSite I also linked to in previous comment:

You are right, I saw it incorrectly.

Cookies with SameSite=None must also specify Secure, meaning they require a secure context.

I personally wouldn't rely on unspecified behavior that might change in future without notice.

I am not sure that I understand. This is not about using SameSite=None. This PR was created to make sure that we can make sure that no SameSite attribute is specified on the cookie.

Reading the article you shared (https://web.dev/articles/samesite-cookies-explained) again, it indeed looks like the combination of

server.servlet.session.cookie.same-site=none
server.servlet.session.cookie.secure=true

should be enough. I'll need to test this for our scenario to validate this fully.

@vpavic
Copy link
Contributor

vpavic commented Apr 17, 2025

This PR was created to make sure that we can make sure that no SameSite attribute is specified on the cookie.

If you read the article I shared, it says that Cookies without a SameSite attribute are treated as SameSite=Lax. From your description, sounds like you're relying on browser behavior that's different from that and is what I refer to as unspecified behavior that might change in the future. There's also a sentence about that at the end of SO answer I linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants