From 5facc0fb32b61ca2289f923a7ea64f7a4443309d Mon Sep 17 00:00:00 2001 From: chelsealong Date: Tue, 24 Nov 2026 17:06:07 +0000 Subject: [PATCH 1/4] fix: accept UTF-8 BOM in bundler JSON readers --- src/specify_cli/bundles/yamlio.py | 4 +-- .../bundles/catalog/test_command_add.py | 17 ++++++++++- tests/specify_cli/bundles/test_yamlio.py | 29 ++++++++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/specify_cli/bundles/yamlio.py b/src/specify_cli/bundles/yamlio.py index 381e9a5624..ccf3f77797 100644 --- a/src/specify_cli/bundles/yamlio.py +++ b/src/specify_cli/bundles/yamlio.py @@ -101,7 +101,7 @@ def load_json(path: Path) -> Any: if not path.exists(): raise BundlerError(f"File not found: {path}") try: - with path.open("r", encoding="utf-8") as handle: + with path.open("r", encoding="utf-8-sig") as handle: return json.load(handle) # JSONDecodeError stays FIRST: it and UnicodeDecodeError are sibling # ValueError subclasses (neither subsumes the other), so malformed-but- @@ -128,7 +128,7 @@ def unique_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]: return result try: - return json.loads(text, object_pairs_hook=unique_object) + return json.loads(text.removeprefix("\ufeff"), object_pairs_hook=unique_object) except json.JSONDecodeError as exc: raise BundlerError(f"Invalid JSON from {origin}: {exc}") from exc diff --git a/tests/specify_cli/bundles/catalog/test_command_add.py b/tests/specify_cli/bundles/catalog/test_command_add.py index 89ddea0744..0b4491b9a1 100644 --- a/tests/specify_cli/bundles/catalog/test_command_add.py +++ b/tests/specify_cli/bundles/catalog/test_command_add.py @@ -1,7 +1,7 @@ from __future__ import annotations import io # noqa: F401 -import json # noqa: F401 +import json from pathlib import Path from unittest.mock import patch # noqa: F401 @@ -40,3 +40,18 @@ def test_catalog_add_and_remove(project: Path): removed = runner.invoke(app, ["bundle", "catalog", "remove", "local"]) assert removed.exit_code == 0 + + +def test_catalog_add_and_search_accept_utf8_bom(project: Path): + catalog = project / "bom-catalog.json" + write_catalog_file(catalog, {"demo": catalog_entry_dict("demo")}) + catalog.write_bytes(b"\xef\xbb\xbf" + catalog.read_bytes()) + + added = runner.invoke( + app, ["bundle", "catalog", "add", str(catalog), "--id", "bom"] + ) + assert added.exit_code == 0, added.output + + searched = runner.invoke(app, ["bundle", "search", "demo", "--offline", "--json"]) + assert searched.exit_code == 0, searched.output + assert any(entry["id"] == "demo" for entry in json.loads(searched.output)) diff --git a/tests/specify_cli/bundles/test_yamlio.py b/tests/specify_cli/bundles/test_yamlio.py index 23f78546c9..6258d7922b 100644 --- a/tests/specify_cli/bundles/test_yamlio.py +++ b/tests/specify_cli/bundles/test_yamlio.py @@ -6,7 +6,7 @@ import pytest from specify_cli.bundler import BundlerError -from specify_cli.bundles.yamlio import dump_yaml, load_json, load_yaml +from specify_cli.bundles.yamlio import dump_yaml, load_json, load_yaml, loads_json def test_dump_yaml_preserves_unicode(tmp_path: Path): @@ -56,3 +56,30 @@ def test_load_json_malformed_still_reports_invalid_json(tmp_path: Path): path.write_text('{"bundles": [', encoding="utf-8") with pytest.raises(BundlerError, match="Invalid JSON"): load_json(path) + + +def test_load_json_accepts_single_utf8_bom(tmp_path: Path): + path = tmp_path / "records.json" + path.write_bytes(b"\xef\xbb\xbf" + '{"name": "café", "note": "\ufeff"}'.encode()) + assert load_json(path) == {"name": "café", "note": "\ufeff"} + + +def test_loads_json_accepts_single_leading_bom(): + assert loads_json('\ufeff{"name": "café", "note": "\ufeff"}') == { + "name": "café", + "note": "\ufeff", + } + + +@pytest.mark.parametrize( + "text", + [ + '\ufeff\ufeff{"name": 1}', + ' \ufeff{"name": 1}', + '\ufeff{"name": ', + '\ufeff{"name": 1, "name": 2}', + ], +) +def test_loads_json_bom_does_not_hide_invalid_json(text: str): + with pytest.raises(BundlerError, match="Invalid JSON"): + loads_json(text) From f7b13b5d3dbedbbba34cf7fa114541c7e8e33bf0 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Tue, 24 Nov 2026 17:39:58 +0000 Subject: [PATCH 2/4] Fix FileHelper migration check for bundler JSON reads --- src/specify_cli/bundles/yamlio.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/specify_cli/bundles/yamlio.py b/src/specify_cli/bundles/yamlio.py index ccf3f77797..ac4cc58b58 100644 --- a/src/specify_cli/bundles/yamlio.py +++ b/src/specify_cli/bundles/yamlio.py @@ -17,6 +17,7 @@ import yaml +from ..file_helper import FileHelper, FileHelperError from . import BundlerError @@ -101,15 +102,15 @@ def load_json(path: Path) -> Any: if not path.exists(): raise BundlerError(f"File not found: {path}") try: - with path.open("r", encoding="utf-8-sig") as handle: - return json.load(handle) + with FileHelper(path.parent, allow_symlinks=True) as files: + return json.loads(files.read_text(path.name, encoding="utf-8-sig")) # JSONDecodeError stays FIRST: it and UnicodeDecodeError are sibling # ValueError subclasses (neither subsumes the other), so malformed-but- # decodable JSON keeps its more specific "Invalid JSON" message while a # decode failure falls through to the read-error clause below. except json.JSONDecodeError as exc: raise BundlerError(f"Invalid JSON in {path}: {exc}") from exc - except (OSError, UnicodeError) as exc: + except (OSError, UnicodeError, FileHelperError) as exc: # See load_yaml: a non-UTF-8 file raises UnicodeDecodeError, which is # not an OSError, and previously escaped as a raw traceback. raise BundlerError(f"Could not read {path}: {exc}") from exc From 555d0d9c896be01b2938236afeb004fab7203a08 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Tue, 24 Nov 2026 17:51:15 +0000 Subject: [PATCH 3/4] Cover invalid BOM input in load_json --- tests/specify_cli/bundles/test_yamlio.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/specify_cli/bundles/test_yamlio.py b/tests/specify_cli/bundles/test_yamlio.py index 6258d7922b..efe2021086 100644 --- a/tests/specify_cli/bundles/test_yamlio.py +++ b/tests/specify_cli/bundles/test_yamlio.py @@ -64,6 +64,21 @@ def test_load_json_accepts_single_utf8_bom(tmp_path: Path): assert load_json(path) == {"name": "café", "note": "\ufeff"} +@pytest.mark.parametrize( + "text", + [ + '\ufeff\ufeff{"name": 1}', + ' \ufeff{"name": 1}', + '\ufeff{"name": ', + ], +) +def test_load_json_bom_does_not_hide_invalid_json(tmp_path: Path, text: str): + path = tmp_path / "records.json" + path.write_bytes(text.encode("utf-8")) + with pytest.raises(BundlerError, match="Invalid JSON"): + load_json(path) + + def test_loads_json_accepts_single_leading_bom(): assert loads_json('\ufeff{"name": "café", "note": "\ufeff"}') == { "name": "café", From 22ae96e0f9c480e0e9dbb9f4f0aa97cd36766970 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Wed, 25 Nov 2026 00:11:50 +0000 Subject: [PATCH 4/4] Keep path.open in load_json while handling one leading BOM --- src/specify_cli/bundles/yamlio.py | 8 ++++---- tests/specify_cli/bundles/test_yamlio.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/specify_cli/bundles/yamlio.py b/src/specify_cli/bundles/yamlio.py index ac4cc58b58..d54b43621e 100644 --- a/src/specify_cli/bundles/yamlio.py +++ b/src/specify_cli/bundles/yamlio.py @@ -17,7 +17,6 @@ import yaml -from ..file_helper import FileHelper, FileHelperError from . import BundlerError @@ -102,15 +101,16 @@ def load_json(path: Path) -> Any: if not path.exists(): raise BundlerError(f"File not found: {path}") try: - with FileHelper(path.parent, allow_symlinks=True) as files: - return json.loads(files.read_text(path.name, encoding="utf-8-sig")) + with path.open("r", encoding="utf-8") as handle: + text = handle.read() + return json.loads(text.removeprefix("\ufeff")) # JSONDecodeError stays FIRST: it and UnicodeDecodeError are sibling # ValueError subclasses (neither subsumes the other), so malformed-but- # decodable JSON keeps its more specific "Invalid JSON" message while a # decode failure falls through to the read-error clause below. except json.JSONDecodeError as exc: raise BundlerError(f"Invalid JSON in {path}: {exc}") from exc - except (OSError, UnicodeError, FileHelperError) as exc: + except (OSError, UnicodeError) as exc: # See load_yaml: a non-UTF-8 file raises UnicodeDecodeError, which is # not an OSError, and previously escaped as a raw traceback. raise BundlerError(f"Could not read {path}: {exc}") from exc diff --git a/tests/specify_cli/bundles/test_yamlio.py b/tests/specify_cli/bundles/test_yamlio.py index efe2021086..af149fc38a 100644 --- a/tests/specify_cli/bundles/test_yamlio.py +++ b/tests/specify_cli/bundles/test_yamlio.py @@ -64,6 +64,19 @@ def test_load_json_accepts_single_utf8_bom(tmp_path: Path): assert load_json(path) == {"name": "café", "note": "\ufeff"} +def test_load_json_reads_symlinked_file_outside_parent(tmp_path: Path): + target = tmp_path / "records.json" + target.write_text('{"name": "café"}', encoding="utf-8") + link_dir = tmp_path / "links" + link_dir.mkdir() + link = link_dir / "records.json" + try: + link.symlink_to(target) + except (OSError, NotImplementedError) as exc: + pytest.skip(f"cannot create symlink: {exc}") + assert load_json(link) == {"name": "café"} + + @pytest.mark.parametrize( "text", [