Apple Calendar can't accept invites stored with DURATION: 412 "Cannot modify property DTEND" (v0.16.23)

Issue Description

When an organizer creates an invite through JMAP, Stalwart stores it with DTSTART + DURATION (from JSCalendar’s duration). When the attendee taps Accept in Apple Calendar (macOS 27.0.1, over CalDAV), Apple PUTs the event back with DTEND in place of DURATION. The start and end are unchanged. Stalwart rejects the PUT with 412 allowed-attendee-scheduling-object-change. Apple ignores the 412, so Accept appears to do nothing and the attendee stays NEEDS-ACTION. Invites stored with DTEND (from Google or Outlook) accept fine.

Expected Behavior

An attendee PUT that only changes PARTSTAT and rewrites DURATION as an equivalent DTEND is accepted, and a REPLY is sent to the organizer.

Actual Behavior

WebDAV PUT request (web-dav.put) ... type = "CalDAV", code = 412, result = "AllowedAttendeeObjectChange", reason = "Cannot modify property DTEND"

Stored (abridged):

DTSTART;TZID=America/Los_Angeles:20261017T100000
DURATION:PT30M
ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION:mailto:[email protected]

Apple’s PUT (abridged):

DTSTART;TZID=America/Los_Angeles:20261017T100000
DTEND;TZID=America/Los_Angeles:20261017T103000
ATTENDEE;CUTYPE=INDIVIDUAL;PARTSTAT=ACCEPTED:mailto:[email protected]

Cause

In attendee_handle_update (crates/groupware/src/scheduling/attendee.rs), the name-count check tolerates a value-only change of a property the attendee may not modify. But DTEND going from 0 to 1 occurrence, and DURATION from 1 to 0, count as the attendee adding and removing restricted properties. The file is unchanged on main through v0.16.26.

Reproduction Steps

  1. Organizer creates an event via JMAP CalendarEvent/set with duration: "PT30M" and an attendee on the same server.
  2. Attendee adds the account to Apple Calendar over CalDAV and taps Accept on the invite.
  3. The PUT gets 412 and the event stays NEEDS-ACTION.

Proposed Fix

For the old and new instance, work out the end (DTEND, otherwise DTSTART + DURATION). When they match, skip DTEND/DURATION in the added and removed checks. Moving the end is still rejected. There are two unit tests, one for each case. The existing tests/resources/itip suite passes unchanged. We run this patch on v0.16.23 in production, and Apple Calendar accepts now work end to end.

Disclosure: an AI coding assistant wrote this patch. I tested it, but I’m posting it as a reference for the fix, not as a pull request.

