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, which requires the asgi extra (e.g. pip install 'modern-csrf[asgi]'). 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.24

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.24
File Size Uploaded Provenance Actions
modern_csrf-2026.10.24.tar.gz 6.6 kB Without provenance Details for modern_csrf-2026.10.24.tar.gz

Built distribution (wheel)

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

Total release size: 13.3 kB

Release files / modern_csrf-2026.10.24.tar.gz

Download URL modern_csrf-2026.10.24.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d39983713c60fb7f685e80cf6eb690587d3cc59117d3958bfb296d4e3db5603d
BLAKE2b-256 checksum
How to use checksums
7b382dc7b5b7cd862c327311ba0b1f5da6750eaaf7e0459967936d9d744b7503
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.24-py3-none-any.whl

Download URL modern_csrf-2026.10.24-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
474fff9c6642431b4ca2bd499b7c75483b0ebc366044b2b5a011139d1d2265ee
BLAKE2b-256 checksum
How to use checksums
e898ba6e1d2d71f6be2742cc859c010dcce0cecf14093e9dbf1883e2545195ac
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.24

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.

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

Release history Release notifications | RSS feed

This release

2026.10.24 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