component AP gateway: fix headers case in signature:

headers where not lower-cased in `headers` field of signature data, resulting in signature
being rejected by Pleroma (but it was working with Mastodon).

Also add `Content-Type` header.


rel 371
This commit is contained in:
Goffi 2022-08-31 17:07:03 +02:00
commit 30774ae9ad

View file

@ -933,7 +933,9 @@ class APGateway:
hashes.SHA256() # type: ignore
)
except InvalidSignature:
raise exceptions.EncryptionError("Invalid signature (using PKC0S1 v1.5 and SHA-256)")
raise exceptions.EncryptionError(
"Invalid signature (using PKC0S1 v1.5 and SHA-256)"
)
return actor_id
@ -955,7 +957,9 @@ class APGateway:
``headers`` is an updated copy of ``headers`` arguments, with pseudo-headers
removed, and ``Signature`` added.
"""
to_sign = "\n".join(f"{k.lower()}: {v}" for k,v in headers.items())
# headers must be lower case
l_headers: Dict[str, str] = {k.lower(): v for k, v in headers.items()}
to_sign = "\n".join(f"{k}: {v}" for k,v in l_headers.items())
signature = base64.b64encode(self.private_key.sign(
to_sign.encode(),
# we have to use PKCS1v15 padding to be compatible with Mastodon
@ -965,7 +969,7 @@ class APGateway:
sign_data = {
"keyId": key_id,
"Algorithm": "rsa-sha256",
"headers": " ".join(headers.keys()),
"headers": " ".join(l_headers.keys()),
"signature": signature
}
new_headers = {k: v for k,v in headers.items() if not k.startswith("(")}
@ -1026,7 +1030,7 @@ class APGateway:
)
else:
raise exceptions.InternalError(f"unexpected element: {item.toXml()}")
resp = await self.signAndPost(inbox, url_actor, ap_item)
await self.signAndPost(inbox, url_actor, ap_item)
async def convertAndPostAttachments(
self,
@ -1182,6 +1186,9 @@ class APGateway:
"Date": http.datetimeToString().decode(),
"Digest": digest
}
headers["Content-Type"] = (
'application/activity+json'
)
headers, __ = self.getSignatureData(self.getKeyId(actor_id), headers)
if self.verbose: