CSRF Prevention in Modern Web Applications
Modern web applications require layered defenses to prevent CSRF attacks, as SameSite cookies alone are not sufficient. This article outlines best practices and technical implementations.

To prevent CSRF in a modern web application, developers must ensure that state-changing requests are never made via GET, set session cookies with SameSite=Lax or Strict, Secure, and HttpOnly attributes, and use the Sec-Fetch-Site header to reject cross-site requests. These measures form the first line of defense. As noted in the Dev.to article, SameSite=Lax cookies prevent most cross-site form posts but do not cover same-site subdomains or cookies without the SameSite attribute. This is why additional layers are necessary (source ID: 1). [1]
A critical implementation detail is the use of the __Host- prefix in cookie names. This prefix ensures that cookies cannot be overwritten by sibling subdomains, adding an extra layer of security. The Dev.to article provides an example of Express 5 configuration that enforces this practice, along with other settings like Secure and HttpOnly (source ID: 1). [1]
Modern browsers send the Sec-Fetch-Site header, which indicates the origin relationship of a request. This header can be used as a reliable signal to determine if a request originated from the same site or not. The Dev.to article describes middleware that checks this header and rejects cross-site requests, while allowing same-origin requests (source ID: 1). [1]
In addition to cookies and headers, synchronizer or double-submit tokens are recommended for forms and older clients. These tokens ensure that only requests originating from the application can change state. The Dev.to article highlights the importance of keeping these tokens in place, especially for server-rendered forms and legacy systems (source ID: 1). [1]
Advertisement
To prevent CSRF in a modern web application, developers must ensure that state-changing requests are never made via GET, set session cookies with SameSite=Lax or Strict, Secure, and HttpOnly attributes, and use the Sec-Fetch-Site header to reject cross-site requests (1). A critical implementation detail is the use of the __Host- prefix in cookie names, which prevents sibling subdomains from overwriting or planting session cookies (1). Modern browsers send the Sec-Fetch-Site header, which indicates the origin relationship of a request (1). [1]
In addition to cookies and headers, synchronizer or double-submit tokens are recommended for forms and older clients (1). The Sec-Fetch-Site header provides a reliable signal that can be used to determine whether a request originated from the same site or a different origin (1). However, this header is not always present, especially on very old browsers and non-browser clients, which is why an origin allowlist should be used as a fallback (1). [1]
Layered defenses are essential to close all potential gaps. For example, SameSite=Lax cookies are effective in blocking most cross-site form posts but do not cover same-site subdomains, state-changing GET routes, or cookies without the attribute (1). The use of the __Host- prefix in cookie names, along with secure and HttpOnly attributes, adds an additional layer of protection (1). [1]
To prevent CSRF in a modern web application, developers must ensure that state-changing requests are never made via GET, set session cookies with SameSite=Lax or Strict, Secure, and HttpOnly attributes, and use the Sec-Fetch-Site header to reject cross-site requests (1). This approach ensures that cookies are not inadvertently sent with requests initiated from other sites, which is a core mechanism of CSRF attacks. The use of the __Host- prefix in cookie names is also critical, as it prevents sibling subdomains from overwriting or planting session cookies (1). [1]
Advertisement
Modern browsers send the Sec-Fetch-Site header, which indicates the origin relationship of a request (1). This header is a reliable signal that can be used to determine whether a request originated from the same site or a different origin. Developers can use this header in middleware to reject cross-site requests that attempt to change state, such as form submissions or API calls (1). This method is particularly effective because the header cannot be forged by web pages, making it a strong defense against CSRF attacks. [1]
In addition to cookies and headers, synchronizer or double-submit tokens are recommended for forms and older clients (1). These tokens provide an additional layer of defense, ensuring that only requests originating from the application itself can change state. While modern defenses like SameSite and Fetch Metadata are effective, they do not cover all scenarios, particularly with older clients or non-browser applications. Therefore, implementing multiple layers of defense is essential to close all potential gaps (1). [1]
To prevent CSRF in a modern web application, developers must ensure that state-changing requests are never made via GET (1). This is a critical step in preventing attacks that exploit the browser's automatic inclusion of cookies in cross-site requests. By restricting state-changing actions to POST, PUT, PATCH, or DELETE methods, the risk of CSRF is significantly reduced. This approach is supported by OWASP guidance and is a foundational step in securing web applications against CSRF vulnerabilities. [1]
Setting session cookies with SameSite=Lax or Strict, Secure, and HttpOnly attributes is another essential measure in CSRF prevention (1). These attributes ensure that cookies are not sent in cross-site requests, thereby preventing an attacker from hijacking a user's session. The __Host- prefix in cookie names further enhances security by ensuring that cookies cannot be overwritten by subdomains, adding an additional layer of defense against CSRF attacks. [1]
Advertisement
Modern browsers send the Sec-Fetch-Site header, which indicates the origin relationship of a request (1). This header can be used to reject cross-site requests that attempt to change the state of a web application. By using middleware that checks the Sec-Fetch-Site header, developers can ensure that only requests originating from the same site or explicitly allowed origins are processed. This layer of defense is particularly effective in modern browsers that support this header, although it may not be available on older clients. [1]
Sources
Prepared with AI assistance from the cited sources.
The Cali Reporter newsletter
The stories worth your time.
California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.
More in Technology
- Modern Web Guidance 1.0 and Browser Compatibility
Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…
- Apache mod_http2 Vulnerability Detection with Nmap
A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.
- Securing Remote Vendor Access in Industrial Networks
Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.
