You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "oscerd (via GitHub)" <gi...@apache.org> on 2026/09/21 07:37:18 UTC

[PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

oscerd opened a new pull request, #26659:
URL: https://github.com/apache/camel/pull/26659

   This fixes [CAMEL-24432](https://issues.apache.org/jira/browse/CAMEL-24432).
   
   Three independent paths turned one hostile or merely unusual message into a consumer that never made progress again — it repeated on every poll until the message was removed out of band:
   
   1. **`MailSorter.compareMessageProperty`** dereferenced values that are optional in RFC 5322 or may be absent on the server (`getSubject()`, the sent/received dates, `getFrom()`/`getRecipients()` before `[0]`), so it threw from inside `Arrays.sort` and aborted the whole poll. Client-side sorting is always used for POP3, and for IMAP when the server has no SORT capability. Comparisons now go through a `compareNullable` helper (missing value ordered first) and address arrays through a `firstAddress` helper that returns null when there is no entry.
   2. **`MailBinding.extractAttachmentsFromMultipart`** recursed on nested multiparts with no depth limit, so a deeply nested message raised `StackOverflowError`. Recursion is now bounded at `MAX_MULTIPART_DEPTH`; the protected two-arg method stays as the entry point and delegates to a private depth-carrying overload, so the subclass extension point is unchanged.
   3. **`MailConverters.toString(Message)`** looped while the content was a `MimeMultipart` but only reassigned it when `getCount() > 0`, so an empty multipart (reachable where `mail.mime.multipart.allowempty` is set) spun forever and pinned the thread. It now returns null.
   
   All three are covered by `MailPoisonMessageTest`, and each fails against the previous code: a `NullPointerException` from `compareTo`, a `StackOverflowError`, and a 5-second timeout respectively.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd merged PR #26659:
URL: https://github.com/apache/camel/pull/26659


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26659:
URL: https://github.com/apache/camel/pull/26659#issuecomment-5757049715

   :star2: Thank you for your contribution to the Apache Camel project! :star2:
     :robot: CI automation will test this PR automatically.
   
     :camel: Apache Camel Committers, please review the following items:
   
     * First-time contributors **require MANUAL approval** for the GitHub Actions to run
     * You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot although they are normally detected and executed by CI.
     * You can label PRs using `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
     * Build and test logs are available in the summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
     
     :warning: Be careful when sharing logs. Review their contents before sharing them publicly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26659:
URL: https://github.com/apache/camel/pull/26659#issuecomment-5757483847

   <!-- ci-tested-modules -->
   :test_tube: **CI tested the following changed modules:**
   
   - `components/camel-mail`
   
   ---
   
   <details><summary>:microscope: Scalpel shadow comparison — Scalpel: 10 of 692 tested, 27 compile-only — current: 10 all tested</summary>
   
   [Maveniverse Scalpel](https://github.com/maveniverse/scalpel) detected **37 affected modules** (current approach: 10).
   
   **Skip-tests mode would test 10 modules** (1 direct + 9 downstream), **skip tests for 27** (generated code, meta-modules)
   
   <details><summary>:warning: Modules only in Scalpel (27)</summary>
   
   - `apache-camel`
   - `camel-allcomponents`
   - `camel-catalog`
   - `camel-catalog-console`
   - `camel-catalog-maven`
   - `camel-catalog-suggest`
   - `camel-componentdsl`
   - `camel-endpointdsl`
   - `camel-endpointdsl-support`
   - `camel-itest`
   - `camel-jbang-core`
   - `camel-jbang-it`
   - `camel-jbang-main`
   - `camel-jbang-plugin-edit`
   - `camel-jbang-plugin-generate`
   - `camel-jbang-plugin-kubernetes`
   - `camel-jbang-plugin-test`
   - `camel-kamelet-main`
   - `camel-launcher`
   - `camel-report-maven-plugin`
   - `camel-route-parser`
   - `camel-yaml-dsl`
   - `camel-yaml-dsl-deserializers`
   - `camel-yaml-dsl-maven-plugin`
   - `coverage`
   - `docs`
   - `dummy-component`
   
   </details>
   
   
   <details><summary>Modules Scalpel would test (10)</summary>
   
   - `camel-jbang-mcp` ← downstream of org.apache.camel:camel-catalog
   - `camel-jbang-plugin-mcp` ← downstream of org.apache.camel:camel-jbang-core
   - `camel-jbang-plugin-route-parser` ← downstream of org.apache.camel:camel-route-parser
   - `camel-jbang-plugin-tui` ← downstream of org.apache.camel:camel-catalog
   - `camel-jbang-plugin-validate` ← downstream of org.apache.camel:camel-yaml-dsl-validator
   - `camel-launcher-container` ← downstream of org.apache.camel:camel-launcher
   - `camel-mail` ← components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java, components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java, components/camel-mail/src/main/java/org/apache/camel/component/mail/MailSorter.java, components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPoisonMessageTest.java
   - `camel-mail-microsoft-oauth` ← downstream of org.apache.camel:camel-mail
   - `camel-yaml-dsl-validator` ← downstream of org.apache.camel:camel-catalog
   - `camel-yaml-dsl-validator-maven-plugin` ← downstream of org.apache.camel:camel-yaml-dsl-validator
   
   </details>
   
   <details><summary>Modules with tests skipped (27)</summary>
   
   - `apache-camel`
   - `camel-allcomponents`
   - `camel-catalog`
   - `camel-catalog-console`
   - `camel-catalog-maven`
   - `camel-catalog-suggest`
   - `camel-componentdsl`
   - `camel-endpointdsl`
   - `camel-endpointdsl-support`
   - `camel-itest`
   - `camel-jbang-core`
   - `camel-jbang-it`
   - `camel-jbang-main`
   - `camel-jbang-plugin-edit`
   - `camel-jbang-plugin-generate`
   - `camel-jbang-plugin-kubernetes`
   - `camel-jbang-plugin-test`
   - `camel-kamelet-main`
   - `camel-launcher`
   - `camel-report-maven-plugin`
   - `camel-route-parser`
   - `camel-yaml-dsl`
   - `camel-yaml-dsl-deserializers`
   - `camel-yaml-dsl-maven-plugin`
   - `coverage`
   - `docs`
   - `dummy-component`
   
   </details>
   
   > :information_source: Shadow mode — Scalpel observes but does not affect test execution. [Learn more](https://github.com/maveniverse/scalpel)
   
   </details>
   
   <details><summary>All tested modules (37 modules, 4m 40s total)</summary>
   
   **Total reactor time:** 4m 40s
   
   | Module | Duration | Status |
   | --- | --- | --- |
   | Camel :: Launcher | 52.2s | SUCCESS |
   | Camel :: JBang :: MCP | 40.2s | SUCCESS |
   | Camel :: JBang :: Plugin :: TUI | 31.7s | SUCCESS |
   | Camel :: Catalog :: Camel Catalog | 21.7s | SUCCESS |
   | Camel :: Component DSL | 19.7s | SUCCESS |
   | Camel :: JBang :: Plugin :: Kubernetes | 14.9s | SUCCESS |
   | Camel :: Docs | 14.6s | SUCCESS |
   | Camel :: Kamelet Main | 10.8s | SUCCESS |
   | Camel :: YAML DSL :: Validator | 10.7s | SUCCESS |
   | Camel :: Catalog :: Camel Report Maven Plugin | 8.1s | SUCCESS |
   | Camel :: JBang :: Plugin :: Testing | 7.7s | SUCCESS |
   | Camel :: Catalog :: Camel Route Parser | 7.1s | SUCCESS |
   | Camel :: YAML DSL :: Deserializers | 5.4s | SUCCESS |
   | Camel :: All Components Sync point | 5.1s | SUCCESS |
   | Camel :: JBang :: Plugin :: Validate | 5.0s | SUCCESS |
   | Camel :: YAML DSL :: Validator Maven Plugin | 3.3s | SUCCESS |
   | Camel :: YAML DSL :: Maven Plugins | 3.2s | SUCCESS |
   | Camel :: Catalog :: Maven | 2.8s | SUCCESS |
   | Camel :: Catalog :: Suggest (deprecated) | 2.4s | SUCCESS |
   | Camel :: Assembly | 1.7s | SUCCESS |
   | Camel :: JBang :: Plugin :: Edit | 1.6s | SUCCESS |
   | Camel :: Coverage | 1.5s | SUCCESS |
   | Camel :: JBang :: Main | 1.3s | SUCCESS |
   | Camel :: Mail :: Microsoft OAuth | 1.0s | SUCCESS |
   | Camel :: JBang :: Integration tests | 1.0s | SUCCESS |
   | Camel :: JBang :: Plugin :: Generate | 0.9s | SUCCESS |
   | Camel :: Catalog :: Console | 0.9s | SUCCESS |
   | Camel :: Endpoint DSL :: Support | 0.9s | SUCCESS |
   | Camel :: Catalog :: Dummy Component | 0.8s | SUCCESS |
   | Camel :: JBang :: Plugin :: MCP | 0.7s | SUCCESS |
   | Camel :: JBang :: Plugin :: Route Parser | 0.6s | SUCCESS |
   | Camel :: Launcher :: Container | 0.5s | SUCCESS |
   | Camel :: Endpoint DSL | n/a |  |
   | Camel :: Integration Tests | n/a |  |
   | Camel :: JBang :: Core | n/a |  |
   | Camel :: Mail | n/a |  |
   | Camel :: YAML DSL | n/a |  |
   
   **Top 20 slowest modules:**
   - `Camel :: Launcher` (52.2s)
   - `Camel :: JBang :: MCP` (40.2s)
   - `Camel :: JBang :: Plugin :: TUI` (31.7s)
   - `Camel :: Catalog :: Camel Catalog` (21.7s)
   - `Camel :: Component DSL` (19.7s)
   - `Camel :: JBang :: Plugin :: Kubernetes` (14.9s)
   - `Camel :: Docs` (14.6s)
   - `Camel :: Kamelet Main` (10.8s)
   - `Camel :: YAML DSL :: Validator` (10.7s)
   - `Camel :: Catalog :: Camel Report Maven Plugin` (8.1s)
   - `Camel :: JBang :: Plugin :: Testing` (7.7s)
   - `Camel :: Catalog :: Camel Route Parser` (7.1s)
   - `Camel :: YAML DSL :: Deserializers` (5.4s)
   - `Camel :: All Components Sync point` (5.1s)
   - `Camel :: JBang :: Plugin :: Validate` (5.0s)
   - `Camel :: YAML DSL :: Validator Maven Plugin` (3.3s)
   - `Camel :: YAML DSL :: Maven Plugins` (3.2s)
   - `Camel :: Catalog :: Maven` (2.8s)
   - `Camel :: Catalog :: Suggest (deprecated)` (2.4s)
   - `Camel :: Assembly` (1.7s)
   
   </details>
   
   ---
   :gear: [View full build and test results](https://github.com/apache/camel/actions/runs/35573825531)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "apupier (via GitHub)" <gi...@apache.org>.
apupier commented on code in PR #26659:
URL: https://github.com/apache/camel/pull/26659#discussion_r4062046711


##########
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailSorter.java:
##########
@@ -114,38 +114,59 @@ private static void sortMessages(Message[] messages, final List<SortTermWithDesc
      * @throws jakarta.mail.MessagingException If message data could not be read.
      */
     private static int compareMessageProperty(Message msg1, Message msg2, SortTerm property) throws MessagingException {
+        // Every value read here is optional in RFC 5322 or may be absent on the server, so each comparison
+        // must tolerate a missing value. A message legitimately lacking the sorted-on property previously
+        // threw from inside the comparator and aborted the whole poll, on every subsequent poll.
         if (property.equals(SortTerm.TO)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.TO)[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.TO)[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getRecipients(Message.RecipientType.TO)),
+                    firstAddress(msg2.getRecipients(Message.RecipientType.TO)));
         } else if (property.equals(SortTerm.CC)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.CC)[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.CC)[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getRecipients(Message.RecipientType.CC)),
+                    firstAddress(msg2.getRecipients(Message.RecipientType.CC)));
         } else if (property.equals(SortTerm.FROM)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getFrom()[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getFrom()[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getFrom()), firstAddress(msg2.getFrom()));
         } else if (property.equals(SortTerm.ARRIVAL)) {
-            Date arr1 = msg1.getReceivedDate();
-            Date arr2 = msg2.getReceivedDate();
-            return arr1.compareTo(arr2);
+            return compareNullable(msg1.getReceivedDate(), msg2.getReceivedDate());
         } else if (property.equals(SortTerm.DATE)) {
-            Date sent1 = msg1.getSentDate();
-            Date sent2 = msg2.getSentDate();
-            return sent1.compareTo(sent2);
+            return compareNullable(msg1.getSentDate(), msg2.getSentDate());
         } else if (property.equals(SortTerm.SIZE)) {
-            int size1 = msg1.getSize();
-            int size2 = msg2.getSize();
-            return Integer.compare(size1, size2);
+            return Integer.compare(msg1.getSize(), msg2.getSize());
         } else if (property.equals(SortTerm.SUBJECT)) {
-            String sub1 = msg1.getSubject();
-            String sub2 = msg2.getSubject();
-            return sub1.compareTo(sub2);
+            return compareNullable(msg1.getSubject(), msg2.getSubject());
         }
         throw new IllegalArgumentException(String.format("Unknown sort term: %s", property.toString()));
     }
 
+    /**
+     * Returns the address of the first entry, or null when the message carries none.
+     */
+    private static String firstAddress(Address[] addresses) {
+        if (addresses == null || addresses.length == 0) {
+            return null;
+        }
+        if (addresses[0] instanceof InternetAddress internetAddress) {
+            return internetAddress.getAddress();
+        }
+        return addresses[0].toString();
+    }
+
+    /**
+     * Compares two optional values, ordering a missing one first. Sorting must not depend on every message carrying the
+     * sorted-on property.
+     */
+    private static <T extends Comparable<T>> int compareNullable(T value1, T value2) {
+        if (value1 == null && value2 == null) {
+            return 0;
+        }
+        if (value1 == null) {
+            return -1;
+        }
+        if (value2 == null) {
+            return 1;
+        }

Review Comment:
   Shouldn't be better to return 0 in case one or the other null? it would allow to completely ignore this value from the comparison.
   But the choice of letting the non-provided value with a lower sort value is fine too.



##########
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java:
##########
@@ -73,6 +73,8 @@
 public class MailBinding {
 
     private static final Logger LOG = LoggerFactory.getLogger(MailBinding.class);
+
+    private static final int MAX_MULTIPART_DEPTH = 20;

Review Comment:
   is there a specific reason for choosing 20?
   
   Shouldn't we make it configurable to ensure potential backward-compatibility?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #26659:
URL: https://github.com/apache/camel/pull/26659#issuecomment-5757070874

   <!-- camel-backport-bot -->
   
   ### 🔄 Backport Bot
   
   This bugfix targets `main` and may need porting to:
   - `camel-4.22.x`
   
   Labels `port/<branch>` have been added. Remove a label to opt out of porting to that branch.
   
   Port PRs will be created automatically when this PR is merged. Comment `/port` to create them now.
   
   â„šī¸ If you push additional commits after `/port`, use `/port` again to update the port PRs.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #26659:
URL: https://github.com/apache/camel/pull/26659#discussion_r4060525603


##########
components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPoisonMessageTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mail;
+
+import java.time.Duration;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import jakarta.mail.Address;
+import jakarta.mail.Message;
+import jakarta.mail.MessagingException;
+import jakarta.mail.Session;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeBodyPart;
+import jakarta.mail.internet.MimeMessage;
+import jakarta.mail.internet.MimeMultipart;
+
+import org.apache.camel.attachment.Attachment;
+import org.eclipse.angus.mail.imap.SortTerm;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+import static org.mockito.Mockito.when;
+
+/**
+ * A single malformed or merely unusual message must not put the consumer into a state it never recovers from. Each of
+ * these previously aborted or hung the poll, and repeated on every subsequent poll until the message was removed out of
+ * band.
+ */
+class MailPoisonMessageTest {
+
+    @Test
+    void sortingToleratesAMessageWithoutTheSortedOnHeader() throws Exception {
+        // Subject is optional in RFC 5322, so a message may legitimately carry none
+        Message withSubject = mockMessage("subject");
+        Message withoutSubject = mockMessage(null);
+
+        Message[] messages = { withSubject, withoutSubject };
+        assertDoesNotThrow(() -> MailSorter.sortMessages(messages, new SortTerm[] { SortTerm.SUBJECT }));
+    }
+
+    @Test
+    void deeplyNestedMultipartDoesNotExhaustTheStack() {
+        assertDoesNotThrow(() -> {
+            MimeMultipart nested = new MimeMultipart();
+            MimeBodyPart leaf = new MimeBodyPart();
+            leaf.setText("payload");
+            nested.addBodyPart(leaf);
+
+            for (int i = 0; i < 8000; i++) {
+                MimeBodyPart wrapper = new MimeBodyPart();
+                wrapper.setContent(nested);
+                // setContent(Multipart) does not update the part's Content-Type header until the
+                // enclosing message is saved, and MailBinding dispatches on isMimeType("multipart/*"),
+                // so the header has to be set explicitly for this to exercise the recursion at all
+                wrapper.setHeader("Content-Type", nested.getContentType());
+                MimeMultipart outer = new MimeMultipart();
+                outer.addBodyPart(wrapper);
+                nested = outer;
+            }
+
+            Map<String, Attachment> map = new HashMap<>();
+            new MailBinding().extractAttachmentsFromMultipart(nested, map);
+        });
+    }
+
+    @Test
+    void emptyMultipartDoesNotSpinForever() {
+        assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
+            MimeMessage message = new MimeMessage(Session.getDefaultInstance(new java.util.Properties()));

Review Comment:
   Nit: the import-style rule in CLAUDE.md asks for no fully qualified class names in test code, so `java.util.Properties` should be imported (alongside `java.util.Map`).
   
   ```suggestion
               MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #26659:
URL: https://github.com/apache/camel/pull/26659#issuecomment-5791429319

   <!-- camel-backport-bot -->
   
   ### 🔄 Backport Bot
   
   **Port PRs created:**
   - ✅ `camel-4.22.x` → #26763
   
   **Conflicts (🤖 agent dispatched to resolve):**
   - âš ī¸ `camel-4.18.x` — Cherry-pick conflict in: catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/imap.json, catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/imaps.json, catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pop3.json, catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pop3s.json, catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/smtp.json
   
   â„šī¸ If you push additional commits after `/port`, use `/port` again to update the port PRs.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-24432: camel-mail - stop a single malformed message from permanently halting the consumer [camel]

Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd commented on code in PR #26659:
URL: https://github.com/apache/camel/pull/26659#discussion_r4071365988


##########
components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPoisonMessageTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mail;
+
+import java.time.Duration;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import jakarta.mail.Address;
+import jakarta.mail.Message;
+import jakarta.mail.MessagingException;
+import jakarta.mail.Session;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeBodyPart;
+import jakarta.mail.internet.MimeMessage;
+import jakarta.mail.internet.MimeMultipart;
+
+import org.apache.camel.attachment.Attachment;
+import org.eclipse.angus.mail.imap.SortTerm;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+import static org.mockito.Mockito.when;
+
+/**
+ * A single malformed or merely unusual message must not put the consumer into a state it never recovers from. Each of
+ * these previously aborted or hung the poll, and repeated on every subsequent poll until the message was removed out of
+ * band.
+ */
+class MailPoisonMessageTest {
+
+    @Test
+    void sortingToleratesAMessageWithoutTheSortedOnHeader() throws Exception {
+        // Subject is optional in RFC 5322, so a message may legitimately carry none
+        Message withSubject = mockMessage("subject");
+        Message withoutSubject = mockMessage(null);
+
+        Message[] messages = { withSubject, withoutSubject };
+        assertDoesNotThrow(() -> MailSorter.sortMessages(messages, new SortTerm[] { SortTerm.SUBJECT }));
+    }
+
+    @Test
+    void deeplyNestedMultipartDoesNotExhaustTheStack() {
+        assertDoesNotThrow(() -> {
+            MimeMultipart nested = new MimeMultipart();
+            MimeBodyPart leaf = new MimeBodyPart();
+            leaf.setText("payload");
+            nested.addBodyPart(leaf);
+
+            for (int i = 0; i < 8000; i++) {
+                MimeBodyPart wrapper = new MimeBodyPart();
+                wrapper.setContent(nested);
+                // setContent(Multipart) does not update the part's Content-Type header until the
+                // enclosing message is saved, and MailBinding dispatches on isMimeType("multipart/*"),
+                // so the header has to be set explicitly for this to exercise the recursion at all
+                wrapper.setHeader("Content-Type", nested.getContentType());
+                MimeMultipart outer = new MimeMultipart();
+                outer.addBodyPart(wrapper);
+                nested = outer;
+            }
+
+            Map<String, Attachment> map = new HashMap<>();
+            new MailBinding().extractAttachmentsFromMultipart(nested, map);
+        });
+    }
+
+    @Test
+    void emptyMultipartDoesNotSpinForever() {
+        assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
+            MimeMessage message = new MimeMessage(Session.getDefaultInstance(new java.util.Properties()));

Review Comment:
   Done in 659b83c — added `import java.util.Properties;` and use the simple name.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java:
##########
@@ -73,6 +73,8 @@
 public class MailBinding {
 
     private static final Logger LOG = LoggerFactory.getLogger(MailBinding.class);
+
+    private static final int MAX_MULTIPART_DEPTH = 20;

Review Comment:
   Good call. 20 was a defensive bound — real mail nests only a few multipart levels, and before this change the recursion was unbounded, so a crafted deeply nested message could throw `StackOverflowError` before the message was even processed, aborting the poll on every retry.
   
   To address the backward-compatibility concern, 659b83c makes it a configurable `maxMultipartDepth` option on `MailConfiguration` (default 20), threaded into `MailBinding`, so a deployment that legitimately produces deeper nesting can raise it. Catalog and DSL regenerated for the new option, and a test asserts the configured bound is honoured.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailSorter.java:
##########
@@ -114,38 +114,59 @@ private static void sortMessages(Message[] messages, final List<SortTermWithDesc
      * @throws jakarta.mail.MessagingException If message data could not be read.
      */
     private static int compareMessageProperty(Message msg1, Message msg2, SortTerm property) throws MessagingException {
+        // Every value read here is optional in RFC 5322 or may be absent on the server, so each comparison
+        // must tolerate a missing value. A message legitimately lacking the sorted-on property previously
+        // threw from inside the comparator and aborted the whole poll, on every subsequent poll.
         if (property.equals(SortTerm.TO)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.TO)[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.TO)[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getRecipients(Message.RecipientType.TO)),
+                    firstAddress(msg2.getRecipients(Message.RecipientType.TO)));
         } else if (property.equals(SortTerm.CC)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getRecipients(Message.RecipientType.CC)[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getRecipients(Message.RecipientType.CC)[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getRecipients(Message.RecipientType.CC)),
+                    firstAddress(msg2.getRecipients(Message.RecipientType.CC)));
         } else if (property.equals(SortTerm.FROM)) {
-            InternetAddress addr1 = (InternetAddress) msg1.getFrom()[0];
-            InternetAddress addr2 = (InternetAddress) msg2.getFrom()[0];
-            return addr1.getAddress().compareTo(addr2.getAddress());
+            return compareNullable(firstAddress(msg1.getFrom()), firstAddress(msg2.getFrom()));
         } else if (property.equals(SortTerm.ARRIVAL)) {
-            Date arr1 = msg1.getReceivedDate();
-            Date arr2 = msg2.getReceivedDate();
-            return arr1.compareTo(arr2);
+            return compareNullable(msg1.getReceivedDate(), msg2.getReceivedDate());
         } else if (property.equals(SortTerm.DATE)) {
-            Date sent1 = msg1.getSentDate();
-            Date sent2 = msg2.getSentDate();
-            return sent1.compareTo(sent2);
+            return compareNullable(msg1.getSentDate(), msg2.getSentDate());
         } else if (property.equals(SortTerm.SIZE)) {
-            int size1 = msg1.getSize();
-            int size2 = msg2.getSize();
-            return Integer.compare(size1, size2);
+            return Integer.compare(msg1.getSize(), msg2.getSize());
         } else if (property.equals(SortTerm.SUBJECT)) {
-            String sub1 = msg1.getSubject();
-            String sub2 = msg2.getSubject();
-            return sub1.compareTo(sub2);
+            return compareNullable(msg1.getSubject(), msg2.getSubject());
         }
         throw new IllegalArgumentException(String.format("Unknown sort term: %s", property.toString()));
     }
 
+    /**
+     * Returns the address of the first entry, or null when the message carries none.
+     */
+    private static String firstAddress(Address[] addresses) {
+        if (addresses == null || addresses.length == 0) {
+            return null;
+        }
+        if (addresses[0] instanceof InternetAddress internetAddress) {
+            return internetAddress.getAddress();
+        }
+        return addresses[0].toString();
+    }
+
+    /**
+     * Compares two optional values, ordering a missing one first. Sorting must not depend on every message carrying the
+     * sorted-on property.
+     */
+    private static <T extends Comparable<T>> int compareNullable(T value1, T value2) {
+        if (value1 == null && value2 == null) {
+            return 0;
+        }
+        if (value1 == null) {
+            return -1;
+        }
+        if (value2 == null) {
+            return 1;
+        }

Review Comment:
   I kept the current behaviour (a missing value sorts first) rather than returning 0. Returning 0 only when one side is null would make the comparator non-transitive: with `a=null` and `b < c`, we'd have `compare(a, b) == 0` and `compare(a, c) == 0` but `compare(b, c) != 0`, which violates the `Comparator` general contract and can make `TimSort` throw *"Comparison method violates its general contract!"*. Ordering a missing value first keeps a consistent total order.
   
   You noted the current choice is fine too — just flagging the transitivity reason in case it's useful.
   
   _Claude Code on behalf of oscerd_
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org