backend/managing-flask-middleware/SKILL.md
Configure Flask middleware for CORS, rate limiting, security headers, and request logging.
npx skillsauth add 7a336e6e/skills Managing Flask MiddlewareInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
4 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Set up middleware in a Flask application to handle cross-origin requests, enforce rate limits, add security headers, and log requests with timing and traceability.
Use flask-cors to manage Cross-Origin Resource Sharing. Always whitelist specific origins rather than allowing all.
from flask_cors import CORS
def configure_cors(app):
"""Set up CORS with explicit origin whitelist."""
allowed_origins = app.config.get("CORS_ORIGINS", [
"http://localhost:3000",
"https://app.example.com",
])
CORS(app, resources={
r"/api/*": {
"origins": allowed_origins,
"methods": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
"allow_headers": ["Content-Type", "Authorization"],
"expose_headers": ["X-Request-ID"],
"supports_credentials": True,
"max_age": 600,
}
})
Key points:
/api/* paths only.max_age so browsers cache preflight responses.supports_credentials only if cookies or auth headers are sent cross-origin.Use flask-limiter to enforce per-endpoint and global rate limits.
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
limiter = Limiter(
key_func=get_remote_address,
default_limits=["200 per hour", "50 per minute"],
storage_uri="redis://localhost:6379/0",
)
def configure_rate_limiting(app):
"""Attach the rate limiter to the app."""
limiter.init_app(app)
Apply stricter limits to sensitive endpoints:
from app.extensions import limiter
@auth_bp.route("/login", methods=["POST"])
@limiter.limit("10 per minute")
def login():
# ... authentication logic
pass
@auth_bp.route("/password-reset", methods=["POST"])
@limiter.limit("3 per hour")
def password_reset():
# ... password reset logic
pass
Add security headers via a @app.after_request hook:
def configure_security_headers(app):
"""Add security headers to every response."""
@app.after_request
def set_security_headers(response):
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["X-XSS-Protection"] = "1; mode=block"
response.headers["Strict-Transport-Security"] = (
"max-age=31536000; includeSubDomains"
)
response.headers["Content-Security-Policy"] = (
"default-src 'self'; script-src 'self'; style-src 'self'"
)
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
# Remove the default Server header to avoid leaking info
response.headers.pop("Server", None)
return response
Assign a unique request ID and log timing for every request:
import uuid
import time
import logging
from flask import g, request
logger = logging.getLogger(__name__)
def configure_request_logging(app):
"""Log each request with a unique ID and response time."""
@app.before_request
def start_timer_and_set_request_id():
g.start_time = time.perf_counter()
g.request_id = request.headers.get("X-Request-ID", str(uuid.uuid4()))
@app.after_request
def log_request(response):
duration_ms = (time.perf_counter() - g.start_time) * 1000
response.headers["X-Request-ID"] = g.request_id
logger.info(
"request_completed",
extra={
"request_id": g.request_id,
"method": request.method,
"path": request.path,
"status": response.status_code,
"duration_ms": round(duration_ms, 2),
"remote_addr": request.remote_addr,
},
)
return response
Wire everything together inside the app factory:
from flask import Flask
from app.middleware.cors import configure_cors
from app.middleware.rate_limiting import configure_rate_limiting
from app.middleware.security import configure_security_headers
from app.middleware.logging import configure_request_logging
def create_app(config_name=None):
app = Flask(__name__)
app.config.from_object(config_by_name[config_name or "development"])
# Middleware (order matters)
configure_request_logging(app)
configure_cors(app)
configure_rate_limiting(app)
configure_security_headers(app)
# Extensions and blueprints ...
return app
create_app() so it respects the app factory pattern.CORS origins to "*" in production.Server header or framework version in responses.Produce each middleware module as a separate file under app/middleware/. Show the updated create_app() function with all middleware calls included.
development
Implement features using the Red-Green-Refactor cycle to ensure testability and correctness from the start.
data-ai
Manage the `tasks.md` ledger with strict locking and collision avoidance protocols to allow multiple agents to work in parallel safely.
development
The git-workflow skill defines branching conventions, commit message formats, and pull request standards that all agents must follow for consistent version control.
development
The environment-config skill standardizes how agents manage environment variables, secrets, and application configuration across local development and deployed environments.