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 c30963b0764ac9be705815b9c0b8e767dc147188 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 17 07:23:01 2026 +0100 [FIX] Body: charge the search the MailAdapter does body :contains with i;ascii-casemap, the default comparator of :contains, is served by MailAdapter#isInBodyText, Raw or Content, which charged nothing: a script repeating the test, or listing thousands of keys, made the adapter scan a large mail again and again without the budget ever being reached. The body test now charges, before handing over to the adapter, the size of the mail plus the length of the key for each key, as a linear :contains would be charged. The MailAdapter javadoc states it. --- .../java/org/apache/jsieve/mail/MailAdapter.java | 9 +++++++ .../org/apache/jsieve/tests/optional/Body.java | 16 +++++++++++- .../org/apache/jsieve/EvaluationBudgetTest.java | 30 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) 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 020aa6e..74463ef 100644 --- a/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java +++ b/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java @@ -170,6 +170,9 @@ public interface MailAdapter { /** * Is the given phrase found in the body text of this mail? * This search should be case insensitive. + * The body test charges the evaluation, before calling it, with + * {@link #getSize()} plus the length of the phrase for each phrase: the + * search should be linear in the size of the mail for each phrase. * @param phrasesCaseInsensitive the phrases to search * @return true when the mail has a textual body and contains the phrase * (case insensitive), false otherwise @@ -181,6 +184,9 @@ public interface MailAdapter { /** * Is the given phrase found in the body raw of this mail? * This search should be case insensitive and the mail should not be decoded. + * The body test charges the evaluation, before calling it, with + * {@link #getSize()} plus the length of the phrase for each phrase: the + * search should be linear in the size of the mail for each phrase. * @param phrasesCaseInsensitive the phrases to search * @return true when the mail has a textual body and contains the phrase * (case insensitive), false otherwise @@ -192,6 +198,9 @@ public interface MailAdapter { /** * Is the given phrase found in the body contents of the specified content types of this mail? * This search should be case insensitive. + * The body test charges the evaluation, before calling it, with + * {@link #getSize()} plus the length of the phrase for each phrase: the + * search should be linear in the size of the mail for each phrase. * @param contentTypes Content types of the body parts we should search into. * @param phrasesCaseInsensitive the phrases to search * @return true when the mail has a textual body and contains the phrase diff --git a/core/src/main/java/org/apache/jsieve/tests/optional/Body.java b/core/src/main/java/org/apache/jsieve/tests/optional/Body.java index 742ed79..fd3de45 100644 --- a/core/src/main/java/org/apache/jsieve/tests/optional/Body.java +++ b/core/src/main/java/org/apache/jsieve/tests/optional/Body.java @@ -123,7 +123,9 @@ public class Body extends AbstractTest { } return false; } - // the case insensitive search the MailAdapter has always offered + // the case insensitive search the MailAdapter has always offered, + // charged as :contains: the size of the mail for each key + chargeSearch(mail, ctx); if (transformation.getTag().equals(RAW)) { return mail.isInBodyRaw(valuesToBeMatched.getList()); } else if (transformation.getTag().equals(CONTENT)) { @@ -135,6 +137,18 @@ public class Body extends AbstractTest { } } + /** + * The MailAdapter searches the mail for each key: charges, as a linear + * :contains, the size of the mail plus the length of the key for each + * key, an unknown size being charged nothing. + */ + private void chargeSearch(MailAdapter mail, SieveContext ctx) throws SieveException { + final long size = Math.max(mail.getSize(), 0); + for (String key : valuesToBeMatched.getList()) { + ctx.chargeComparison(size + 1L + key.length() + 1L); + } + } + private List<String> bodyValues(MailAdapter mail) throws SieveException { if (transformation.getTag().equals(RAW)) { return mail.getBodyRaw(); diff --git a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java index 407e0cc..fc2a4f7 100644 --- a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java +++ b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java @@ -102,6 +102,28 @@ public class EvaluationBudgetTest { assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class); } + @Test + public void theSearchOfTheBodyByTheMailAdapterShouldBeCharged() throws Exception { + SieveMailAdapter mail = mailWithBody("lorem ipsum dolor sit amet ".repeat(40_000)); + String test = "if body :contains \"absent\" { discard; }"; + String script = "require \"body\"; " + test.repeat(100); + + assertThatThrownBy(() -> factoryWithBudget(10_000_000).interpret(mail, script(script))) + .isInstanceOf(SieveException.class) + .hasMessageContaining("budget"); + assertThat(mail.getActions()).isEmpty(); + } + + @Test + public void aSearchOfTheBodyWithinTheBudgetShouldBeEvaluated() throws Exception { + SieveMailAdapter mail = mailWithBody("Gone for holidays"); + String script = "require \"body\"; if body :contains [\"absent\", \"HOLIDAYS\"] { discard; }"; + + factoryWithBudget(10_000).interpret(mail, script(script)); + + assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class); + } + @Test public void aComparatorWithoutCostShouldBeChargedTheWorstCase() throws Exception { ConfigurationManager configurationManager = new ConfigurationManager(); @@ -192,6 +214,14 @@ public class EvaluationBudgetTest { return new ByteArrayInputStream(script.getBytes(StandardCharsets.UTF_8)); } + private static SieveMailAdapter mailWithBody(String body) throws Exception { + SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail(); + mail.getMessage().setText(body); + mail.getMessage().saveChanges(); + // copied so that JavaMail reports the size of the whole message + return (SieveMailAdapter) JUnitUtils.copyMail(mail); + } + private static SieveMailAdapter mailWithSubject(String subject) throws Exception { SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail(); mail.getMessage().setHeader("Subject", subject); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
