diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6761f04 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +name: CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + name: Test on Python ${{ matrix.python-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + cache: "pip" + + - name: Install package and dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[dev]" + + - name: Run test suite + run: | + pytest diff --git a/README.md b/README.md new file mode 100644 index 0000000..55befd6 --- /dev/null +++ b/README.md @@ -0,0 +1,183 @@ +# πŸ›‘οΈ FastAPI Security Headers + +[![CI](https://github.com/alejandrotg-code/fastapi-security-headers/actions/workflows/ci.yml/badge.svg)](https://github.com/alejandrotg-code/fastapi-security-headers/actions/workflows/ci.yml) +[![PyPI version](https://img.shields.io/pypi/v/fastapi-security-headers.svg)](https://pypi.org/project/fastapi-security-headers/) +[![Python versions](https://img.shields.io/pypi/pyversions/fastapi-security-headers.svg)](https://pypi.org/project/fastapi-security-headers/) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) +[![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)](https://github.com/alejandrotg-code/fastapi-security-headers) +[![Ko-fi](https://img.shields.io/badge/Ko--fi-Support-FF5E5B?logo=kofi&logoColor=white)](https://ko-fi.com/alejandrotg) + +**The missing security headers middleware for FastAPI.** Protect your API against XSS, clickjacking, MIME sniffing, and OWASP Top 10 web vulnerabilities with secure-by-default configurations and zero overhead. + +--- + +## ⚑ Highlights + +* **πŸš€ Zero Performance Overhead (Pure ASGI):** Bypasses heavy Starlette wrappers. Injects headers directly into ASGI raw message streams without body buffering. +* **πŸ“¦ Zero Dependencies:** Uses only Python's standard library (`dataclasses`, `typing`). Zero bloat in your dependency tree. +* **⚑ Pre-compiled Bytes:** Header tuples are encoded to `(bytes, bytes)` once at application startup, running in nanoseconds per request. +* **πŸŽ›οΈ Out-of-the-box Presets:** Ready-made configurations for APIs, high-compliance environments, and mixed apps. +* **πŸ“– Swagger / ReDoc Friendly:** Includes a dedicated preset that **won't break your interactive `/docs` documentation**. +* **πŸ”„ Route-Aware:** Intelligently respects custom headers set by individual route handlers unless explicitly configured to override. +* **🌐 WebSockets & Streaming Safe:** Passes WebSockets and large streaming responses (`StreamingResponse`) seamlessly. + +--- + +## πŸ“¦ Installation + +```bash +pip install fastapi-security-headers +``` + +--- + +## πŸš€ Quickstart (30 Seconds) + +Add the middleware to your FastAPI application in just 2 lines of code: + +```python +from fastapi import FastAPI +from fastapi_security_headers import SecurityHeadersMiddleware + +app = FastAPI() + +# Enable OWASP recommended security headers with secure defaults +app.add_middleware(SecurityHeadersMiddleware) + +@app.get("/") +async def root(): + return {"message": "Protected by fastapi-security-headers"} +``` + +--- + +## πŸ›‘οΈ The Security Headers Matrix + +By default, `fastapi-security-headers` turns an **F** score on security scanners into an **A+**: + +| HTTP Header | Default Value | Attack Vector Mitigated | +| :--- | :--- | :--- | +| **`X-Content-Type-Options`** | `nosniff` | **MIME-Sniffing:** Prevents browsers from guessing content types, blocking malicious scripts disguised as images or JSON. | +| **`X-Frame-Options`** | `DENY` | **Clickjacking:** Prevents external sites from embedding your API inside hidden `