Skip to content

Commit 2a613d3

Browse files
committed
Name the malformed domain name in the error message about that domain.
1 parent 7767a29 commit 2a613d3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

core/src/main/java/org/apache/james/core/Domain.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,26 @@ public static Domain of(String domain) {
5555
Preconditions.checkArgument(domain.length() <= MAXIMUM_DOMAIN_LENGTH,
5656
"Domain name length should not exceed %s characters", MAXIMUM_DOMAIN_LENGTH);
5757

58-
String domainWithoutBrackets = IDN.toASCII(removeBrackets(domain), IDN.ALLOW_UNASSIGNED);
58+
String domainWithoutBrackets;
59+
try {
60+
domainWithoutBrackets = IDN.toASCII(removeBrackets(domain), IDN.ALLOW_UNASSIGNED);
61+
} catch (IllegalArgumentException e) {
62+
// IDN.toASCII's own message can be cryptic ("Empty label is not
63+
// a legal name", "A prohibited code point was found in the
64+
// input..."). Let's save wear and tear on the poor developer's
65+
// brain.
66+
throw new IllegalArgumentException(
67+
"Domain '" + domain + "' is invalid according to IDNA: " + e.getMessage(), e);
68+
}
5969
Preconditions.checkArgument(PART_CHAR_MATCHER.matchesAllOf(domainWithoutBrackets),
6070
"Domain parts ASCII chars must be a-z A-Z 0-9 - or _ in %s", domain);
6171

6272
if (domainWithoutBrackets.startsWith("xn--") ||
6373
domainWithoutBrackets.contains(".xn--")) {
6474
domainWithoutBrackets = IDN.toUnicode(domainWithoutBrackets);
6575
Preconditions.checkArgument(!domainWithoutBrackets.startsWith("xn--") &&
66-
!domainWithoutBrackets.contains(".xn--"));
76+
!domainWithoutBrackets.contains(".xn--"),
77+
"A-label could not be decoded to Unicode in %s", domain);
6778
}
6879

6980
int pos = 0;

core/src/test/java/org/apache/james/core/DomainTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ void testMalformedDomains(String malformed) {
7676
.as("rejecting malformed domain " + malformed)
7777
.isInstanceOf(IllegalArgumentException.class);
7878
}
79+
80+
@ParameterizedTest
81+
@MethodSource("malformedDomains")
82+
void exceptionForMalformedDomainShouldNameTheOffendingInput(String malformed) {
83+
// Without the offending input in the message, "Domain invalid
84+
// according to IDNA" leaves a future debugger guessing what
85+
// string actually triggered it.
86+
assertThatThrownBy(() -> Domain.of(malformed))
87+
.hasMessageContaining(malformed);
88+
}
7989
}
8090

8191

0 commit comments

Comments
 (0)