Rembg is a tool to remove images background
fix: block IPv6 transition addresses in SSRF guard (GHSA-5xrj-892h-hqc3)
Daniel Gatisd7ac7e9ae2e9
diff --git a/rembg/commands/s_command.py b/rembg/commands/s_command.pyindex 62f2aca..8511619 100644--- a/rembg/commands/s_command.py+++ b/rembg/commands/s_command.py@@ -21,6 +21,52 @@ from ..sessions import sessions_namesfrom ..sessions.base import BaseSession+def _unwrap_ipv6(+ ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address],+) -> Union[ipaddress.IPv4Address, ipaddress.IPv6Address]:+ """Return the IPv4 address embedded in an IPv6 transition address.++ 6to4 (`2002::/16`), NAT64 (`64:ff9b::/96`), Teredo and IPv4-mapped+ addresses all carry an IPv4 address inside them. Whether the wrapper+ itself is classified as private depends on the interpreter: on+ Python 3.11.9, `2002:c0a8:0101::` reports `is_private=False` even though+ it embeds 192.168.1.1, so checking only the outer address lets a request+ through to an internal host. Unwrap first, then classify.+ """+ if not isinstance(ip, ipaddress.IPv6Address):+ return ip++ if ip.ipv4_mapped is not None:+ return ip.ipv4_mapped+ if ip.sixtofour is not None:+ return ip.sixtofour+ if ip.teredo is not None:+ # (server, client); the client is the address traffic reaches.+ return ip.teredo[1]+ if ip in ipaddress.ip_network("64:ff9b::/96"):+ return ipaddress.ip_address(int(ip) & 0xFFFFFFFF)++ return ip+++def _is_blocked_ip(ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]) -> bool:+ def blocked(addr) -> bool:+ return (+ addr.is_private+ or addr.is_loopback+ or addr.is_link_local+ or addr.is_reserved+ or addr.is_multicast+ or addr.is_unspecified+ )++ # Both the wrapper and what it embeds have to be public. Note that some+ # interpreters already classify whole transition ranges (6to4, NAT64) as+ # private, so those are refused outright rather than by their payload --+ # acceptable, as 6to4 is deprecated (RFC 7526) and NAT64 is operator-local.+ return blocked(ip) or blocked(_unwrap_ipv6(ip))++@click.command( # type: ignorename="s",help="for a http server",@@ -253,16 +299,6 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -RunVar("_default_thread_limiter").set(CapacityLimiter(threads))- def _is_blocked_ip(ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]) -> bool:- return (- ip.is_private- or ip.is_loopback- or ip.is_link_local- or ip.is_reserved- or ip.is_multicast- or ip.is_unspecified- )-def _resolve_public_ips(host: str) -> list[str]:"""Resolve a hostname to IPs, rejecting the request if any resolvedaddress is private/internal. Returns the safe IPs so the caller candiff --git a/tests/test_ssrf_ipv6_transition.py b/tests/test_ssrf_ipv6_transition.pynew file mode 100644index 0000000..cac707e--- /dev/null+++ b/tests/test_ssrf_ipv6_transition.py@@ -0,0 +1,89 @@+import ipaddress++import pytest++from rembg.commands.s_command import _is_blocked_ip, _unwrap_ipv6+++ "addr, embedded",+ [+ # 6to4 wrapping a private v4. On Python 3.11.9 the wrapper alone+ # reports is_private=False, which is the reported bypass.+ ("2002:c0a8:0101::", "192.168.1.1"),+ ("2002:7f00:0001::", "127.0.0.1"),+ ("2002:a9fe:a9fe::", "169.254.169.254"), # cloud metadata+ ("2002:0a00:0001::", "10.0.0.1"),+ # NAT64.+ ("64:ff9b::c0a8:101", "192.168.1.1"),+ ("64:ff9b::a9fe:a9fe", "169.254.169.254"),+ # IPv4-mapped.+ ("::ffff:127.0.0.1", "127.0.0.1"),+ ("::ffff:169.254.169.254", "169.254.169.254"),+ ],+)+def test_transition_addresses_wrapping_internal_v4_are_blocked(addr, embedded):+ """An internal IPv4 stays blocked however it is wrapped in IPv6."""+ assert _is_blocked_ip(ipaddress.ip_address(addr)) is True+++ "addr",+ [+ "192.168.1.1",+ "127.0.0.1",+ "10.0.0.1",+ "169.254.169.254",+ "::1",+ "fe80::1",+ "fc00::1",+ "0.0.0.0",+ ],+)+def test_plain_internal_addresses_stay_blocked(addr):+ """The original checks still hold; unwrapping did not weaken them."""+ assert _is_blocked_ip(ipaddress.ip_address(addr)) is True+++ "addr",+ [+ "8.8.8.8",+ "1.1.1.1",+ "93.184.216.34",+ "2606:4700:4700::1111",+ "2001:4860:4860::8888",+ ],+)+def test_public_addresses_are_allowed(addr):+ """Ordinary public destinations are not caught by the widened check."""+ assert _is_blocked_ip(ipaddress.ip_address(addr)) is False+++def test_sixtofour_is_refused_even_when_it_wraps_a_public_v4():+ """The whole 6to4 range is refused, not just the private payloads.++ Python classifies 2002::/16 as private on its own, so a 6to4 address is+ blocked before its payload is consulted. 6to4 is deprecated (RFC 7526), so+ losing it costs nothing and keeps the guard conservative.+ """+ assert _is_blocked_ip(ipaddress.ip_address("2002:0808:0808::")) is True+++def test_unwrap_extracts_embedded_v4():+ assert _unwrap_ipv6(+ ipaddress.ip_address("2002:c0a8:0101::")+ ) == ipaddress.ip_address("192.168.1.1")+ assert _unwrap_ipv6(+ ipaddress.ip_address("::ffff:127.0.0.1")+ ) == ipaddress.ip_address("127.0.0.1")+ assert _unwrap_ipv6(+ ipaddress.ip_address("64:ff9b::c0a8:101")+ ) == ipaddress.ip_address("192.168.1.1")+++def test_unwrap_passes_through_plain_addresses():+ """A v4 address, or a v6 that embeds nothing, is returned unchanged."""+ for addr in ("8.8.8.8", "2606:4700:4700::1111", "::1"):+ ip = ipaddress.ip_address(addr)+ assert _unwrap_ipv6(ip) == ip