You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fastapi-armor

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastapi-armor

HTTP security headers middleware for FastAPI.

1.0.2
pipPyPI
Maintainers
1

fastapi-armor logo

Secure your FastAPI apps with a single line of code 🛡️

fastapi-armor is a security middleware that sets modern HTTP security headers for every response. It provides presets for common configurations (strict, relaxed, none) and allows overrides for full customization.

PyPI version Total downloads

🚀 Features

  • 📦 Simple plug-and-play integration with FastAPI
  • 🛡️ Protects your app with modern HTTP security headers
  • ⚙️ Fully customizable settings
  • 🧱 Built on top of Starlette and fully async

📦 Installation

Install via pip:

pip install fastapi-armor

⚙️ Usage Example

Here’s how to use ArmorMiddleware in a FastAPI application:

from fastapi import FastAPI
from fastapi_armor.middleware import ArmorMiddleware

app = FastAPI()

app.add_middleware(
    ArmorMiddleware,
    preset="strict",  # apply secure default set
    permissions_policy="geolocation=(), microphone=()"  # optionally override specific header
)

@app.get("/")
async def read_root():
    return {"message": "FastAPI with Armor Middleware is running!"}

▶️ Running the App

To run this FastAPI app locally using uvicorn, first install the required packages:

pip install fastapi uvicorn

Then start the app:

uvicorn example.main:app --reload

Visit your app at http://127.0.0.1:8000

You can inspect the HTTP headers in the browser or via curl:

curl -I http://127.0.0.1:8000

🎛️ Available Presets

You can use built-in presets to quickly apply a set of secure headers. These presets are designed for different use cases:

PresetDescription
strictApplies all recommended security headers with strict values for maximum protection.
relaxedApplies a lighter set of headers suitable for more flexible or development environments.
noneDisables all headers. Useful for debugging or local development where security is not a concern.

You can also override any individual header even when using a preset:

app.add_middleware(
    ArmorMiddleware,
    preset="strict",
    permissions_policy="geolocation=(), microphone=()"
)

🦩 Header Parameter Mapping

This table shows how to customize headers in the middleware by mapping FastAPI-Armor's parameter names to actual HTTP header fields:

Middleware ParameterHeader SetExample Value
content_security_policyContent-Security-Policy"default-src 'self'; img-src *;"
frame_optionsX-Frame-Options"DENY" or "SAMEORIGIN"
hstsStrict-Transport-Security"max-age=63072000; includeSubDomains; preload"
x_content_type_optionsX-Content-Type-Options"nosniff"
referrer_policyReferrer-Policy"no-referrer" or "strict-origin"
permissions_policyPermissions-Policy"geolocation=(), microphone=()"
dns_prefetch_controlX-DNS-Prefetch-Control"off" or "on"
expect_ctExpect-CT"max-age=86400, enforce"
origin_agent_clusterOrigin-Agent-Cluster"?1" or "?0"
cross_origin_embedder_policyCross-Origin-Embedder-Policy"require-corp"
cross_origin_opener_policyCross-Origin-Opener-Policy"same-origin" or "unsafe-none"
cross_origin_resource_policyCross-Origin-Resource-Policy"same-origin", "same-site", or "cross-origin"

Use these parameter names when configuring the middleware. For example, permissions_policy="geolocation=()" will set the Permissions-Policy HTTP header.

🛡️ Included Headers & Their Purpose

By default or optionally, ArmorMiddleware can apply the following headers:

HeaderDescription
Content-Security-PolicyMitigates XSS and data injection attacks by specifying allowed content sources.
X-Frame-OptionsPrevents clickjacking by disallowing rendering inside <iframe>.
Strict-Transport-SecurityForces use of HTTPS for future requests, helping prevent man-in-the-middle attacks.
X-Content-Type-OptionsDisables MIME-type sniffing to avoid content-type confusion.
Referrer-PolicyControls the Referer header sent in requests — reduces accidental info leakage.
Permissions-PolicyLimits access to browser APIs like geolocation, camera, microphone, etc.
X-DNS-Prefetch-ControlPrevents browsers from resolving DNS of external domains before user interaction.
Expect-CTEnsures valid Certificate Transparency logs for HTTPS connections.
Origin-Agent-ClusterProvides context isolation for enhanced privacy and safety.
Cross-Origin-Embedder-Policy (COEP)Blocks loading resources unless they explicitly allow being embedded.
Cross-Origin-Opener-Policy (COOP)Helps isolate browsing contexts to prevent cross-window attacks.
Cross-Origin-Resource-Policy (CORP)Restricts which origins can load resources from your site.

📚 Standards References

For more details on these headers and their standard definitions, refer to the following official resources:

Official Standards & Specifications

Security Organizations & Best Practices

Documentation & Practical Implementation

These resources represent officially accepted standards, specifications, and industry best practices for implementing security headers in modern web applications.

👥 Contributors

Special thanks to the following contributors who have helped improve this project:

If you'd like to contribute, please feel free to submit a pull request!

📄 License

This project is licensed under the MIT License. © 2025 Inan Delibas

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts