Complete worked example
python
from flask import Flask, session
def session_roundtrip(value, tamper=False):
app = Flask(__name__)
app.config.update(TESTING=True, SECRET_KEY='owned-course-key')
@app.get('/set')
def store():
session['theme'] = value
return {'saved': True}
@app.get('/get')
def read():
return {'theme': session.get('theme', 'default')}
client = app.test_client()
client.get('/set')
if tamper:
cookie = client.get_cookie('session').value
client.set_cookie('session', cookie + 'broken')
response = client.get('/get')
return response.json- Reasoning
- The helper creates a fresh app and client, stores a theme, then reads it through another request. An optional tamper step corrupts the signature-bearing cookie.