Skip to content

Commit 6ebfdf0

Browse files
authored
Merge pull request searx#971 from kvch/image-proxy-compatibility
fix hmac python3 compatibility
2 parents 9804ab7 + e73cb14 commit 6ebfdf0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

searx/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import csv
2+
import hashlib
3+
import hmac
24
import os
35
import re
46

@@ -321,3 +323,10 @@ def load_module(filename, module_dir):
321323
module = load_source(modname, filepath)
322324
module.name = modname
323325
return module
326+
327+
328+
def new_hmac(secret_key, url):
329+
if sys.version_info[0] == 2:
330+
return hmac.new(bytes(secret_key), url, hashlib.sha256).hexdigest()
331+
else:
332+
return hmac.new(bytes(secret_key, 'utf-8'), url, hashlib.sha256).hexdigest()

searx/webapp.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from searx.preferences import Preferences, ValidationException
7070
from searx.answerers import answerers
7171
from searx.url_utils import urlencode, urlparse, urljoin
72+
from searx.utils import new_hmac
7273

7374
# check if the pyopenssl package is installed.
7475
# It is needed for SSL connection without trouble, see #298
@@ -290,7 +291,7 @@ def image_proxify(url):
290291
if settings.get('result_proxy'):
291292
return proxify(url)
292293

293-
h = hmac.new(settings['server']['secret_key'], url.encode('utf-8'), hashlib.sha256).hexdigest()
294+
h = new_hmac(settings['server']['secret_key'], url.encode('utf-8'))
294295

295296
return '{0}?{1}'.format(url_for('image_proxy'),
296297
urlencode(dict(url=url.encode('utf-8'), h=h)))
@@ -704,7 +705,7 @@ def image_proxy():
704705
if not url:
705706
return '', 400
706707

707-
h = hmac.new(settings['server']['secret_key'], url, hashlib.sha256).hexdigest()
708+
h = new_hmac(settings['server']['secret_key'], url)
708709

709710
if h != request.args.get('h'):
710711
return '', 400
@@ -731,7 +732,7 @@ def image_proxy():
731732
logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
732733
return '', 400
733734

734-
img = ''
735+
img = b''
735736
chunk_counter = 0
736737

737738
for chunk in resp.iter_content(1024 * 1024):

0 commit comments

Comments
 (0)