From fb767fdf92efdcfdbdb539a05533d7dd8eb984a4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 15 2016 09:36:27 +0000 Subject: [PATCH 1/5] Ensure the email sent to be converted to avatar is proper utf-8 --- diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index b05b17d..0600f81 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -275,6 +275,8 @@ def avatar_url(email, size=64): """ Template filter sorting the given branches, Fedora first then EPEL, then whatever is left. """ + if isinstance(email, unicode): + email = email.encode('utf-8') return pagure.lib.avatar_url_from_openid(email, size) From 103bf4df1c0d85b7111cbeb0ab69d2fe2353b591 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 15 2016 09:36:27 +0000 Subject: [PATCH 2/5] Move the encoding to the backend so it is done once for all the calls --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 3ce0f09..136de74 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2341,6 +2341,8 @@ def avatar_url_from_openid(openid, size=64, default='retro', dns=False): """ Our own implementation since fas doesn't support this nicely yet. """ + if isinstance(openid, unicode): + openid = openid.encode('utf-8') if dns: # pragma: no cover # This makes an extra DNS SRV query, which can slow down our webapps. diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index 0600f81..b05b17d 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -275,8 +275,6 @@ def avatar_url(email, size=64): """ Template filter sorting the given branches, Fedora first then EPEL, then whatever is left. """ - if isinstance(email, unicode): - email = email.encode('utf-8') return pagure.lib.avatar_url_from_openid(email, size) From 0ded36325ae80e123538976614da7a8d18b33231 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 15 2016 09:36:28 +0000 Subject: [PATCH 3/5] Add unit-tests for pagure.lib.avatar_url_from_openid using an unicode string --- diff --git a/tests/test_pagure_lib.py b/tests/test_pagure_lib.py index e26a565..a781f20 100644 --- a/tests/test_pagure_lib.py +++ b/tests/test_pagure_lib.py @@ -1140,6 +1140,13 @@ class PagureLibtests(tests.Modeltests): 'b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928' '?s=64&d=retro') + output = pagure.lib.avatar_url_from_openid(u'zoé@çëfò.org') + self.assertEqual( + output, + 'https://seccdn.libravatar.org/avatar/' + '8fa6110d1f6a7a013969f012e1149ff89bf1252d4f15d25edee31d4662878656' + '?s=64&d=retro') + def test_fork_project(self): """ Test the fork_project of pagure.lib. """ gitfolder = os.path.join(self.path, 'repos') From 2ea0622107767364ae777076607274330a8f672e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 15 2016 09:41:15 +0000 Subject: [PATCH 4/5] Rename avatar_url_from_openid to avatar_url_from_email Since the variable provided is actually an email address, this naming makes more sense. We also renamed the variable as it is an email not an openid identifier. --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 136de74..3abb384 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2337,19 +2337,19 @@ def update_user_ssh(session, user, ssh_key, keydir): session.flush() -def avatar_url_from_openid(openid, size=64, default='retro', dns=False): +def avatar_url_from_email(email, size=64, default='retro', dns=False): """ Our own implementation since fas doesn't support this nicely yet. """ - if isinstance(openid, unicode): - openid = openid.encode('utf-8') + if isinstance(email, unicode): + email = email.encode('utf-8') if dns: # pragma: no cover # This makes an extra DNS SRV query, which can slow down our webapps. # It is necessary for libravatar federation, though. import libravatar return libravatar.libravatar_url( - openid=openid, + openid=email, size=size, default=default, ) @@ -2357,7 +2357,7 @@ def avatar_url_from_openid(openid, size=64, default='retro', dns=False): import urllib import hashlib query = urllib.urlencode({'s': size, 'd': default}) - hashhex = hashlib.sha256(openid).hexdigest() + hashhex = hashlib.sha256(email).hexdigest() return "https://seccdn.libravatar.org/avatar/%s?%s" % ( hashhex, query) From 01d545af695b7673ea48c68c2b27dd504e30c19e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 15 2016 09:42:11 +0000 Subject: [PATCH 5/5] Adjust all the calls of avatar_url_from_openid to avatar_url_from_email --- diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 5ed6023..fc65844 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -284,8 +284,8 @@ def api_users(): 'mention': [{ 'username': usr.username, 'name': usr.fullname, - 'image': pagure.lib.avatar_url_from_openid(usr.default_email, - size=16) + 'image': pagure.lib.avatar_url_from_email( + usr.default_email, size=16) } for usr in users] } ) diff --git a/pagure/api/issue.py b/pagure/api/issue.py index ad0ddfc..9df2ff3 100644 --- a/pagure/api/issue.py +++ b/pagure/api/issue.py @@ -474,7 +474,7 @@ def api_view_issue_comment( 404, error_code=APIERROR.ENOCOMMENT) output = comment.to_json(public=True) - output['avatar_url'] = pagure.lib.avatar_url_from_openid( + output['avatar_url'] = pagure.lib.avatar_url_from_email( comment.user.default_email, size=16) output['comment_date'] = comment.date_created.strftime( '%Y-%m-%d %H:%M:%S') diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 3abb384..a753b25 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -301,7 +301,7 @@ def add_issue_comment(session, issue, comment, user, ticketfolder, 'project': issue.project.fullname, 'comment_added': text2markdown(issue_comment.comment), 'comment_user': issue_comment.user.user, - 'avatar_url': avatar_url_from_openid( + 'avatar_url': avatar_url_from_email( issue_comment.user.default_email, size=16), 'comment_date': issue_comment.date_created.strftime( '%Y-%m-%d %H:%M:%S'), @@ -899,7 +899,7 @@ def add_pull_request_comment(session, request, commit, tree_id, filename, 'comment_added': comment_text, 'comment_user': pr_comment.user.user, 'comment_id': pr_comment.id, - 'avatar_url': avatar_url_from_openid( + 'avatar_url': avatar_url_from_email( pr_comment.user.default_email, size=16), 'comment_date': pr_comment.date_created.strftime( '%Y-%m-%d %H:%M:%S'), @@ -983,7 +983,7 @@ def edit_comment(session, parent, comment, user, 'comment_id': comment.id, 'parent_id': comment.parent.id, 'comment_editor': user_obj.user, - 'avatar_url': avatar_url_from_openid( + 'avatar_url': avatar_url_from_email( comment.user.default_email, size=16), 'comment_date': comment.edited_on.strftime( '%Y-%m-%d %H:%M:%S'), diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index b05b17d..092b77b 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -275,7 +275,7 @@ def avatar_url(email, size=64): """ Template filter sorting the given branches, Fedora first then EPEL, then whatever is left. """ - return pagure.lib.avatar_url_from_openid(email, size) + return pagure.lib.avatar_url_from_email(email, size) @APP.template_filter('short') diff --git a/tests/test_pagure_lib.py b/tests/test_pagure_lib.py index a781f20..84483b1 100644 --- a/tests/test_pagure_lib.py +++ b/tests/test_pagure_lib.py @@ -1131,16 +1131,16 @@ class PagureLibtests(tests.Modeltests): user = pagure.lib.search_user(self.session, username='foo') self.assertEqual(user.public_ssh_key, None) - def test_avatar_url_from_openid(self): + def avatar_url_from_email(self): """ Test the avatar_url_from_openid of pagure.lib. """ - output = pagure.lib.avatar_url_from_openid('pingou@fedoraproject.org') + output = pagure.lib.avatar_url_from_email('pingou@fedoraproject.org') self.assertEqual( output, 'https://seccdn.libravatar.org/avatar/' 'b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928' '?s=64&d=retro') - output = pagure.lib.avatar_url_from_openid(u'zoé@çëfò.org') + output = pagure.lib.avatar_url_from_email(u'zoé@çëfò.org') self.assertEqual( output, 'https://seccdn.libravatar.org/avatar/'