User-controllable input must be sanitized before it's included in output used to dynamically generate a web page. Unsanitized user input can introduce cross-side scripting (XSS) vulnerabilities that can lead to inadvertedly running malicious code in a trusted context.
1from flask import app
2
3
4@app.route('/redirect')
5def redirect_url_noncompliant():
6 from flask import request, redirect
7 endpoint = request.args['url']
8 # Noncompliant: redirect to a user-supplied URL without sanitization.
9 return redirect(endpoint)
1from flask import app
2
3
4@app.route('/redirect')
5def redirect_url_compliant():
6 from flask import request, url_for, redirect
7 endpoint = request.args['url']
8 # Compliant: user-supplied URL is sanitized before redirecting to it.
9 return redirect(url_for(endpoint))