githubEdit

422.41 XSS and CSRF

Protect software from two of the most common web-based attacks by validating input, managing sessions, and restricting unauthorised commands.

Overview

Cross-site scripting (XSS) and cross-site request forgery (CSRF) are two common and dangerous web-based attacks. They exploit the relationship between a user, their browser, and a trusted web application. Both rely on tricking users into performing unintended actions or injecting malicious code into otherwise trusted systems.

Understanding these attacks helps developers write more secure code and recognise vulnerable patterns in existing systems. While the technical implementation can be complex, the core concepts are straightforward and the prevention methods follow clear principles that you can apply to Flask applications like those you've built.

Learning Targets

In this topic, students learn to:

  • Explain how XSS and CSRF attacks exploit web applications

  • Identify scenarios where user input or session handling is vulnerable

  • Apply secure development techniques to prevent XSS and CSRF

  • Evaluate how cookies, tokens, and input validation improve web security

What is XSS (Cross-site scripting)?

XSS occurs when an attacker injects malicious scripts (usually JavaScript) into a web page viewed by other users. It happens when user input is not properly validated or escaped before being displayed in HTML templates.

Simple Flask Example

from flask import Flask, request

app = Flask(__name__)

@app.route('/welcome')
def welcome():
    # VULNERABLE: User input included directly in HTML
    username = request.args.get('username', '')
    return f"<h1>Welcome {username}!</h1>"

# If someone visits: /welcome?username=<script>alert('Hacked')</script>
# The JavaScript will execute in anyone's browser who views this page

Types of XSS

Stored XSS: The malicious script is saved in a database and served to multiple users

Reflected XSS: The script appears in a URL and is immediately reflected back

DOM-based XSS: Client-side JavaScript processes user input unsafely

XSS Prevention Techniques

Content Security Policy (CSP) Header:

What is CSRF (Cross-site request forgery)?

CSRF tricks a logged-in user into submitting a request they didn't intend, usually by clicking a link or loading a page that sends a hidden request on their behalf. If the user is authenticated with a session cookie, the attacker's request appears valid to your Flask application.

How CSRF Works Against Flask Apps

CSRF Prevention in Flask

Method 1: CSRF Tokens

Method 2: SameSite Cookie Configuration

Method 3: Using Flask-WTF Extension

Key Differences Between XSS and CSRF

Feature
XSS
CSRF

Target

End users viewing the page

Web application's trust in user

Exploits

Unescaped user input

Authenticated user's session

Attack Method

Malicious scripts in content

Forged requests from external sites

Prevention

Input escaping, CSP headers

CSRF tokens, SameSite cookies

Impact

Script execution in browser

Unauthorised actions as legitimate user

Flask Protection

escape(), Jinja2 auto-escaping

Flask-WTF, session tokens

Code Interpretation Examples

Real-World Examples in Your Flask Projects

XSS in a Blog App:

CSRF in User Settings:

Summary

  • XSS allows attackers to inject malicious scripts into web pages viewed by other users

  • CSRF tricks authenticated users into sending unauthorised requests to your Flask application

  • Flask provides tools like escape(), Jinja2 auto-escaping, and Flask-WTF for protection

  • Always validate input and include CSRF tokens in forms that change data

  • Use secure session configuration with SameSite cookies

  • These vulnerabilities are common in web applications but preventable with proper coding practices

Understanding these attacks helps you secure the Flask applications you build and identify potential vulnerabilities during development and testing.

Last updated

Was this helpful?