Best way to stop cross site request forging (CSRF)

Best way to stop cross site request forging (CSRF)

A form of web security flaw called cross-site request forgery (CSRF) enables an attacker to carry out illegal operations on behalf of a user. While a user is currently logged in to a susceptible website, this attack takes place when a malicious website or script sends a request to that website. The attack makes use of the user’s session and authentication to carry out evil deeds secretly. Effective security controls must be put in place to stop CSRF attacks. This blog post will examine the most effective methods for preventing cross-site request forgery (CSRF).

Use Anti-CSRF Tokens

One of the most effective ways to stop CSRF attacks is to use anti-CSRF tokens. The tokens are a random value that is generated on the server-side and is included in the response of the initial request. The client then includes the token in the subsequent requests. The server checks the token to verify that the request is legitimate.

The token should be unique for each user session and not easily guessable. The token can be included as a cookie or in the response body, and the client can include it as a query parameter or a header. This approach ensures that the attacker cannot guess the token, making it difficult to execute a CSRF attack.

Implement Same-Site Cookies

Same-site cookies are sent solely to the origin from which they were originally sent. This indicates that a website’s cookies are exclusively accessible by that website and cannot be accessed by other websites. Same-site cookies can stop CSRF attacks by preventing the use of the cookies that a website sets by the website of the attacker.

Set the “SameSite” property in the cookie’s header to “Strict” or “Lax” to implement same-site cookies. The “Lax” setting permits the cookie to be sent for cross-site requests if the user clicked a link from the website, whereas the “Strict” value assures that the cookie is only sent for same-site requests.

Enable CORS with Proper Configuration

A website can designate who is permitted to access its resources thanks to the online security standard known as Cross-Origin Resource Sharing (CORS). By enabling only specific websites to access the website’s resources, CORS can be utilized to stop CSRF attacks. To make sure that only reputable websites are allowed to access the resources, CORS should be enabled with the right settings.

The server must send the “Access-Control-Allow-Origin” header with the permitted origins in order to enable CORS. The permitted methods and headers for each request should also be listed in the header. This safeguards against CSRF attacks and ensures that only reputable websites can access the resources.


CSRF attacks are a serious web security flaw that could let attackers carry out unauthorized operations on users’ behalf. CSRF attacks can be avoided by putting in place strong security measures like anti-CSRF tokens, same-site cookies, and adequate CORS settings. Using these precautions is crucial if you want to guarantee the integrity and security of your website and safeguard your users against malicious attacks.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *