Repository navigation
Read a malformed settings file in doctor like one that is not JSON - #165
Conversation
install() and uninstall() assumed every section of settings.json had the type Claude Code documents, and raised AttributeError when one did not. uninstall() was worse with a deny object: it iterated the object's keys and, when it removed the hook, wrote them back as a list. Both now read the file through _read_settings(), which checks the top level, hooks, permissions, the two hook events and deny, and raises a ValueError naming the key before anything is written; the CLI already prints a ValueError as "error: ..." and exits 1. There is no safe way to add rules to a deny that is an object, so stopping beats a rewrite. Inside a list of the right type, a hook entry that is not an object, or whose hooks field is not a list, is not keyfence's, so _is_ours() and _sync_command() skip it and it stays in place, as non-string deny entries do.
keyfence doctor and install-hooks --list parsed settings.json with their own json.loads and walked hooks.<event> with .get, so a section of the wrong type (hooks as a list, an event as a string) raised AttributeError. They now read through hooks._read_settings(), whose ValueError they already catch for invalid JSON, and walk an entry's hooks through hooks._entry_hooks(), which skips one that is not an object. Treating the file as unreadable means the hook shows as not installed; install-hooks claude-code then stops with the error that names the key.
aminueza
left a comment
There was a problem hiding this comment.
The doctor side is right in shape. Reading through _read_settings turns every traceback I tried into the same "not installed" line that an invalid-JSON file gets. One test needs to follow the change requested on #164.
- No tracebacks.
keyfence doctorandinstall-hooks --liston the 15 shapes from my #164 QA show none, against 7 tracebacks for each command onmain. - Teeth. Reverting
hook_installedorkeyfence_hooktojson.loads, orkeyfence_hooktoentry.get("hooks", []), fails the new tests. - Suite. 1267 passed, coverage 98.69%. The same 5 failures, local to my machine, also fail on
main. - Your open question.
claude doctor(2.1.317) answers it: apermissions.denythat is an object is reported asExpected array, but received object. The settings docs class a rejected value as a Settings Error, which Claude Code drops (interactive) or skips (-p). So "not installed" is a fair reading of that file.
Needs to change with #164: test_keyfence_hook_skips_a_hook_that_is_not_an_object asserts hook_installed is true for {"hooks": {"PreToolUse": ["x", {"hooks": ["y", ours]}]}}. Claude Code applies nothing in that file ("a PreToolUse/PermissionRequest hook that cannot be loaded … nothing it sits in is applied"), so doctor should say "not installed" there. With the _read_settings check proposed on #164, that happens on its own and this test is the only doctor test that fails. It should assert the opposite, or move to a UserPromptSubmit entry, where Claude Code skips only the bad entry.
Optional follow-up: for both an invalid-JSON file and a malformed one, doctor shows info … not installed. Something like cannot read <path>: <reason> would say why, and could point to claude doctor. That changes existing behaviour, so a separate PR is the right place, as you said.
A PreToolUse entry that is not an object, has no hooks list, or holds a
hook that is not an object makes Claude Code apply nothing in the file
that holds it ("nothing it sits in is applied", claude doctor on 2.1.311
and 2.1.317). install-hooks left such an entry in place and wrote the
hook and the deny rules beside it, so it and keyfence doctor reported a
protection that was not in effect.
_read_settings now raises a ValueError naming the entry, in the same
wording as the wrong-type section errors, so install and --remove stop
before any write. A malformed UserPromptSubmit entry is still left in
place: Claude Code ignores only that entry there.
…' into doctor-malformed-settings # Conflicts: # CHANGELOG.md
… cannot load test_keyfence_hook_skips_a_hook_that_is_not_an_object asserted that doctor reports the hook as installed in a file whose PreToolUse list holds an entry that is not an object. Claude Code applies nothing in such a file, and since doctor reads through _read_settings, which now rejects that entry, doctor and install-hooks --list say "not installed" there. Replace it with two tests. A file keyfence installed into, with a malformed PreToolUse entry added (including a null before keyfence's own hook, which crashed doctor before it read through _entry_hooks), shows both hooks as not installed in doctor and in the --list scopes. Malformed UserPromptSubmit entries, and a null inside keyfence's own prompt entry, do not hide the prompt hook, because Claude Code ignores only those.
|
Done: with #164 merged in,
Reverting any of the three readers to The |
aminueza
left a comment
There was a problem hiding this comment.
Re-checked at 70f7c24. Splitting the old test into a PreToolUse case and a UserPromptSubmit case is the right call, and both now match what Claude Code does:
- The five
PreToolUsebreakages you test, including anullbefore keyfence's own hook, all show "not installed", indoctorand in both--listscopes. Claude Code applies nothing in such a file. - Malformed
UserPromptSubmitentries keep the prompt hook visible. Claude Code reports them asmatcher ignored.orentry ignored.and applies the rest, which I confirmed on 2.1.269 and 2.1.317.
The suite passes, 1290 tests, with guard.py byte-identical, and CI is green.
Ready to merge after #164.
…ings # Conflicts: # CHANGELOG.md
Currently,
keyfence doctorandkeyfence install-hooks --listcrash with anAttributeErrorwhen a section ofsettings.jsonhas the wrong type, for example{"hooks": []}. This PR makes them read such a file the way they already read one that is not valid JSON: the hook shows as not installed, andkeyfence install-hooks claude-code(after #164) stops with an error that names the key to fix.The same goes for a
PreToolUseentry that Claude Code cannot load. Claude Code applies nothing in a file that holds one, so a file keyfence installed into earlier no longer shows the hook as installed. A malformedUserPromptSubmitentry still does not hide keyfence's prompt hook, because Claude Code ignores only that entry.This is stacked on #164, because it reuses the settings check that #164 adds. Merge #164 first; this PR then targets
main.How to test
pytest tests/test_doctor.py -k "wrong_type or cannot_load or does_not_hide"(8 of the 9 cases fail on Stop install-hooks on a malformed settings section or PreToolUse entry #164's branch; all 9 fail onmain)..claude/settings.jsonholding{"hooks": []}, then runHOME=<that directory> keyfence doctor. Both Claude Code hook lines say "not installed", with no traceback.HOME=<that directory> keyfence install-hooks --list. Both scopes show "not installed".keyfence install-hooks claude-code --project, then add"x"as the first item of thePreToolUselist in.claude/settings.json.keyfence doctorandkeyfence install-hooks --listnow show both hooks of that project as "not installed".Implementation record
keyfence/doctor.pyhook_installed,prompt_hook_installedandkeyfence_hookread the file throughhooks._read_settings()instead ofjson.loads, inside theexcept ValueErrorthey already had.keyfence_hookwalks an entry's hooks throughhooks._entry_hooks(). The unusedjsonimport is gone.tests/test_doctor.pyPreToolUseentry added, shows both hooks as not installed in doctor and in the--listscopes (five shapes, one of them anullbefore keyfence's own hook). MalformedUserPromptSubmitentries, and anullinside keyfence's own prompt entry, do not hide the prompt hook.CHANGELOG.mdDecisions:
install-hooks claude-code, which names the key. A clearer doctor line for both cases ("cannot read : ") would change the existing invalid-JSON behavior, so it is left for a separate decision (install-hooks and doctor report the Claude Code hook as installed when Claude Code does not apply the settings file #166).denyof the wrong type also makes the file read as unreadable, even when the hook entry itself is fine.claude doctorreportspermissions.deny: Expected array, but received object(2.1.311 here, 2.1.317 in the review), and the settings docs class a rejected value as a settings error that Claude Code drops, as aminueza pointed out in the review.PreToolUseentry that is not an object now asserts "not installed", since Stop install-hooks on a malformed settings section or PreToolUse entry #164 rejects that entry in_read_settings(). Its coverage ofkeyfence_hookskipping a hook that is not an object moved toUserPromptSubmit:claude doctor2.1.311 reports anullthere as "entry ignored", and keeps the other hooks of that entry.Verification at 70f7c24 (with #164 at ad36111 merged in), 2026-11-05, macOS, Python 3.12.15:
pytestwith the coverage gate (keyfence proxy and CA variables cleared from the shell) exits 0, 1290 passed, coverage 98.72%.main(64e6f0d), with this test file.hook_installed,prompt_hook_installedorkeyfence_hooktojson.loads, or removing thePreToolUseentry check in_read_settings(), fails the fivePreToolUsecases. Revertingkeyfence_hooktoentry.get("hooks", [])fails theUserPromptSubmittest with theAttributeErrordoctor used to raise. Extending the entry check toUserPromptSubmitfails that test too.