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-jsieve.git

commit 6e014d30e0f6b3183a4e98d53c63f9b31c916c3c
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 26 08:45:26 2026 +0100

    [FIX] SieveContext: parse only the address headers the budget charged
    
    parseAddresses charged the values getMatchingHeader found, then let the
    adapter look the header up again with its own matching. James's adapter
    matches with String#equalsIgnoreCase, which folds U+017F to s: the name
    "ſubject" found no value in jSieve, was charged a few hundred units,
    and still had James parse the whole Subject. The cache was keyed on the
    exact name, so each spelling was another parse, and a script of many
    spellings parsed a large header as many times nearly uncharged.
    
    A name with no value found by getMatchingHeader, or outside ASCII, which
    no field name is (RFC 5322 section 3.6.8), now holds no address without
    being parsed. The parses are cached on the ASCII upper case of the name,
    so its ASCII case spellings are one parse.
---
 .../main/java/org/apache/jsieve/SieveContext.java  | 40 +++++++++++++++++-----
 .../java/org/apache/jsieve/mail/MailAdapter.java   |  6 ++++
 .../org/apache/jsieve/EvaluationBudgetTest.java    | 39 +++++++++++++++++++++
 3 files changed, 77 insertions(+), 8 deletions(-)

diff --git a/core/src/main/java/org/apache/jsieve/SieveContext.java 
b/core/src/main/java/org/apache/jsieve/SieveContext.java
index 66afdee..9e9c89e 100644
--- a/core/src/main/java/org/apache/jsieve/SieveContext.java
+++ b/core/src/main/java/org/apache/jsieve/SieveContext.java
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.james.mime4j.util.MimeUtil;
+import org.apache.jsieve.comparators.AsciiCasemap;
 import org.apache.jsieve.exception.InternetAddressException;
 import org.apache.jsieve.exception.SieveException;
 import org.apache.jsieve.mail.MailAdapter;
