Skip to main content

starlette-early-data

Middleware and decorator for processing TLSv1.3 early data requests in Starlette

PyPI: starlette-early-data Code Style: Black

Installation

pip install starlette-early-data

What this package can do:

  • Detect if request is early data and mark it (request.state.early_data = True)
  • Deny all early data requests (pass deny_all=True to add_middleware)
  • Deny early data requests to specific endpoints (use decorator @deny_early_data)

Usage example with Starlette

import uvicorn

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import PlainTextResponse

from starlette_early_data import EarlyDataMiddleware, deny_early_data

app = Starlette()
app.add_middleware(EarlyDataMiddleware, deny_all=False)


@app.route("/")
async def home(request: Request) -> PlainTextResponse:
    return PlainTextResponse("Always OK if deny_all=False")


@app.route("/security_risk")
@deny_early_data
async def security_risk(request: Request) -> PlainTextResponse:
    return PlainTextResponse("Security action done after handshake")


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080)

Send request to http://127.0.0.1:8080/security_risk with header Early-Data=1 and you will get 425 Early Data.

Request to http://127.0.0.1:8080/ with the same header will only return 425 Early Data if you pass deny_all=True to app.add_middleware(...)

Usage example with FastAPI

The only thing you need to do for it to work with FastAPI - write request: Request as first argument for endpoints

(maybe there is a way to inject it on decorator level, but I haven't found it, I welcome your PR though!)

import uvicorn

from fastapi import FastAPI, Request, Query
from fastapi.responses import PlainTextResponse

from starlette_early_data import EarlyDataMiddleware, deny_early_data

app = FastAPI()
app.add_middleware(EarlyDataMiddleware, deny_all=False)


@app.route("/")
async def home(request: Request, q: Optional[int] = Query(None)) -> PlainTextResponse:
    return PlainTextResponse("Always OK if deny_all=False")


@app.route("/security_risk")
@deny_early_data
async def security_risk(request: Request, q: Optional[int] = Query(None)) -> PlainTextResponse:
    return PlainTextResponse("Security action done after handshake")


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080)

Send request to http://127.0.0.1:8080/security_risk with header Early-Data=1 and you will get 425 Early Data.

Request to http://127.0.0.1:8080/ with the same header will only return 425 Early Data if you pass deny_all=True to app.add_middleware(...)

Testing

  • Run ./scripts/install to set up virtualenv
  • Run ./scripts/test to run tests
  • Run ./scripts/lint to lint code

Metadata

Release files for starlette-early-data 1.1.0

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-early-data 1.1.0
File Size Uploaded Provenance Actions
starlette-early-data-1.1.0.tar.gz 3.8 kB Without provenance Details for starlette-early-data-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for starlette-early-data 1.1.0
File Interpreter ABI Platform Actions
starlette_early_data-1.1.0-py3-none-any.whl Python 3 none any Details for starlette_early_data-1.1.0-py3-none-any.whl

Total release size: 8.6 kB

Release files / starlette-early-data-1.1.0.tar.gz

Download URL starlette-early-data-1.1.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8a34fe0e4f489f8bc89b324152424d91474406f3f6e420b8d64ff8fa5bd90176
BLAKE2b-256 checksum
How to use checksums
96de2e6c2c8b75397e9b15c0f8a1ea7018f25117167c79be773aeb0e24d4ad99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.9

Release files / starlette_early_data-1.1.0-py3-none-any.whl

Download URL starlette_early_data-1.1.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48e37e1f0e4c08631497d2fd77284f2b0cdfd69ad6f3b14706249c92d852a8c6
BLAKE2b-256 checksum
How to use checksums
7958e56a1da3c90f719946a623bc25fe01a69768a4003d9b7b0bd4c8ba9beff4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.9

Security for starlette-early-data 1.1.0

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