From cb8583b628f8966d1476104ee3a6e7856655b68c Mon Sep 17 00:00:00 2001 From: Alcides1123 Date: Wed, 11 Nov 2026 21:45:29 -0500 Subject: [PATCH] Report a plist with an unknown encoding instead of crashing --- CHANGES.md | 4 ++++ src/markfind/importers.py | 9 ++++++++- tests/test_safari.py | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index cd7f4c9..f6dcafe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +- Fixed: `markfind import` crashed with a traceback on a property list whose + XML declaration named an unknown encoding. It is now reported like any other + unreadable file. Found by fuzzing the parsers (#78). + - Fixed: a bookmark pointing at a page that only redirects, through ``, was indexed as the stub. One real example indexed 28 characters of "Click here to be redirected." instead of the diff --git a/src/markfind/importers.py b/src/markfind/importers.py index c0a05c7..0705612 100644 --- a/src/markfind/importers.py +++ b/src/markfind/importers.py @@ -324,7 +324,14 @@ def parse_safari_plist(data: bytes) -> list[Bookmark]: """Parse Safari's ``Bookmarks.plist`` (binary or XML property list).""" try: root = plistlib.loads(data) - except (plistlib.InvalidFileException, ExpatError, ValueError, OverflowError) as exc: + # LookupError: the XML declaration names an encoding Python doesn't have. + except ( + plistlib.InvalidFileException, + ExpatError, + ValueError, + OverflowError, + LookupError, + ) as exc: raise ValueError(f"not a readable Safari bookmarks file: {exc}") from exc if not isinstance(root, dict): raise ValueError("not a readable Safari bookmarks file: unexpected contents") diff --git a/tests/test_safari.py b/tests/test_safari.py index ade3d63..64e93eb 100644 --- a/tests/test_safari.py +++ b/tests/test_safari.py @@ -83,6 +83,8 @@ def test_load_file_detects_plists(tmp_path, fmt): [ b"bplist00 this is not really a binary plist", b'x', + # An encoding Python doesn't have: plistlib raises LookupError, not ValueError. + b'', plistlib.dumps(["a list, not a dict"]), ], ) @@ -101,6 +103,13 @@ def test_binary_plist_written_by_apples_own_tool(tmp_path): assert load_file(path) == EXPECTED +def test_a_corrupt_plist_file_is_reported_not_raised(tmp_path, capsys): + path = tmp_path / "Bookmarks.plist" + path.write_bytes(b'') + assert cli.main(["--db", str(tmp_path / "db.sqlite"), "import", str(path)]) == 1 + assert "could not read" in capsys.readouterr().err + + def test_other_formats_still_detected(tmp_path): html = tmp_path / "export.html" html.write_text('
A', encoding="utf-8")