Secweb: Security Middlewares for FastAPI and Starlette

1.30.10 · active · verified Thu Apr 16

Secweb is a pack of security middlewares for FastAPI and Starlette servers, providing features like Content Security Policy (CSP), HTTP Strict Transport Security (HSTS), and many more. It aims to offer easily configurable security headers with minimal overhead, implementing recommendations from MDN and OWASP. The library is currently at version 1.30.10 and is actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `Secweb` with a FastAPI application. By initializing `SecWeb` with your FastAPI app, all default security headers are automatically applied, enhancing the application's security posture. To test, run the app and inspect the HTTP response headers.

import uvicorn
from fastapi import FastAPI
from Secweb import SecWeb

app = FastAPI()

# Initialize SecWeb to apply all default security headers.
# Custom options can be passed via the 'Option' dictionary parameter.
SecWeb(app=app)

@app.get("/")
async def read_root():
    return {"message": "Hello, secured World!"}

# To run this example:
# 1. Save as a Python file (e.g., main.py)
# 2. Run from your terminal: uvicorn main:app --reload
# Check browser developer tools for applied security headers.

view raw JSON →