Skip to main content

secure

PyPI Version Python Versions License

Define HTTP security headers once. Apply them consistently across Python web apps.

secure provides a small, dependency-free API for configuring modern security headers across common Python web frameworks through ASGI middleware, WSGI middleware, or framework response hooks.

Use it when you want to avoid copy-pasted header strings spread across handlers, hooks, and middleware.

secure hex

Quick links: Quick start · Headers · Middleware


Why use secure

Setting headers manually is fine for one endpoint. It gets harder to review when values are copied across routes, response hooks, reverse-proxy settings, and different framework integrations.

secure helps you:

  • Keep one Secure policy object instead of scattered header strings
  • Apply the same policy through ASGI middleware, WSGI middleware, or response hooks
  • Start with practical presets, then customize headers for your application
  • Use builders for complex headers such as Content Security Policy and Permissions Policy
  • Make duplicate handling, header overwrites, and validation explicit

The defaults are a reasonable starting point, not a substitute for application-specific review. Content Security Policy in particular should be adjusted for the scripts, styles, assets, and third-party services your app actually uses.


Installation

uv add secure
# or
pip install secure

Quick start

FastAPI / ASGI middleware

Use middleware when you can attach secure once and cover the whole application.

from fastapi import FastAPI
from secure import Secure
from secure.middleware import SecureASGIMiddleware

app = FastAPI()
secure_headers = Secure.with_default_headers()

app.add_middleware(SecureASGIMiddleware, secure=secure_headers)

Response hooks and handlers

Use the same policy object in framework hooks, middleware callbacks, or handlers that expose a response object with headers support.

from secure import Secure

secure_headers = Secure.with_default_headers()

secure_headers.set_headers(response)
# or
await secure_headers.set_headers_async(response)

Secure.with_default_headers() maps to Preset.BALANCED, a practical default designed to be customized.


What headers are applied by default

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
Content-Security-Policy: default-src 'self'; base-uri 'self'; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests
Strict-Transport-Security: max-age=31536000; includeSubDomains
Permissions-Policy: geolocation=(), microphone=(), camera=()
Referrer-Policy: strict-origin-when-cross-origin
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

This preset reflects modern browser guidance from MDN and OWASP. It reduces cross-origin risk, prevents MIME sniffing, and provides a conservative Content Security Policy you can extend.

The default CSP avoids unsafe script execution by default, but CSP is context-sensitive. Interactive apps, dashboards, CDNs, analytics, embedded content, or other third-party integrations may require additional configuration.


Presets

from secure import Preset, Secure

Secure.from_preset(Preset.BALANCED)
Secure.from_preset(Preset.BASIC)
Secure.from_preset(Preset.STRICT)
  • BALANCED: practical default for many applications
  • BASIC: Helmet-style compatibility
  • STRICT: tighter CSP and isolation

Start with BALANCED, review the generated headers, and move stricter only when needed.


Middleware

secure provides both ASGI and WSGI middleware.

  • Overwrites headers by default
  • Allows controlled duplication via multi_ok
  • Only modifies HTTP responses in ASGI apps

ASGI

from secure import Secure
from secure.middleware import SecureASGIMiddleware

secure_headers = Secure.with_default_headers()
app = SecureASGIMiddleware(app, secure=secure_headers)

WSGI

from secure import Secure
from secure.middleware import SecureWSGIMiddleware

secure_headers = Secure.with_default_headers()
app = SecureWSGIMiddleware(app, secure=secure_headers)

Django example

from secure import Secure

class SecureHeadersMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        self.secure = Secure.with_default_headers()

    def __call__(self, request):
        response = self.get_response(request)
        self.secure.set_headers(response)
        return response

Framework integration examples

These examples show common integration paths. See the full framework integration guide for additional frameworks and notes about first-class middleware versus minimal fallback examples.

FastAPI

from secure.middleware import SecureASGIMiddleware

app.add_middleware(SecureASGIMiddleware, secure=secure_headers)

Flask

@app.after_request
def add_security_headers(response):
    secure_headers.set_headers(response)
    return response

Shiny for Python

from shiny import App
from secure.middleware import SecureASGIMiddleware

app = SecureASGIMiddleware(App(), secure=secure_headers)

Interactive apps often need CSP configuration for their script, style, and asset loading patterns.


Policy builders

Content Security Policy

from secure.headers import ContentSecurityPolicy

csp = (
    ContentSecurityPolicy()
    .default_src("'self'")
    .script_src("'self'", "cdn.example.com")
)

Permissions Policy

from secure.headers import PermissionsPolicy

permissions = (
    PermissionsPolicy()
    .geolocation("'self'")
    .camera("'none'")
)

Header pipeline and validation

Use the optional pipeline when you need stricter control:

secure_headers = (
    Secure.with_default_headers()
    .allowlist_headers(...)
    .deduplicate_headers(...)
    .validate_and_normalize_headers(...)
)

This makes header allowlists, deduplication, normalization, and validation explicit instead of leaving those choices spread across application code.


Requirements

  • Python 3.10+
  • No external dependencies

Documentation

Read the full documentation.


Learn more


License

MIT License


Contributing

Issues and pull requests are welcome.


Acknowledgements

Built using guidance from MDN and OWASP secure headers recommendations.

Metadata

Release files for secure 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for secure 2.0.2
File Size Uploaded Provenance Actions
secure-2.0.2.tar.gz 41.4 kB With provenance Details for secure-2.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for secure 2.0.2
File Interpreter ABI Platform Actions
secure-2.0.2-py3-none-any.whl Python 3 none any Details for secure-2.0.2-py3-none-any.whl

Total release size: 96.0 kB

Release files / secure-2.0.2.tar.gz

Download URL secure-2.0.2.tar.gz
Size 41.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d6f242bfd9e56a7b279aed0554b80f1b1558559e20ec707cc71dc50de9835a50
BLAKE2b-256 checksum
How to use checksums
c2fca3f344f77fbaddeb33cf0362c31124d9ebacf6d473025ef86b6fb88dae10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Release files / secure-2.0.2-py3-none-any.whl

Download URL secure-2.0.2-py3-none-any.whl
Size 54.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f31fd8a104f13826bdc5b2f80525b0cbf81487b19dfa551f73dbc2a4cb17fa59
BLAKE2b-256 checksum
How to use checksums
247cfe587d84cbd0aaf14f90d8612a22a31097aa385964ac240486dd6f1d13b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Security for secure 2.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for secure-2.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page