Skip to main content

Block Cross-Origin Requests

This project implements the modern Cross-Site Request Forgery (CSRF) defenses as described by Filippo Valsorda. This is the Fetch metadata approach described on MDN. Unsafe cross-origin requests are rejected with a 403 Forbidden status by default.

This approach protects users of browsers less than a few years old (and anyone using an older browser than that likely has worse problems to worry about). This approach is useful because it can be cleanly implemented as a middleware without requiring close coordination with the application. That doesn't mean that you can't use CSRF tokens too.

How it works

The specific checks performed are:

  1. Allow requests with one of the safe HTTP methods: GET, HEAD, or OPTIONS.
  2. Allow requests from trusted origins.
  3. If the Sec-Fetch-Site header is set, allow requests with Sec-Fetch-Site set to same-origin or none, and reject other requests.
  4. Allow requests with neither of the Sec-Fetch-Site or Origin headers set.
  5. If the Origin and Host headers are both set, allow requests where the host-and-port portion of the origin matches the host.
  6. Reject any remaining requests.

Things to watch out for

In order for your application to be protected by this middleware, you must not change the application state in response to a request with one of the "safe" methods (GET, HEAD, OPTIONS). Note that this is not an additional requirement because there are other places within the HTTP stack where this assumption already exists. For example, requests with these methods may be automatically retried without user intervention.

For this to work well, your application should be served from a trustworthy origin, i.e. over HTTPS or via localhost. There may be false rejections if you do not use a trustworthy origin.

Protecting your application

Two versions of the middleware are provided: a Web Server Gateway Interface (WSGI) middleware modern_csrf.wsgi.BlockCrossOriginRequests, and an Asynchronous Server Gateway Interface (ASGI) middleware, modern_csrf.asgi.BlockCrossOriginRequests. Both versions have the same basic shape: the middleware is constructed with the next application as the first argument, and can be configured by setting keyword arguments. The available configuration parameters are:

  • trusted_origins: A collection of origins from which requests are always allowed. An "origin" is specified as scheme://host[:port] string and must match exactly, including case.

    Default: () (i.e. no trusted origins)

    Example: ["https://example.com", "https://example.net:1234"]

  • reject_handler: An application used to reject requests that should be rejected.

    The default value returns a 403 Forbidden status with a fixed message.

  • log_rejected_requests: Should detailed information about rejected requests be logged? For WSGI, the message is written to the WSGI error stream. For ASGI, the message is logged via the standard library logging infrastructure.

    Default: False

    Example: True

Metadata

Release files for modern-csrf 2026.10.20

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

Source distribution (sdist)

Source distribution for modern-csrf 2026.10.20
File Size Uploaded Provenance
modern_csrf-2026.10.20.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for modern-csrf 2026.10.20
File Interpreter ABI Platform
modern_csrf-2026.10.20-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / modern_csrf-2026.10.20.tar.gz

Download URL modern_csrf-2026.10.20.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4fd0e1e717f6f0037971b864bcaa190896a6111abd4b5f8477983fc93c968a48
BLAKE2b-256 checksum
How to use checksums
1e04a546371087a666d022226298ac0f2bcb59968b9bbdc3edf33d3226d934c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / modern_csrf-2026.10.20-py3-none-any.whl

Download URL modern_csrf-2026.10.20-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ddeb9a517576d255e328ceb9bd4e6937be7a97bdd8a6dc0933ca6afb16c2475a
BLAKE2b-256 checksum
How to use checksums
c7f7b75fa7253211ef6a6fc15e476e5525e009f1d6bb1c6446162391d33a5b78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for modern-csrf 2026.10.20

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

2026.10.20 This release

2 release files No provenance

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