未验证 提交 a43addc2 编写于 作者: A Ansgar

gpg_get_key_addresses: fix handling non-UTF-8 uid

The Python 3 transition introduced a regression in
2f95fde0 which assumed all of
gpg's output is UTF-8.

Closes: #980182
上级 4565f5b3
......@@ -714,11 +714,11 @@ def gpg_keyring_args(keyrings=None):
def _gpg_get_addresses_from_listing(output: bytes):
addresses = []
for l in six.ensure_str(output).split('\n'):
parts = l.split(':')
if parts[0] not in ("uid", "pub"):
for line in output.split(b'\n'):
parts = line.split(b':')
if parts[0] not in (b"uid", b"pub"):
continue
if parts[1] in ("i", "d", "r"):
if parts[1] in (b"i", b"d", b"r"):
# Skip uid that is invalid, disabled or revoked
continue
try:
......@@ -726,9 +726,11 @@ def _gpg_get_addresses_from_listing(output: bytes):
except IndexError:
continue
try:
uid = six.ensure_text(uid)
uid = uid.decode(encoding='utf-8')
except UnicodeDecodeError:
uid = uid.decode("latin1") # does not fail
# If the uid is not valid UTF-8, we assume it is an old uid
# still encoding in Latin-1.
uid = uid.decode(encoding='latin1')
m = re_parse_maintainer.match(uid)
if not m:
continue
......
......@@ -23,6 +23,7 @@ from base_test import DakTestCase
from daklib.utils import (is_in_debug_section,
parse_built_using,
extract_component_from_section,
_gpg_get_addresses_from_listing,
ArchKey)
apt_pkg.init()
......@@ -120,6 +121,22 @@ class UtilsTest(DakTestCase):
for v, r in data:
self.assertEqual(extract_component_from_section(v), r)
def test_gpg_get_addresses_from_listing(self):
# Test with output containing a uid encoded in Latin-1 and UTF-8 each
data = (
b"tru::1:1610798976:1673870895:3:1:5\n"
b"pub:u:3072:1:4847924E3DEDDF1E:1610798895:1673870895::u:::scESC::::::23::0:\n"
b"fpr:::::::::AC6DFC4B78223BFCC1C064004847924E3DEDDF1E:\n"
b"uid:u::::1610798964::1D4AB6BD4AB8A446C196A1801F367B83B6141CC0::Markus Mustermann-S\xf6der <mustermann-soeder@example.org>::::::::::0:\n"
b"uid:u::::1610798895::F60CF610557A38E2943DECEEA5B02EE57B1ECC24::Markus Mustermann-S\xc3\xb6der <mustermann-soeder@example.com>::::::::::0:\n"
b"sub:u:3072:1:C3BCC6A42035DDB3:1610798895:1673870895:::::e::::::23:\n"
b"fpr:::::::::C2CC5D5744746B5E7F5F7286C3BCC6A42035DDB3:\n"
)
addresses = _gpg_get_addresses_from_listing(data)
expected = ['mustermann-soeder@example.org', 'mustermann-soeder@example.com']
self.assertEqual(addresses, expected)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册