Skip to main content

Secure cookie middleware for Starlette applications.

Project description

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' header
    Middleware->>+Application: Decrypted cookies
    Application->>-Middleware: Plaintext cookies
    Middleware->>-Browser: Encrypted 'Set-Cookie' header
    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 parsed and filter. Only cookies in the included_cookies and excluded_cookies parameters are parsed. All cookies are included by default.
  3. The cookies are decrypted. If 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 as usual.
  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 excluded_cookies parameters are re-encrypted, and their attributes (like "SameSite" and "HttpOnly") are overridden by the parameters set in SecureCookiesMiddleware if set.
  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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_securecookies-1.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

starlette_securecookies-1.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file starlette_securecookies-1.1.0.tar.gz.

File metadata

File hashes

Hashes for starlette_securecookies-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3de6e164773c10de162fbe3101151d28e32c4c3514dbbfb2c8c1974098c9507e
MD5 4420f769b6a908ee2cd05bb33126d7b1
BLAKE2b-256 9c8dda36f390b7d0fde8d4995e3ed456bdce113dd5f5deb93be2b802871a7ebc

See more details on using hashes here.

File details

Details for the file starlette_securecookies-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_securecookies-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa7cda9620cbdfffee9ffea2103a488648ad8e6ced574e7c18973d18af627eee
MD5 fe78810365a387ae41b2eeb32bea37f3
BLAKE2b-256 ff86c2263db75d76b5d64d0e95c9fd902a0b394142dba059735d8f33c83efc0e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page