From b341d78ca8caabcf7c4bd528ed72306b25050eb1 Mon Sep 17 00:00:00 2001 From: rpanchyk Date: Wed, 12 Aug 2020 21:23:13 +0300 Subject: [PATCH] Improve IpAddressHelper for public address determining (#857) --- .../server/auction/IpAddressHelper.java | 2 ++ .../server/auction/IpAddressHelperTest.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/org/prebid/server/auction/IpAddressHelper.java b/src/main/java/org/prebid/server/auction/IpAddressHelper.java index e5a263ea508..2e1c528957d 100644 --- a/src/main/java/org/prebid/server/auction/IpAddressHelper.java +++ b/src/main/java/org/prebid/server/auction/IpAddressHelper.java @@ -112,6 +112,8 @@ private boolean isIpPublic(IPAddress ipAddress) { return ipAddress != null && !ipAddress.isLocal() && !ipAddress.isLoopback() + && !ipAddress.isMulticast() + && !ipAddress.isMax() && ipv6LocalNetworkMaskAddresses.stream().noneMatch(network -> network.contains(ipAddress)); } } diff --git a/src/test/java/org/prebid/server/auction/IpAddressHelperTest.java b/src/test/java/org/prebid/server/auction/IpAddressHelperTest.java index 6edc3522531..67527e18333 100644 --- a/src/test/java/org/prebid/server/auction/IpAddressHelperTest.java +++ b/src/test/java/org/prebid/server/auction/IpAddressHelperTest.java @@ -81,11 +81,41 @@ public void toIpAddressShouldReturnNullIfIpIsV4AndSiteLocal() { assertThat(ipAddressHelper.toIpAddress("192.168.0.1")).isNull(); } + @Test + public void toIpAddressShouldReturnNullIfIpIsV4AndMulticast() { + assertThat(ipAddressHelper.toIpAddress("224.0.0.0/4")).isNull(); + } + + @Test + public void toIpAddressShouldReturnNullIfIpIsV4AndZero() { + assertThat(ipAddressHelper.toIpAddress("0.0.0.0")).isNull(); + } + + @Test + public void toIpAddressShouldReturnNullIfIpIsV4AndMax() { + assertThat(ipAddressHelper.toIpAddress("255.255.255.255")).isNull(); + } + @Test public void toIpAddressShouldReturnNullIfIpIsV6AndLocal() { assertThat(ipAddressHelper.toIpAddress("fc00:0000:0000:0000:0000:0000:0000:0001")).isNull(); } + @Test + public void toIpAddressShouldReturnNullIfIpIsV6AndMulticast() { + assertThat(ipAddressHelper.toIpAddress("ff00::/64")).isNull(); + } + + @Test + public void toIpAddressShouldReturnNullIfIpIsV6AndZero() { + assertThat(ipAddressHelper.toIpAddress("0000:0000:0000:0000:0000:0000:0000:0000")).isNull(); + } + + @Test + public void toIpAddressShouldReturnNullIfIpIsV6AndMax() { + assertThat(ipAddressHelper.toIpAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")).isNull(); + } + @Test public void toIpAddressShouldReturnIpV4Address() { assertThat(ipAddressHelper.toIpAddress("12.34.56.78")).isEqualTo(IpAddress.of("12.34.56.78", IpAddress.IP.v4));