Cross Site Request Forgery (CSRF) is a type of security attack in which an attacker tricks a victim into executing an unintended request that changes the victim’s state of data, without the victim’s realization. Per OWASP, other names for CSRF include “XSRF, “Sea Surf”, Session Riding, Cross-Site Reference Forgery, and Hostile Linking”. Granted success, examples of consequences include funds transferring from the victim’s account into the attacker’s account, granting the attacker access to resources, impersonating the victim’s account etc …
Below are some notes on CSRF, most of which are from OSWASP.
Usually, an attacker carries out a CSRF attack through the use of social engineering, malicious scripts, an iFrame etc … Let’s consider an example.
Bob usually purchases merchandises through a popular, yet insecure online retailer – XYZInc. Knowing about XYZInc’s vulnerability, the attacker who owns the malicious website which Bob also visits frequently, includes a malicious javascript which upon execution, submits a request to purchase an electronic gift card and sends to the attacker’s email. Bob has been logged into XYZInc and has not logged out of his account. When Bob visits the attacker’s website, the javascript loads automatically to submit the form, along with the browser’s cookie which contains Bob’s session with XYZInc. Without Bob’s realization, Bob has executed the request to purchase the gift card that gets sent to the attacker.
<script>
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://xyzinc.com/purchase?type=egift", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
amount: 1000,
email: 'attackeremail@yahoo.com'
paymentMethod: 'CardOnFile'
}));
xhr.onload = function() {
console.log("Successfully purchased gift card.")
}
</script>
}
The standard way of protecting against CSRF is via an Anti-CSRF token. The idea is that a web application should always send back the token to a user’s browser (often when generating html page for the browser to display, or upon successful authentication). When the browser submits a request to the application, the application makes sure the request contains the token, and that token matches the one the application sent. If the checks fail, then the request fails. A proper implementation using Anti-CSRF token follows these rules:
Using MSAL angular to authenticate a user against azure ADB2C via authorization code flow with Proof Key for Code Exchange.
Azure AD authentication in angular using MSAL angular v2 library
Authenticate against azure ad using certificate in a client credentials flow
Migrating from Microsoft.AspNetCore.Authentication.AzureAD to Microsoft Identity Web authentication library to integrate with Azure AD.
Getting started with Azure AD Self-Service Sign-Up via user flows
Integrate Azure AD B2C reset password user flow in angular using oidc-client-js.
Integrate Azure AD B2C profile editing user flow in angular using oidc-client-js.
Using OAuth2 Client Credentials grant type in Azure ADB2C