Skip to main content

Flask Payload Shield

Pluggable Flask decorators for encrypting and decrypting request and response payloads. The package preserves the same handler API and wire format as the FastAPI sibling project while using Flask's synchronous request context.

Installation

pip install flask-payloadshield

Project page: https://pypi.org/project/flask-payloadshield/

For local development:

git clone https://github.com/PayloadShield/FlaskPS.git
cd FlaskPS
python -m venv .venv

# Windows PowerShell
.venv\Scripts\Activate.ps1

# Linux/macOS
# source .venv/bin/activate

python -m pip install -e ".[dev]"

Quick Start

from flask import Flask
from flask_payloadshield import PayloadShield, PayloadShieldEnc

app = Flask(__name__)
PayloadShieldEnc.init({"Key": "12345678901234567890123456789012"})

@app.get("/data")
@PayloadShield.encrypt("aes-gcm-256")
def get_data():
    return {"message": "hello"}

@app.post("/process")
@PayloadShield.decrypt("aes-gcm-256")
def process_data(data: dict):
    return {"received": data}

@app.post("/secure")
@PayloadShield.crypt("aes-gcm-256")
def secure_endpoint(data: dict):
    return {"processed": data}

if __name__ == "__main__":
    app.run(debug=True)

Send encrypted requests using this envelope:

{"encrypted": "<encoded payload>"}

The decrypt and crypt decorators inject the decrypted object into the first route parameter, such as data above. Decryption errors return HTTP 400 with an error field.

Built-in Handlers

Name Algorithm Keys
base64 Base64 encoding None; obfuscation only
fernet Fernet Key
aes-gcm-256 AES-256-GCM Key resolving to 32 bytes
chacha20-poly1305 ChaCha20-Poly1305 Key resolving to 32 bytes
rsa-hybrid RSA-OAEP + AES-256-GCM PublicKey, PrivateKey
ecdh-aes-gcm ECDH P-256 + AES-256-GCM ECPublicKey, ECPrivateKey
ecies ECDH P-256 + AES-CTR + HMAC ECPublicKey, ECPrivateKey
hpke RFC 9180 HPKE with X25519 HPKEPublicKey, HPKEPrivateKey

PEM values may be raw key content or file paths. PayloadShieldEnc.init() resets omitted keys to None and get_config() returns the current config.

Example and Postman

cd examples
python main.py

The example runs on http://127.0.0.1:5000, generates missing RSA, EC, and X25519 PEM files, and prints ready-to-paste Postman request bodies for every handler at startup. Use Ctrl+C to stop it.

In another terminal, verify the app:

curl http://127.0.0.1:5000/health
curl http://127.0.0.1:5000/aes

Development

pip install -e ".[dev]"
pytest

Publishing

Set a PyPI API token and run the publishing script from the project root:

export PYPI_TOKEN="pypi-..."
bash publish.sh

The script builds and checks the package before uploading it to https://pypi.org/project/flask-payloadshield/. The package version in pyproject.toml must be incremented before publishing a new release.

License

Apache-2.0 - See LICENSE.


📞 Support

Release files for flask-payloadshield 1.0.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 flask-payloadshield 1.0.0
File Size Uploaded
flask_payloadshield-1.0.0.tar.gz 23.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-payloadshield 1.0.0
File Interpreter ABI Platform
flask_payloadshield-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 45.9 kB

Release files / flask_payloadshield-1.0.0.tar.gz

Download URL flask_payloadshield-1.0.0.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c8587a024299cb785a0a1adc2134c8518ff15808184ce852d82fa7bbd5889c58
BLAKE2b-256 checksum
How to use checksums
b88553d4a6b00e97be7644ef72341f53fa872ef572d9958abb6ad2eac84632af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / flask_payloadshield-1.0.0-py3-none-any.whl

Download URL flask_payloadshield-1.0.0-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f60185263bf52e1962c03915f4ac252fb3225beee55d4c3bc26752e40dfb586
BLAKE2b-256 checksum
How to use checksums
19e52948b60bd6f18771e2baf7144ca5adf6a5ec1358cbbf962960f65ac2358f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release history Release notifications | RSS feed

1.0.1

2 release files

This release

1.0.0 This release

2 release files

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