Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-FLASK-AUDIT-009 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects Flask response.set_cookie() calls where the secure or httponly flags are explicitly set to False. Cookies that carry session identifiers, authentication tokens, or any sensitive value must be protected by these flags.
The Secure flag prevents the browser from transmitting the cookie over unencrypted HTTP connections. Without it, a session cookie can be stolen by a passive network observer on any unencrypted path between the client and server -- public Wi-Fi, misconfigured proxies, or HTTP-to-HTTPS redirect chains. The HttpOnly flag prevents JavaScript running on the page (including injected XSS payloads) from accessing the cookie via document.cookie. Without it, a successful XSS attack instantly yields the session token.
The detection uses an Or pattern: Or(calls("*.set_cookie", match_name={"secure": False}), calls("*.set_cookie", match_name={"httponly": False})). This matches any object's set_cookie() method where either flag is explicitly disabled. The wildcard (*) on the receiver covers both response objects returned by Flask directly and response objects created via make_response().
Security Implications
Potential attack scenarios if this vulnerability is exploited
Session Hijacking via Network Eavesdropping
Without the Secure flag, a session cookie is sent over plain HTTP. On any network path that includes an unencrypted segment -- public Wi-Fi, HTTP-to-HTTPS redirect, mixed content -- a passive observer can capture the cookie and replay it to impersonate the authenticated user.
Session Hijacking via XSS
Without the HttpOnly flag, any JavaScript running on the page can read document.cookie and exfiltrate session tokens to an attacker-controlled server. A single XSS vulnerability anywhere on the domain can compromise every logged-in user's session.
Cross-Site Request Forgery Amplification
Cookies without the SameSite attribute (an additional flag not covered by this rule but worth setting) can be sent with cross-site requests, making CSRF attacks easier. Explicitly setting secure and httponly is the baseline; SameSite=Strict or Lax should also be configured.
Persistent Cookie Exposure
If a cookie is set without the Secure flag and the application later migrates to HTTPS, existing cookies issued to users during the HTTP period remain readable over plain HTTP until they expire or the user logs out.
How to Fix
Recommended remediation steps
- 1Set secure=True on all cookies that carry session identifiers, authentication tokens, or any sensitive data so they are never transmitted over plain HTTP.
- 2Set httponly=True on all session and authentication cookies so JavaScript cannot access them via document.cookie, limiting XSS damage.
- 3Set samesite='Strict' or samesite='Lax' to reduce CSRF risk. Use 'None' only for third-party contexts, and only in combination with secure=True.
- 4Configure Flask's global session cookie settings in app.config: SESSION_COOKIE_SECURE=True, SESSION_COOKIE_HTTPONLY=True, SESSION_COOKIE_SAMESITE='Lax'. These protect the built-in session cookie without requiring per-route set_cookie() calls.
- 5Review cookie usage during code review as a security-sensitive change and include it in your application's security checklist.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule uses Or(calls("*.set_cookie", match_name={"secure": False}), calls("*.set_cookie", match_name={"httponly": False})) to match any set_cookie() method call on any object where either the secure or httponly keyword argument is explicitly False. The wildcard receiver (*) covers response objects from both make_response() and direct route return-value manipulation. Calls that omit the secure or httponly argument entirely are not flagged by this rule -- consider those a separate audit (missing flag vs. explicit False). The rule operates at the call-site level without cross-file dataflow analysis.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Flask CORS Wildcard Origin
Detects Flask-CORS configured with origins="*", allowing any domain to make cross-origin requests to the application.
Flask Code Injection via eval()
User input from Flask request parameters flows to eval(). Replace with ast.literal_eval() for data parsing or json.loads() for structured input.
Frequently Asked Questions
Common questions about Flask Cookie Without Secure Flags
New feature
Get these findings posted directly on your GitHub pull requests
The Flask Cookie Without Secure Flags rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.