Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(geoip): skip non-public addresses before the maxmind lookup (#119327)

posthog[bot]947f4a3faeb3
diff --git a/posthog/geoip.py b/posthog/geoip.py
index 389f7f262b3f..e226ccd497be 100644
--- a/posthog/geoip.py
+++ b/posthog/geoip.py
@@ -39,6 +39,19 @@ GEOIP_KEY_MAPPING = {"city": "city_name"}
GEOIP_LOCATION_CACHE_SIZE = 4096
+def _is_non_public_ip(ip_address: str) -> bool:
+ """True for addresses geoip can't usefully locate — private/reserved ranges (incl. IPv6) and
+ malformed input. Without this, RFC1918 (10/8, 172.16/12), loopback (::1), link-local, etc. would
+ fall through to geoip.city() and raise "not in the database" on every such request."""
+ try:
+ parsed = ipaddress.ip_address(ip_address)
+ except ValueError:
+ return True
+ return (
+ parsed.is_private or parsed.is_loopback or parsed.is_link_local or parsed.is_reserved or parsed.is_unspecified
+ )
+
+
def get_geoip_properties(ip_address: Optional[str]) -> dict[str, str]:
"""
Returns a dictionary of geoip properties for the given ip address.
@@ -52,8 +65,7 @@ def get_geoip_properties(ip_address: Optional[str]) -> dict[str, str]:
$geoip_postal_code
$geoip_time_zone
"""
- if not ip_address or not geoip or ip_address == "127.0.0.1" or ip_address.startswith("192.168."):
- # Local addresses would otherwise throw "The address 127.0.0.1 is not in the database." below
+ if not ip_address or not geoip or _is_non_public_ip(ip_address):
return {}
try:
@@ -77,19 +89,6 @@ class GeoLocation(TypedDict, total=False):
country_code: str
-def _is_non_public_ip(ip_address: str) -> bool:
- """True for addresses geoip can't usefully locate — private/reserved ranges (incl. IPv6) and
- malformed input. Without this, RFC1918 (10/8, 172.16/12), loopback (::1), link-local, etc. would
- fall through to geoip.city() and raise "not in the database" on every such request."""
- try:
- parsed = ipaddress.ip_address(ip_address)
- except ValueError:
- return True
- return (
- parsed.is_private or parsed.is_loopback or parsed.is_link_local or parsed.is_reserved or parsed.is_unspecified
- )
-
-
@dataclass(frozen=True, kw_only=True)
class CachedLocation:
"""One memoized geoip result. Keyword-only because latitude and longitude share a type and would
diff --git a/posthog/test/test_geoip.py b/posthog/test/test_geoip.py
index 3a941002d9f0..e2dc433bd66a 100644
--- a/posthog/test/test_geoip.py
+++ b/posthog/test/test_geoip.py
@@ -4,7 +4,7 @@ from django.test import SimpleTestCase
from parameterized import parameterized
-from posthog.geoip import _lookup_location, get_geoip_location
+from posthog.geoip import _lookup_location, get_geoip_location, get_geoip_properties
class TestGeoipLocation(SimpleTestCase):
@@ -19,20 +19,34 @@ class TestGeoipLocation(SimpleTestCase):
@parameterized.expand(
[
- ("rfc1918_10", "10.0.0.5"),
- ("rfc1918_172", "172.16.3.4"),
- ("rfc1918_192", "192.168.1.1"),
- ("ipv6_loopback", "::1"),
- ("link_local", "169.254.0.1"),
+ (f"{lookup.__name__}_{name}", lookup, ip)
+ for lookup in (get_geoip_location, get_geoip_properties)
+ for name, ip in [
+ ("rfc1918_10", "10.0.0.5"),
+ ("rfc1918_172", "172.16.3.4"),
+ ("rfc1918_192", "192.168.1.1"),
+ ("ipv6_loopback", "::1"),
+ ("ipv6_unique_local", "fd00::1"),
+ ("link_local", "169.254.0.1"),
+ ("malformed", "not-an-ip"),
+ ]
]
)
@patch("posthog.geoip.geoip")
- def test_returns_empty_for_non_public_ip(self, _name, ip, mock_geoip):
+ def test_returns_empty_for_non_public_ip(self, _name, lookup, ip, mock_geoip):
# geoip is mocked truthy, so an empty result can only come from the private/reserved guard,
# not a missing DB — and city() must never be reached for these ranges.
- self.assertEqual(get_geoip_location(ip), {})
+ self.assertEqual(lookup(ip), {})
mock_geoip.city.assert_not_called()
+ @parameterized.expand([("ipv4", "8.8.8.8"), ("ipv4_mapped_ipv6", "::ffff:8.8.8.8")])
+ @patch("posthog.geoip.geoip")
+ def test_properties_for_public_ip(self, _name, ip, mock_geoip):
+ # A mapped address takes the flags of its embedded IPv4 on our Python, so the guard must let it through.
+ mock_geoip.city.return_value = {"city": "NYC", "country_code": "US", "latitude": 40.7}
+ self.assertEqual(get_geoip_properties(ip), {"$geoip_city_name": "NYC", "$geoip_country_code": "US"})
+ mock_geoip.city.assert_called_once_with(ip)
+
@patch("posthog.geoip.geoip")
def test_returns_coordinates_and_country(self, mock_geoip):
mock_geoip.city.return_value = {"latitude": 40.7, "longitude": -74.0, "country_code": "US", "city": "NYC"}