CWE 352 - Cross-Sire Request Forgery
About CWE ID 352
Cross-Site Request Forgery (CSRF)
CWE ID 352 refers to "Cross-Site Request Forgery (CSRF)" vulnerability which could allow an attacker to execute unwanted actions on a web application on behalf of an authenticated user. The impact of this vulnerability could range from unauthorized actions to disclosure of sensitive information.
Impact
Allows an attacker to bypass authentication and access sensitive data.
Can be used to execute arbitrary code and take control of a system.
Can be used to modify, delete, or add data to a system, potentially causing data loss or corruption.
Can be used to launch attacks against other systems or networks from the compromised system.
Can result in reputational damage, financial losses, and legal repercussions for the affected organization.
Example with Code explanation
JAVA
Let us consider an example case and understand the CWE 352 with context of Vulnerable code and Mitigated code.
Vulnerable code.
This code is vulnerable to CSRF because there is no mechanism to ensure that the request is coming from a legitimate source. An attacker can create a forged request, tricking the user into performing unwanted actions without their knowledge. This can be done by crafting a specially designed link or by submitting a form on a malicious website.
This can be mitigated by
To mitigate this vulnerability, the server should include a unique token with each form or link, and validate that the token is correct when the request is submitted. This technique is called CSRF token or anti-CSRF token. The token should be unpredictable, unique, and tied to the user session to prevent replay attacks.
Mitigated code
This code is mitigated against the following vulnerabilities:
The server generates a unique CSRF token and includes it as a hidden field in the form that submits the transfer request.
When the request is processed, the server checks that the token included in the request matches the one that was generated and stored in the user's session.
If the tokens don't match, the request is rejected. This prevents an attacker from crafting a request that includes a valid transfer request but with an invalid or missing CSRF token.
Python
Vulnerable code.
This code is vulnerable to CSRF attack because it does not include any CSRF protection mechanism, such as the use of CSRF tokens or same-site cookies.
An attacker could craft a malicious website that sends a POST request to the "/transfer" endpoint, causing the user's browser to automatically submit the request with the user's existing session cookies, thereby making the request appear legitimate to the server.
The attacker could then transfer money from the user's account without their knowledge or consent.
This can be mitigated by
Store the token in the user session
Server need to verify the token respective of the user’s session
Drop the request if the token is missing or invalid
Mitigated code
This code is mitigated against CWE-352
In the mitigated code, a CSRF token is generated and stored in the user session. When the transfer request is made, the server verifies the token to ensure that the request is legitimate and not a CSRF attack. If the token is missing or invalid, an error response is sent.
.NET
Vulnerable code.
This code is vulnerable to CSRF attacks as it does not include any mechanism to prevent unauthorized requests.
Mitigated code
The ValidateAntiForgeryToken
attribute ensures that the token in the hidden input field matches the token stored in the server-side session state. If the tokens do not match, the action method will not be executed. This mechanism helps prevent CSRF attacks by ensuring that only authorized requests are processed.
Nodejs
Vulnerable code.
The code appears to be vulnerable to CSRF because it does not include any mechanism to verify the origin of the request.
An attacker could create a malicious form on their own website that would submit a transfer request to the victim's account on the bank's website, resulting in unauthorized transfer of funds.
Mitigated code
This modified code, a CSRF token is generated when the user first visits the site and is stored in the session.
The token is included in the transfer request, and the server verifies that the token in the request matches the one stored in the session before allowing the transfer to proceed.
This prevents attackers from submitting fake transfer requests from another website.
Mitigation
Use anti-CSRF tokens: Include anti-CSRF tokens in all sensitive forms and requests. The token should be unpredictable and unique for each request. Upon receiving a form submission or request, verify that the token is present and valid before executing the action.
Implement SameSite cookies: Set the SameSite attribute on all cookies to either "Strict" or "Lax" to prevent cross-site requests. This attribute ensures that cookies are only sent with requests to the same site that originated them.
Limit HTTP methods: Limit the use of HTTP methods that modify state, such as POST and PUT, to only those requests that require it. Use GET for all read-only operations.
Implement re-authentication: For sensitive actions, require the user to re-authenticate with a stronger factor, such as a password or biometric identifier.
Use referer headers: Check the Referer header on incoming requests to ensure that they originate from a page on your own site. Be aware that this header is not always reliable and may be stripped by some browsers.
References
Cross Site Request Forgery (CSRF) | OWASP Foundation
Cross-Site Request Forgery Prevention - OWASP Cheat Sheet Series
Last updated
Was this helpful?