From ae9963a40cbf7714f3f30e0093ff41f2f200b6e8 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 21 Jul 2014 16:37:49 -0700 Subject: [PATCH] Fix NPE while decoding authority section of a DNS response Motivation: NullPointerException is raised when a DNS response conrains a resource record whose NAME is empty, which is the case for the authority section. Modification: Allow an empty name for DnsEntry. Only disallow an empty name for DnsQuestion. Result: Fixes #2686 --- .../io/netty/handler/codec/dns/DnsEntry.java | 3 -- .../netty/handler/codec/dns/DnsQuestion.java | 4 ++ .../handler/codec/dns/DnsResponseDecoder.java | 3 +- .../handler/codec/dns/DnsResponseTest.java | 48 ++++++++++++++----- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsEntry.java b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsEntry.java index 104d777cf3..94d77501d5 100644 --- a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsEntry.java +++ b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsEntry.java @@ -317,9 +317,6 @@ public class DnsEntry { if (name == null) { throw new NullPointerException("name"); } - if (name.isEmpty()) { - throw new IllegalArgumentException("name must not be left blank."); - } if ((type & 0xffff) != type) { throw new IllegalArgumentException("type must be an unsigned short."); } diff --git a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsQuestion.java b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsQuestion.java index 61721c87af..05ea266227 100644 --- a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsQuestion.java +++ b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsQuestion.java @@ -49,6 +49,10 @@ public final class DnsQuestion extends DnsEntry { */ public DnsQuestion(String name, int type, int qClass) { super(name, type, qClass); + + if (name.isEmpty()) { + throw new IllegalArgumentException("name must not be left blank."); + } } @Override diff --git a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsResponseDecoder.java b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsResponseDecoder.java index ea89379534..3b52a9ca9d 100644 --- a/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsResponseDecoder.java +++ b/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsResponseDecoder.java @@ -110,8 +110,9 @@ public class DnsResponseDecoder extends MessageToMessageDecoder buf.readerIndex(position); } if (name.length() == 0) { - return null; + return ""; } + return name.substring(0, name.length() - 1); } diff --git a/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsResponseTest.java b/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsResponseTest.java index 45895c17a1..8e205462ad 100644 --- a/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsResponseTest.java +++ b/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsResponseTest.java @@ -31,20 +31,42 @@ import java.net.InetSocketAddress; public class DnsResponseTest { private static final byte[][] packets = { - { 0, 1, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, - 111, 109, 0, 0, 1, 0, 1, -64, 12, 0, 1, 0, 1, 0, 0, 16, -113, 0, 4, -64, 0, 43, 10 }, - { 0, 1, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, - 111, 109, 0, 0, 28, 0, 1, -64, 12, 0, 28, 0, 1, 0, 0, 69, -8, 0, 16, 32, 1, 5, 0, 0, -120, 2, 0, 0, - 0, 0, 0, 0, 0, 0, 16 }, - { 0, 2, -127, -128, 0, 1, 0, 0, 0, 1, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, - 111, 109, 0, 0, 15, 0, 1, -64, 16, 0, 6, 0, 1, 0, 0, 3, -43, 0, 45, 3, 115, 110, 115, 3, 100, 110, - 115, 5, 105, 99, 97, 110, 110, 3, 111, 114, 103, 0, 3, 110, 111, 99, -64, 49, 119, -4, 39, 112, 0, - 0, 28, 32, 0, 0, 14, 16, 0, 18, 117, 0, 0, 0, 14, 16 }, - { 0, 3, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, 99, - 111, 109, 0, 0, 16, 0, 1, -64, 12, 0, 16, 0, 1, 0, 0, 84, 75, 0, 12, 11, 118, 61, 115, 112, 102, - 49, 32, 45, 97, 108, 108 } }; + { + 0, 1, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, + 99, 111, 109, 0, 0, 1, 0, 1, -64, 12, 0, 1, 0, 1, 0, 0, 16, -113, 0, 4, -64, 0, 43, 10 + }, + { + 0, 1, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, + 99, 111, 109, 0, 0, 28, 0, 1, -64, 12, 0, 28, 0, 1, 0, 0, 69, -8, 0, 16, 32, 1, 5, 0, 0, -120, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 16 + }, + { + 0, 2, -127, -128, 0, 1, 0, 0, 0, 1, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, + 99, 111, 109, 0, 0, 15, 0, 1, -64, 16, 0, 6, 0, 1, 0, 0, 3, -43, 0, 45, 3, 115, 110, 115, 3, 100, + 110, 115, 5, 105, 99, 97, 110, 110, 3, 111, 114, 103, 0, 3, 110, 111, 99, -64, 49, 119, -4, 39, + 112, 0, 0, 28, 32, 0, 0, 14, 16, 0, 18, 117, 0, 0, 0, 14, 16 + }, + { + 0, 3, -127, -128, 0, 1, 0, 1, 0, 0, 0, 0, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, + 99, 111, 109, 0, 0, 16, 0, 1, -64, 12, 0, 16, 0, 1, 0, 0, 84, 75, 0, 12, 11, 118, 61, 115, 112, + 102, 49, 32, 45, 97, 108, 108 + }, + { + -105, 19, -127, 0, 0, 1, 0, 0, 0, 13, 0, 0, 2, 104, 112, 11, 116, 105, 109, 98, 111, 117, 100, 114, + 101, 97, 117, 3, 111, 114, 103, 0, 0, 1, 0, 1, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 20, 1, 68, 12, 82, + 79, 79, 84, 45, 83, 69, 82, 86, 69, 82, 83, 3, 78, 69, 84, 0, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, + 70, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 69, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, + 1, 75, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 67, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, + 4, 1, 76, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 71, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, + 0, 4, 1, 73, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 66, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, + 0, 0, 4, 1, 77, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 65, -64, 49, 0, 0, 2, 0, 1, 0, 7, + -23, 0, 0, 4, 1, 72, -64, 49, 0, 0, 2, 0, 1, 0, 7, -23, 0, 0, 4, 1, 74, -64, 49 + } + }; - private static final byte[] malformedLoopPacket = { 0, 4, -127, -128, 0, 1, 0, 0, 0, 0, 0, 0, -64, 12, 0, 1, 0, 1 }; + private static final byte[] malformedLoopPacket = { + 0, 4, -127, -128, 0, 1, 0, 0, 0, 0, 0, 0, -64, 12, 0, 1, 0, 1 + }; @Test public void readResponseTest() throws Exception {