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 /