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
Learning Targets
What is XSS (Cross-site scripting)?
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 pageTypes of XSS
XSS Prevention Techniques
What is CSRF (Cross-site request forgery)?
How CSRF Works Against Flask Apps
CSRF Prevention in Flask
Key Differences Between XSS and CSRF
Feature
XSS
CSRF
Code Interpretation Examples
Real-World Examples in Your Flask Projects
Summary
Last updated
Was this helpful?