GeezerVison is showing github.com — Clarify documentation on max HTTP header size (#14672) · netty/netty@4f95cae
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/http test/java/io/netty/handler/codec/http
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpDecoderConfig.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,6 +76,9 @@ public int getMaxHeaderSize() { | 76 | 76 | /** | 77 | 77 | * Set the maximum line length of header lines. | 78 | 78 | * This limits how much memory Netty will use when parsing HTTP header key-value pairs. | | 79 | + * The limit applies to the sum of all the headers, so it applies equally to many short header-lines, | | 80 | + * or fewer but longer header lines. | | 81 | + * <p> | 79 | 82 | * You would typically set this to the same value as {@link #setMaxInitialLineLength(int)}. | 80 | 83 | * | 81 | 84 | * @param maxHeaderSize The maximum length, in bytes. | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,12 +126,22 @@ public class HttpRequestDecoder extends HttpObjectDecoder { | 126 | 126 | * Creates a new instance with the default | 127 | 127 | * {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and | 128 | 128 | * {@code maxChunkSize (8192)}. | | 129 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 130 | + * the configuration parameters. | 129 | 131 | */ | 130 | 132 | public HttpRequestDecoder() { | 131 | 133 | } | 132 | 134 |
| 133 | 135 | /** | 134 | 136 | * Creates a new instance with the specified parameters. | | 137 | + * | | 138 | + * @param maxInitialLineLength the initial size of the temporary buffer used when parsing the lines of the | | 139 | + * HTTP headers. | | 140 | + * @param maxHeaderSize the maximum permitted combined size of all headers in any one request. | | 141 | + * @param maxChunkSize The maximum amount of data that the decoder will buffer | | 142 | + * before sending chunks down the pipeline. | | 143 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 144 | + * the configuration parameters. | 135 | 145 | */ | 136 | 146 | public HttpRequestDecoder( | 137 | 147 | int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) { | @@ -144,6 +154,8 @@ public HttpRequestDecoder( | 144 | 154 | /** | 145 | 155 | * @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor, | 146 | 156 | * to always have header validation enabled. | | 157 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 158 | + * the configuration parameters. | 147 | 159 | */ | 148 | 160 | @Deprecated | 149 | 161 | public HttpRequestDecoder( | @@ -154,6 +166,8 @@ public HttpRequestDecoder( | 154 | 166 | /** | 155 | 167 | * @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor, | 156 | 168 | * to always have header validation enabled. | | 169 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 170 | + * the configuration parameters. | 157 | 171 | */ | 158 | 172 | @Deprecated | 159 | 173 | public HttpRequestDecoder( | @@ -166,6 +180,8 @@ public HttpRequestDecoder( | 166 | 180 | /** | 167 | 181 | * @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor, | 168 | 182 | * to always have header validation enabled. | | 183 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 184 | + * the configuration parameters. | 169 | 185 | */ | 170 | 186 | @Deprecated | 171 | 187 | public HttpRequestDecoder( | @@ -178,6 +194,8 @@ public HttpRequestDecoder( | 178 | 194 | /** | 179 | 195 | * @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor, | 180 | 196 | * to always have header validation enabled. | | 197 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 198 | + * the configuration parameters. | 181 | 199 | */ | 182 | 200 | @Deprecated | 183 | 201 | public HttpRequestDecoder( | @@ -189,6 +207,8 @@ public HttpRequestDecoder( | 189 | 207 |
| 190 | 208 | /** | 191 | 209 | * Creates a new instance with the specified configuration. | | 210 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 211 | + * the configuration parameters. | 192 | 212 | */ | 193 | 213 | public HttpRequestDecoder(HttpDecoderConfig config) { | 194 | 214 | super(config); | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -124,12 +124,22 @@ public class HttpResponseDecoder extends HttpObjectDecoder { | 124 | 124 | * Creates a new instance with the default | 125 | 125 | * {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and | 126 | 126 | * {@code maxChunkSize (8192)}. | | 127 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 128 | + * the configuration parameters. | 127 | 129 | */ | 128 | 130 | public HttpResponseDecoder() { | 129 | 131 | } | 130 | 132 |
| 131 | 133 | /** | 132 | 134 | * Creates a new instance with the specified parameters. | | 135 | + * | | 136 | + * @param maxInitialLineLength the initial size of the temporary buffer used when parsing the lines of the | | 137 | + * HTTP headers. | | 138 | + * @param maxHeaderSize the maximum permitted combined size of all headers in any one response. | | 139 | + * @param maxChunkSize The maximum amount of data that the decoder will buffer | | 140 | + * before sending chunks down the pipeline. | | 141 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 142 | + * the configuration parameters. | 133 | 143 | */ | 134 | 144 | public HttpResponseDecoder( | 135 | 145 | int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) { | @@ -141,6 +151,8 @@ public HttpResponseDecoder( | 141 | 151 |
| 142 | 152 | /** | 143 | 153 | * @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor. | | 154 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 155 | + * the configuration parameters. | 144 | 156 | */ | 145 | 157 | @Deprecated | 146 | 158 | public HttpResponseDecoder( | @@ -150,6 +162,8 @@ public HttpResponseDecoder( | 150 | 162 |
| 151 | 163 | /** | 152 | 164 | * @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor. | | 165 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 166 | + * the configuration parameters. | 153 | 167 | */ | 154 | 168 | @Deprecated | 155 | 169 | public HttpResponseDecoder( | @@ -161,6 +175,8 @@ public HttpResponseDecoder( | 161 | 175 |
| 162 | 176 | /** | 163 | 177 | * @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor. | | 178 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 179 | + * the configuration parameters. | 164 | 180 | */ | 165 | 181 | @Deprecated | 166 | 182 | public HttpResponseDecoder( | @@ -172,6 +188,8 @@ public HttpResponseDecoder( | 172 | 188 |
| 173 | 189 | /** | 174 | 190 | * @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor. | | 191 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 192 | + * the configuration parameters. | 175 | 193 | */ | 176 | 194 | @Deprecated | 177 | 195 | public HttpResponseDecoder( | @@ -183,6 +201,8 @@ public HttpResponseDecoder( | 183 | 201 |
| 184 | 202 | /** | 185 | 203 | * Creates a new instance with the specified configuration. | | 204 | + * @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of | | 205 | + * the configuration parameters. | 186 | 206 | */ | 187 | 207 | public HttpResponseDecoder(HttpDecoderConfig config) { | 188 | 208 | super(config); | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -381,6 +381,33 @@ public void testTooLargeHeaders() { | 381 | 381 | assertFalse(channel.finish()); | 382 | 382 | } | 383 | 383 |
| | 384 | + @Test | | 385 | + void testTotalHeaderLimit() throws Exception { | | 386 | + String requestStr = "GET /some/path HTTP/1.1\r\n" + | | 387 | + "Host: a.b\r\n" + // 9 content bytes | | 388 | + "a1: b\r\n" + // + 5 = 14 bytes, | | 389 | + "a2: b\r\n\r\n"; // + 5 = 19 bytes | | 390 | + | | 391 | + // Decoding with a max header size of 18 bytes must fail: | | 392 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder(1024, 18, 1024)); | | 393 | + assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII))); | | 394 | + HttpRequest request = channel.readInbound(); | | 395 | + assertTrue(request.decoderResult().isFailure()); | | 396 | + assertInstanceOf(TooLongHttpHeaderException.class, request.decoderResult().cause()); | | 397 | + assertFalse(channel.finish()); | | 398 | + | | 399 | + // Decoding with a max header size of 19 must pass: | | 400 | + channel = new EmbeddedChannel(new HttpRequestDecoder(1024, 19, 1024)); | | 401 | + assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII))); | | 402 | + request = channel.readInbound(); | | 403 | + assertTrue(request.decoderResult().isSuccess()); | | 404 | + assertEquals("a.b", request.headers().get("Host")); | | 405 | + assertEquals("b", request.headers().get("a1")); | | 406 | + assertEquals("b", request.headers().get("a2")); | | 407 | + assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, channel.readInbound()); | | 408 | + assertFalse(channel.finish()); | | 409 | + } | | 410 | + | 384 | 411 | @Test | 385 | 412 | public void testHeaderNameStartsWithControlChar1c() { | 386 | 413 | testHeaderNameStartsWithControlChar(0x1c); | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 40 | 40 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; | 41 | 41 | import static org.junit.jupiter.api.Assertions.assertEquals; | 42 | 42 | import static org.junit.jupiter.api.Assertions.assertFalse; | | 43 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; | 43 | 44 | import static org.junit.jupiter.api.Assertions.assertNull; | 44 | 45 | import static org.junit.jupiter.api.Assertions.assertTrue; | 45 | 46 |
| @@ -108,6 +109,34 @@ public void testMaxHeaderSize2() { | 108 | 109 | assertNull(ch.readInbound()); | 109 | 110 | } | 110 | 111 |
| | 112 | + @Test | | 113 | + void testTotalHeaderLimit() throws Exception { | | 114 | + String requestStr = "HTTP/1.1 200 OK\r\n" + | | 115 | + "Server: X\r\n" + // 9 content bytes | | 116 | + "a1: b\r\n" + // + 5 = 14 bytes, | | 117 | + "a2: b\r\n\r\n"; // + 5 = 19 bytes | | 118 | + | | 119 | + // Decoding with a max header size of 18 bytes must fail: | | 120 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseDecoder(1024, 18, 1024)); | | 121 | + assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII))); | | 122 | + HttpResponse response = channel.readInbound(); | | 123 | + assertTrue(response.decoderResult().isFailure()); | | 124 | + assertInstanceOf(TooLongHttpHeaderException.class, response.decoderResult().cause()); | | 125 | + assertFalse(channel.finish()); | | 126 | + | | 127 | + // Decoding with a max header size of 19 must pass: | | 128 | + channel = new EmbeddedChannel(new HttpResponseDecoder(1024, 19, 1024)); | | 129 | + assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII))); | | 130 | + response = channel.readInbound(); | | 131 | + assertTrue(response.decoderResult().isSuccess()); | | 132 | + assertEquals("X", response.headers().get("Server")); | | 133 | + assertEquals("b", response.headers().get("a1")); | | 134 | + assertEquals("b", response.headers().get("a2")); | | 135 | + channel.close(); | | 136 | + assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, channel.readInbound()); | | 137 | + assertFalse(channel.finish()); | | 138 | + } | | 139 | + | 111 | 140 | @Test | 112 | 141 | public void testResponseChunked() { | 113 | 142 | EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder()); | |
|