CSRF Protection
Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user’s web browser to perform an unwanted action on a trusted site when the user is authenticated.
Source: OWASP
Defense Principle
FoalTS combines two defenses to protect your application against a CSRF attack. It uses the SameSite
cookie directive and a token-based technique to have in-depth protection.
When enabled, authentication cookies have their SameSite
attribute set to lax
in order to prevent third-party websites from sending authenticated requests to your server. When they make a POST, PUT, PATCH or DELETE request to your application, the authentication cookie is not sent. As of November 2022, this protection is supported by 96% of modern browsers.
In addition, the framework provides token-based mitigation that works with either state (session tokens) or stateless (JWT). The client can read the CSRF token either from the HTML page (using a template) or from the XSRF-Token
cookie. Then, the token must be included in the X-XSRF-Token
header, the X-CSRF-Token
header or in the body with the _csrf
field in any POST, PUT, PATCH or DELETE request sent to the server (see examples).
Authentication with Session Tokens
- YAML
- JSON
- JS
settings:
session:
csrf:
enabled: true
{
"settings": {
"session": {
"csrf": {
"enabled": true
}
}
}
}
module.exports = {
settings: {
session: {
csrf: {
enabled: true
}
}
}
}
Authentication with JSON Web Tokens
- YAML
- JSON
- JS
settings:
jwt:
csrf:
enabled: true
{
"settings": {
"jwt": {
"csrf": {
"enabled": true
}
}
}
}
module.exports = {
settings: {
jwt: {
csrf: {
enabled: true
}
}
}
}