Skip to content

Commit e76c7a5

Browse files
authored
Permit the ServiceBrowser to browse overlong types (#666)
- At least one type "tivo-videostream" exists in the wild so we are permissive about what we will look for, and strict about what we will announce. Fixes #661
1 parent aaf8a36 commit e76c7a5

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

tests/test_exceptions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_bad_service_names(self):
6060
'_x-._tcp.local.',
6161
'_22._udp.local.',
6262
'_2-2._tcp.local.',
63-
'_1234567890-abcde._udp.local.',
6463
'\x00._x._udp.local.',
6564
)
6665
for name in bad_names_to_try:

tests/test_services.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,3 +1258,20 @@ def test_changing_name_updates_serviceinfo_key():
12581258
assert info_service.key == "mytesthome._homeassistant._tcp.local."
12591259
info_service.name = "YourTestHome._homeassistant._tcp.local."
12601260
assert info_service.key == "yourtesthome._homeassistant._tcp.local."
1261+
1262+
1263+
def test_servicebrowser_uses_non_strict_names():
1264+
"""Verify we can look for technically invalid names as we cannot change what others do."""
1265+
1266+
# dummy service callback
1267+
def on_service_state_change(zeroconf, service_type, state_change, name):
1268+
pass
1269+
1270+
zc = r.Zeroconf(interfaces=['127.0.0.1'])
1271+
browser = ServiceBrowser(zc, ["_tivo-videostream._tcp.local."], [on_service_state_change])
1272+
browser.cancel()
1273+
1274+
# Still fail on completely invalid
1275+
with pytest.raises(r.BadTypeInNameException):
1276+
browser = ServiceBrowser(zc, ["tivo-videostream._tcp.local."], [on_service_state_change])
1277+
zc.close()

tests/utils/test_name.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
"""Unit tests for zeroconf._utils.name."""
6+
7+
import pytest
8+
9+
from zeroconf._utils import name as nameutils
10+
from zeroconf import BadTypeInNameException
11+
12+
13+
def test_service_type_name_overlong_type():
14+
"""Test overlong service_type_name type."""
15+
with pytest.raises(BadTypeInNameException):
16+
nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.")
17+
nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.", strict=False)
18+
19+
20+
def test_service_type_name_overlong_full_name():
21+
"""Test overlong service_type_name full name."""
22+
long_name = "Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1" * 100
23+
with pytest.raises(BadTypeInNameException):
24+
nameutils.service_type_name(f"{long_name}._tivo-videostream._tcp.local.")
25+
with pytest.raises(BadTypeInNameException):
26+
nameutils.service_type_name(f"{long_name}._tivo-videostream._tcp.local.", strict=False)

zeroconf/_utils/name.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def service_type_name(type_: str, *, strict: bool = True) -> str: # pylint: dis
7474
:param type_: Type, SubType or service name to validate
7575
:return: fully qualified service name (eg: _http._tcp.local.)
7676
"""
77+
if len(type_) > 256:
78+
# https://datatracker.ietf.org/doc/html/rfc6763#section-7.2
79+
raise BadTypeInNameException("Full name (%s) must be > 256 bytes" % type_)
7780

7881
if type_.endswith((_TCP_PROTOCOL_LOCAL_TRAILER, _NONTCP_PROTOCOL_LOCAL_TRAILER)):
7982
remaining = type_[: -len(_TCP_PROTOCOL_LOCAL_TRAILER)].split('.')
@@ -104,7 +107,8 @@ def service_type_name(type_: str, *, strict: bool = True) -> str: # pylint: dis
104107

105108
test_service_name = service_name[1:]
106109

107-
if len(test_service_name) > 15:
110+
if strict and len(test_service_name) > 15:
111+
# https://datatracker.ietf.org/doc/html/rfc6763#section-7.2
108112
raise BadTypeInNameException("Service name (%s) must be <= 15 bytes" % test_service_name)
109113

110114
if '--' in test_service_name:

0 commit comments

Comments
 (0)