Skip to content

Commit 31032b4

Browse files
MaxFreedomPollardteknium1
authored andcommitted
fix(auth): a transient read failure is not corruption
_load_auth_store() treated every exception from reading auth.json as corruption and returned an empty store. EMFILE under fd exhaustion, EACCES, EIO and a stalled network mount all reached that branch. This module does read-modify-write in roughly fifteen places, so the empty store was one _save_auth_store() away from erasing every stored credential. Separate OSError from parse failure: a file that exists but cannot be read now raises, naming the real cause and leaving the file on disk untouched. Only a genuine parse failure takes the preserve-and-start- empty branch, which is unchanged. The backup was also unreliable in exactly the conditions that triggered it: shutil.copy2 opens a file, so under EMFILE it failed too, its bare except swallowed that, and the log still said "Corrupt file preserved at ..." when nothing had been written. Track whether the copy landed and say so accurately.
1 parent d358edd commit 31032b4

2 files changed

Lines changed: 131 additions & 6 deletions

File tree

‎hermes_cli/auth.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,18 +1124,45 @@ def _load_auth_store(auth_file: Optional[Path] = None) -> Dict[str, Any]:
11241124

11251125
try:
11261126
raw = json.loads(auth_file.read_text(encoding="utf-8"))
1127+
except OSError:
1128+
# The file exists (checked above) but could not be READ: EMFILE under
1129+
# fd exhaustion, EACCES, EIO, a stalled network mount. None of those
1130+
# mean the contents are bad, and this module does read-modify-write in
1131+
# ~15 places, so degrading to an empty store here is one
1132+
# _save_auth_store() away from erasing every stored credential.
1133+
# Fail loudly instead and leave the file on disk untouched.
1134+
logger.warning(
1135+
"auth: could not read %s, leaving the store on disk untouched "
1136+
"rather than degrading to an empty one",
1137+
auth_file, exc_info=True,
1138+
)
1139+
raise
11271140
except Exception as exc:
1141+
# Genuine corruption: unparseable JSON, or bytes that are not UTF-8.
11281142
corrupt_path = auth_file.with_suffix(".json.corrupt")
1143+
preserved = False
11291144
try:
11301145
import shutil
11311146
shutil.copy2(auth_file, corrupt_path)
1147+
preserved = True
11321148
except Exception:
1133-
pass
1134-
logger.warning(
1135-
"auth: failed to parse %s (%s) — starting with empty store. "
1136-
"Corrupt file preserved at %s",
1137-
auth_file, exc, corrupt_path,
1138-
)
1149+
logger.debug(
1150+
"auth: could not preserve a copy of the corrupt store at %s",
1151+
corrupt_path, exc_info=True,
1152+
)
1153+
if preserved:
1154+
logger.warning(
1155+
"auth: failed to parse %s (%s), starting with empty store. "
1156+
"Corrupt file preserved at %s",
1157+
auth_file, exc, corrupt_path,
1158+
)
1159+
else:
1160+
# Do not advertise a backup that was never written.
1161+
logger.warning(
1162+
"auth: failed to parse %s (%s), starting with empty store. "
1163+
"A copy could NOT be preserved at %s",
1164+
auth_file, exc, corrupt_path,
1165+
)
11391166
return {"version": AUTH_STORE_VERSION, "providers": {}}
11401167

11411168
if isinstance(raw, dict) and (
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""A transient read failure on auth.json must not degrade to an empty store.
2+
3+
``_load_auth_store`` treated every exception as corruption and returned
4+
``{"version": ..., "providers": {}}``. This module does read-modify-write in
5+
roughly fifteen places, so an ``OSError`` (EMFILE under fd exhaustion, EACCES,
6+
EIO, a stalled mount) followed by any ``_save_auth_store`` rewrote auth.json
7+
with an empty provider set and destroyed every stored credential.
8+
9+
Genuine corruption still degrades, still preserves a copy, and now only claims
10+
to have preserved one when the copy actually landed.
11+
"""
12+
13+
import errno
14+
import json
15+
import logging
16+
17+
import pytest
18+
19+
import hermes_cli.auth as auth
20+
21+
22+
@pytest.fixture
23+
def store_file(tmp_path):
24+
f = tmp_path / "auth.json"
25+
f.write_text(
26+
json.dumps({"version": 1, "providers": {"nous": {"api_key": "secret"}}}),
27+
encoding="utf-8",
28+
)
29+
return f
30+
31+
32+
def _fail_read(exc):
33+
def _read(self, *args, **kwargs):
34+
raise exc
35+
return _read
36+
37+
38+
@pytest.mark.parametrize(
39+
"exc",
40+
[
41+
OSError(errno.EMFILE, "Too many open files"),
42+
PermissionError(errno.EACCES, "Permission denied"),
43+
OSError(errno.EIO, "Input/output error"),
44+
],
45+
ids=["emfile", "eacces", "eio"],
46+
)
47+
def test_read_failure_raises_and_leaves_the_store_alone(store_file, monkeypatch, exc):
48+
from pathlib import Path
49+
50+
before = store_file.read_bytes()
51+
monkeypatch.setattr(Path, "read_text", _fail_read(exc))
52+
53+
with pytest.raises(OSError):
54+
auth._load_auth_store(store_file)
55+
56+
assert store_file.read_bytes() == before, "the store on disk was modified"
57+
assert not store_file.with_suffix(".json.corrupt").exists(), (
58+
"a read failure is not corruption and must not write a .corrupt sidecar"
59+
)
60+
61+
62+
def test_unparseable_json_still_degrades_and_preserves_a_copy(store_file):
63+
store_file.write_text("{ not json", encoding="utf-8")
64+
65+
result = auth._load_auth_store(store_file)
66+
67+
assert result == {"version": auth.AUTH_STORE_VERSION, "providers": {}}
68+
corrupt = store_file.with_suffix(".json.corrupt")
69+
assert corrupt.exists(), "genuine corruption must still be preserved"
70+
assert corrupt.read_text(encoding="utf-8") == "{ not json"
71+
72+
73+
def test_healthy_store_is_returned_unchanged(store_file):
74+
result = auth._load_auth_store(store_file)
75+
assert result["providers"]["nous"]["api_key"] == "secret"
76+
77+
78+
def test_log_does_not_claim_a_backup_that_was_not_written(
79+
store_file, monkeypatch, caplog
80+
):
81+
"""The old message advertised the .corrupt path even when copy2 failed."""
82+
import shutil
83+
84+
store_file.write_text("{ not json", encoding="utf-8")
85+
86+
def _no_copy(*args, **kwargs):
87+
raise OSError(errno.EMFILE, "Too many open files")
88+
89+
monkeypatch.setattr(shutil, "copy2", _no_copy)
90+
91+
with caplog.at_level(logging.WARNING, logger="hermes_cli.auth"):
92+
result = auth._load_auth_store(store_file)
93+
94+
assert result == {"version": auth.AUTH_STORE_VERSION, "providers": {}}
95+
assert not store_file.with_suffix(".json.corrupt").exists()
96+
text = caplog.text
97+
assert "could NOT be preserved" in text
98+
assert "Corrupt file preserved at" not in text

0 commit comments

Comments
 (0)