Prevention Methods

Tutorial 2 of 4

1. Introduction

In this tutorial, we will discuss Cross-Site Request Forgery (CSRF), a common web application vulnerability, and discover several methods for preventing such attacks. By the end of this tutorial, you will understand what CSRF attacks are, why they are dangerous, and how to implement preventative measures in your HTML code.

What You Will Learn

  • What is a CSRF attack
  • Importance of preventing CSRF attacks
  • Methods of CSRF prevention
  • Implementing the prevention methods in your HTML code

Prerequisites

  • Basic knowledge of HTML and JavaScript
  • Familiarity with web development concepts

2. Step-by-Step Guide

CSRF attacks trick the victim into submitting a malicious request. They use the identity and privileges of the victim to perform an undesired function on their behalf. Prevention techniques include using CSRF tokens, SameSite Cookie Attribute, and validating the Referer Header.

2.1 CSRF Tokens

CSRF tokens are random, unique values associated with a user's session. They are added as an additional parameter or header in requests from the client to the server.

<!-- A hidden field is added to the form with the CSRF token -->
<form action="/transfer" method="POST">
  <input type="hidden" name="csrf_token" value="YOUR_CSRF_TOKEN">
  <!-- Other form fields -->
</form>

2.2 SameSite Cookie Attribute

The SameSite attribute can be set to Strict or Lax. If set to Strict, the browser sends the cookie only for same-site requests (requests originating from the site that set the cookie).

// Setting the SameSite attribute in JavaScript
document.cookie = "key=value; SameSite=Strict";

2.3 Validating the Referer Header

This method involves checking the HTTP Referer header and ensuring that the request was made from the same site.

3. Code Examples

Let's walk through some examples that employ these prevention methods.

3.1 CSRF Tokens

The following is an example of a server-side JavaScript function that generates a CSRF token and inserts it into a form.

var csrf = require('csurf');
var express = require('express');

var csrfProtection = csrf({ cookie: true });
var app = express();

app.get('/form', csrfProtection, function(req, res) {
  // Passing the CSRF token to the view
  res.render('send', { csrfToken: req.csrfToken() });
});

3.2 SameSite Cookie Attribute

Here's how you can set the SameSite attribute for cookies in an Express.js app:

var express = require('express');
var cookieParser = require('cookie-parser');

var app = express();
app.use(cookieParser());

app.use(function(req, res, next) {
  // Set cookie with SameSite=Strict
  res.cookie('myCookie', 'value', { sameSite: 'strict' });
  next();
});

4. Summary

We have covered what CSRF attacks are, why they are dangerous, and several methods of preventing them, including CSRF tokens, the SameSite cookie attribute, and validating the Referer header.

5. Practice Exercises

  1. Write an HTML form that includes a hidden CSRF token field.
  2. Create an Express.js middleware function that sets a cookie with the SameSite attribute set to Strict.

Solutions

  1. Here's an example of an HTML form with a hidden CSRF token field:
<form action="/transfer" method="POST">
  <input type="hidden" name="csrf_token" value="YOUR_CSRF_TOKEN">
  <!-- Other form fields -->
</form>
  1. Below is a simple Express.js middleware function that sets a cookie with the SameSite attribute set to Strict:
var express = require('express');
var cookieParser = require('cookie-parser');

var app = express();
app.use(cookieParser());

app.use(function(req, res, next) {
  // Set cookie with SameSite=Strict
  res.cookie('myCookie', 'value', { sameSite: 'strict' });
  next();
});

To continue learning about web security, consider exploring other common web vulnerabilities like XSS (Cross-Site Scripting) and SQL injection.