Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions caldav/calendarobjectresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,13 @@ def delete(
``if_match=True`` with neither tag cached, or an empty string,
raises ``ValueError`` rather than deleting unconditionally;
so does a cached ETag that is weak, as it can never match.
Where the server deletes on a stale ``If-Match`` anyway
(``delete.if-match`` broken), the ETag is read first and a
changed object refused with ``ETagMismatchError`` before the
DELETE; that leaves a window between the two requests. If that
read fails, nothing is deleted; if it gives no ETag, the DELETE
goes ahead unchecked. A Schedule-Tag precondition is sent as it
is, with no read first.

``precondition`` works as in ``save()``: ``"etag"`` makes
``if_match=True`` prefer ``If-Match`` over a cached Schedule-Tag.
Expand Down Expand Up @@ -1371,8 +1378,59 @@ def delete(
)
else:
headers = {"if-match": if_match}
if self._ignores_delete_if_match(headers):
if self.is_async_client:
return self._async_delete_unless_changed(headers)
cached = self.etag
try:
current = self.get_property(dav.GetEtag())
finally:
self._restore_etag(cached)
self._refuse_changed(current, headers["if-match"])
return self._delete(headers)

def _ignores_delete_if_match(self, headers: dict[str, str]) -> bool:
"""Does the server delete on a stale If-Match (``delete.if-match`` broken)?

Then the ETag is read first, and a stale one refused here: a
window between the read and the DELETE, where the server's own
check has none. ``*`` needs no check, a DELETE of a missing
object fails anyway (``delete.missing``).
"""
etag = headers.get("if-match")
if etag is None or etag == "*":
return False
features = self.client.features if self.client is not None else None
return features is not None and features.is_supported("delete.if-match", str) == "broken"

async def _async_delete_unless_changed(self, headers: dict[str, str]) -> None:
cached = self.etag
try:
current = await self.get_property(dav.GetEtag())
finally:
self._restore_etag(cached)
self._refuse_changed(current, headers["if-match"])
await self._delete(headers)

def _restore_etag(self, cached: str | None) -> None:
"""Put back the ETag the object was read with: the check reads the
server's into ``props``, and a refused delete must not leave a newer
ETag behind for a retry, or a save(), to pass on."""
if cached is None:
self.props.pop(dav.GetEtag.tag, None)
else:
self.props[dav.GetEtag.tag] = cached

def _refuse_changed(self, current: str | None, expected: str) -> None:
"""ETagMismatchError unless ``current`` is ``expected``, compared weakly
(RFC 9110 section 8.8.3.2): the opaque tag is what tells a version.
No ``current`` passes: the server gave nothing to compare."""
if current is not None and current.removeprefix("W/") != expected.removeprefix("W/"):
raise error.ETagMismatchError(
str(self.url),
f"the object has ETag {current}, not {expected}: it changed since it was read",
)

def cancel(self, **kwargs: Any) -> "Self | Coroutine[Any, Any, Self]":
"""Mark the object as cancelled (``STATUS:CANCELLED``) and save it.

Expand Down
74 changes: 66 additions & 8 deletions caldav/compatibility_hints.py

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion caldav/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def _normalize_href(text: str, preserve_at: bool = False) -> str:
return decode(text)


def _only_xml_declaration(raw: bytes | str) -> bool:
"""Is ``raw`` an XML declaration followed by nothing but whitespace?"""
if len(raw) > 256: # a declaration is short; spare a large body the decoding
return False
text = raw.decode("utf-8", "replace") if isinstance(raw, bytes) else raw
text = text.strip().lstrip("\ufeff")
return text.startswith("<?xml") and text.endswith("?>") and text.count("<") == 1


def _validate_status(status: str | None) -> None:
"""Validate a status string like "HTTP/1.1 404 Not Found".

Expand Down Expand Up @@ -387,7 +396,9 @@ def _init_from_response(self, response: "Response", davclient: Any = None) -> No
content_length = int(self.headers["Content-Length"])
except (KeyError, ValueError, TypeError):
content_length = -1
if content_length == 0 or not self._raw:
## Zimbra answers some requests with an XML declaration and no
## document; that is no content, not a malformed document
if content_length == 0 or not self._raw or _only_xml_declaration(self._raw):
self._raw = ""
self.tree = None
log.debug("No content delivered")
Expand Down Expand Up @@ -475,7 +486,11 @@ def _strip_to_multistatus(self) -> _Element | list[_Element]:

(The equivalent of this method could probably be found with a
simple XPath query, but I'm not much into XPath)

A response without content has no responses.
"""
if self.tree is None:
return []
return _strip_to_multistatus(self.tree)

def validate_status(self, status: str) -> None:
Expand Down
1 change: 1 addition & 0 deletions changes/+delete-if-match-ignored.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`delete(if_match=...)` deleted a changed object on servers that ignore `If-Match` on DELETE (SOGo, Zimbra, Bedework, Posteo: `delete.if-match` broken). There it now reads the ETag first and raises `ETagMismatchError` if the object changed; a window remains between the read and the DELETE.
1 change: 1 addition & 0 deletions changes/+todo-values-x-parameter-multiget-deleted.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New server hints: `save-load.todo.status` (RFC 5545's statuses), its child `save-load.todo.status.rfc10051` (PENDING and FAILED) and `save-load.todo.priority`, with the extra keys `values`, `map` and `refused` naming what a task keeps, stores as another, and is refused with; `supported-task-modes`, whether a server advertises RFC 10051 with `CALDAV:supported-task-mode-set`; `save-load.icalendar.x-parameter`; and `multiget.deleted` (Zimbra still returns a deleted object from a calendar-multiget). `save-load.stable-url` has the extra key `uid_ics_components`, for a server naming only some components `<uid>.ics` (Ox: tasks). The Ox and Zimbra profiles carry what the probes found.
1 change: 1 addition & 0 deletions changes/+xml-declaration-only.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A response holding only an XML declaration, as Zimbra gives to a calendar-multiget naming an href without `.ics`, raised lxml's `XMLSyntaxError`. It is now taken as a response without content.
16 changes: 16 additions & 0 deletions tests/test_async_davclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ def test_response_with_empty_content(self) -> None:
assert dav_response.tree is None
assert dav_response._raw == ""

@pytest.mark.parametrize("bom", [b"", b"\xef\xbb\xbf"])
def test_response_with_only_an_xml_declaration(self, bom: bytes) -> None:
"""Zimbra answers a calendar-multiget naming an href without .ics
with 200 and an XML declaration but no document: no content."""
resp = create_mock_response(
content=bom + b'<?xml version="1.0" encoding="UTF-8"?>\n\n',
status_code=200,
headers={"Content-Type": 'text/xml; charset="UTF-8"'},
)

dav_response = DAVResponse(resp)

assert dav_response.status == 200
assert dav_response.tree is None
assert dav_response.expand_simple_props([]) == {}

def test_response_with_non_xml_content(self) -> None:
"""Test response with non-XML content."""
resp = create_mock_response(
Expand Down
118 changes: 118 additions & 0 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7599,6 +7599,124 @@ def test_unknown_precondition_is_refused(self, base, value) -> None:
assert sent == []


class TestDeleteIfMatchIgnored:
"""Where ``delete.if-match`` is broken, the server deletes on a stale
If-Match (SOGo, Zimbra, Bedework, Posteo). delete(if_match=...) then
reads the current ETag first and refuses a stale one itself, as save()
stands in for the wildcard preconditions a server gets wrong."""

URL = "http://cal.example.com/dav/user/mycal/ev1.ics"

@pytest.fixture(params=["sync", "async"])
def base(self, request):
if request.param == "sync":
return DAVClient
from caldav.async_davclient import AsyncDAVClient

return AsyncDAVClient

@staticmethod
def _etag_multistatus(etag):
return (
'<?xml version="1.0" encoding="utf-8"?>\n<D:multistatus xmlns:D="DAV:">'
"<D:response><D:href>/dav/user/mycal/ev1.ics</D:href><D:propstat><D:prop>"
f"<D:getetag>{etag}</D:getetag></D:prop>"
"<D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>"
).encode()

def _event(self, base, stored_etag, support="broken", propfind_status=207):
sent = []
features = {"delete.if-match": {"support": support}}
client = base(url="http://cal.example.com/dav/", features=features)

def request(url, method="GET", body="", headers=None):
method = method.upper()
sent.append((method, {k.lower(): v for k, v in (headers or {}).items()}))
resp = mock.MagicMock()
resp.reason = "whatever"
if method == "PROPFIND":
resp.status_code = propfind_status
resp.content = (
self._etag_multistatus(stored_etag) if propfind_status == 207 else b""
)
else:
resp.status_code = 204
resp.content = b""
resp.headers = {"Content-Type": "application/xml"} if resp.content else {}
resp.text = resp.content.decode()
return DAVResponse(resp, client)

if base is DAVClient:
client.request = request
else:

async def async_request(*args, **kwargs):
return request(*args, **kwargs)

client.request = async_request
cal = Calendar(client, url="http://cal.example.com/dav/user/mycal/")
event = Event(client, url=self.URL, data=ev1, parent=cal)
event.props[dav.GetEtag.tag] = '"etag-a"'
return event, sent

_run = staticmethod(TestConditionalWrite._run)

def test_stale_etag_is_refused_before_the_delete(self, base) -> None:
event, sent = self._event(base, stored_etag='"etag-b"')
with pytest.raises(error.ETagMismatchError):
self._run(event, if_match=True)
assert [m for m, _ in sent] == ["PROPFIND"]

def test_refusal_keeps_the_etag_the_object_was_read_with(self, base) -> None:
"""The newer ETag from the check must not let a retry, or a save(), pass."""
event, sent = self._event(base, stored_etag='"etag-b"')
with pytest.raises(error.ETagMismatchError):
self._run(event, if_match=True)
assert event.etag == '"etag-a"'
with pytest.raises(error.ETagMismatchError):
self._run(event, if_match=True)
assert [m for m, _ in sent] == ["PROPFIND", "PROPFIND"]

def test_current_etag_deletes(self, base) -> None:
event, sent = self._event(base, stored_etag='"etag-a"')
self._run(event, if_match='"etag-a"')
assert [m for m, _ in sent] == ["PROPFIND", "DELETE"]
assert sent[-1][1]["if-match"] == '"etag-a"'

def test_weak_form_of_the_same_etag_deletes(self, base) -> None:
event, sent = self._event(base, stored_etag='W/"etag-a"')
self._run(event, if_match=True)
assert [m for m, _ in sent] == ["PROPFIND", "DELETE"]

def test_missing_object_raises_not_found(self, base) -> None:
event, sent = self._event(base, stored_etag=None, propfind_status=404)
with pytest.raises(error.NotFoundError):
self._run(event, if_match=True)
assert [m for m, _ in sent] == ["PROPFIND"]

def test_wildcard_needs_no_check(self, base) -> None:
event, sent = self._event(base, stored_etag='"etag-b"')
self._run(event, if_match="*")
assert [m for m, _ in sent] == ["DELETE"]

@pytest.mark.parametrize("support", ["full", "unsupported"])
def test_server_honouring_if_match_is_not_asked_first(self, base, support) -> None:
"""'unsupported' refuses the current etag too: a check first would not help."""
event, sent = self._event(base, stored_etag='"etag-b"', support=support)
self._run(event, if_match=True)
assert [m for m, _ in sent] == ["DELETE"]

@pytest.mark.parametrize(
"server", ["sogo", "zimbra_8_8", "bedework_3_10_3", "bedework_5_0_0", "posteo"]
)
def test_hints_of_servers_ignoring_if_match_on_delete(self, server) -> None:
from caldav import compatibility_hints
from caldav.compatibility_hints import FeatureSet

fs = FeatureSet(getattr(compatibility_hints, server))
assert fs.is_supported("delete.if-match", str) == "broken"


class TestDelayedPropertyWrite:
"""``synchronous-write.proppatch`` declared unsupported with a ``delay``:
a PROPPATCH is answered at once but a PROPFIND keeps returning the old
Expand Down
36 changes: 36 additions & 0 deletions tests/test_compatibility_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,39 @@ def test_fresh_account_has_no_calendar(self, profile: str) -> None:

features = FeatureSet(getattr(compatibility_hints, profile))
assert features.is_supported(self.HAS_CAL) is False


class TestTodoStatusHints:
"""RFC 5545's statuses and RFC 10051's are hinted apart, so a server keeping
the first and not the second (Ox) is full on the parent."""

STATUS = "save-load.todo.status"
RFC10051 = "save-load.todo.status.rfc10051"

def test_ox_keeps_the_rfc_5545_statuses_only(self) -> None:
features = FeatureSet(ch.ox)
assert features.is_supported(self.STATUS, str) == "full"
assert features.is_supported(self.RFC10051, dict)["values"] == []

def test_zimbra_refuses_cancelled_and_maps_the_rfc_10051_statuses(self) -> None:
features = FeatureSet(ch.zimbra_8_8)
assert features.is_supported(self.STATUS, dict)["refused"] == ["CANCELLED"]
assert features.is_supported(self.RFC10051, dict)["map"] == {
"PENDING": "NEEDS-ACTION",
"FAILED": "NEEDS-ACTION",
}

@pytest.mark.parametrize("profile", ["ox", "zimbra_8_8"])
def test_a_profile_stating_the_parent_states_the_child(self, profile: str) -> None:
"""The child would otherwise inherit the parent's node, values and all."""
hints = getattr(ch, profile)
assert self.STATUS not in hints or self.RFC10051 in hints

def test_rfc_10051_statuses_unknown_unless_probed(self) -> None:
"""An optional extension: a server not probed is not known to keep them."""
assert FeatureSet({}).is_supported(self.RFC10051, str) == "unknown"

def test_task_modes_unknown_unless_probed(self) -> None:
"""RFC 10051: absent means unsupported, but an unprobed server is not known to lack it."""
assert FeatureSet({}).is_supported("supported-task-modes", str) == "unknown"
assert FeatureSet(ch.ox).is_supported("supported-task-modes", str) == "unsupported"
Loading