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-jdkim.git
commit e3114292927fc83d5f5a6103706a7806c342a184 Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Oct 30 08:38:35 2026 +0100 [FIX] PublicKeyRecordImpl: let the g= wildcard match line terminators The wildcard was translated to ".*" without DOTALL, so the default g=* did not match an i= local part holding CR, LF or U+0085 (the second byte of several UTF-8 characters such as C3 85), and such signatures were rejected as "inapplicable key identity". The literal parts are now quoted with Pattern.quote instead of a hand-written \Q...\E. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- .../james/jdkim/tagvalue/PublicKeyRecordImpl.java | 12 +++++------- .../java/org/apache/james/jdkim/DKIMVerifierTest.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/main/src/main/java/org/apache/james/jdkim/tagvalue/PublicKeyRecordImpl.java b/main/src/main/java/org/apache/james/jdkim/tagvalue/PublicKeyRecordImpl.java index c5cb7b8..ff2739e 100644 --- a/main/src/main/java/org/apache/james/jdkim/tagvalue/PublicKeyRecordImpl.java +++ b/main/src/main/java/org/apache/james/jdkim/tagvalue/PublicKeyRecordImpl.java @@ -141,14 +141,12 @@ public class PublicKeyRecordImpl extends TagValue implements PublicKeyRecord { throw new IllegalStateException( "Invalid granularity using more than one wildcard: " + g); - String pattern = "^\\Q" + g.subSequence(0, pStar).toString() - + "\\E.*\\Q" - + g.subSequence(pStar + 1, g.length()).toString() + "\\E$"; - return Pattern.compile(pattern); + // DOTALL: the wildcard matches any local part, line terminators included + String pattern = "^" + Pattern.quote(g.substring(0, pStar)) + + ".*" + Pattern.quote(g.substring(pStar + 1)) + "$"; + return Pattern.compile(pattern, Pattern.DOTALL); } else { - // TODO we need some escaping. On Java 5 we have Pattern.quote that - // is better - return Pattern.compile("^\\Q" + g + "\\E$"); + return Pattern.compile("^" + Pattern.quote(g) + "$"); } } diff --git a/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java b/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java index 9667926..62ae643 100644 --- a/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java +++ b/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java @@ -19,6 +19,7 @@ package org.apache.james.jdkim; +import static org.assertj.core.api.Assertions.assertThatCode; import org.apache.james.jdkim.impl.Message; import java.util.Base64; import java.security.Signature; @@ -172,4 +173,21 @@ public class DKIMVerifierTest { assertThat(verifier.hasAnyValidSignature()).isFalse(); assertThat(verifier.getResults()).hasSize(1); } + + @Test + public void defaultGranularityShouldMatchAnyLocalPart() throws Exception { + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; p=AAAA"); + SignatureRecordImpl sign = new SignatureRecordImpl( + "v=1; a=rsa-sha256; d=example.com; s=sel; h=from; bh=AA==; b=AA==; [email protected]"); + + assertThatCode(() -> DKIMVerifier.apply(key, sign)).doesNotThrowAnyException(); + } + + @Test + public void granularityWildcardShouldMatchLineTerminators() throws Exception { + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=a*b; p=AAAA"); + + assertThat(key.getGranularityPattern().matcher("a\r\nb").matches()).isTrue(); + assertThat(key.getGranularityPattern().matcher("ab\n").matches()).isFalse(); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
