In short: turn debug mode off, take the secret key out of the code and run on a real WSGI server. Secure the session cookie, leave autoescape on, add CSRF protection yourself and set TRUSTED_HOSTS.

Why Flask asks more of you

Flask deliberately does little by itself. It escapes templates, but CSRF protection, security headers and a production server are yours to arrange. Flask's documentation says so too: Flask solves some of it, the rest is up to you.

What to look at in a Flask app before production; our check reads nine of them1Debug mode off2The secret key is not in the code3The session cookie over HTTPS only,with SameSite4No template built from input5Autoescape stays on6CSRF protection on forms7A WSGI server, not the developmentserver8Uploads with a safe file name9TRUSTED_HOSTS set10Security headers in the responses
What to look at in a Flask app before production; our check reads nine of them1Debug mode off2The secret key is not in thecode3The session cookie over HTTPSonly, with SameSite4No template built from input5Autoescape stays on6CSRF protection on forms7A WSGI server, not thedevelopment server8Uploads with a safe file name9TRUSTED_HOSTS set10Security headers in theresponses
  • our check reads this
  • our check does not read this
Ten points from Flask's documentation. With a check mark: what our check reads in the code. The security headers in your server's responses we do not see.

Debug mode off and the right server

In debug mode Flask shows an interactive debugger on errors and reloads your code on every change. The documentation is clear: do not enable debug mode in production.

The built-in development server does not belong in production either. Flask says it is not designed to be secure, stable or efficient. Serve your app with a WSGI server such as Gunicorn or Waitress, with a web server in front if you like.

The secret key

Flask signs the session cookie with SECRET_KEY. Whoever has the key can make a session of their own. Flask advises a long random value and adds: do not reveal it in code you commit.

In production, read the key from the environment. If you have to replace it, old keys can stay in SECRET_KEY_FALLBACKS for a while.

The session cookie

By default Flask sends the session cookie over plain connections too. Turn SESSION_COOKIE_SECURE on when your site runs on HTTPS. Set SESSION_COOKIE_SAMESITE to Lax, the value Flask recommends. HttpOnly is on by default; leave it that way.

Templates and CSRF

Flask has Jinja escape every value automatically. That does not protect you when you build HTML yourself, call Markup on user input, or build a template from text with input in it. Also always put an attribute in a template between quotes.

Against CSRF, Flask does nothing by itself. The documentation explains that a form library is the place for it, and Flask has none. So use an extension such as Flask-WTF.

Uploads and hosts

A file name from a user cannot be trusted. A name with ../ in it can overwrite a file outside your upload folder. Pass every name through secure_filename and allow only the file types you expect. Limit the size with MAX_CONTENT_LENGTH; by default there is no limit.

By default Flask accepts any Host header. Set TRUSTED_HOSTS to the addresses your site runs on. If a proxy sits in front, tell Flask which proxy it may trust.

Security headers

Flask advises sending a few headers with every response. They are HSTS for HTTPS, a Content-Security-Policy, X-Content-Type-Options and X-Frame-Options. The Flask-Talisman extension can handle this for you.

What we check of this

We read the code of your Flask app for nine of the points above. We look at debug mode, the secret key in the code and the session cookie's settings. We also look at templates built from input, autoescape switched off, the CSRF protection, the development server, file names on uploads and TRUSTED_HOSTS. We read the files and do not run your code.

Which headers your server really sends, we do not see. We change nothing.

Join the pilot

Sources, opened on 2 December 2026

Written by the BlueLandscape team with the help of AI. Every statement was checked against the source above.