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 on macOS, Windows, and Linux.

How it works?

sequenceDiagram
    Browser->>+Middleware: Encrypted cookies
    Middleware->>+Application: Filtered / Decrypted cookies
    Application->>-Middleware: Plaintext cookies
    Middleware->>-Browser: Encrypted 'Set-Cookie' headers

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. All responses returned by your application are intercepted by SecureCookiesMiddleware.
  3. 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.
  4. The cookies in the response are replaced by the re-encrypted cookies, and the response is propagated to Starlette to return to the client's browser.

Installation

$ poetry 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 application (like FastAPI or Starlite).

For example,

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

from securecookies import SecureCookiesMiddleware

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

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

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.0.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

starlette_securecookies-1.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file starlette-securecookies-1.0.2.tar.gz.

File metadata

  • Download URL: starlette-securecookies-1.0.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.2 Windows/10

File hashes

Hashes for starlette-securecookies-1.0.2.tar.gz
Algorithm Hash digest
SHA256 cd7ac9de6f5e720986b9fe790b0e62227fd5f77650953808cea5f6be8d4b1afe
MD5 77a016a32c6ba392ff2d6358c6b13bff
BLAKE2b-256 1b9195fd22a7124716402de76f96abd2ae69d351095c6257f317ad4c60c1ba41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for starlette_securecookies-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c4d1d08de22102e0a8162f31f4a4c2d0537693a251581d5bb8a941a520fe58c0
MD5 1084706e756ae360661ea1522fa530a6
BLAKE2b-256 15829ec82a79f78033692b937ed94c125b354d367c73f12f487c5eaf633a6803

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