Broken Authentication

Broken Auth in Flask: CVE-2021-33026 Fix [Sep 2021] [CVE-2021-33026]

[Updated Sep 2021] Updated CVE-2021-33026

Overview

The Flask ecosystem can be affected by insecure deserialization when using the Flask-Caching extension. CVE-2021-33026 describes how the extension, up to version 1.10.1, relies on Python's pickle for serializing cached values. If an attacker gains write access to the underlying cache storage (such as filesystem cache, Memcached, or Redis) they can insert a crafted pickle payload. When the application later retrieves the cached value, unpickling can execute arbitrary Python code, potentially enabling remote code execution or local privilege escalation. This risk aligns with CWE-502 (Deserialization of Untrusted Data). In practice, exploitation is most probable in environments where an attacker can write to cache storage; otherwise, the likelihood is low if the cache is properly protected. The vulnerability is documented specifically for Flask-Caching up to 1.10.1 with a patch available to mitigate the issue. Exploitation typically involves an attacker writing a malicious pickle blob into the cache and the Flask application later deserializing it upon cache retrieval. This can affect authentication-related data stored in the cache or influence control flow if cache entries govern access decisions. The remediation emphasizes upgrading the caching component and avoiding pickle-based deserialization for cached data. In Flask contexts, securing the cache is a practical step toward reducing broken/authentication-related risk by preventing cache-poisoning from untrusted sources. To fix, upgrade Flask-Caching to a patched release that disables or eliminates insecure pickle-based deserialization, or explicitly configure a safe serializer (such as JSON) for the cache. Additionally, enforce strong access controls on cache backends, restrict caching to JSON-serializable data, rotate secrets, and use encrypted or authenticated connections for cache backends like Redis. These steps help align Flask deployments with secure authentication practices by preventing cache-based tampering from compromising credentials or tokens.

Affected Versions

Flask-Caching <= 1.10.1 (through 1.10.1); patch available

Code Fix Example

Flask API Security Remediation
Vulnerable pattern (illustrative; pickle-based serializer used by default):
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
# Vulnerable: Redis backends with default pickle-based serialization
app.config['CACHE_TYPE'] = 'RedisCache'
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
cache = Cache(app)

@app.route('/config_vuln')
def get_config_vuln():
    # Attacker-controlled payloads written to cache can be deserialized here,
    # potentially triggering arbitrary code execution on access.
    return str(cache.get('config'))

# Fixed pattern (safe serializer):
app = Flask(__name__)
app.config['CACHE_TYPE'] = 'RedisCache'
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
app.config['CACHE_SERIALIZER'] = 'json'
cache = Cache(app)

@app.route('/config_fix')
def get_config_fix():
    # Safely deserializes cached data as JSON
    return str(cache.get('config'))

CVE References

Choose which optional cookies to allow. You can change this any time.