Skip to content

Read a malformed settings file in doctor like one that is not JSON - #165

Merged
felladrin merged 7 commits into
mainfrom
doctor-malformed-settings
Nov 6, 2026
Merged

felladrin merged 7 commits into
mainfrom
doctor-malformed-settings

Conversation

@felladrin

@felladrin felladrin commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Currently, keyfence doctor and keyfence install-hooks --list crash with an AttributeError when a section of settings.json has 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, and keyfence install-hooks claude-code (after #164) stops with an error that names the key to fix.

The same goes for a PreToolUse entry 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 malformed UserPromptSubmit entry 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

  1. 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 on main).
  2. Make a home directory with .claude/settings.json holding {"hooks": []}, then run HOME=<that directory> keyfence doctor. Both Claude Code hook lines say "not installed", with no traceback.
  3. Run HOME=<that directory> keyfence install-hooks --list. Both scopes show "not installed".
  4. In an empty project directory, run keyfence install-hooks claude-code --project, then add "x" as the first item of the PreToolUse list in .claude/settings.json. keyfence doctor and keyfence install-hooks --list now show both hooks of that project as "not installed".
Implementation record
File Change
keyfence/doctor.py hook_installed, prompt_hook_installed and keyfence_hook read the file through hooks._read_settings() instead of json.loads, inside the except ValueError they already had. keyfence_hook walks an entry's hooks through hooks._entry_hooks(). The unused json import is gone.
tests/test_doctor.py A wrong-type section reads like a file that is not JSON (three shapes). A file keyfence installed into, with a malformed PreToolUse entry added, shows both hooks as not installed in doctor and in the --list scopes (five shapes, one of them a null before keyfence's own hook). Malformed UserPromptSubmit entries, and a null inside keyfence's own prompt entry, do not hide the prompt hook.
CHANGELOG.md Unreleased entry, under #164's.

Decisions:

  • "Not installed", not a new FAIL line: it is what doctor already says for a file that is not valid JSON, and it sends the user to 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).
  • A deny of the wrong type also makes the file read as unreadable, even when the hook entry itself is fine. claude doctor reports permissions.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.
  • The test that asserted "installed" next to a PreToolUse entry 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 of keyfence_hook skipping a hook that is not an object moved to UserPromptSubmit: claude doctor 2.1.311 reports a null there 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:

  • pytest with the coverage gate (keyfence proxy and CA variables cleared from the shell) exits 0, 1290 passed, coverage 98.72%.
  • The step 1 counts were measured against exports of Stop install-hooks on a malformed settings section or PreToolUse entry #164's branch (ad36111) and main (64e6f0d), with this test file.
  • Reverting hook_installed, prompt_hook_installed or keyfence_hook to json.loads, or removing the PreToolUse entry check in _read_settings(), fails the five PreToolUse cases. Reverting keyfence_hook to entry.get("hooks", []) fails the UserPromptSubmit test with the AttributeError doctor used to raise. Extending the entry check to UserPromptSubmit fails that test too.
  • Steps 2 to 4 were run through the CLI with the results as written.

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 aminueza left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 doctor and install-hooks --list on the 15 shapes from my #164 QA show none, against 7 tracebacks for each command on main.
  • Teeth. Reverting hook_installed or keyfence_hook to json.loads, or keyfence_hook to entry.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: a permissions.deny that is an object is reported as Expected 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.
@felladrin

Copy link
Copy Markdown
Collaborator Author

Done: with #164 merged in, test_keyfence_hook_skips_a_hook_that_is_not_an_object became two tests.

  • A file keyfence installed into, with a malformed PreToolUse entry added, shows both hooks as "not installed" in doctor and in the --list scopes. It covers your three shapes, an entry with no hooks, and a null before keyfence's own hook.
  • Malformed UserPromptSubmit entries, and a null inside keyfence's own prompt entry, do not hide the prompt hook. claude doctor reports that null as "entry ignored" and keeps the rest of the entry, so this is where the _entry_hooks coverage of keyfence_hook lives now.

Reverting any of the three readers to json.loads, or keyfence_hook to entry.get("hooks", []), still fails the new tests. Suite: 1290 passed, coverage 98.72%.

The cannot read <path>: <reason> line is #166.

@aminueza aminueza left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PreToolUse breakages you test, including a null before keyfence's own hook, all show "not installed", in doctor and in both --list scopes. Claude Code applies nothing in such a file.
  • Malformed UserPromptSubmit entries keep the prompt hook visible. Claude Code reports them as matcher ignored. or entry 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.

Base automatically changed from reject-malformed-settings-shapes to main November 6, 2026 16:02
@felladrin
felladrin merged commit f71f838 into main Nov 6, 2026
18 of 19 checks passed
@felladrin
felladrin deleted the doctor-malformed-settings branch November 6, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants