From 3c22261405da9c0235112d913362132aef681fb8 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Mon, 7 Jan 2019 07:35:39 -0800 Subject: [PATCH] When dealing with an IDNA 2003 non-punycode label, escapify it before unicode conversion. This ensures that labels with codepoints that need escaping get it, as opposed to raising an exception because we tried to interpret a non-UTF-8 sequence as UTF-8. --- dns/name.py | 2 -- tests/test_name.py | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/dns/name.py b/dns/name.py index a9fc177b..c580f5f8 100644 --- a/dns/name.py +++ b/dns/name.py @@ -121,8 +121,6 @@ class IDNACodec(object): label = label[4:].decode('punycode') except Exception as e: raise IDNAException(idna_exception=e) - else: - label = label.decode() return _escapify(label) diff --git a/tests/test_name.py b/tests/test_name.py index bea30a2d..54b0f31f 100644 --- a/tests/test_name.py +++ b/tests/test_name.py @@ -266,11 +266,11 @@ class NameTestCase(unittest.TestCase): self.assertEqual(t, r'\150\151\152\153\154\155\156\157\158\159.') def testToText14(self): - # You can't send this to_unicode() as it wasn't unicode to begin with. - def bad(): - n = dns.name.from_text(r'\150\151\152\153\154\155\156\157\158\159.') - t = n.to_unicode() - self.failUnlessRaises(UnicodeDecodeError, bad) + # Something that didn't start as unicode should go to escapes and not + # raise due to interpreting arbitrary binary DNS labels as UTF-8. + n = dns.name.from_text(r'\150\151\152\153\154\155\156\157\158\159.') + t = n.to_unicode() + self.assertEqual(t, r'\150\151\152\153\154\155\156\157\158\159.') def testSlice1(self): n = dns.name.from_text(r'a.b.c.', origin=None) -- 2.47.3