Skip to main content

django-dbsc

CI Coverage PyPI Python versions Django versions License: MIT

Stolen session cookies stop working. django-dbsc adds Device Bound Session Credentials (DBSC) to Django: after login, the browser binds the session to a private key that never leaves the device (TPM or Secure Enclave). An attacker who copies the sessionid cookie with malware or an infostealer gets a 401, because they can't prove they hold that key.

Without DBSC:  stolen sessionid  ──▶  attacker is logged in as the victim
With DBSC:     stolen sessionid  ──▶  401 (no device key, no valid bound cookie)
  • Drop-in: one app, one middleware, two URLs. Your login and logout views stay as they are.
  • Graceful: browsers without DBSC support keep working exactly as before.
  • Your choice of strictness: protect everyone opportunistically, guard sensitive views, or require binding for staff.
  • WSGI and ASGI: the middleware and decorator run natively async under ASGI.

Status: alpha. It has been verified end to end in Chrome 156 (see docs/real-browser-testing.md), but it has not had an independent security review yet. The API may still change before 1.0. See the CHANGELOG for known limitations. Feedback and review are very welcome. To report a vulnerability, see SECURITY.md.

See it work

A real run of the example/ project in Chrome 157 on macOS (output abridged). Alice logs in, Chrome binds her session to a key in the Secure Enclave, and then an "attacker" replays her sessionid cookie from outside the browser:

$ python example/scripts/drive_chrome.py login
  landed on https://localhost:8443/protected/: 'protected ok user=alice dbsc_bound=False'
  cookies: ['csrftoken', 'dbsc_bound', 'sessionid']
  GET /protected/ -> 200 'protected ok user=alice dbsc_bound=True'

$ python example/scripts/drive_chrome.py steal
  attacker replaying only sessionid -> 401

The server log shows what happened to the stolen cookie:

INFO    django_dbsc [DBSC] session bound user_id=1 dbsc_id=tEJtINGR… alg=ES256
WARNING django_dbsc [DBSC] bound cookie missing/invalid, ending session dbsc_id=tEJtINGR… path=/protected/

The first page after login is still unbound because Chrome registers in the background. The replayed cookie is refused, and the session is ended on the server, so the cookie is worthless from then on. To reproduce it, follow docs/real-browser-testing.md.

Requirements

  • Python ≥ 3.12, Django ≥ 5.2
  • HTTPS, including in local development. Chrome ignores DBSC on plain http://, even on localhost.
  • A cache shared by all processes (Redis, Memcached or the database cache). The default per-process LocMemCache breaks with more than one worker.

Quickstart

uv add "django-dbsc>=0.1.0"        # or: pip install "django-dbsc>=0.1.0"

(0.0.1 on PyPI is an empty name placeholder. Make sure you get 0.1.0 or later.)

# settings.py
INSTALLED_APPS = [
    # ...
    "django_dbsc",
]

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    # ...
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_dbsc.middleware.DeviceBoundSessionMiddleware",  # after Session and Authentication middleware
    # ...
]

DJANGO_DBSC = {
    # Paths the browser must reach without a bound cookie. Always keep the DBSC endpoints here.
    "EXEMPT_PATH_PREFIXES": ["/dbsc/", "/static/"],
}
# urls.py
from django.urls import include, path

urlpatterns = [
    # ...
    path("dbsc/", include("django_dbsc.urls")),
]
python manage.py migrate

That's it. Every login now offers the browser a device binding, and bound sessions are protected automatically.

To require a proven device for a sensitive view:

from django.contrib.auth.decorators import login_required
from django_dbsc.decorators import device_bound_required


@login_required
@device_bound_required
def change_password(request): ...

Documentation

Read this If you want to
User guide understand the three protection modes, use request.dbsc_bound and the decorator, revoke devices, deploy
Configuration look up a setting, its default and when to change it
How it works & security model know exactly what is and isn't protected
Troubleshooting find out why a browser doesn't bind, or why users get logged out

Browser support

DBSC is a W3C draft. At the time of writing:

Browser Status
Chrome on Windows Enabled by default since Chrome 145 (needs a TPM)
Chrome on macOS Since Chrome 147, rolling out gradually (Secure Enclave)
Firefox, Safari Not supported. Sessions work as before, without binding.

You don't need to detect support. Browsers without DBSC ignore the registration offer.

License

MIT

Metadata

Release files for django-dbsc 0.2.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 django-dbsc 0.2.1
File Size Uploaded Provenance Actions
django_dbsc-0.2.1.tar.gz 22.0 kB With provenance Details for django_dbsc-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-dbsc 0.2.1
File Interpreter ABI Platform Actions
django_dbsc-0.2.1-py3-none-any.whl Python 3 none any Details for django_dbsc-0.2.1-py3-none-any.whl

Total release size: 52.2 kB

Release files / django_dbsc-0.2.1.tar.gz

Download URL django_dbsc-0.2.1.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b9914ad5cece4239152aa514e5d445ecb5f6244ecba7fe683489f5f24e1faab0
BLAKE2b-256 checksum
How to use checksums
891b36f55fd67d6ea391483cbc8296fd28aacfbaaa1d661638b55874b88ba4e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / django_dbsc-0.2.1-py3-none-any.whl

Download URL django_dbsc-0.2.1-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0cbbdc943e16fe223eb7253a3099865109e3548ed4cb6b2f59d00b649485d499
BLAKE2b-256 checksum
How to use checksums
6417c6cca675ec2f64dadaebe377a61a859135a24ab9a6315c5736b3cb6f2c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for django-dbsc 0.2.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_dbsc-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

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