diff --git a/crates/groupware/src/scheduling/attendee.rs b/crates/groupware/src/scheduling/attendee.rs
index fcf3d50..615a143 100644
--- a/crates/groupware/src/scheduling/attendee.rs
+++ b/crates/groupware/src/scheduling/attendee.rs
@@ -51,6 +51,13 @@ pub(crate) fn attendee_handle_update(
                     let old_name_counts = count_entry_names(&old_instance.entries);
                     let new_name_counts = count_entry_names(&instance.entries);
 
+                    // Clients may rewrite DURATION as DTEND (or the reverse) when saving an
+                    // attendee's reply; that is not a change as long as the event ends at the same time
+                    let same_end = matches!(
+                        (effective_end(&old_instance.entries), effective_end(&instance.entries)),
+                        (Some(old_end), Some(new_end)) if old_end == new_end
+                    );
+
                     // Check added fields
                     let mut send_update = false;
                     for new_entry in instance.entries.difference(&old_instance.entries) {
@@ -85,6 +92,9 @@ pub(crate) fn attendee_handle_update(
                                     &instance.comp.component_type,
                                     new_entry.name,
                                 ) {
+                                    if same_end && is_end_property(new_entry.name) {
+                                        continue;
+                                    }
                                     if name_count(&new_name_counts, new_entry.name)
                                         > name_count(&old_name_counts, new_entry.name)
                                     {
@@ -158,8 +168,9 @@ pub(crate) fn attendee_handle_update(
                         if !can_attendee_modify_property(
                             &instance.comp.component_type,
                             removed_entry.name,
-                        ) && name_count(&old_name_counts, removed_entry.name)
-                            > name_count(&new_name_counts, removed_entry.name)
+                        ) && !(same_end && is_end_property(removed_entry.name))
+                            && name_count(&old_name_counts, removed_entry.name)
+                                > name_count(&new_name_counts, removed_entry.name)
                         {
                             // Removing these properties is not allowed
                             return Err(ItipError::CannotModifyProperty(
@@ -375,3 +386,94 @@ fn count_entry_names<'x>(entries: &'x ItipEntries<'x>) -> AHashMap<&'x ICalendar
 fn name_count(counts: &AHashMap<&ICalendarProperty, usize>, name: &ICalendarProperty) -> usize {
     counts.get(name).copied().unwrap_or(0)
 }
+
+fn is_end_property(name: &ICalendarProperty) -> bool {
+    matches!(name, ICalendarProperty::Dtend | ICalendarProperty::Duration)
+}
+
+// When the instance ends, from DTEND or else DTSTART plus DURATION
+fn effective_end(entries: &ItipEntries<'_>) -> Option<i64> {
+    let mut start = None;
+    let mut end = None;
+    let mut duration = None;
+    for entry in entries {
+        match (entry.name, &entry.value) {
+            (ICalendarProperty::Dtstart, ItipEntryValue::DateTime(dt)) => {
+                start = Some(dt.timestamp)
+            }
+            (ICalendarProperty::Dtend, ItipEntryValue::DateTime(dt)) => end = Some(dt.timestamp),
+            (ICalendarProperty::Duration, ItipEntryValue::Duration(d)) => {
+                duration = Some(d.as_seconds())
+            }
+            _ => {}
+        }
+    }
+    end.or_else(|| Some(start? + duration?))
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::scheduling::{ItipError, event_update::itip_update};
+    use calcard::icalendar::{ICalendar, ICalendarProperty};
+
+    // An invite stored with DURATION, as JMAP clients create them
+    const STORED: &str = "BEGIN:VCALENDAR\r
+VERSION:2.0\r
+PRODID:-//Stalwart Labs LLC//Stalwart Server//EN\r
+BEGIN:VEVENT\r
+DTSTAMP:20261015T003841Z\r
+SEQUENCE:0\r
+UID:9921fb3b-cf47-462f-96ce-27909eb4dbef\r
+DTSTART;TZID=America/Los_Angeles:20261017T100000\r
+SUMMARY:Test\r
+DURATION:PT30M\r
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]\r
+ATTENDEE;RSVP=TRUE;PARTSTAT=NEEDS-ACTION:mailto:[email protected]\r
+ORGANIZER:mailto:[email protected]\r
+END:VEVENT\r
+END:VCALENDAR\r
+";
+
+    // The same invite as Apple Calendar writes it back when the attendee accepts
+    fn accepted(dtend: &str) -> String {
+        format!(
+            "BEGIN:VCALENDAR\r
+VERSION:2.0\r
+PRODID:-//Apple Inc.//macOS 27.0.1//EN\r
+BEGIN:VEVENT\r
+ATTENDEE;CUTYPE=INDIVIDUAL;PARTSTAT=ACCEPTED:mailto:[email protected]\r
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:[email protected]\r
+DTEND;TZID=America/Los_Angeles:{dtend}\r
+DTSTAMP:20261015T004802Z\r
+DTSTART;TZID=America/Los_Angeles:20261017T100000\r
+ORGANIZER:mailto:[email protected]\r
+SEQUENCE:0\r
+SUMMARY:Test\r
+UID:9921fb3b-cf47-462f-96ce-27909eb4dbef\r
+END:VEVENT\r
+END:VCALENDAR\r
+"
+        )
+    }
+
+    #[test]
+    fn attendee_may_rewrite_duration_as_dtend() {
+        let old = ICalendar::parse(STORED).unwrap();
+        let mut new = ICalendar::parse(accepted("20261017T103000")).unwrap();
+        let messages = itip_update(&mut new, &old, &["[email protected]".to_string()])
+            .expect("an equivalent DTEND should be accepted");
+        assert_eq!(messages.len(), 1);
+        assert_eq!(messages[0].to, vec!["[email protected]".to_string()]);
+        assert!(messages[0].message.to_string().contains("METHOD:REPLY"));
+    }
+
+    #[test]
+    fn attendee_may_not_move_the_end() {
+        let old = ICalendar::parse(STORED).unwrap();
+        let mut new = ICalendar::parse(accepted("20261017T110000")).unwrap();
+        assert!(matches!(
+            itip_update(&mut new, &old, &["[email protected]".to_string()]),
+            Err(ItipError::CannotModifyProperty(ICalendarProperty::Dtend))
+        ));
+    }
+}

Stalwart Version
v0.16.23 (code path unchanged through v0.16.26)

Installation Method
Docker (official image layout, our build of v0.16.23)

Additional Context
Client: Apple Calendar on macOS 27.0.1 (dataaccessd), CalDAV over HTTPS.

Fixed in v1.0.0 (expected at the end of this month/early November).