Skip to content

Commit

Permalink
fix(telegram): Don't crash on invalid tgAuthResult
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Apr 25, 2024
1 parent a7a5399 commit aa1a78d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions allauth/socialaccount/providers/telegram/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import binascii
import hashlib
import hmac
import json
Expand Down Expand Up @@ -41,9 +42,19 @@ def post(self, request):
provider=provider,
)

result = request.POST.get("tgAuthResult")
padding = "=" * (4 - (len(result) % 4))
data = json.loads(base64.b64decode(result + padding))
try:
result = request.POST.get("tgAuthResult")
padding = "=" * (4 - (len(result) % 4))
data = json.loads(base64.b64decode(result + padding))
if not isinstance(data, dict) or "hash" not in data:
raise ValueError("Invalid tgAuthResult")
except (binascii.Error, json.JSONDecodeError, ValueError) as e:
return render_authentication_error(
request,
provider=provider,
exception=e,
extra_context={"state_id": state_id},
)
hash = data.pop("hash")
payload = "\n".join(sorted(["{}={}".format(k, v) for k, v in data.items()]))
token = provider.app.secret
Expand Down

0 comments on commit aa1a78d

Please sign in to comment.