__init__.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2014 Jean-Marc Martins
  5. # Copyright © 2008-2017 Guillaume Ayoub
  6. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  7. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  8. #
  9. # This library is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  21. """
  22. Module for address books and calendar entries (see ``Item``).
  23. """
  24. import binascii
  25. import contextlib
  26. import math
  27. import os
  28. import re
  29. from datetime import datetime, timedelta
  30. from hashlib import sha256
  31. from itertools import chain
  32. from typing import (Any, Callable, List, MutableMapping, Optional, Sequence,
  33. Tuple)
  34. import vobject
  35. from radicale import storage # noqa:F401
  36. from radicale import pathutils, utils
  37. from radicale.item import filter as radicale_filter
  38. from radicale.log import logger
  39. def read_components(s: str) -> List[vobject.base.Component]:
  40. """Wrapper for vobject.readComponents"""
  41. # Workaround for bug in InfCloud
  42. # PHOTO is a data URI
  43. s = re.sub(r"^(PHOTO(?:;[^:\r\n]*)?;ENCODING=b(?:;[^:\r\n]*)?:)"
  44. r"data:[^;,\r\n]*;base64,", r"\1", s,
  45. flags=re.MULTILINE | re.IGNORECASE)
  46. # Workaround for bug with malformed ICS files containing control codes
  47. # Filter out all control codes except those we expect to find:
  48. # * 0x09 Horizontal Tab
  49. # * 0x0A Line Feed
  50. # * 0x0D Carriage Return
  51. s = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F]', '', s)
  52. return list(vobject.readComponents(s, allowQP=True))
  53. def predict_tag_of_parent_collection(
  54. vobject_items: Sequence[vobject.base.Component]) -> Optional[str]:
  55. """Returns the predicted tag or `None`"""
  56. if len(vobject_items) != 1:
  57. return None
  58. if vobject_items[0].name == "VCALENDAR":
  59. return "VCALENDAR"
  60. if vobject_items[0].name in ("VCARD", "VLIST"):
  61. return "VADDRESSBOOK"
  62. return None
  63. def predict_tag_of_whole_collection(
  64. vobject_items: Sequence[vobject.base.Component],
  65. fallback_tag: Optional[str] = None) -> Optional[str]:
  66. """Returns the predicted tag or `fallback_tag`"""
  67. if vobject_items and vobject_items[0].name == "VCALENDAR":
  68. return "VCALENDAR"
  69. if vobject_items and vobject_items[0].name in ("VCARD", "VLIST"):
  70. return "VADDRESSBOOK"
  71. if not fallback_tag and not vobject_items:
  72. # Maybe an empty address book
  73. return "VADDRESSBOOK"
  74. return fallback_tag
  75. def check_and_sanitize_items(
  76. vobject_items: List[vobject.base.Component],
  77. is_collection: bool = False, tag: str = "") -> None:
  78. """Check vobject items for common errors and add missing UIDs.
  79. Modifies the list `vobject_items`.
  80. ``is_collection`` indicates that vobject_item contains unrelated
  81. components.
  82. The ``tag`` of the collection.
  83. """
  84. if tag and tag not in ("VCALENDAR", "VADDRESSBOOK", "VSUBSCRIBED"):
  85. raise ValueError("Unsupported collection tag: %r" % tag)
  86. if not is_collection and len(vobject_items) != 1:
  87. raise ValueError("Item contains %d components" % len(vobject_items))
  88. if tag == "VCALENDAR":
  89. if len(vobject_items) > 1:
  90. raise RuntimeError("VCALENDAR collection contains %d "
  91. "components" % len(vobject_items))
  92. vobject_item = vobject_items[0]
  93. if vobject_item.name != "VCALENDAR":
  94. raise ValueError("Item type %r not supported in %r "
  95. "collection" % (vobject_item.name, tag))
  96. component_uids = set()
  97. for component in vobject_item.components():
  98. if component.name in ("VTODO", "VEVENT", "VJOURNAL"):
  99. component_uid = get_uid(component)
  100. if component_uid:
  101. component_uids.add(component_uid)
  102. component_name = None
  103. object_uid = None
  104. object_uid_set = False
  105. for component in vobject_item.components():
  106. # https://tools.ietf.org/html/rfc4791#section-4.1
  107. if component.name == "VTIMEZONE":
  108. continue
  109. if component_name is None or is_collection:
  110. component_name = component.name
  111. elif component_name != component.name:
  112. raise ValueError("Multiple component types in object: %r, %r" %
  113. (component_name, component.name))
  114. if component_name not in ("VTODO", "VEVENT", "VJOURNAL"):
  115. continue
  116. component_uid = get_uid(component)
  117. if not object_uid_set or is_collection:
  118. object_uid_set = True
  119. object_uid = component_uid
  120. if not component_uid:
  121. if not is_collection:
  122. raise ValueError("%s component without UID in object" %
  123. component_name)
  124. component_uid = find_available_uid(
  125. component_uids.__contains__)
  126. component_uids.add(component_uid)
  127. if hasattr(component, "uid"):
  128. component.uid.value = component_uid
  129. else:
  130. component.add("UID").value = component_uid
  131. elif not object_uid or not component_uid:
  132. raise ValueError("Multiple %s components without UID in "
  133. "object" % component_name)
  134. elif object_uid != component_uid:
  135. raise ValueError(
  136. "Multiple %s components with different UIDs in object: "
  137. "%r, %r" % (component_name, object_uid, component_uid))
  138. # Workaround for bug in Lightning (Thunderbird)
  139. # Rescheduling a single occurrence from a repeating event creates
  140. # an event with DTEND and DURATION:PT0S
  141. if (hasattr(component, "dtend") and
  142. hasattr(component, "duration") and
  143. component.duration.value == timedelta(0)):
  144. logger.debug("Quirks: Removing zero duration from %s in "
  145. "object %r", component_name, component_uid)
  146. del component.duration
  147. # Workaround for Evolution
  148. # EXDATE has value DATE even if DTSTART/DTEND is DATE-TIME.
  149. # The RFC is vaguely formulated on the issue.
  150. # To resolve the issue convert EXDATE and RDATE to
  151. # the same type as DTDSTART
  152. if hasattr(component, "dtstart"):
  153. ref_date = component.dtstart.value
  154. ref_value_param = component.dtstart.params.get("VALUE")
  155. for dates in chain(component.contents.get("exdate", []),
  156. component.contents.get("rdate", [])):
  157. if all(type(d) is type(ref_date) for d in dates.value):
  158. continue
  159. for i, date in enumerate(dates.value):
  160. dates.value[i] = ref_date.replace(
  161. date.year, date.month, date.day)
  162. with contextlib.suppress(KeyError):
  163. del dates.params["VALUE"]
  164. if ref_value_param is not None:
  165. dates.params["VALUE"] = ref_value_param
  166. # vobject interprets recurrence rules on demand
  167. try:
  168. component.rruleset
  169. except Exception as e:
  170. raise ValueError("Invalid recurrence rules in %s in object %r"
  171. % (component.name, component_uid)) from e
  172. elif tag == "VADDRESSBOOK":
  173. # https://tools.ietf.org/html/rfc6352#section-5.1
  174. object_uids = set()
  175. for vobject_item in vobject_items:
  176. if vobject_item.name == "VCARD":
  177. object_uid = get_uid(vobject_item)
  178. if object_uid:
  179. object_uids.add(object_uid)
  180. for vobject_item in vobject_items:
  181. if vobject_item.name == "VLIST":
  182. # Custom format used by SOGo Connector to store lists of
  183. # contacts
  184. continue
  185. if vobject_item.name != "VCARD":
  186. raise ValueError("Item type %r not supported in %r "
  187. "collection" % (vobject_item.name, tag))
  188. object_uid = get_uid(vobject_item)
  189. if not object_uid:
  190. if not is_collection:
  191. raise ValueError("%s object without UID" %
  192. vobject_item.name)
  193. object_uid = find_available_uid(object_uids.__contains__)
  194. object_uids.add(object_uid)
  195. if hasattr(vobject_item, "uid"):
  196. vobject_item.uid.value = object_uid
  197. else:
  198. vobject_item.add("UID").value = object_uid
  199. else:
  200. for item in vobject_items:
  201. raise ValueError("Item type %r not supported in %s collection" %
  202. (item.name, repr(tag) if tag else "generic"))
  203. def check_and_sanitize_props(props: MutableMapping[Any, Any]
  204. ) -> MutableMapping[str, str]:
  205. """Check collection properties for common errors.
  206. Modifies the dict `props`.
  207. """
  208. for k, v in list(props.items()): # Make copy to be able to delete items
  209. if not isinstance(k, str):
  210. raise ValueError("Key must be %r not %r: %r" % (
  211. str.__name__, type(k).__name__, k))
  212. if not isinstance(v, str):
  213. if v is None:
  214. del props[k]
  215. continue
  216. raise ValueError("Value of %r must be %r not %r: %r" % (
  217. k, str.__name__, type(v).__name__, v))
  218. if k == "tag":
  219. if v not in ("", "VCALENDAR", "VADDRESSBOOK", "VSUBSCRIBED"):
  220. raise ValueError("Unsupported collection tag: %r" % v)
  221. return props
  222. def find_available_uid(exists_fn: Callable[[str], bool], suffix: str = ""
  223. ) -> str:
  224. """Generate a pseudo-random UID"""
  225. # Prevent infinite loop
  226. for _ in range(1000):
  227. r = binascii.hexlify(os.urandom(16)).decode("ascii")
  228. name = "%s-%s-%s-%s-%s%s" % (
  229. r[:8], r[8:12], r[12:16], r[16:20], r[20:], suffix)
  230. if not exists_fn(name):
  231. return name
  232. # Something is wrong with the PRNG or `exists_fn`
  233. raise RuntimeError("No available random UID found")
  234. def get_etag(text: str) -> str:
  235. """Etag from collection or item.
  236. Encoded as quoted-string (see RFC 2616).
  237. """
  238. etag = sha256()
  239. etag.update(text.encode())
  240. return '"%s"' % etag.hexdigest()
  241. def get_uid(vobject_component: vobject.base.Component) -> str:
  242. """UID value of an item if defined."""
  243. return (vobject_component.uid.value or ""
  244. if hasattr(vobject_component, "uid") else "")
  245. def get_uid_from_object(vobject_item: vobject.base.Component) -> str:
  246. """UID value of an calendar/addressbook object."""
  247. if vobject_item.name == "VCALENDAR":
  248. if hasattr(vobject_item, "vevent"):
  249. return get_uid(vobject_item.vevent)
  250. if hasattr(vobject_item, "vjournal"):
  251. return get_uid(vobject_item.vjournal)
  252. if hasattr(vobject_item, "vtodo"):
  253. return get_uid(vobject_item.vtodo)
  254. elif vobject_item.name == "VCARD":
  255. return get_uid(vobject_item)
  256. return ""
  257. def find_tag(vobject_item: vobject.base.Component) -> str:
  258. """Find component name from ``vobject_item``."""
  259. if vobject_item.name == "VCALENDAR":
  260. for component in vobject_item.components():
  261. if component.name != "VTIMEZONE":
  262. return component.name or ""
  263. return ""
  264. def find_time_range(vobject_item: vobject.base.Component, tag: str
  265. ) -> Tuple[int, int]:
  266. """Find enclosing time range from ``vobject item``.
  267. ``tag`` must be set to the return value of ``find_tag``.
  268. Returns a tuple (``start``, ``end``) where ``start`` and ``end`` are
  269. POSIX timestamps.
  270. This is intended to be used for matching against simplified prefilters.
  271. """
  272. if not tag:
  273. return radicale_filter.TIMESTAMP_MIN, radicale_filter.TIMESTAMP_MAX
  274. start = end = None
  275. def range_fn(range_start: datetime, range_end: datetime,
  276. is_recurrence: bool) -> bool:
  277. nonlocal start, end
  278. if start is None or range_start < start:
  279. start = range_start
  280. if end is None or end < range_end:
  281. end = range_end
  282. return False
  283. def infinity_fn(range_start: datetime) -> bool:
  284. nonlocal start, end
  285. if start is None or range_start < start:
  286. start = range_start
  287. end = radicale_filter.DATETIME_MAX
  288. return True
  289. radicale_filter.visit_time_ranges(vobject_item, tag, range_fn, infinity_fn)
  290. if start is None:
  291. start = radicale_filter.DATETIME_MIN
  292. if end is None:
  293. end = radicale_filter.DATETIME_MAX
  294. return math.floor(start.timestamp()), math.ceil(end.timestamp())
  295. def verify(file: str, encoding: str):
  296. logger.info("Verifying item: %s", file)
  297. with open(file, "rb") as f:
  298. content_raw = f.read()
  299. content = content_raw.decode(encoding)
  300. logger.info("Verifying item: %s has sha256sum %r", file, utils.sha256_bytes(content_raw))
  301. try:
  302. vobject_items = read_components(content) # noqa: F841
  303. except Exception as e:
  304. logger.error("Verifying item: %s problem: %s", file, e)
  305. logger.warning("Item content:\n%s", utils.textwrap_str(content))
  306. logger.info("Item content (hexdump):\n%s", utils.hexdump_str(content))
  307. logger.info("Item content (hexdump/lines):\n%s", utils.hexdump_lines(content))
  308. return False
  309. else:
  310. logger.info("Verifying item: %s successful", file)
  311. return True
  312. class Item:
  313. """Class for address book and calendar entries."""
  314. collection: Optional["storage.BaseCollection"]
  315. href: Optional[str]
  316. last_modified: Optional[str]
  317. _collection_path: str
  318. _text: Optional[str]
  319. _vobject_item: Optional[vobject.base.Component]
  320. _etag: Optional[str]
  321. _uid: Optional[str]
  322. _name: Optional[str]
  323. _component_name: Optional[str]
  324. _time_range: Optional[Tuple[int, int]]
  325. def __init__(self,
  326. collection_path: Optional[str] = None,
  327. collection: Optional["storage.BaseCollection"] = None,
  328. vobject_item: Optional[vobject.base.Component] = None,
  329. href: Optional[str] = None,
  330. last_modified: Optional[str] = None,
  331. text: Optional[str] = None,
  332. etag: Optional[str] = None,
  333. uid: Optional[str] = None,
  334. name: Optional[str] = None,
  335. component_name: Optional[str] = None,
  336. time_range: Optional[Tuple[int, int]] = None):
  337. """Initialize an item.
  338. ``collection_path`` the path of the parent collection (optional if
  339. ``collection`` is set).
  340. ``collection`` the parent collection (optional).
  341. ``href`` the href of the item.
  342. ``last_modified`` the HTTP-datetime of when the item was modified.
  343. ``text`` the text representation of the item (optional if
  344. ``vobject_item`` is set).
  345. ``vobject_item`` the vobject item (optional if ``text`` is set).
  346. ``etag`` the etag of the item (optional). See ``get_etag``.
  347. ``uid`` the UID of the object (optional). See ``get_uid_from_object``.
  348. ``name`` the name of the item (optional). See ``vobject_item.name``.
  349. ``component_name`` the name of the primary component (optional).
  350. See ``find_tag``.
  351. ``time_range`` the enclosing time range. See ``find_time_range``.
  352. """
  353. if text is None and vobject_item is None:
  354. raise ValueError(
  355. "At least one of 'text' or 'vobject_item' must be set")
  356. if collection_path is None:
  357. if collection is None:
  358. raise ValueError("At least one of 'collection_path' or "
  359. "'collection' must be set")
  360. collection_path = collection.path
  361. assert collection_path == pathutils.strip_path(
  362. pathutils.sanitize_path(collection_path))
  363. self._collection_path = collection_path
  364. self.collection = collection
  365. self.href = href
  366. self.last_modified = last_modified
  367. self._text = text
  368. self._vobject_item = vobject_item
  369. self._etag = etag
  370. self._uid = uid
  371. self._name = name
  372. self._component_name = component_name
  373. self._time_range = time_range
  374. def serialize(self) -> str:
  375. if self._text is None:
  376. try:
  377. self._text = self.vobject_item.serialize()
  378. except Exception as e:
  379. raise RuntimeError("Failed to serialize item %r from %r: %s" %
  380. (self.href, self._collection_path,
  381. e)) from e
  382. return self._text
  383. @property
  384. def vobject_item(self):
  385. if self._vobject_item is None:
  386. try:
  387. self._vobject_item = vobject.readOne(self._text)
  388. except Exception as e:
  389. raise RuntimeError("Failed to parse item %r from %r: %s" %
  390. (self.href, self._collection_path,
  391. e)) from e
  392. return self._vobject_item
  393. @property
  394. def etag(self) -> str:
  395. """Encoded as quoted-string (see RFC 2616)."""
  396. if self._etag is None:
  397. self._etag = get_etag(self.serialize())
  398. return self._etag
  399. @property
  400. def uid(self) -> str:
  401. if self._uid is None:
  402. self._uid = get_uid_from_object(self.vobject_item)
  403. return self._uid
  404. @property
  405. def name(self) -> str:
  406. if self._name is None:
  407. self._name = self.vobject_item.name or ""
  408. return self._name
  409. @property
  410. def component_name(self) -> str:
  411. if self._component_name is None:
  412. self._component_name = find_tag(self.vobject_item)
  413. return self._component_name
  414. @property
  415. def time_range(self) -> Tuple[int, int]:
  416. if self._time_range is None:
  417. self._time_range = find_time_range(
  418. self.vobject_item, self.component_name)
  419. return self._time_range
  420. def prepare(self) -> None:
  421. """Fill cache with values."""
  422. orig_vobject_item = self._vobject_item
  423. self.serialize()
  424. self.etag
  425. self.uid
  426. self.name
  427. self.time_range
  428. self.component_name
  429. self._vobject_item = orig_vobject_item