@@ -176,8 +177,10 @@ public abstract class SieveContext {
      * Parses the addresses of the headers of the mail with the given name,
      * see {@link MailAdapter#parseAddresses(String)}: none when they do not
      * parse as address lists. A name is parsed once per context, that is per
-     * evaluation. A parse is charged to the evaluation budget: the header
-     * names of the mail it walks and the values it parses.
+     * evaluation, whatever its ASCII case. A parse is charged to the
+     * evaluation budget: the header names of the mail it walks and the values
+     * it parses, as {@link #getMatchingHeader(MailAdapter, String)} looks
+     * them up. A name with no such value, or outside ASCII, is not parsed.
      *
      * @param mail not null
      * @param headerName not null
@@ -188,20 +191,41 @@ public abstract class SieveContext {
         if (parsedAddresses == null) {
             parsedAddresses = new HashMap<>();
         }
-        MailAdapter.Address[] addresses = parsedAddresses.get(headerName);
+        // The spellings of a name that differ in ASCII case are one name
+        final String key = AsciiCasemap.toAsciiUpperCase(headerName);
+        MailAdapter.Address[] addresses = parsedAddresses.get(key);
         if (addresses == null) {
             final List<String> values = getMatchingHeader(mail, headerName);
-            chargeComparison(headerNamesLength(mail) + length(values));
-            try {
-                addresses = mail.parseAddresses(headerName);
-            } catch (InternetAddressException e) {
+            if (values.isEmpty() || !isAscii(headerName)) {
+                // The adapter parses the fields it looks up itself, and may
+                // match more names than the ASCII ones of getMatchingHeader
+                // (String#equalsIgnoreCase folds U+017F to s): only a parse of
+                // the values charged here is charged right. A field name is
+                // ASCII (RFC 5322 section 3.6.8), so a name outside ASCII
+                // holds no address.
                 addresses = new MailAdapter.Address[0];
+            } else {
+                chargeComparison(headerNamesLength(mail) + length(values));
+                try {
+                    addresses = mail.parseAddresses(headerName);
+                } catch (InternetAddressException e) {
+                    addresses = new MailAdapter.Address[0];
+                }
             }
-            parsedAddresses.put(headerName, addresses);
+            parsedAddresses.put(key, addresses);
         }
         return addresses;
     }
 
+    private static boolean isAscii(String string) {
+        for (int i = 0; i < string.length(); i++) {
+            if (string.charAt(i) > 0x7F) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     private long headerNamesLength(MailAdapter mail) throws SieveException {
         if (headerNamesLength < 0) {
             final List<String> headerNames = mail.getHeaderNames();
diff --git a/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java 
b/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
index 74463ef..e367676 100644
--- a/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
+++ b/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
@@ -120,6 +120,12 @@ public interface MailAdapter {
      * are considered equal.
      * </p>
      * 
+     * <p>
+     * The address test asks {@link #parseAddresses(String)} only for a name
+     * this method answers values for, and charges the parse by those values:
+     * it must answer every field that parseAddresses parses.
+     * </p>
+     * 
      * @param name
      * @return <code>List</code>, not null possibly empty, possible
      *         unmodifiable
diff --git a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java 
b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
index 1ab89a7..113c5e5 100644
--- a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
+++ b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
@@ -35,6 +35,7 @@ import org.apache.jsieve.comparators.AsciiCasemap;
 import org.apache.jsieve.comparators.Contains;
 import org.apache.jsieve.comparators.Equals;
 import org.apache.jsieve.comparators.Matches;
+import org.apache.jsieve.exception.InternetAddressException;
 import org.apache.jsieve.exception.SieveException;
 import org.apache.jsieve.mail.ActionDiscard;
 import org.apache.jsieve.mail.ActionKeep;
@@ -150,6 +151,30 @@ public class EvaluationBudgetTest {
         
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionKeep.class);
     }
 
+    @Test
+    public void aNameMatchingNoHeaderShouldNotBeParsed() throws Exception {
+        ParseCountingMailAdapter mail = new ParseCountingMailAdapter();
+        mail.getMessage().setHeader("Subject", "(" + "a".repeat(1_000_000) + 
") <");
+        // U+017F folds to s under String#equalsIgnoreCase, not under 
i;ascii-casemap
+        String script = "if address :all [\"\u017Fubject\", \"\u017FUBJECT\", 
\"X-Absent\"] \"key\" { discard; }";
+
+        factoryWithBudget(100_000).interpret(mail, script(script));
+
+        assertThat(mail.parses).isZero();
+        
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionKeep.class);
+    }
+
+    @Test
+    public void theAsciiCaseSpellingsOfANameShouldBeParsedOnce() throws 
Exception {
+        ParseCountingMailAdapter mail = new ParseCountingMailAdapter();
+        mail.getMessage().setHeader("From", "[email protected]");
+        String script = "if address :all [\"From\", \"FROM\", \"from\", 
\"fRoM\"] \"key\" { discard; }";
+
+        factoryWithBudget(100_000).interpret(mail, script(script));
+
+        assertThat(mail.parses).isEqualTo(1);
+    }
+
     @Test
     public void theHeaderLookupsOfExistsShouldBeCharged() throws Exception {
         SieveMailAdapter mail = mailWithHeaders(10_000);
@@ -387,6 +412,20 @@ public class EvaluationBudgetTest {
         return mail;
     }
 
+    private static class ParseCountingMailAdapter extends SieveMailAdapter {
+        int parses;
+
+        ParseCountingMailAdapter() {
+            super(((SieveMailAdapter) JUnitUtils.createMail()).getMessage());
+        }
+
+        @Override
+        public Address[] parseAddresses(String headerName) throws 
SieveMailException, InternetAddressException {
+            parses++;
+            return super.parseAddresses(headerName);
+        }
+    }
+
     /**
      * Answers the fields of every case of the name, as JavaMail does and as
      * James's adapter relies on.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to