Flask uses Jinja2 template engine and Flask enables automatic escaping on Jinja2 by default.

If you really want to allow XSS, change {{ task.content }} to {{ task.content|safe }} on your template.

More information: https://flask.palletsprojects.com/en/1.1.x/templating/#controlling-autoescaping

Answer from vremes on Stack Overflow
🌐
Semgrep
semgrep.dev › write rules › secure coding › python › xss in flask
XSS in Flask | Semgrep
Cheat sheet for the prevention of Cross-site Scripting (XSS) vulnerabilities for Python and Flask.
🌐
Flask
flask.palletsprojects.com › en › stable › web-security
Security Considerations — Flask Documentation (3.1.x)
Flask configures Jinja to automatically escape all values unless explicitly told otherwise. This should rule out all XSS problems caused in templates, but there are still other places where you have to be careful:
Discussions

security - Is possible to write XSS vulnerable flask web app? - Stack Overflow
I'm trying to write a stored xss vulnerable flask web app. My app receive input via this input field and then s... More on stackoverflow.com
🌐 stackoverflow.com
web application - Flask XSS prevention - Information Security Stack Exchange
I'm trying to fix a possible XSS vulnerability in WebApp (Flask) and I'm not sure what is the best way to prevent XSS without breaking functionality. For example, I have the following code block: .... More on security.stackexchange.com
🌐 security.stackexchange.com
how does jinja2 prevent XSS attacks? - Stack Overflow
According to the Jinja2 docs Link, it provides: powerful automatic HTML escaping system for XSS prevention According to the Flask docs, it prevents XSS attacks by configuring Jinja2 to automatic... More on stackoverflow.com
🌐 stackoverflow.com
Prevent XSS and SQL Injection in chat system
SQL Alchemy auto escapes parameters in SQL queries, and Jinja auto escapes values in {} tags. So, unless you add | safe in Jinja tags or write your own SQL queries with users' provided data, you should be set for sanitization More on reddit.com
🌐 r/flask
8
3
January 2, 2024
🌐
GitHub
github.com › bgres › xss-demo
GitHub - bgres/xss-demo: Simple flask website to demonstrate reflected and stored XSS attacks. · GitHub
Minimal, flask-powered python web app to demonstrate reflected and stored XSS attacks.
Starred by 28 users
Forked by 18 users
Languages   CSS 46.1% | Python 28.5% | HTML 25.4%
🌐
Medium
medium.com › @pnandhiniofficial › implementation-of-cross-site-scripting-xss-in-a-flask-web-application-4dfb6d40c189
IMPLEMENTATION OF CROSS-SITE SCRIPTING (XSS) IN A FLASK WEB APPLICATION | by Nandhini P | Medium
April 23, 2025 - To prevent the XSS vulnerability, sanitize the user input by escaping special characters before rendering it on the page. ... Use the escape() function from markupsafe to escape any special HTML characters like <, >, ", and ' in user input. ...
🌐
Snyk
security.snyk.io › snyk vulnerability database › pip
Cross-site Scripting (XSS) in flask-admin | Snyk
September 5, 2018 - flask-admin is a batteries-included, simple-to-use Flask extension that lets add admin interfaces to Flask applications. Affected versions of this package are vulnerable to Cross-site Scripting (XSS) attacks.
Find elsewhere
🌐
Beautiful Soup
tedboy.github.io › flask › flask_doc.security.html
3. Security Considerations — Flask API
Flask configures Jinja2 to automatically escape all values unless explicitly told otherwise. This should rule out all XSS problems caused in templates, but there are still other places where you have to be careful:
🌐
Escape Tech
escape.tech › home › blog
How to protect your Flask applications ⎜Escape Blog
April 14, 2025 - Flask makes use of third-party ... become vulnerable to a range of exploits, including injection attacks, cross-site scripting (XSS), and data breaches....
🌐
Nvisium
blog.nvisium.com › injecting-flask
Injecting Flask
July 5, 2018 - def hello_xss(): name = "world" template = 'hello.unsafe' # 'unsafe' file extension... totally legit.
🌐
GitHub
github.com › terrabitz › Flask_XSS
GitHub - terrabitz/Flask_XSS: An intentionally vulnerable flask app designed to explore XSS vulnerabilities
An intentionally vulnerable flask app designed to explore XSS vulnerabilities - terrabitz/Flask_XSS
Author   terrabitz
🌐
Stack Exchange
security.stackexchange.com › questions › 254148 › flask-xss-prevention
web application - Flask XSS prevention - Information Security Stack Exchange
To solve your problem, strip out the <b>guestbook</b> (and any other "required tags") from the message.text, move that literal string into the template, and use Flask's HTML output encoding on the user's actual custom text.
🌐
GitHub
github.com › JasonHinds13 › hackable
GitHub - JasonHinds13/hackable: A python flask app that is purposefully vulnerable to SQL injection and XSS attacks. To be used for demonstrating attacks
A python flask app that is purposefully vulnerable to SQL injection and XSS attacks. To be used for demonstrating attacks - JasonHinds13/hackable
Starred by 71 users
Forked by 60 users
Languages   Python 40.6% | HTML 28.9% | JavaScript 21.5% | CSS 9.0% | Python 40.6% | HTML 28.9% | JavaScript 21.5% | CSS 9.0%
🌐
Sourcery
sourcery.ai › vulnerabilities › python-flask-security-audit-xss-make-response-with-unknown-content
Cross-site scripting (XSS) from unescaped response content via Flask make_response | Security Vulnerability Database | Sourcery
XSS vulnerability in Flask applications where response content is built with make_response and renders untrusted data as HTML without auto-escaping or encoding, allowing attackers to execute scripts in users browsers.
🌐
Snyk
snyk.io › blog › secure-python-flask-applications
How to secure Python Flask applications | Snyk
May 21, 2024 - Unfortunately, just like any other web framework, Flask is susceptible to vulnerabilities if it's not properly secured. The most common security risks for Flask include cross-site scripting (XSS), cross-site request forgery (CSRF), and SQL injection.
🌐
Medium
medium.com › @arif.rahman.rhm › protecting-your-web-app-from-stored-xss-flask-fastapi-django-and-expressjs-8e796935c3f7
Protecting Your Web App from Stored XSS: Flask, FastAPI, Django, and ExpressJS | by Arif Rahman | Medium
February 5, 2024 - Protecting Your Web App from Stored XSS: Flask, FastAPI, Django, and ExpressJS Stored XSS attacks lurk in the shadows of web applications, waiting to inject malicious code and wreak havoc. This …
🌐
Qwiet AI
qwiet.ai › appsec-resources › securing-your-flask-applications-essential-extensions-and-best-practices
Securing Your Flask Applications: Essential Extensions and Best Practices - Preventing the Unpreventable | Qwietᴬᴵ
February 7, 2025 - In this article, we have discussed securing Flask applications by addressing critical vulnerabilities such as Cross-Site Scripting (XSS), SQL injection, and Cross-Site Request Forgery (CSRF) attacks. We detailed the implementation of essential security extensions, including Flask-Security, Flask-Talisman, and Flask-SeaSurf.
🌐
Reddit
reddit.com › r/flask › flask api and xss
r/flask on Reddit: Flask API and XSS
July 11, 2017 -

Hi guys,

I'm learning some Flask, and for that I'm creating a little API (with Flask-RESTful) that will then be consumed by a SPA (Vue.Js) and maybe a mobile app later on.

Now, from what I've read, XSS issues are mainly handled by Jinja2, which will take care of encoding the output and all that stuff. However, given that this is an API and therefore I won't be using templates/Jinja2, the encoding will have to be done in the API views. OWASP suggests Python-Bleach (https://github.com/mozilla/bleach) for this, would you consider this the Python/Flask go-to solution when not using Jinja2?

Any opinions and suggestions are welcomed!

Cheers.