Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept hs2019-obfuscated http signatures #2811

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions bookwyrm/signatures.py
Expand Up @@ -57,16 +57,14 @@ def make_digest(data):
def verify_digest(request):
"""checks if a digest is syntactically valid and matches the message"""
algorithm, digest = request.headers["digest"].split("=", 1)
if algorithm == "SHA-256":
hash_function = hashlib.sha256
elif algorithm == "SHA-512":
hash_function = hashlib.sha512
else:
raise ValueError(f"Unsupported hash function: {algorithm}")

expected = hash_function(request.body).digest()
if b64decode(digest) != expected:
raise ValueError("Invalid HTTP Digest header")
# actual algorithm may be stated in the header or may be obscured as "hs2019".
# loop over the algorithms we support and see if one of them matches.
hash_functions = [hashlib.sha256, hashlib.sha512]
for hash_function in hash_functions:
expected = hash_function(request.body).digest()
if b64decode(digest) == expected:
return
raise ValueError(f"Invalid HTTP Digest header: {algorithm}")


class Signature:
Expand Down