Skip to content

get_calendars() crashes when calendar-home-set contains multiple hrefs #888

Done

Description

@ejrichards

Disclosure: The root cause investigation and suggested fix were produced by an LLM from my traceback and RFCs 4791 / 4918. This was an interactive session, not an autonomous agent. I can personally confirm I hit the bug with Stalwart and Home Assistant's CalDAV integration.

We cannot turn on Stalwart's server-wide spec-breaking enableAssistedDiscovery setting, as it breaks other CalDAV clients like pimsync.

Stalwart source: https://github.com/stalwartlabs/stalwart

Below is LLM text:

Description

RFC 4791 §6.2.1 defines <!ELEMENT calendar-home-set (DAV:href*)>, so the property may hold more than one href. When a server returns more than one, get_principal().get_calendars() raises AttributeError: 'list' object has no attribute 'startswith'.

In current master, _element_to_value() in caldav/response.py returns a list for a multi-href calendar-home-set:

if tag == cdav.CalendarHomeSet.tag:
    hrefs = [child.text for child in elem if child.tag == dav.Href.tag and child.text]
    return hrefs[0] if len(hrefs) == 1 else hrefs

_calendar_home_urls() in caldav/base_client.py passes that value to _make_absolute_url(), which expects a string:

calendar_home_url = extract_home_set(home_set_response.results, features=self.features)
if calendar_home_url:
    urls = [self._make_absolute_url(calendar_home_url)]

Server and setup

Stalwart offers two ways to expose calendars shared with a user from other accounts, controlled by the WebDav setting enableAssistedDiscovery:

  • On (Stalwart's default): a Depth: 1 PROPFIND on the user's home (/dav/cal/bob/) also returns other accounts' collections, such as /dav/cal/jane/default/. This breaks RFC 4918. Depth: 1 covers "the resource and its internal members only" (§10.2), and an internal member URL is "the URL of the collection (including trailing slash) plus the path segment identifying the internal member" (§3). Stalwart's source says so too (crates/dav/src/common/propfind/get.rs): "This is invalid but it's the only workaround for clients which do not support multiple home-sets."
  • Off: build_home_set() in crates/dav/src/principal/propfind.rs returns the user's own home first, then one href per account that shares calendars with them, all in one calendar-home-set. The hrefs have the form /dav/cal/<account>/. This is the mode that triggers the crash.

So a server that avoids the spec-breaking discovery mode cannot be used with this library by any user who has shared calendars.

Traceback

Via Home Assistant's CalDAV integration on version 2026.11.3:

  File ".../caldav/collection.py", line 728, in get_calendars
    return self.client.get_calendars(self)
  File ".../caldav/davclient.py", line 489, in get_calendars
    for n, calendar_home_url in enumerate(self._calendar_home_urls(response, principal)):
  File ".../caldav/base_client.py", line 468, in _calendar_home_urls
    urls = [self._make_absolute_url(calendar_home_url)]
  File ".../caldav/base_client.py", line 147, in _make_absolute_url
    if url and not url.startswith("http"):
AttributeError: 'list' object has no attribute 'startswith'

Expected

get_calendars() returns the calendars found under every href in calendar-home-set.

Suggested fix

  1. In _calendar_home_urls(), accept a list and make each href absolute.
  2. In DAVClient.get_calendars(), the loop currently stops at the first URL that yields calendars (if calendars: break). That suits the fallback guesses used when there's no home-set, but not real home-set hrefs. Those should all be queried and their results combined.

Activity

  1. tobixen commented on Dec 2, 2026

    @tobixen
    Member

    I've had Claude reproduce this with some unit tests. Although Stalwart is part of the test suite, it doesn't quite make sense to reproduce this in an integration test as it would require special configuration of Stalwart to reproduce the problem.

    I'm soon going to bed, but I'll try to get the work done submitted so you can build a PR on top of it if you want.

  2. tobixen commented on Dec 2, 2026

    @tobixen
    Member

    Disclaimer: AI-generated text below the line, generated by Claude Opus 5.5, from the prompts:

    Please try to reproduce #888

    You had some arguments against the proposed fix (...) draft a comment.


    Thanks for the report. Reproduced against a local Stalwart container: with enableAssistedDiscovery off and a calendar shared from user1 to testuser through a WebDAV ACL request, testuser's principal answers

    <A:calendar-home-set>
      <D:href>/dav/cal/testuser%40example.org/</D:href>
      <D:href>/dav/cal/user1%40example.org/</D:href>
    </A:calendar-home-set>

    and get_calendars() crashes as described. principal.calendar_home_set crashes too (the list reaches urllib), so it is not only the get_calendars() path, and the async client has its own copy of both. Failing unit tests are in draft PR #889; a Stalwart integration test would need the server-wide setting flipped in the docker setup, so it is left out for now.

    On the suggested fix, I think it is the right direction:

    • RFC 4791 section 6.2.1 explicitly allows several hrefs, and nothing in it ranks one over the others, so querying all of them is the faithful reading. Taking only the first would silently drop the shared calendars, which is worse than the crash: nobody would notice.
    • It is the only way for a Stalwart user to see shared calendars without the assisted-discovery mode, which breaks RFC 4918 and other clients.
    • The if calendars: break in get_calendars() exists for the guessed URLs (principal URL, client URL) used when no home-set is given. Hrefs the server lists are authoritative, so combining their results is a different case, not a change to the fallback.
    • It costs nothing for the common case: servers listing one href get exactly the same requests as today, and one extra PROPFIND per extra home otherwise.
    • Stalwart lists the user's own home first, so the user's own calendars stay first in the returned list.

    The one thing I would keep: principal.calendar_home_set (used by make_calendar() and friends) should stay a single collection, the first href. Creating a calendar should not land in somebody else's home, and changing it to a list would be an API break. The draft tests encode that choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions