Skip to content

Commit 0697e5b

Browse files
committed
Have constructing relative Name with length() == MAXNAME fail
The Name() constructor will currently fail if it's length() would end up being longer than MAXNAME (255). However, there is an edge case where a relative name can be exactly MAXNAME long. This is legal but the lookup mechanism will then fail to make the name absolute by appending an empty label to be able to use it for lookups.
1 parent 8535a5b commit 0697e5b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/main/java/org/xbill/DNS/Name.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ public Name(String s, Name origin) throws TextParseException {
271271
if (origin != null && !absolute) {
272272
appendFromString(s, origin.name, origin.offset(0), origin.labels);
273273
}
274+
// A relative name that is MAXNAME octets long is a strange and wonderful thing.
275+
// Not technically in violation, but it can not be used for queries as it needs
276+
// to be made absolute by appending at the very least the an empty label at the
277+
// end, which there is no room for. To make life easier for everyone, let's only
278+
// allow Names that are MAXNAME long if they are absolute.
279+
if (!absolute && length() == MAXNAME) {
280+
throw parseException(s, "Name too long");
281+
}
274282
}
275283

276284
/**

src/test/java/org/xbill/DNS/NameTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//
3535
package org.xbill.DNS;
3636

37+
import static java.lang.String.format;
3738
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -45,6 +46,8 @@
4546
import static org.junit.jupiter.api.Assertions.assertTrue;
4647

4748
import java.io.IOException;
49+
import java.util.stream.Collectors;
50+
import java.util.stream.IntStream;
4851
import org.junit.jupiter.api.BeforeEach;
4952
import org.junit.jupiter.api.Test;
5053

@@ -286,15 +289,26 @@ void ctor_toobig_label() {
286289
}
287290

288291
@Test
289-
void ctor_max_length_rel() throws TextParseException {
290-
// relative name with three 63-char labels and a 62-char label
292+
void ctor_length_too_long_rel() {
293+
// We want to fail to crate this as there is no way to make a Name that long absolute,
294+
// which means that it can not be used for lookups or updates.
295+
String label63 = IntStream.range(0, 63).mapToObj(i -> "a").collect(Collectors.joining());
296+
String label62 = IntStream.range(0, 62).mapToObj(i -> "a").collect(Collectors.joining());
297+
assertThrows(
298+
TextParseException.class,
299+
() -> new Name(format("%s.%s.%s.%s", label63, label63, label63, label62)));
300+
}
301+
302+
@Test
303+
void ctor_longest_allowed_rel() throws TextParseException {
304+
// relative name with three 63-char labels and a 61-char label
291305
Name n =
292306
new Name(
293-
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc.dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
307+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
294308
assertFalse(n.isAbsolute());
295309
assertFalse(n.isWild());
296310
assertEquals(4, n.labels());
297-
assertEquals(255, n.length());
311+
assertEquals(254, n.length());
298312
}
299313

300314
@Test

0 commit comments

Comments
 (0)