Implements a middleware that protects FastAPI endpoints with RFC 3230 Digest headers.
Project description
FastAPI RFC 3230 Digest Header Middleware
Introduction
This package provides a FastAPI middleware that enforces RFC 3230 Digest headers for HTTP requests. It validates the Digest header against the request body, ensuring message integrity and allowing you to specify which digest algorithms are accepted.
Installation
Install via pip:
pip install fastapi-rfc3230-digest-header-middleware
Usage Example
Add the middleware to your FastAPI app:
from fastapi import FastAPI, Request
from fastapi_rfc3230_digest_header_middleware import Middleware
app = FastAPI()
app.add_middleware(Middleware)
@app.post("/echo")
async def echo(request: Request):
body = await request.body()
return body
This will require all POST requests to /echo to include a valid Digest
header matching the request body. If the client sends an invalid request, the
server will respond with a 422 Unprocessable Entity error and include details
about the validation failure. The response will also include a Want-Digest header
indicating the accepted digest algorithms.
Client side
Sending requests with Digest Header
The client must compute the digest of the request body using one of the accepted
algorithms and include it in the Digest header. For example, to compute a
SHA-256 digest in Python you can use the rfc3230-digest-headers package:
from rfc3230_digest_headers import create_digest
body = b"Hello, World!"
digest_header = create_digest(body)
headers = {"Digest": digest_header.header_value}
if you want to manually create the header, you can do it like this:
import hashlib
import base64
body = b"Hello, World!"
sha256_digest = hashlib.sha256(body).digest()
digest_value = base64.b64encode(sha256_digest).decode('utf-8')
digest_header = f"SHA-256={digest_value}"
headers = {"Digest": digest_header}
Client side handling of Want-Digest Header
The client should also be able to handle the Want-Digest header in case of a 422 response. The rfc3230-digest-headers package can help with parsing this header as well.
from rfc3230_digest_headers import create_digest
want_digest_header = response.headers.get("Want-Digest", "")
# The `digest_value` will include the appropriate digests according to the server's Want-Digest header
digest_header = create_digest(body, want_digest_header)
Configuration
You can customize which digest algorithms are allowed or provide a custom callback to extract the bytes to validate:
Allow Only Specific Algorithms
from fastapi import FastAPI, Request
from fastapi_rfc3230_digest_header_middleware import Middleware
from rfc3230_digest_headers import DigestHeaderAlgorithm
qvalues = {
DigestHeaderAlgorithm.SHA256: None, # allow SHA-256
DigestHeaderAlgorithm.MD5: 0.0, # explicitly reject MD5
}
app = FastAPI()
app.add_middleware(Middleware, qvalues=qvalues)
@app.post("/echo")
async def echo(request: Request):
body = await request.body()
return body
Custom Instance Bytes Callback
The instance are the bytes the server and client agreed on to include in the
digest. By default, this is the entire request body, but instance of a request
may not always be the request body. You can provide a custom callback to extract
the bytes to validate:
async def get_instance_bytes(request: Request) -> bytes:
# Default instance bytes logic
return await request.body()
app = FastAPI()
app.add_middleware(Middleware, instance_bytes_callback=get_instance_bytes)
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_rfc3230_digest_header_middleware-1.0.3.tar.gz.
File metadata
- Download URL: fastapi_rfc3230_digest_header_middleware-1.0.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a49e008c436ed531417d00abf941a0965abb2789e506c66458ebfcfab9ffae9b
|
|
| MD5 |
420e4a816d1e08f6badb0272c4d617ed
|
|
| BLAKE2b-256 |
ae83410becf3ff1ce8c6895d83a54bab8c22ce6eacda85c06cd6f0a4b330a7ad
|
Provenance
The following attestation bundles were made for fastapi_rfc3230_digest_header_middleware-1.0.3.tar.gz:
Publisher:
publish.yml on Mari6814/fastapi-rfc3230-digest-header-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rfc3230_digest_header_middleware-1.0.3.tar.gz -
Subject digest:
a49e008c436ed531417d00abf941a0965abb2789e506c66458ebfcfab9ffae9b - Sigstore transparency entry: 724287936
- Sigstore integration time:
-
Permalink:
Mari6814/fastapi-rfc3230-digest-header-middleware@76f129464b2e49c8bf7009f30aa92bd155bce157 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/Mari6814
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@76f129464b2e49c8bf7009f30aa92bd155bce157 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_rfc3230_digest_header_middleware-1.0.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_rfc3230_digest_header_middleware-1.0.3-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ac39840c8cc25fcf89b6c222578e30637f77ba75542c1af21be0b3eadc7c8e6
|
|
| MD5 |
bbbcc87c86d8c8dc8c98ab53b0dfe776
|
|
| BLAKE2b-256 |
0c0a2254909a340dcb1b2cd355a63be1eae9d39a6ba80289685637b2ba11f2af
|
Provenance
The following attestation bundles were made for fastapi_rfc3230_digest_header_middleware-1.0.3-py3-none-any.whl:
Publisher:
publish.yml on Mari6814/fastapi-rfc3230-digest-header-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rfc3230_digest_header_middleware-1.0.3-py3-none-any.whl -
Subject digest:
6ac39840c8cc25fcf89b6c222578e30637f77ba75542c1af21be0b3eadc7c8e6 - Sigstore transparency entry: 724287952
- Sigstore integration time:
-
Permalink:
Mari6814/fastapi-rfc3230-digest-header-middleware@76f129464b2e49c8bf7009f30aa92bd155bce157 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/Mari6814
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@76f129464b2e49c8bf7009f30aa92bd155bce157 -
Trigger Event:
release
-
Statement type: