You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by "prosgarz35 (via GitHub)" <gi...@apache.org> on 2026/10/06 02:22:49 UTC

[PR] [FIX] Fix RFC compliance issues and race conditions across SMTP and JMAP RFC-8621 [james-project]

prosgarz35 opened a new pull request, #3236:
URL: https://github.com/apache/james-project/pull/3236

   ## Summary
   
   This pull request addresses several RFC compliance issues, race conditions, and determinism inconsistencies found during protocol verification of SMTP and JMAP RFC-8621 implementations.
   
   ---
   
   ## Changes
   
   ### 1. SMTP: Remove extraneous trailing parenthesis in EHLO greeting (RFC 5321 §4.1.1.1)
   - **File:** `protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java`
   - **Issue:** The EHLO greeting response string formatted the client remote address with an unbalanced closing parenthesis `])` instead of `]`:
     ```
     250 hostname Hello client [192.168.1.1])
     ```
   - **Fix:** Changed `.append("])")` to `.append("]")`, conforming strictly to RFC 5321 §4.1.1.1 syntax.
   
   ---
   
   ### 2. JMAP RFC-8621: Ensure atomicity in `Email/set update` and eliminate race window (RFC 8621 §3)
   - **File:** `server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSetUpdatePerformer.scala`
   - **Issue:** Previously, `updateSingleMessage` only used atomic execution when both mailbox IDs and flags were updated simultaneously (`isMailboxUpdate && isFlagUpdate`). In the `else` branch, flag updates and mailbox ID updates were executed in a chained non-atomic fashion (`updateFlags(...).flatMap(... => updateMailboxIds(...))`), introducing a partial-update failure window and potential data race between concurrent updates.
   - **Fix:**
     - Branching is now strictly separated into dedicated, atomic store operations:
       - Combined update: calls `messageIdManager.updateEmail(...)` (single atomic operation).
       - Flags-only update: calls `messageIdManager.setFlagsReactive(...)` directly.
       - Mailbox-only update: calls `messageIdManager.setInMailboxesReactive(...)` directly.
     - Removed redundant non-atomic helper methods `updateFlags` and `updateMailboxIds`.
   
   ---
   
   ### 3. JMAP RFC-8621: Deterministic message copy selection for multi-mailbox emails (RFC 8621 §4.1)
   - **File:** `server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala`
   - **Issue:** When an email exists across multiple mailboxes, `message._2` contains a `Seq[MessageResult]` whose order depends on the backend store driver and is not guaranteed. View factories (`EmailMetadataViewFactory`, `EmailHeaderViewFactory`, `EmailFullViewFactory`, `EmailFastViewReader`, and `EmailFastViewWithAttachmentsMetadataReader`) previously called `.headOption`, which could lead to non-deterministic selection of the representative message metadata (such as `size` and `receivedAt`).
   - **Fix:** Added `Email.pickFirstMessage(messages: Seq[MessageResult])` which deterministically sorts the message results by internal date timestamp and mailbox ID (`messages.sortBy(m => (m.getInternalDate.getTime, m.getMailboxId.serialize())).headOption`). All view factories now use this deterministic selection helper.
   
   ---
   
   ### 4. JMAP RFC-8621: Fix typo in `validateTextBody` error message
   - **File:** `server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala`
   - **Issue:** `validateTextBody` raised an `IllegalArgumentException` stating `"Expecting htmlBody type to be text/html"` when validating a non-`text/plain` part due to copy-paste from `validateHtmlBody`.
   - **Fix:** Updated the error message to accurately state `"Expecting textBody type to be text/plain"`.


-- 
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: notifications-unsubscribe@james.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


Re: [PR] [FIX] Fix RFC compliance issues and race conditions across SMTP and JMAP RFC-8621 [james-project]

Posted by "chibenwa (via GitHub)" <gi...@apache.org>.
chibenwa commented on PR #3236:
URL: https://github.com/apache/james-project/pull/3236#issuecomment-6010873829

   Have tests for your changes please.


