Skip to content

Commit 351c09d

Browse files
authored
Merge pull request #632 from deepujain/fix/629-plugin-json-hooks
fix(analyzer): analyze hooks declared in .claude-plugin/plugin.json
2 parents 00c2d99 + 5c41a80 commit 351c09d

2 files changed

Lines changed: 372 additions & 2 deletions

File tree

‎src/skillspector/nodes/analyzers/bundled_execution_surface.py‎

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import posixpath
1212
import re
1313
import socket
14-
from collections.abc import Callable
14+
from collections.abc import Callable, Mapping
1515
from dataclasses import dataclass
1616
from typing import Final
1717
from urllib.parse import urlsplit
@@ -42,6 +42,10 @@
4242
".claude/settings.local.json",
4343
}
4444
)
45+
# Claude Code also loads plugin hooks from the `hooks` field of
46+
# `.claude-plugin/plugin.json` (a path, a list of paths, or an inline object).
47+
_PLUGIN_MANIFEST_PATH: Final = ".claude-plugin/plugin.json"
48+
_MAX_PLUGIN_HOOK_PATHS: Final = 16
4549
_MAX_DECLARATIONS: Final = 2_048
4650
_MAX_DECLARATION_CHARS: Final = 16_384
4751
_VALID_DEFAULT_MODES: Final = frozenset(
@@ -1933,6 +1937,92 @@ def _analyze_document(
19331937
)
19341938

19351939

1940+
def _plugin_hook_documents(
1941+
file_cache: Mapping[str, str],
1942+
) -> tuple[dict[str, str], dict[str, InspectionLedgerEvent]]:
1943+
"""Resolve bounded plugin hooks and retain every uninspected declaration."""
1944+
documents: dict[str, str] = {}
1945+
partial: dict[str, InspectionLedgerEvent] = {}
1946+
1947+
def incomplete(path: str, reason: LedgerReason, **bounds: int) -> None:
1948+
partial[path] = ledger_event(
1949+
outcome=LedgerOutcome.PARTIAL,
1950+
phase="static",
1951+
analyzer_id=ANALYZER_ID,
1952+
path=path,
1953+
reason=reason,
1954+
**bounds,
1955+
)
1956+
1957+
content = file_cache.get(_PLUGIN_MANIFEST_PATH)
1958+
if content is None:
1959+
return documents, partial
1960+
if len(content) > MAX_FILE_CHARS:
1961+
incomplete(
1962+
_PLUGIN_MANIFEST_PATH,
1963+
LedgerReason.SIZE_LIMIT,
1964+
observed_characters=len(content),
1965+
limit_characters=MAX_FILE_CHARS,
1966+
)
1967+
return documents, partial
1968+
try:
1969+
manifest = _parse_document(content)
1970+
except (RecursionError, ValueError):
1971+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.OPAQUE_CONTENT)
1972+
return documents, partial
1973+
if not isinstance(manifest, dict):
1974+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.OPAQUE_CONTENT)
1975+
return documents, partial
1976+
raw_hooks = manifest.get("hooks")
1977+
if raw_hooks is None:
1978+
return documents, partial
1979+
candidates = raw_hooks if isinstance(raw_hooks, list) else [raw_hooks]
1980+
inline: dict[str, list] = {}
1981+
for candidate in candidates[:_MAX_PLUGIN_HOOK_PATHS]:
1982+
if isinstance(candidate, dict):
1983+
for event, groups in candidate.items():
1984+
if isinstance(groups, list):
1985+
inline.setdefault(event, []).extend(groups)
1986+
else:
1987+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.OPAQUE_CONTENT)
1988+
continue
1989+
if not isinstance(candidate, str):
1990+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.OPAQUE_CONTENT)
1991+
continue
1992+
try:
1993+
candidate.encode("utf-8")
1994+
except UnicodeEncodeError:
1995+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.REFERENCED_UNINSPECTED)
1996+
continue
1997+
resolved = posixpath.normpath(candidate)
1998+
if (
1999+
not resolved
2000+
or resolved in {".", ".."}
2001+
or resolved.startswith("../")
2002+
or posixpath.isabs(candidate)
2003+
or "\\" in candidate
2004+
or (len(candidate) > 1 and candidate[1] == ":")
2005+
or (len(resolved) > 1 and resolved[1] == ":")
2006+
):
2007+
incomplete(_PLUGIN_MANIFEST_PATH, LedgerReason.REFERENCED_UNINSPECTED)
2008+
continue
2009+
hook_content = file_cache.get(resolved)
2010+
if hook_content is None:
2011+
incomplete(resolved, LedgerReason.REFERENCED_UNINSPECTED)
2012+
continue
2013+
documents[resolved] = hook_content
2014+
if inline:
2015+
documents[_PLUGIN_MANIFEST_PATH] = json.dumps({"hooks": inline}, ensure_ascii=False)
2016+
if len(candidates) > _MAX_PLUGIN_HOOK_PATHS:
2017+
incomplete(
2018+
_PLUGIN_MANIFEST_PATH,
2019+
LedgerReason.OUTPUT_LIMIT,
2020+
observed_records=len(candidates),
2021+
limit_records=_MAX_PLUGIN_HOOK_PATHS,
2022+
)
2023+
return documents, partial
2024+
2025+
19362026
def node(state: SkillspectorState) -> AnalyzerNodeResponse:
19372027
"""Inspect exact bundled hook/settings paths using bounded literal classifiers."""
19382028
components = state.get("components") or []
@@ -1945,9 +2035,20 @@ def node(state: SkillspectorState) -> AnalyzerNodeResponse:
19452035
ledger_events: list[InspectionLedgerEvent] = []
19462036
previous_settings_hook_ids: set[_HookIdentity] = set()
19472037
applicable_paths = set(components).intersection(_APPLICABLE_PATHS)
2038+
plugin_documents, plugin_partial = _plugin_hook_documents(file_cache)
2039+
if decodable.get(_PLUGIN_MANIFEST_PATH) is False:
2040+
plugin_partial[_PLUGIN_MANIFEST_PATH] = ledger_event(
2041+
outcome=LedgerOutcome.PARTIAL,
2042+
phase="static",
2043+
analyzer_id=ANALYZER_ID,
2044+
path=_PLUGIN_MANIFEST_PATH,
2045+
reason=LedgerReason.OPAQUE_CONTENT,
2046+
)
2047+
applicable_paths |= set(plugin_documents)
19482048
hooks_disabled = _bundled_hooks_are_disabled(applicable_paths, file_cache, decodable)
19492049

19502050
for path in sorted(applicable_paths):
2051+
partial_event = plugin_partial.pop(path, None)
19512052
if decodable.get(path) is False:
19522053
ledger_events.append(
19532054
ledger_event(
@@ -1959,7 +2060,7 @@ def node(state: SkillspectorState) -> AnalyzerNodeResponse:
19592060
)
19602061
)
19612062
continue
1962-
content = file_cache.get(path)
2063+
content = plugin_documents.get(path, file_cache.get(path))
19632064
if content is None:
19642065
ledger_events.append(
19652066
ledger_event(
@@ -1994,8 +2095,12 @@ def node(state: SkillspectorState) -> AnalyzerNodeResponse:
19942095
)
19952096
continue
19962097
findings.extend(path_findings)
2098+
if partial_event is not None:
2099+
event.update(partial_event)
2100+
event["emitted_finding_ids"] = [finding.finding_id for finding in path_findings]
19972101
ledger_events.append(event)
19982102

2103+
ledger_events.extend(plugin_partial.values())
19992104
status = analyzer_status_for_events(ANALYZER_ID, ledger_events)
20002105
return {
20012106
"findings": findings,

0 commit comments

Comments
 (0)