diff --git a/modules/repos/lib.py b/modules/repos/lib.py index b2470cec..409d28e5 100644 --- a/modules/repos/lib.py +++ b/modules/repos/lib.py @@ -33,10 +33,10 @@ logger = logger.getChild(__name__) __all__ = ['ParsedRepoURL', 'parse_repo_url', 'repo_url_key', 'git_command', 'git_env', 'create_repository', 'update_repository', 'delete_repository', 'get_repositories', 'list_repo_tree', 'find_readme', - 'repo_destination_variables', 'is_valid_branch', 'submodule_url_error', 'submodule_fetch_config', - 'read_readme_text', 'search_repos', 'count_repos', 'search_repos_by_name', 'get_repo_log', - 'get_repo_root', 'repo_archive_name', 'stream_repo_archive', 'ReposConfig', 'repos_config', - 'get_repos_config', 'save_repos_config', 'import_repos_config', 'DEFAULT_REPO_FREQUENCY'] + 'repo_destination_variables', 'is_valid_branch', 'submodule_url_error', 'is_public_address', + 'submodule_fetch_config', 'read_readme_text', 'search_repos', 'count_repos', 'search_repos_by_name', + 'get_repo_log', 'get_repo_root', 'repo_archive_name', 'stream_repo_archive', 'ReposConfig', + 'repos_config', 'get_repos_config', 'save_repos_config', 'import_repos_config', 'DEFAULT_REPO_FREQUENCY'] DEFAULT_REPO_FREQUENCY = DownloadFrequency.weekly.value @@ -128,6 +128,32 @@ def resolve_host_addresses(host: str) -> List[str]: return list({i[4][0] for i in socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)}) +# IPv6 ranges which carry an IPv4 address, or are not public. +NAT64_NETWORK = ipaddress.ip_network('64:ff9b::/96') +LOCAL_NAT64_NETWORK = ipaddress.ip_network('64:ff9b:1::/48') +TEREDO_NETWORK = ipaddress.ip_network('2001::/32') +SITE_LOCAL_NETWORK = ipaddress.ip_network('fec0::/10') + + +def is_public_address(address: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool: + """Is `address` a public (globally routed) unicast address? + + `is_global` alone is not enough: it accepts multicast, deprecated site-local, and IPv6 addresses which carry a + private IPv4 address (e.g. NAT64 `64:ff9b::169.254.169.254`, the metadata address).""" + if isinstance(address, ipaddress.IPv6Address): + if address.ipv4_mapped: + return is_public_address(address.ipv4_mapped) + if address in NAT64_NETWORK: + return is_public_address(ipaddress.IPv4Address(int(address) & 0xFFFFFFFF)) + if address.sixtofour: + return is_public_address(address.sixtofour) + if address in LOCAL_NAT64_NETWORK or address in TEREDO_NETWORK or address in SITE_LOCAL_NETWORK: + return False + return address.is_global and not ( + address.is_multicast or address.is_reserved or address.is_link_local or address.is_loopback + or address.is_private or address.is_unspecified) + + # Submodule URLs must have a shape that every URL parser (Python's, git's, curl's) reads the same way: no userinfo, # backslashes, queries, fragments or whitespace. ABSOLUTE_SUBMODULE_URL_RE = re.compile( @@ -158,7 +184,7 @@ def _check_submodule_url(url: str, repo_host: str) -> Tuple[Optional[str], Optio except (OSError, ValueError): return f'could not resolve {host}', None pin = (host, port, [str(i) for i in addresses]) - if not addresses or not all(i.is_global for i in addresses): + if not addresses or not all(is_public_address(i) for i in addresses): return f'{host} is not a public address', None return None, pin diff --git a/modules/repos/test/test_extras.py b/modules/repos/test/test_extras.py index d7510356..5c7deadb 100644 --- a/modules/repos/test/test_extras.py +++ b/modules/repos/test/test_extras.py @@ -329,3 +329,43 @@ async def test_relative_submodule_url(test_session, git_remote_factory, repos_do assert download.status == DownloadStatus.deferred assert 'Refusing to download submodule https://10.0.0.5/library' in download.error assert not (repo.directory / 'vendor/library/README.md').exists() + + +@pytest.mark.parametrize('address,public', [ + ('8.8.8.8', True), + ('2606:4700::1111', True), + ('64:ff9b::808:808', True), # NAT64 of 8.8.8.8 + ('2002:808:808::1', True), # 6to4 of 8.8.8.8 + ('10.0.0.5', False), + ('100.64.0.1', False), # Carrier-grade NAT + ('127.0.0.1', False), + ('169.254.169.254', False), + ('224.0.0.1', False), # Multicast + ('255.255.255.255', False), + ('0.0.0.0', False), + ('::1', False), + ('::', False), + ('fe80::1', False), + ('fc00::1', False), + ('fec0::1', False), # Deprecated site-local + ('ff02::1', False), # Multicast + ('::ffff:169.254.169.254', False), + ('64:ff9b::169.254.169.254', False), # NAT64 of the metadata address + ('64:ff9b::a9fe:a9fe', False), + ('64:ff9b:1::808:808', False), # Local-use NAT64 + ('2002:a00:5::1', False), # 6to4 of 10.0.0.5 + ('2001:0:4136:e378:8000:63bf:3fff:fdd2', False), # Teredo +]) +def test_is_public_address(address, public): + import ipaddress + assert lib.is_public_address(ipaddress.ip_address(address)) is public + + +@pytest.mark.parametrize('url', [ + 'https://[64:ff9b::169.254.169.254]/latest/meta-data', + 'https://[64:ff9b::a9fe:a9fe]/', + 'https://[fec0::1]/', + 'https://224.0.0.1/', +]) +def test_submodule_url_error_rejects_non_public_literals(url): + assert lib.submodule_url_error(url, 'git.example.com')