From de1f1a61f3ddbbe2e8f706f24bab05487ab4d88f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 3 Jun 2015 11:22:33 +0200 Subject: [PATCH] Ensure no null values are used when add/set headers. Motivation: We need to ensure we never allow to have null values set on headers, otherwise we will see a NPE during encoding them. Modifications: Add null check. Result: Correctly throw exception when a null header value is added/set --- .../netty/handler/codec/http/DefaultHttpHeaders.java | 6 +++--- .../io/netty/handler/codec/http/HttpHeadersTest.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java index 0711fb3916..2b70656508 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java @@ -29,6 +29,8 @@ import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Set; +import static io.netty.util.internal.ObjectUtil.checkNotNull; + public class DefaultHttpHeaders extends HttpHeaders { private static final int BUCKET_SIZE = 17; @@ -382,9 +384,7 @@ public class DefaultHttpHeaders extends HttpHeaders { } private static CharSequence toCharSequence(Object value) { - if (value == null) { - return null; - } + checkNotNull(value, "value"); if (value instanceof CharSequence) { return (CharSequence) value; } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java index 5d98726905..47302c7425 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java @@ -55,4 +55,16 @@ public class HttpHeadersTest { assertThat(HttpHeaders.equalsIgnoreCase("bar", null), is(false)); assertThat(HttpHeaders.equalsIgnoreCase("FoO", "fOo"), is(true)); } + + @Test(expected = NullPointerException.class) + public void testSetNullHeaderValueValidate() { + HttpHeaders headers = new DefaultHttpHeaders(true); + headers.set("test", (CharSequence) null); + } + + @Test(expected = NullPointerException.class) + public void testSetNullHeaderValueNotValidate() { + HttpHeaders headers = new DefaultHttpHeaders(false); + headers.set("test", (CharSequence) null); + } }