camo: Clean up type ignores.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2019-08-09 15:30:33 -07:00
parent 9b392917c5
commit bb5507009f
2 changed files with 5 additions and 5 deletions

View File

@ -1,5 +1,5 @@
from django.conf import settings from django.conf import settings
import codecs import binascii
import hashlib import hashlib
import hmac import hmac
@ -7,7 +7,7 @@ def generate_camo_url(url: str) -> str:
encoded_url = url.encode("utf-8") encoded_url = url.encode("utf-8")
encoded_camo_key = settings.CAMO_KEY.encode("utf-8") encoded_camo_key = settings.CAMO_KEY.encode("utf-8")
digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest() digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest()
hex_encoded_url = codecs.encode(encoded_url, "hex") # type: ignore # https://github.com/python/typeshed/issues/300 hex_encoded_url = binascii.b2a_hex(encoded_url)
return "%s/%s" % (digest, hex_encoded_url.decode("utf-8")) return "%s/%s" % (digest, hex_encoded_url.decode("utf-8"))
# Encodes the provided URL using the same algorithm used by the camo # Encodes the provided URL using the same algorithm used by the camo

View File

@ -7,7 +7,7 @@ from django.http import (
from zerver.lib.camo import is_camo_url_valid from zerver.lib.camo import is_camo_url_valid
from zerver.lib.thumbnail import generate_thumbnail_url from zerver.lib.thumbnail import generate_thumbnail_url
import codecs import binascii
def handle_camo_url(request: HttpRequest, digest: str, def handle_camo_url(request: HttpRequest, digest: str,
received_url: str) -> HttpResponse: received_url: str) -> HttpResponse:
@ -15,8 +15,8 @@ def handle_camo_url(request: HttpRequest, digest: str,
return HttpResponseNotFound() return HttpResponseNotFound()
hex_encoded_url = received_url.encode('utf-8') hex_encoded_url = received_url.encode('utf-8')
hex_decoded_url = codecs.decode(hex_encoded_url, 'hex') hex_decoded_url = binascii.a2b_hex(hex_encoded_url)
original_url = hex_decoded_url.decode('utf-8') # type: ignore # https://github.com/python/typeshed/issues/300 original_url = hex_decoded_url.decode('utf-8')
if is_camo_url_valid(digest, original_url): if is_camo_url_valid(digest, original_url):
return redirect(generate_thumbnail_url(original_url, is_camo_url=True)) return redirect(generate_thumbnail_url(original_url, is_camo_url=True))
else: else: