Lightweight tools for signing and encrypting cookies, urls and stuff. This package isn't really secure, but it is secure enough for most needs.
Project description
Quick Overview
This package is "insecure", but secure enough.
The idea behing being "secure_enough" is to allow for "autologin cookies" and "instant login" urls for social web applications.
This package is very similar to "ItsDangerous" - which is now popular, but was unknown when this package was first written.
Two important things to note:
- You should not use this module for financial transactions or sensitive info. That would be egregiously silly.
- If you log someone in with this, you should note the login as "insecure" and subsequently require them to provide a password (or other authentication) to view sensitive data or any 'write' activity.
This package supports the following schemes for encrypting data:
- RSA encryption
- AES encryption
This package supports the following schemes for signing data:
- No signing ( just serialize )
- HMAC SHA1 signing
- HMAC SHA256 signing
- Request signing, as compatible with Facebook's auth scheme.
The data transformation order is as follows :
- serialize ( convert to JSON )
- base64 encode
- ? obfuscate
- ? encrypt
- ? sign
Background
Long ago, I had a class that would do a trivial encryption on cookie data, coupled with a lightweight hash to handle timeout events. This way you wouldn't always have to decrypt data to do a light verification.
The general flow was this:
To encode: cookie = encypted_data + timestamp + hash(encrypted_data + timestamp + secret)
To decode: (payload, timestamp, hash) = cookie if hash != hash (payload, timestamp, secret): raise InvalidHash() if timestamp > timeout: raise Timeout() data = decrypt(payload)
The encryption I used was a lightweight port from a CPAN (Perl) module, so it could be blown away in seconds today.
When i decided to re-implement this, looking around I found a handful of similar projects - which I've borrowed heavily from.
They include:
- https://github.com/dziegler/django-urlcrypt/blob/master/urlcrypt/lib.py
- http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/session.html#pyramid.session.signed_serialize
- https://developers.facebook.com/docs/authentication/signed_request/
This largely re-implements all of those, along with some other functionality.
Right now, data is a base64_url_encoded
version of a string, concatenated list,
or json object (for dicts). I opted against using pickle
, because this format
makes it easier to work with other web technologies (js, php, etc).
This might move to an all json version one day.
Check demo.py
to see an overview of how this works.
Signed Requests
signed_request_create
and signed_request_verify
are both handled as @classmethods
- along with their support functions.
That means you can call them directly without an object instance.
I built them as @classmethods
instead of package functions...
because if you want to extend the options for digest mods, you can just
subclass SecureEnough
and overwrite _digestmod
to add more providers.
Encrypting and Signing Cookies
Encrypting cookies currently happens via a 'global' RSA key for an instance of
SecureEnough()
. [you provide details for it in the __init__()
]
You can use timestamped based app_secrets, obfuscators & rsa keys.
The flow is as such:
-
Subclass the
ConfigurationProvider()
and overwrite the relevant hooks. The requesting mehtods pass a single argument -timestamp
- which should give you enough to go on. Note thatapp_secret
returns a string, while the obfuscator must return an object that canobfuscate
anddeobfuscate
; andrsa_key
requires an object that canencrypt
anddecrypt
. This libray provides default functionality through wrapper objects you can mimic. -
Instantiate a
SecureEnough()
object, and register the relevant providers -
When encrypting data,
SecureEnough()
will ask theConfigurationProvider()
for the approprite keys/secrets for the currenttime()
. When decrypting data,SecureEnough()
will ask theConfigurationProvider()
for the approprite keys/secrets for the time in the cookie/hash (if there is one).
This flow will allow you to easily create a plethora of site secrets and RSA keys -- as in a new one each day -- which means that while this module is not actually secure, it is Secure Enough for most web applications.
UNTESTED
- You can create "configuration objects" that accept a timestamp and return an appropriate secret/encryption key
===================
The following files give an interactive demo:
https://github.com/jvanasco/insecure_but_secure_enough/blob/main/demo.py
https://github.com/jvanasco/insecure_but_secure_enough/blob/main/demo_performance.py
Also note that the github source distribution contains tests.
===================
ToDo:
The timebased providers are largely untested.
- build out the demo and the test suite to support it.
insecure_but_secure_enough
is released under the 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
File details
Details for the file insecure_but_secure_enough-0.2.0.tar.gz
.
File metadata
- Download URL: insecure_but_secure_enough-0.2.0.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/5.0.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5f25dcbc0a1008e9b201a4b29ee822873944536ddcc850e80e7640c7da2b92b |
|
MD5 | 15b5bf4a1ce7a99c8e330d113e99c447 |
|
BLAKE2b-256 | 60fe0405be4ceee56a4b05942b6c877ed0c5b7a09f5c21346815942124ec56e6 |