|
|
@@ -27,7 +27,6 @@ entry.
|
|
|
|
|
|
import binascii
|
|
|
import contextlib
|
|
|
-import errno
|
|
|
import json
|
|
|
import os
|
|
|
import pickle
|
|
|
@@ -115,6 +114,20 @@ def load(configuration, logger):
|
|
|
return CollectionCopy
|
|
|
|
|
|
|
|
|
+def check_item(vobject_item):
|
|
|
+ """Check vobject items for common errors."""
|
|
|
+ if vobject_item.name == "VCALENDAR":
|
|
|
+ for component in vobject_item.components():
|
|
|
+ if (component.name in ("VTODO", "VEVENT", "VJOURNAL") and
|
|
|
+ not get_uid(component)):
|
|
|
+ raise ValueError("UID in %s is missing" % component.name)
|
|
|
+ elif vobject_item.name == "VCARD":
|
|
|
+ if not get_uid(vobject_item):
|
|
|
+ raise ValueError("UID in VCARD is missing")
|
|
|
+ else:
|
|
|
+ raise ValueError("Unknown item type: %r" % vobject_item.name)
|
|
|
+
|
|
|
+
|
|
|
def scandir(path, only_dirs=False, only_files=False):
|
|
|
"""Iterator for directory elements. (For compatibility with Python < 3.5)
|
|
|
|
|
|
@@ -149,7 +162,7 @@ def get_etag(text):
|
|
|
|
|
|
def get_uid(item):
|
|
|
"""UID value of an item if defined."""
|
|
|
- return hasattr(item, "uid") and item.uid.value
|
|
|
+ return (hasattr(item, "uid") or None) and item.uid.value
|
|
|
|
|
|
|
|
|
def sanitize_path(path):
|
|
|
@@ -541,13 +554,14 @@ class Collection(BaseCollection):
|
|
|
self._sync_directory(directory)
|
|
|
|
|
|
@staticmethod
|
|
|
- def _find_available_file_name(exists_fn):
|
|
|
+ def _find_available_file_name(exists_fn, suffix=""):
|
|
|
# Prevent infinite loop
|
|
|
- for _ in range(10000):
|
|
|
- file_name = hex(getrandbits(32))[2:]
|
|
|
+ for _ in range(1000):
|
|
|
+ file_name = "%016x" % getrandbits(64) + suffix
|
|
|
if not exists_fn(file_name):
|
|
|
return file_name
|
|
|
- raise FileExistsError(errno.EEXIST, "No usable file name found")
|
|
|
+ # something is wrong with the PRNG
|
|
|
+ raise RuntimeError("No unique random sequence found")
|
|
|
|
|
|
@classmethod
|
|
|
def _fsync(cls, fd):
|
|
|
@@ -690,15 +704,19 @@ class Collection(BaseCollection):
|
|
|
new_collection = vobject.iCalendar()
|
|
|
for item in items:
|
|
|
new_collection.add(item)
|
|
|
+ # href must comply to is_safe_filesystem_path_component
|
|
|
+ # and no file name collisions must exist between hrefs
|
|
|
href = self._find_available_file_name(
|
|
|
- vobject_items.get)
|
|
|
+ vobject_items.get, suffix=".ics")
|
|
|
vobject_items[href] = new_collection
|
|
|
self.upload_all_nonatomic(vobject_items)
|
|
|
elif props.get("tag") == "VCARD":
|
|
|
vobject_items = {}
|
|
|
for card in collection:
|
|
|
+ # href must comply to is_safe_filesystem_path_component
|
|
|
+ # and no file name collisions must exist between hrefs
|
|
|
href = self._find_available_file_name(
|
|
|
- vobject_items.get)
|
|
|
+ vobject_items.get, suffix=".vcf")
|
|
|
vobject_items[href] = card
|
|
|
self.upload_all_nonatomic(vobject_items)
|
|
|
|
|
|
@@ -982,8 +1000,13 @@ class Collection(BaseCollection):
|
|
|
cinput_hash = cetag = ctext = ctag = cstart = cend = None
|
|
|
vobject_item = None
|
|
|
if input_hash != cinput_hash:
|
|
|
- vobject_item = Item(self, href=href,
|
|
|
- text=btext.decode(self.encoding)).item
|
|
|
+ try:
|
|
|
+ vobject_item = Item(self, href=href,
|
|
|
+ text=btext.decode(self.encoding)).item
|
|
|
+ check_item(vobject_item)
|
|
|
+ except Exception as e:
|
|
|
+ raise RuntimeError("Failed to parse item %r from %r: %s" %
|
|
|
+ (href, self.path, e)) from e
|
|
|
# Serialize the object again, to normalize the text representation.
|
|
|
# The storage may have been edited externally.
|
|
|
ctext = vobject_item.serialize()
|