Member-only story

Exploiting a Flask Server Using a Known Secret Key

How knowing the secret key of a Flask server can be used to create a custom session cookie and bypass authentication

Francesco Pastore
4 min readJan 13, 2025

Setting a long and complex application key in Flask is critical when using the library’s built-in authentication logic.

If an attacker gains access to this value, they can potentially forge custom cookies and fool the server into thinking that requests are legitimate.

In this article, we’ll examine the critical role of app keys in Flask and how improper generation can lead to security vulnerabilities.

Image by Michal Jarmoluk from Pixabay

Flask servers and secret keys

Flask uses an application key to sign cookies, ensuring their validity by verifying the associated signature.

The signature is generated using the HMAC algorithm, which hashes the cookie content (encoded in base64) and the application key to produce a unique value.

Since only the server should know the application key, external users should not be able to generate a valid signature without it.

However, if an attacker has access to the application key, they can create cookies with arbitrary data that Flask incorrectly accepts as valid.

--

--

Francesco Pastore
Francesco Pastore

Written by Francesco Pastore

An engineering student in Milan and a web developer for an IT company. Write about programming and cybersecurity topics.

No responses yet