From ebc09216479c5fb9ca51513035ad241f0f0258f7 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Mon, 16 Nov 2026 10:14:01 -0600 Subject: [PATCH] fix(cold-store): wrap non-UTF-8 decode failures in ColdStoreError (#10820) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit load_state and load_credentials caught only json.JSONDecodeError around json.loads(path.read_text()), but Path.read_text() decodes before json.loads runs — non-UTF-8 bytes raised a raw UnicodeDecodeError, breaking the corrupt-file contract from #4361. Catch UnicodeDecodeError alongside JSONDecodeError, keeping the existing messages that name the file and give repair guidance. Adds byte-level regression tests for both files. --- pkgs/cold-store-reaper/cold_store.py | 4 ++-- .../tests/test_cold_store.py | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkgs/cold-store-reaper/cold_store.py b/pkgs/cold-store-reaper/cold_store.py index aa0f7c4cd0..6398ba0e57 100755 --- a/pkgs/cold-store-reaper/cold_store.py +++ b/pkgs/cold-store-reaper/cold_store.py @@ -526,7 +526,7 @@ def load_credentials(self) -> dict: ) try: creds = json.loads(self.credentials_path.read_text()) - except json.JSONDecodeError as e: + except (json.JSONDecodeError, UnicodeDecodeError) as e: raise ColdStoreError( f"credentials.json at {self.credentials_path} is corrupt " f"(invalid JSON: {e}) - repair or delete the file and " @@ -556,7 +556,7 @@ def load_state(self) -> dict: if self.state_path.exists(): try: state = json.loads(self.state_path.read_text()) - except json.JSONDecodeError as e: + except (json.JSONDecodeError, UnicodeDecodeError) as e: raise ColdStoreError( f"state.json at {self.state_path} is corrupt " f"(invalid JSON: {e}) - repair the file, or delete " diff --git a/pkgs/cold-store-reaper/tests/test_cold_store.py b/pkgs/cold-store-reaper/tests/test_cold_store.py index 69d4decc95..3fbea7cc11 100644 --- a/pkgs/cold-store-reaper/tests/test_cold_store.py +++ b/pkgs/cold-store-reaper/tests/test_cold_store.py @@ -843,6 +843,26 @@ def test_corrupt_credentials_raises_cold_store_error(self): self.assertNotIsInstance(ctx.exception, json.JSONDecodeError) self.assertIn("credentials.json", str(ctx.exception)) + def test_non_utf8_state_raises_cold_store_error(self): + # #10820: Path.read_text() decodes before json.loads runs, so + # non-UTF-8 bytes raised a raw UnicodeDecodeError, never the + # ColdStoreError contract PR #4361 established. + self.store.state_path.write_bytes(b'{\xff\xfe not utf-8 \x80\x81') + with self.assertRaises(cold_store.ColdStoreError) as ctx: + self.store.load_state() + self.assertNotIsInstance(ctx.exception, UnicodeDecodeError) + self.assertIn("state.json", str(ctx.exception)) + self.assertIn("repair", str(ctx.exception)) + + def test_non_utf8_credentials_raises_cold_store_error(self): + # #10820: same decode-before-parse gap as state.json. + self.store.credentials_path.write_bytes(b'{\xff\xfe not utf-8 \x80\x81') + with self.assertRaises(cold_store.ColdStoreError) as ctx: + self.store.load_credentials() + self.assertNotIsInstance(ctx.exception, UnicodeDecodeError) + self.assertIn("credentials.json", str(ctx.exception)) + self.assertIn("repair", str(ctx.exception)) + class TestWrongShapePrivateJson(ColdStoreTestBase): """#6845: valid JSON of the wrong shape in credentials.json /