GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge commit from fork · netty/netty@6459a28

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-dns/src
    • main/java/io/netty/handler/codec/dns
      • DnsCodecUtil.java
    • test/java/io/netty/handler/codec/dns
      • DnsCodecUtilTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java‎

Copy file name to clipboardExpand all lines: codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java
+25-3Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufUtil;
2121
import io.netty.handler.codec.CorruptedFrameException;
22+
import io.netty.handler.codec.TooLongFrameException;
2223
import io.netty.util.CharsetUtil;
2324

2425
import static io.netty.handler.codec.dns.DefaultDnsRecordDecoder.*;
@@ -35,14 +36,27 @@ static void encodeDomainName(String name, ByteBuf buf) {
3536
return;
3637
}
3738

39+
int totalLength = 0;
3840
final String[] labels = name.split("\\.");
3941
for (String label : labels) {
4042
final int labelLen = label.length();
4143
if (labelLen == 0) {
42-
// zero-length label means the end of the name.
43-
break;
44+
throw new IllegalArgumentException("DNS name contains empty label: " + name);
45+
}
46+
if (labelLen > 63) {
47+
throw new IllegalArgumentException(
48+
"DNS label length " + labelLen + " exceeds maximum of 63: " + name);
49+
}
50+
int idx = label.indexOf('\0');
51+
if (idx != -1) {
52+
throw new IllegalArgumentException(
53+
"DNS label contains null byte at index " + idx);
54+
}
55+
totalLength += 1 + labelLen;
56+
if (totalLength > 255) {
57+
throw new IllegalArgumentException(
58+
"DNS name exceeds maximum length of 255: " + name);
4459
}
45-
4660
buf.writeByte(labelLen);
4761
ByteBufUtil.writeAscii(buf, label);
4862
}
@@ -95,8 +109,16 @@ static String decodeDomainName(ByteBuf in) {
95109
if (!in.isReadable(len)) {
96110
throw new CorruptedFrameException("truncated label in a name");
97111
}
112+
// See https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.4
113+
if (len > 63) {
114+
throw new TooLongFrameException("label must be <= 63 but was " + len);
115+
}
98116
name.append(in.toString(in.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
99117
in.skipBytes(len);
118+
// See https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.4
119+
if (name.length() > 255) {
120+
throw new TooLongFrameException("domain name must be <= 255 but was " + name.length());
121+
}
100122
} else { // len == 0
101123
break;
102124
}
Collapse file

‎codec-dns/src/test/java/io/netty/handler/codec/dns/DnsCodecUtilTest.java‎

Copy file name to clipboard
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.dns;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import io.netty.handler.codec.TooLongFrameException;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
25+
public class DnsCodecUtilTest {
26+
27+
@Test
28+
void rejectTooLongLabelWhileDecoding() {
29+
ByteBuf buf = Unpooled.buffer(256);
30+
// 63 is the maximum label length
31+
writeLabel(buf, 64);
32+
writeLabel(buf, 3);
33+
buf.writeByte(0);
34+
35+
assertThrows(TooLongFrameException.class, () -> DnsCodecUtil.decodeDomainName(buf));
36+
buf.release();
37+
}
38+
39+
@Test
40+
void rejectTooLongDomainNameWhileDecoding() {
41+
// 255 is the maximum domain name
42+
ByteBuf buf = Unpooled.buffer(512);
43+
writeLabel(buf, 50);
44+
writeLabel(buf, 50);
45+
writeLabel(buf, 50);
46+
writeLabel(buf, 50);
47+
writeLabel(buf, 56);
48+
buf.writeByte(0);
49+
50+
assertThrows(TooLongFrameException.class, () -> DnsCodecUtil.decodeDomainName(buf));
51+
buf.release();
52+
}
53+
54+
@Test
55+
void rejectTooLongLabelWhileEncoding() {
56+
ByteBuf buf = Unpooled.buffer(256);
57+
// 63 is the maximum label length
58+
StringBuilder sb = new StringBuilder();
59+
appendLabel(sb, 64);
60+
assertThrows(IllegalArgumentException.class, () -> DnsCodecUtil.encodeDomainName(sb.toString(), buf));
61+
buf.release();
62+
}
63+
64+
@Test
65+
void rejectTooLongDomainNameWhileEncoding() {
66+
ByteBuf buf = Unpooled.buffer(256);
67+
// 255 is the maximum domain name
68+
StringBuilder sb = new StringBuilder();
69+
appendLabel(sb, 50);
70+
appendLabel(sb, 50);
71+
appendLabel(sb, 50);
72+
appendLabel(sb, 50);
73+
appendLabel(sb, 56);
74+
75+
assertThrows(IllegalArgumentException.class, () -> DnsCodecUtil.encodeDomainName(sb.toString(), buf));
76+
buf.release();
77+
}
78+
79+
private static void writeLabel(ByteBuf buf, int length) {
80+
buf.writeByte(length);
81+
for (int i = 1; i <= length; i++) {
82+
buf.writeByte(i);
83+
}
84+
}
85+
86+
private static void appendLabel(StringBuilder sb, int length) {
87+
for (int i = 0; i < length; i++) {
88+
sb.append('a');
89+
}
90+
sb.append('.');
91+
}
92+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.