Prevent CSRF, XSS and XSF attacks

Best practices to audit and configure web app again CSRF, XSS and XSF attacks

Introduction

TL;DR : Use the security built-in your framework, and do not use custom injection of code. Enable the different securities integrated in your framework, such as CSRF token.
Deny all iframe, or scope it to trusted domains if needed

CSRF attacks

Definition


SRF (Cross-Site Request Forgery) attacks involve tricking authenticated users into unknowingly performing actions on a web application. The attacker exploits the trust between the victim's browser and the application to execute unauthorized actions.

The classic example is a victim receiving an email containing an image tag that secretly initiates a fund transfer request to an attacker's website. When the victim's browser loads the image, it sends a request to the attacker's site with the victim's session cookie. As a result, funds are transferred from the victim's account to the attacker's account without the victim's knowledge or consent. This demonstrates how CSRF attacks exploit trust to perform unauthorized actions on web applications.

Security risks


The security risks related to CSRF attacks can be significant:

  1. Unauthorized actions: CSRF attacks allow attackers to perform actions on behalf of the victim, potentially leading to unauthorized changes, data breaches, or financial loss.

  2. Bypassing authentication: As CSRF attacks use the victim's authenticated session, they can bypass any authentication checks implemented by the web application, making it difficult to detect and prevent such attacks.

  3. Trust exploitation: The attack leverages the trust relationship between the victim's browser and the web application, taking advantage of the fact that the web application treats the victim's requests as legitimate.

  4. Social engineering: CSRF attacks often rely on social engineering techniques to deceive users into visiting malicious webpages or clicking on malicious links, making them more susceptible to exploitation.

How to prevent it


There is 2 solutions that can be used altogether to prevent CSRF attacks :

Anti-CSRF token are implemented in most of frameworks.
But you often need to enable them and enable the CSRF check.

Do not use None attribute as it enables the use of the cookie in cross site request, no matter the origin.


XSS attacks

Definition


XSS (Cross-Site Scripting) attacks are security vulnerabilities in web applications where an attacker injects malicious scripts into trusted websites, allowing them to execute arbitrary code in the victim's browser. This can lead to unauthorized access, data theft, cookie hijacking, and website defacement.

*Classic example : An attacker submits a comment on a forum with a malicious script embedded in it with

<script>
  //get user's cookie
</script>

When other users view the comment, the script gets executed in their browsers, giving the attacker access to their sensitive information or allowing them to perform actions on their behalf.*

Security risks


Security risks associated with XSS attacks include:

  1. Data theft: Attackers can steal sensitive information, such as login credentials, personal data, or financial details, from users who unknowingly execute the malicious scripts.

  2. Cookie hijacking: By injecting scripts, attackers can access or manipulate user cookies, leading to session hijacking or impersonation.

  3. Session riding: Attackers can exploit XSS vulnerabilities to piggyback on authenticated sessions and perform actions on behalf of users, leading to unauthorized access and privilege escalation.

  4. Malware distribution: Attackers can use XSS to deliver malware or malicious payloads to unsuspecting users, leading to system compromise or further attacks.

  5. Phishing attacks: XSS vulnerabilities can be leveraged to create convincing phishing pages or pop-ups, tricking users into disclosing sensitive information or installing malicious software.

How to prevent it


There is 3 solutions that can be used altogether to prevent XSS attacks :

All user’s input, event file’s name must be sanitized.

Always use your framework var injection with {} which encode for you the variable content.
Obviously, never inject your variables in dangerouslySetInnerHTML unless you want to inject js, that cannot be controlled by the user. But it is really not recommended.

Content-Security-Policy: script-src 'self' *.example.com;