Cross-site scripting High

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.

Detector ID
python/cross-site-scripting@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

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)

Compliant example

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))