-- 
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: notifications-unsubscribe@james.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


Re: [PR] [FIX] Fix RFC compliance issues and race conditions across SMTP and JMAP RFC-8621 [james-project]

Posted by "chibenwa (via GitHub)" <gi...@apache.org>.
chibenwa commented on code in PR #3236:
URL: https://github.com/apache/james-project/pull/3236#discussion_r4192276356


##########
protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java:
##########
@@ -96,7 +96,7 @@ private Response doEHLO(SMTPSession session, String argument) {
 
         SMTPResponse resp = new SMTPResponse(SMTPRetCode.MAIL_OK, new StringBuilder(session.getConfiguration().getHelloName()).append(" Hello ").append(argument)
                 .append(" [")
-                .append(session.getRemoteAddress().getAddress().getHostAddress()).append("])"));
+                .append(session.getRemoteAddress().getAddress().getHostAddress()).append("]"));

Review Comment:
   his isn't an RFC 5321 compliance issue: the text after the domain in an EHLO reply is free-form. It's a cosmetic fix, and still worth doing.
   
   HeloCmdHandler has the same "])" at protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/HeloCmdHandler.java:85. Please fix it too.



##########
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSetUpdatePerformer.scala:
##########
@@ -230,30 +230,34 @@ class EmailSetUpdatePerformer @Inject() (serializer: EmailSetSerializer,
           .doOnSuccess(_ => auditMove(Seq(messageId), mailboxIds.value, targetIds.value, session))
           .onErrorResume(e => SMono.just[EmailUpdateResult](EmailUpdateFailure(EmailSet.asUnparsed(messageId), e)))
           .switchIfEmpty(SMono.just[EmailUpdateResult](EmailUpdateSuccess(messageId)))
-      } else {

Review Comment:
    the race described doesn't exist
   
     - The combined case is already handled. When both mailboxes and flags change, the code already calls the atomic updateEmail (JAMES-3728).
     - The else branch only ever does one real operation. It's reached only when at most one of the two changes. One of updateFlags/updateMailboxIds then returns EmailUpdateSuccess
       without touching the store. There was never a "partial update window" between two writes.
     - The refactor is equivalent but brings no benefit. It is a bit more explicit, at the cost of copy-pasting the flag computation (the fold over originalFlags), which now appears
       twice.
     - Verdict: a cosmetic refactor sold as a race-condition fix. Drop it.



##########
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala:
##########
@@ -412,7 +412,7 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
   private def validateTextBody(blobResolvers: BlobResolvers, mailboxSession: MailboxSession): SMono[ClientBodyPart] =
     textBody match {
       case None => SMono.empty
-      case Some(text :: Nil) if !text.`type`.value.equals("text/plain") => SMono.error(new IllegalArgumentException("Expecting htmlBody type to be text/html"))
+      case Some(text :: Nil) if !text.`type`.value.equals("text/plain") => SMono.error(new IllegalArgumentException("Expecting textBody type to be text/plain"))

Review Comment:
   Ok ;-)



##########
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala:
##########
@@ -76,6 +76,10 @@ object Email {
       .map(list => list.reduce(_ ++ _))
   }
 
+  // RFC 8621 sec.4.1: Deterministically pick representative message copy across multiple mailboxes

Review Comment:
   We aggregate flag and the rest of the message is immutable. Please drop this unneeded commit.



-- 
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: notifications-unsubscribe@james.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


Re: [PR] [FIX] Fix RFC compliance issues and race conditions across SMTP and JMAP RFC-8621 [james-project]

Posted by "chibenwa (via GitHub)" <gi...@apache.org>.
chibenwa commented on PR #3236:
URL: https://github.com/apache/james-project/pull/3236#issuecomment-6127999930

   Hello @prosgarz35 
   Would you find time to shrink this work to what is a real life issue?
   Best regards,
   Benoit


-- 
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: notifications-unsubscribe@james.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org