From a598c3b69b55f930b91a8265f42d1a3cceb75ddd Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 4 May 2018 13:38:08 +0200 Subject: [PATCH] Enforce sane upper limit for TTL in DefaultDnsCache. (#7907) Motivation: In b47fb817991b42ec8808c7d26538f3f2464e1fa6 we limited the max supported delay to match what our internal implementat can support. Because of this it was possible that DefaultDnsCache produced an IllegalArgumentException when it tried to schedule a expiration > 3 years. Modifications: Limit the max supported TTL to 2 years which is safe for all our EventLoop implementations. Result: No more exceptions when adding records to the cache with a huge TTL. --- .../netty/resolver/dns/DefaultDnsCache.java | 9 ++++++--- .../resolver/dns/DefaultDnsCacheTest.java | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DefaultDnsCache.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DefaultDnsCache.java index 4d963dfa87..2bc509fec8 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DefaultDnsCache.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DefaultDnsCache.java @@ -43,6 +43,9 @@ public class DefaultDnsCache implements DnsCache { private final ConcurrentMap resolveCache = PlatformDependent.newConcurrentHashMap(); + // Two years are supported by all our EventLoop implementations and so safe to use as maximum. + // See also: https://github.com/netty/netty/commit/b47fb817991b42ec8808c7d26538f3f2464e1fa6 + private static final int MAX_SUPPORTED_TTL_SECS = (int) TimeUnit.DAYS.toSeconds(365 * 2); private final int minTtl; private final int maxTtl; private final int negativeTtl; @@ -52,7 +55,7 @@ public class DefaultDnsCache implements DnsCache { * and doesn't cache negative responses. */ public DefaultDnsCache() { - this(0, Integer.MAX_VALUE, 0); + this(0, MAX_SUPPORTED_TTL_SECS, 0); } /** @@ -141,7 +144,7 @@ public class DefaultDnsCache implements DnsCache { if (maxTtl == 0 || !emptyAdditionals(additionals)) { return e; } - cache0(e, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop); + cache0(e, Math.max(minTtl, Math.min(MAX_SUPPORTED_TTL_SECS, (int) Math.min(maxTtl, originalTtl))), loop); return e; } @@ -156,7 +159,7 @@ public class DefaultDnsCache implements DnsCache { return e; } - cache0(e, negativeTtl, loop); + cache0(e, Math.min(MAX_SUPPORTED_TTL_SECS, negativeTtl), loop); return e; } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java index 7d519e8793..6fa8bc7bcb 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DefaultDnsCacheTest.java @@ -18,6 +18,7 @@ package io.netty.resolver.dns; import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.NetUtil; import org.junit.Assert; import org.junit.Test; @@ -55,4 +56,23 @@ public class DefaultDnsCacheTest { group.shutdownGracefully(); } } + + @Test + public void testExpireWithDifferentTTLs() { + testExpireWithTTL0(1); + testExpireWithTTL0(1000); + testExpireWithTTL0(1000000); + } + + private static void testExpireWithTTL0(int days) { + EventLoopGroup group = new NioEventLoopGroup(1); + + try { + EventLoop loop = group.next(); + final DefaultDnsCache cache = new DefaultDnsCache(); + Assert.assertNotNull(cache.cache("netty.io", null, NetUtil.LOCALHOST, days, loop)); + } finally { + group.shutdownGracefully(); + } + } }