This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit 5c10f60e63bc20c4e62c7d5b5d25b73ff384f24e Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 12:02:55 2026 +0100 [FIX] EncoderUtil#encodeAddressDomain: a domain literal cannot add recipients Since the fix of #77, a domain literal was written unchanged as long as it held only dtext, which includes "," and ";". LenientAddressParser stops its search for "]" at such a delimiter, so Mailbox("a", "[x,y]") written as "a@[x,y]" was read back as two mailboxes, and "team: a@[;];" ended its group early. The domain comes from values given to the builders, or from an address DefaultAddressParser accepted. The dtext characters a lenient reader takes for the end of an address (",", ";", "<", ">", "\"", "@") are now dropped from a domain literal, as they already are from any other malformed domain. ":" (IPv6 literals) and "(" ")", which the reader keeps inside a literal, are kept. --- .../org/apache/james/mime4j/codec/EncoderUtil.java | 28 +++++++++++++++++++--- .../apache/james/mime4j/codec/EncoderUtilTest.java | 8 +++++++ .../field/address/LenientAddressBuilderTest.java | 19 +++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java index 9613e717..316986c7 100644 --- a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java +++ b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java @@ -71,6 +71,8 @@ public class EncoderUtil { /** Characters that end or restructure an address in a header: never written raw in a domain */ private static final String DOMAIN_STRUCTURE_CHARS = ",;:<>()\"@\\"; + // Inside a domain literal: ":" (IPv6 literals) and "(" ")" are read back as dtext + private static final String DOMAIN_LITERAL_STRUCTURE_CHARS = ",;<>\"@"; private static BitSet initChars(String specials) { BitSet bs = new BitSet(128); @@ -164,8 +166,9 @@ public class EncoderUtil { /** * Encodes the domain of an address specification as described in RFC 5322 section 3.4.1. A - * dot-atom (non ASCII characters allowed, as in internationalized domains) or a domain literal - * is returned unchanged. From any other domain, such as the ones a lenient parser reads from + * dot-atom (non ASCII characters allowed, as in internationalized domains) is returned + * unchanged. From a domain literal, the dtext characters a lenient reader takes for the end + * of the address (",", ";", "<", ">", "\"", "@") are dropped. From any other domain, such as the ones a lenient parser reads from * malformed addresses, the characters that would change the structure of the header (e.g. a * "," adding recipients) are dropped. * @@ -177,9 +180,12 @@ public class EncoderUtil { // domain = dot-atom / domain-literal // domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] // dtext = %d33-90 / %d94-126 - if (isDotAtomText(domain, true) || isDomainLiteral(domain)) { + if (isDotAtomText(domain, true)) { return domain; } + if (isDomainLiteral(domain)) { + return removeChars(domain, DOMAIN_LITERAL_STRUCTURE_CHARS); + } StringBuilder sb = new StringBuilder(domain.length()); for (int idx = 0; idx < domain.length(); idx++) { char ch = domain.charAt(idx); @@ -190,6 +196,22 @@ public class EncoderUtil { return sb.toString(); } + private static String removeChars(String str, String chars) { + StringBuilder sb = null; + for (int idx = 0; idx < str.length(); idx++) { + char ch = str.charAt(idx); + if (chars.indexOf(ch) != -1) { + if (sb == null) { + sb = new StringBuilder(str.length()); + sb.append(str, 0, idx); + } + } else if (sb != null) { + sb.append(ch); + } + } + return sb == null ? str : sb.toString(); + } + private static boolean isDomainLiteral(String str) { final int length = str.length(); if (length < 2 || str.charAt(0) != '[' || str.charAt(length - 1) != ']') { diff --git a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java index 1b506f02..3229804a 100644 --- a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java +++ b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java @@ -365,6 +365,14 @@ public class EncoderUtilTest { Assert.assertEquals("a", EncoderUtil.encodeAddressDomain("a>")); } + @Test + public void domainLiteralsShouldLoseTheCharactersThatEndAnAddress() { + Assert.assertEquals("[]", EncoderUtil.encodeAddressDomain("[,]")); + Assert.assertEquals("[xy]", EncoderUtil.encodeAddressDomain("[x,y]")); + Assert.assertEquals("[]", EncoderUtil.encodeAddressDomain("[;<>\"@]")); + Assert.assertEquals("[IPv6:2001:db8::1]", EncoderUtil.encodeAddressDomain("[IPv6:2001:db8::1]")); + } + @Test public void harmlessMalformedDomainsShouldBeKept() { Assert.assertEquals("[", EncoderUtil.encodeAddressDomain("[")); diff --git a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java index 97d2ee8c..71423be5 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java @@ -666,4 +666,23 @@ public class LenientAddressBuilderTest { Assert.assertEquals(2, list.size()); Assert.assertEquals("[1.2.3.4]", ((Mailbox) list.get(1)).getDomain()); } + + @Test + public void domainLiteralHoldingACommaIsOneRecipient() { + String[][] mailboxes = {{"a", "[x,y]"}, {"", "[,]"}, {"a", "[;]"}, {"a", "[<b@c>]"}}; + for (String[] m : mailboxes) { + String header = AddressFormatter.DEFAULT.encode(new Mailbox(m[0], m[1])); + AddressList read = LenientAddressParser.DEFAULT.parseAddressList(header); + Assert.assertEquals(header, 1, read.flatten().size()); + } + } + + @Test + public void domainLiteralHoldingASemicolonKeepsTheGroupWhole() { + Group group = new Group("team", new Mailbox("a", "[;]"), new Mailbox("b", "c")); + String header = AddressFormatter.DEFAULT.encode(group); + AddressList read = LenientAddressParser.DEFAULT.parseAddressList(header); + Assert.assertEquals(header, 1, read.size()); + Assert.assertEquals(header, 2, read.flatten().size()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
