Merge pull request #1103 from jasonccox/main

Require matching BEGIN and END lines in vobjects
This commit is contained in:
Hugo 2024-01-28 20:13:28 +01:00 committed by GitHub
commit 762d369560
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 0 deletions

View file

@ -9,6 +9,7 @@ In alphabetical order:
- Corey Hinshaw
- Kai Herlemann
- Hugo Osvaldo Barrera
- Jason Cox
- Julian Mehne
- Malte Kiefer
- Marek Marczykowski-Górecki

View file

@ -15,6 +15,7 @@ Version 0.19.3
- Added a no_delete option to the storage configuration. :gh:`1090`
- Fix crash when running ``vdirsyncer repair`` on a collection. :gh:`1019`
- Add an option to request vCard v4.0. :gh:`1066`
- Require matching ``BEGIN`` and ``END`` lines in vobjects. :gh:`1103`
Version 0.19.2
==============

View file

@ -237,6 +237,31 @@ def test_broken_item():
assert item.parsed is None
def test_mismatched_end():
with pytest.raises(ValueError) as excinfo:
vobject._Component.parse(
[
"BEGIN:FOO",
"END:BAR",
]
)
assert "Got END:BAR, expected END:FOO at line 2" in str(excinfo.value)
def test_missing_end():
with pytest.raises(ValueError) as excinfo:
vobject._Component.parse(
[
"BEGIN:FOO",
"BEGIN:BAR",
"END:BAR",
]
)
assert "Missing END for component(s): FOO" in str(excinfo.value)
def test_multiple_items():
with pytest.raises(ValueError) as excinfo:
vobject._Component.parse(

View file

@ -281,6 +281,12 @@ class _Component:
stack.append(cls(c_name, [], []))
elif line.startswith("END:"):
component = stack.pop()
c_name = line[len("END:") :].strip().upper()
if c_name != component.name:
raise ValueError(
f"Got END:{c_name}, expected END:{component.name}"
+ f" at line {_i + 1}"
)
if stack:
stack[-1].subcomponents.append(component)
else:
@ -291,6 +297,11 @@ class _Component:
except IndexError:
raise ValueError(f"Parsing error at line {_i + 1}")
if len(stack) > 0:
raise ValueError(
f"Missing END for component(s): {', '.join(c.name for c in stack)}"
)
if multiple:
return rv
elif len(rv) != 1: