Starting with Chrome 51, a new SameSite
attribute has been added to the browser cookie to prevent CSRF attacks and user tracking.
1. What is a CSRF attack?
Cookies are often used to store a user’s identity information, and a malicious website can manage to forge an HTTP request with the correct cookie, which is a CSRF attack.
For example, a user logs into the bank’s website your-bank.com
and a cookie is sent from the bank’s server.
|
|
The user then visits the malicious website malicious.com
with a form on it.
Once a user is tricked into sending this form, the bank’s website will receive the request with the correct cookie. To prevent this attack, the form is usually accompanied by a random token that tells the server that this is a genuine request.
This type of cookie, known as a third-party cookie, is used in addition to CSRF attacks and can be used for user tracking.
For example, Facebook inserts an invisible image into a third-party website.
|
|
When the browser loads the above code, it sends a request to Facebook with a cookie so that Facebook will know who you are and what websites you visit.
2. SameSite Property
The SameSite
property of a cookie is used to restrict third-party cookies and thus reduce security risks.
It can be set to three values.
- Strict
- Lax
- None
2.1 Strict
Strict is the most restrictive, forbidding third-party cookies altogether and not sending cookies under any circumstances when crossing sites; in other words, only if the URL of the current page is the same as the target of the request will it carry a cookie.
|
|
This rule is too strict and can cause a very bad user experience. For example, if there is a GitHub link on the current page, the user will not submit a GitHub cookie when they click on the link to enter the Github site, resulting in a non-logged-in state when entering Github from the current page.
2.2 Lax
The Lax
rule is slightly relaxed, and in most cases no third-party cookies are sent, except for Get requests that navigate to the target URL.
|
|
GET requests that navigate to the target URL include only three cases: links, preload requests, and GET forms. See the table below for details.
Type | Example | Normal situation | Lax |
---|---|---|---|
Link | <a href="..."></a> |
Send Cookie | Send Cookie |
preload | <link rel="prerender" href="..."/> |
Send Cookie | Send Cookie |
GET Form | <form method="GET" action="..."> |
Send Cookie | Send Cookie |
POST Form | <form method="POST" action="..."> |
Send Cookie | Not sent |
iframe | <iframe src="..."></iframe> |
Send Cookie | Not sent |
AJAX | $.get("...") |
Send Cookie | Not sent |
Image | <img src="..."> |
Send Cookie | Not sent |
After setting Strict
or Lax
, CSRF attacks are basically eliminated. Of course, this assumes that the user’s browser supports the SameSite property.
2.3 None
Chrome plans to make Lax
the default setting. In this case, sites can choose to explicitly turn off the SameSite
property by setting it to None
. However, this is only possible if the Secure
property is also set (cookies can only be sent over the HTTPS protocol), otherwise it will not work.
The following setting is not valid.
|
|
The following settings are valid.
|
|
3. Spring Application
We generally set cookies through the javax.servlet.http.Cookie
object provided by the Servlet, but it does not currently implement the SameSite
property.
|
|
ResponseCookie
As we all know, a cookie is actually an HttpHeader only.
Spring provides a tool class ResponseCookie
that can be used to write a cookie to the client with the SameSite property. it is also very simple to use.
|
|
Request this Controller and you will get the following response.
|
|
As you can see, a cookie with a SameSite value of Lax was successfully set.
SameSite property of the HttpSession cookie
HttpSession relies on a cookie with the name JSESSIONID
(default name).
For setting the JSESSIONID
cookie, you can modify the following configuration to set the SameSite
property.
|
|
Note that if your SpringBoot version is less than 2.6, then you cannot use this configuration.
SpringBoot version below 2.6
If you use Tomcat as the server, then you can set the SameSite property of the session cookie by the following configuration.
|
|
If you are using Spring-Session then you can use the following configuration to set the SameSite property of the cookie.
|
|
Reference https://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html