python-flask-web-server-security

This document is designed as a security spec that supports: 1) Secure-by-default code generation for new Flask code.

Flask (Python) Web Security Spec (Flask 3.1.x, Python 3.x)

This document is designed as a security spec that supports: 1) Secure-by-default code generation for new Flask code. 2) Security review / vulnerability hunting in existing Flask code (passive “notice issues while working” and active “scan the repo and report findings”).

It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).


0) Safety, boundaries, and anti-abuse constraints (MUST FOLLOW)


1) Operating modes

1.1 Generation mode (default)

When asked to write new Flask code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in a Flask repo (even if the user did not ask for a security scan):

1.3 Active audit mode (explicit scan request)

When the user asks to “scan”, “audit”, or “hunt for vulns”:

Recommended audit order: 1) App entrypoints / deployment scripts / Dockerfiles / Procfiles. 2) Flask configuration and environment handling. 3) Auth + sessions + cookies. 4) CSRF protections and state-changing routes. 5) Template rendering and XSS/SSTI. 6) File handling (uploads + downloads) and path traversal. 7) Injection classes (SQL, command execution, unsafe deserialization). 8) Outbound requests (SSRF). 9) Redirect handling (open redirects). 10) CORS and security headers.


2) Definitions and review guidance

2.1 Untrusted input (treat as attacker-controlled unless proven otherwise)

Examples include:

2.2 State-changing request

A request is state-changing if it can create/update/delete data, change auth/session state, trigger side effects (purchase, email send, webhook send), or initiate privileged actions.

2.3 Required audit finding format

For each issue found, output:


3) Secure baseline: minimum production configuration (MUST in production)

This is the smallest “production baseline” that prevents common Flask misconfigurations.

3.1 App initialization pattern (SHOULD)

SHOULD use an app factory and environment-based config so production config is not hard-coded.

Example skeleton (illustrative; adjust to your project):

Key baseline config targets:


4) Rules (generation + audit)

Each rule contains: required practice, insecure patterns, detection hints, and remediation.

FLASK-DEPLOY-001: Do not use Flask’s development server in production

Severity: High (if production)

Required:

Insecure patterns:

Detection hints:

Fix:

Note:


FLASK-DEPLOY-002: Debug mode MUST be disabled in production

Severity: Critical

Required:

Insecure patterns:

Detection hints:

Fix:

Note:


FLASK-CONFIG-001: SECRET_KEY must be strong, secret, and rotated safely

Severity: High (Critical if missing in production with sessions or signing)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


FLASK-SESS-001: Session cookies must use secure attributes in production

Severity: Medium

Required (production, HTTPS):

Insecure patterns:

Detection hints:

Fix:

Notes:


FLASK-SESS-002: Sessions must be bounded and resistant to fixation/replay

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-CSRF-001: State-changing requests using cookie auth MUST be CSRF-protected

Severity: High

Required:

If tokens are impractical, or for small applications:

Insecure patterns:

Detection hints:

Fix:

Notes:


FLASK-XSS-001: Prevent reflected/stored XSS in templates and HTML generation

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-SSTI-001: Never render untrusted templates (Server-Side Template Injection)

Severity: Critical

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-HEADERS-001: Set essential security headers (in app or at the edge)

Severity: Medium

Required (typical web app):

NOTE: Security headers may be set via a proxy or other cloud provider. Check to see if there is evidence of that.

Insecure patterns:

Detection hints:

Fix:


FLASK-LIMITS-001: Request size and form parsing limits MUST be set appropriately

Severity: Low (Medium if file uploads / large bodies are possible)

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-HOST-001: Host header must be validated in production

Severity: Low (depends on app’s use of external URLs)

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-PROXY-001: Reverse proxy trust must be configured correctly

Severity: Medium (High if relying on IPs for auth)

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-PATH-001: Prevent path traversal and unsafe file serving

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Note: safe_join is imported from werkzeug.security


FLASK-UPLOAD-001: File uploads must be validated, stored safely, and served safely

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-INJECT-001: Prevent SQL injection (use parameterized queries / ORM)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-INJECT-002: Prevent OS command injection

Severity: Critical to High (depends on exposure)

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-SSRF-001: Prevent server-side request forgery (SSRF) in outbound HTTP

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-REDIRECT-001: Prevent open redirects

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-HTTP-001: Use HTTP methods safely; do not change state via GET; avoid secrets in URLs

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-CORS-001: CORS must be explicit and least-privilege

Severity: Medium (High if misconfigured with credentials)

Required:

Insecure patterns:

Detection hints:

Fix:


FLASK-SUPPLY-001: Dependency and patch hygiene (focus on security-relevant deps)

Severity: Low

Required:

Audit focus example:

Detection hints:

Fix:


5) Practical scanning heuristics (how to “hunt”)

When actively scanning, use these high-signal patterns:

Always try to confirm:


6) Sources (accessed 2026-01-26)

Primary framework documentation:

Werkzeug documentation & advisories:

OWASP Cheat Sheet Series:

Template safety references:

HTTP semantics: