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 8efad020eecb12900601c1a0275831d9d5606ad6 Author: Benoit TELLIER <[email protected]> AuthorDate: Wed Nov 25 09:20:31 2026 +0100 [FIX] PublicKeyRecordImpl: match g= without a backtracking regex The g= wildcard was matched against the i= local part with "^prefix.*suffix$": the ".*" gives back one character at a time and the suffix is compared at each position, so a long g= and a long local part take quadratic time. Add PublicKeyRecord.isGranularityMatching, implemented in PublicKeyRecordImpl by comparing the prefix and suffix around the wildcard, and use it in DKIMVerifier.apply. getGranularityPattern is kept. --- .../java/org/apache/james/jdkim/DKIMVerifier.java | 6 +-- .../apache/james/jdkim/api/PublicKeyRecord.java | 8 ++++ .../james/jdkim/tagvalue/PublicKeyRecordImpl.java | 26 ++++++++++++ .../org/apache/james/jdkim/DKIMVerifierTest.java | 47 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java b/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java index 9c63055..1fa457d 100644 --- a/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java +++ b/main/src/main/java/org/apache/james/jdkim/DKIMVerifier.java @@ -145,11 +145,9 @@ public class DKIMVerifier { throw new PermFailException("inapplicable key: service type (s=) does not include email", sign); } - if (!pkr.getGranularityPattern().matcher(sign.getIdentityLocalPart()) - .matches()) { + if (!pkr.isGranularityMatching(sign.getIdentityLocalPart())) { throw new PermFailException("inapplicable key identity local=" - + sign.getIdentityLocalPart() + " Pattern: " - + pkr.getGranularityPattern().pattern(), sign); + + sign.getIdentityLocalPart() + " does not match the key granularity", sign); } if (!pkr.isHashMethodSupported(sign.getHashMethod())) { diff --git a/main/src/main/java/org/apache/james/jdkim/api/PublicKeyRecord.java b/main/src/main/java/org/apache/james/jdkim/api/PublicKeyRecord.java index da8f585..270c052 100644 --- a/main/src/main/java/org/apache/james/jdkim/api/PublicKeyRecord.java +++ b/main/src/main/java/org/apache/james/jdkim/api/PublicKeyRecord.java @@ -55,6 +55,14 @@ public interface PublicKeyRecord { Pattern getGranularityPattern(); + /** + * @param localPart the local part of the signature identity (i=) + * @return true if the granularity (g=) of this key matches it + */ + default boolean isGranularityMatching(CharSequence localPart) { + return getGranularityPattern().matcher(localPart).matches(); + } + PublicKey getPublicKey(); List<CharSequence> getFlags(); 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 f35365e..cbf1da1 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 @@ -190,6 +190,32 @@ public class PublicKeyRecordImpl extends TagValue implements PublicKeyRecord { return cached.get(); } + /** + * Compares the local part with the prefix and suffix around the wildcard, instead of running the + * pattern: its ".*" backtracks over the local part, and both g= and i= may be long. + * + * @see org.apache.james.jdkim.api.PublicKeyRecord#isGranularityMatching(CharSequence) + */ + @Override + public boolean isGranularityMatching(CharSequence localPart) { + // validates g= as the pattern does, and caches it + getGranularityPattern(); + String g = getValue("g").toString(); + if (g.isEmpty()) { + // SPEC: An empty "g=" value never matches any addresses. + return false; + } + String local = localPart.toString(); + int star = g.indexOf('*'); + if (star == -1) { + return local.equals(g); + } + String prefix = g.substring(0, star); + String suffix = g.substring(star + 1); + return local.length() >= prefix.length() + suffix.length() + && local.startsWith(prefix) && local.endsWith(suffix); + } + /** * Any run of atext, "*" included: the same values as the former "(atom)?\*?(atom)?". A loop over the * chars, since the value is chosen by the domain owner and may be long. 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 f2cb04e..47d6f2c 100644 --- a/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java +++ b/main/src/test/java/org/apache/james/jdkim/DKIMVerifierTest.java @@ -214,6 +214,53 @@ public class DKIMVerifierTest { assertThatCode(() -> DKIMVerifier.apply(key, sign)).doesNotThrowAnyException(); } + @Test(timeout = 2000) + public void granularityShouldBeMatchedInLinearTime() throws Exception { + StringBuilder suffix = new StringBuilder(); + for (int i = 0; i < 50000; i++) { + suffix.append('a'); + } + StringBuilder local = new StringBuilder(); + for (int i = 0; i < 200000; i++) { + local.append('a'); + } + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=*" + suffix + "b; p=AAAA"); + SignatureRecordImpl sign = new SignatureRecordImpl( + "v=1; a=rsa-sha256; d=example.com; s=sel; h=from; bh=AA==; b=AA==; i=" + local + "@example.com"); + + assertThatThrownBy(() -> DKIMVerifier.apply(key, sign)) + .isInstanceOf(PermFailException.class) + .hasMessageContaining("does not match the key granularity"); + } + + @Test + public void granularityShouldMatchPrefixAndSuffix() { + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=ab*ba; p=AAAA"); + + assertThat(key.isGranularityMatching("abba")).isTrue(); + assertThat(key.isGranularityMatching("abxyzba")).isTrue(); + assertThat(key.isGranularityMatching("aba")).isFalse(); + assertThat(key.isGranularityMatching("abxyz")).isFalse(); + assertThat(key.isGranularityMatching("xyzba")).isFalse(); + } + + @Test + public void granularityWithoutWildcardShouldMatchExactly() { + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=joe; p=AAAA"); + + assertThat(key.isGranularityMatching("joe")).isTrue(); + assertThat(key.isGranularityMatching("joey")).isFalse(); + assertThat(key.isGranularityMatching("")).isFalse(); + } + + @Test + public void emptyGranularityShouldMatchNothing() { + PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=; p=AAAA"); + + assertThat(key.isGranularityMatching("")).isFalse(); + assertThat(key.isGranularityMatching("joe")).isFalse(); + } + @Test public void granularityWildcardShouldMatchLineTerminators() throws Exception { PublicKeyRecordImpl key = new PublicKeyRecordImpl("v=DKIM1; k=rsa; g=a*b; p=AAAA"); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
