Skip to content

Commit

Permalink
Encode domain names using UTF-8 with UTS-46 canonicalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ntninja committed Jan 12, 2020
1 parent df7176c commit 3183161
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions multiaddr/codecs/idna.py → multiaddr/codecs/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@


def to_bytes(proto, string):
return idna.encode(string, uts46=True)
return idna.uts46_remap(string).encode("utf-8")


def to_string(proto, buf):
return idna.decode(buf)
string = buf.decode("utf-8")
for label in string.split("."):
idna.check_label(label)
return string
8 changes: 4 additions & 4 deletions multiaddr/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ def __repr__(self):
Protocol(P_DCCP, 'dccp', 'uint16be'),
Protocol(P_IP6, 'ip6', 'ip6'),
Protocol(P_IP6ZONE, 'ip6zone', 'utf8'),
Protocol(P_DNS, 'dns', 'idna'),
Protocol(P_DNS4, 'dns4', 'idna'),
Protocol(P_DNS6, 'dns6', 'idna'),
Protocol(P_DNSADDR, 'dnsaddr', 'idna'),
Protocol(P_DNS, 'dns', 'domain'),
Protocol(P_DNS4, 'dns4', 'domain'),
Protocol(P_DNS6, 'dns6', 'domain'),
Protocol(P_DNSADDR, 'dnsaddr', 'domain'),
Protocol(P_SCTP, 'sctp', 'uint16be'),
Protocol(P_UDT, 'udt', None),
Protocol(P_UTP, 'utp', None),
Expand Down
6 changes: 4 additions & 2 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@

# Additional test data
(_names_to_protocols['dns4'],
b'xn--4gbrim.xn----ymcbaaajlc6dj7bxne2c.xn--wgbh1c',
b'\xd9\x85\xd9\x88\xd9\x82\xd8\xb9.\xd9\x88\xd8\xb2\xd8\xa7\xd8\xb1\xd8\xa9'
b'-\xd8\xa7\xd9\x84\xd8\xa7\xd8\xaa\xd8\xb5\xd8\xa7\xd9\x84\xd8\xa7\xd8\xaa'
b'.\xd9\x85\xd8\xb5\xd8\xb1',
# Explicitly mark this as unicode to force the text to be LTR in editors
u'موقع.وزارة-الاتصالات.مصر'),
(_names_to_protocols['dns4'],
b'xn--fuball-cta.example',
b'fu\xc3\x9fball.example',
u'fußball.example'), # This will fail if IDNA-2003/NamePrep is used
]

Expand Down

0 comments on commit 3183161

Please sign in to comment.