Skip to content

ipaddress: IPv6Address/IPv6Network/IPv6Interface hashes are not randomized, allowing hash collisions (quadratic set/dict/collapse_addresses) #162850

Backlog

Description

@canozal

Bug description:

IPv6Address, IPv6Network and IPv6Interface hash plain tuples of ints, so their hashes are deterministic and easy to collide. A list of IPv6 addresses chosen by an attacker makes set(), dict, and ipaddress.collapse_addresses() quadratic.

(I first reported this privately to the PSRT. They asked me to open a public issue.)

Root cause (Lib/ipaddress.py on main)

  • _BaseAddress.__hash__ already uses hash(hex(int(self._ip))), which goes through the randomized str hash.
  • IPv6Address.__hash__ overrides it with hash((self._ip, self._scope_id)).
  • _BaseNetwork.__hash__ uses hash((int(self.network_address), int(self.netmask))).
  • IPv6Interface.__hash__ uses hash((self._ip, self._prefixlen, int(self.network.network_address))).

hash(int) is n mod (2**61 - 1). For 128-bit IPv6 values, a, a + M, a + 2M, … (with M = 2**61 - 1) therefore all have the same hash. Every probe in the collision chain also calls the pure-Python __eq__, which makes each collision expensive.

IPv4 is not affected in practice, because values below 2**32 hash to themselves.

Reproducer

import ipaddress, time

M = 2**61 - 1
base = int(ipaddress.IPv6Address('2001:db8::'))
for n in (2000, 4000, 8000):
    col = [str(ipaddress.IPv6Address(base + 2*j*M)) for j in range(n)]      # same hash
    ctl = [str(ipaddress.IPv6Address(base + 2*j*M + j)) for j in range(n)]  # distinct hashes
    t = time.perf_counter(); set(map(ipaddress.ip_address, col)); a = time.perf_counter() - t
    t = time.perf_counter(); set(map(ipaddress.ip_address, ctl)); b = time.perf_counter() - t
    print(n, f"colliding {a:.2f}s", f"control {b:.3f}s")

Measured on 3.14.3 (Apple arm64):

Operation n Colliding Control
set(IPv6Address) 2k / 4k / 8k / 16k 0.24 / 0.97 / 3.75 / 14.8 s 0.007 / 0.013 / 0.025 / 0.028 s
set(IPv6Network) 16k 22.9 s 0.044 s
set(IPv6Interface) 16k 20.9 s 0.071 s
ipaddress.collapse_addresses() 16k 53.4 s 0.12 s

16k addresses are about 0.6 MB of text, and they all lie inside a single /52.

Where this matters

Code that de-duplicates, counts, groups or collapses IP lists supplied by users: allow-list / ACL management APIs, IPAM tools, log, X-Forwarded-For and threat-feed ingestion, and collapse_addresses() on submitted ranges.

Related

gh-134062 replaced the XOR-based network hash with the current tuple hash. In that discussion, a randomized str/bytes-based hash was left as an option "if anyone demonstrates it still being a practical problem". The table above is meant as that demonstration.

Suggested fix

Hash a randomized representation, as _BaseAddress already does:

 class _BaseNetwork:
     def __hash__(self):
-        return hash((int(self.network_address), int(self.netmask)))
+        return hash((hex(int(self.network_address)), int(self.netmask)))

 class IPv6Address:
     def __hash__(self):
-        return hash((self._ip, self._scope_id))
+        return hash((hex(self._ip), self._scope_id))

 class IPv6Interface:
     def __hash__(self):
-        return hash((self._ip, self._prefixlen, int(self.network.network_address)))
+        return hash((hex(self._ip), self._prefixlen, hex(int(self.network.network_address))))

Hashing self.packed (bytes) would work as well. I'm happy to open a PR if this direction is acceptable.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

macOS

Activity

  1. added
    stdlib
    performance
    type-security
    on Nov 30, 2026
  2. added
    type-bug
    and removed
    type-security
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancestdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions