Skip to main content

starlette-securecookies

GitHub Workflow Status PyPI - Downloads GitHub Buy a tree

Customizable middleware for adding automatic cookie encryption and decryption to Starlette applications.

Tested support on Python 3.7, 3.8, 3.9, and 3.10, 3.11 on macOS, Windows, and Linux.

How it works?

sequenceDiagram
    Browser->>+Middleware: Encrypted 'Cookie' headers
    Middleware->>+Application: Decrypted cookies
    Application->>-Middleware: Plaintext cookies
    Middleware->>-Browser: Encrypted 'Set-Cookie' headers
    Note over Application: *The Application may be your service<br />or any additional middleware.

For any incoming cookies:

  1. Requests sent from the client's browser to your application are intercepted by SecureCookiesMiddleware.
  2. All Cookie headers are filtered and parsed. Only cookies in the included_cookies and excluded_cookies parameters are parsed. All cookies are included by default.
  3. The cookies are decrypted. If a cookie cannot be decrypted, or is otherwise invalid, it is discarded by default (discard_invalid=True).
  4. Any included and valid encrypted cookies in the ASGI request scope are replaced by the decrypted ones.
  5. The request scope is passed to any future middleware, and eventually your application. Cookies can be read normally anywhere downstream.

For any outgoing cookies:

  1. Your application sets cookies with response.set_cookie, or by any other means of creating Set-Cookie headers.
  2. Other middleware run and add additional cookies, like SessionMiddleware.
  3. All responses returned by your application are intercepted by SecureCookiesMiddleware.
  4. Cookies in the included_cookies and not in the excluded_cookies parameters are re-encrypted, and their attributes (like "SameSite" and "HttpOnly") are overridden by any parameters set in SecureCookiesMiddleware.
  5. The cookies in the response are replaced by the re-encrypted cookies, and the response is eventually propagated to the client's browser.

Installation

$ pdm add starlette-securecookies
# or
$ python -m pip install --user starlette-securecookies

Usage

This is a Starlette-based middleware, so it can be used in any Starlette application or Starlette-based framework (like FastAPI).

For example,

from starlette.applications import Starlette
from starlette.middleware import Middleware

from securecookies import SecureCookiesMiddleware

middleware = [
    Middleware(
        SecureCookiesMiddleware, secrets=["SUPER SECRET SECRET"],
        # your other middleware
    )
]

app = Starlette(routes=..., middleware=middleware)

Note that if you're using another middleware that injects cookies into the response (such as SessionMiddleware), you have to make sure SecureCookiesMiddleware executes after it so the cookie is present at encryption-time. Counter intuitively, in practice this means ensuring SecureCookiesMiddleware is first in the list of middleware.

Extras

starlette-securecookies provides some extras that introduce or patch secure cookie functionality into existing tools. They all reside in the securecookies.extras module. Currently there is only one, but more are welcome by recommendation or Pull Request!

  • csrf.SecureCSRFMiddleware: Adds compatibility to the CSRF middleware provided by starlette_csrf. To use it, simply add it to your list of middleware (keep in mind the ordering). If you don't want to specify starlette_csrf as a direct dependency, you can also install it through the [csrf] package extra.

License

This software is licensed under the BSD 3-Clause License.

This package is Treeware. If you use it in production, consider buying the world a tree to thank me for my work. By contributing to my forest, you’ll be creating employment for local families and restoring wildlife habitats.

Metadata

Release files for starlette-securecookies 1.1.1

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

Source distribution (sdist)

Source distribution for starlette-securecookies 1.1.1
File Size Uploaded Provenance Actions
starlette_securecookies-1.1.1.tar.gz 12.2 kB Without provenance Details for starlette_securecookies-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for starlette-securecookies 1.1.1
File Interpreter ABI Platform Actions
starlette_securecookies-1.1.1-py3-none-any.whl Python 3 none any Details for starlette_securecookies-1.1.1-py3-none-any.whl

Total release size: 22.4 kB

Release files / starlette_securecookies-1.1.1.tar.gz

Download URL starlette_securecookies-1.1.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
28d3a888a02f02abb8a25674254456cda5028b45c68c1e4697fcefe7461a3642
BLAKE2b-256 checksum
How to use checksums
63e42a9c4e928c8eb629876c13da0f768860dea7450f95302dc9af3fb6ccead2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.8.0a2 CPython/3.11.2

Release files / starlette_securecookies-1.1.1-py3-none-any.whl

Download URL starlette_securecookies-1.1.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f54615ee2f01649729442bb06cd4831b377fd0617b8f734bba9e5893a24e2b0
BLAKE2b-256 checksum
How to use checksums
d9818e1e7bfff99f5ddea7a25333e5ba5f8e21355e985b6899786c260ef6c633
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.8.0a2 CPython/3.11.2

Security for starlette-securecookies 1.1.1

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.

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