1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Phase-1 plugin SKILL ingestion: discovery, namespacing, and body
//! resolution (issue #3539).
//!
//! Why: a plugin's `skills/<name>/SKILL.md` files are the exact same
//! progressive-disclosure format `crate::skills` already parses for project
//! skills — cheap frontmatter (`name`/`description`) at discovery time, the
//! full body only on demand. This module adds namespacing
//! (`<plugin>:<name>`) on top; it deliberately does NOT interact with
//! `crate::skills::discover_skill_metadata`'s bundled/project
//! whole-catalog-replacement threshold (PR #3465) — plugin skills are an
//! independent additive tier that is listed and resolved regardless of
//! which side of that threshold a project's own `.claude/skills/` falls on
//! (#3539's locked precedence-interaction requirement).
//! What: [`discover_plugin_skills`] lists every plugin skill's cheap
//! metadata, namespaced (used by `skills::protocol::skills_list`'s `plugin`
//! tier and `skills::FsSkillResolver`'s cached catalog).
//! [`resolve_plugin_skill_body`] lazily loads one namespaced skill's full
//! body on demand (used by `skills::FsSkillResolver::resolve`).
//! Test: `tests::*` — discovery finds and namespaces skills, a skill
//! directory without `SKILL.md` is skipped, body resolution finds/misses a
//! namespaced name, a symlinked `<name>`/`SKILL.md` is rejected at both the
//! discovery and resolution paths (issue #3547 CRITICAL 5).
use Path;
use crateSkillMetadata;
use ;
/// List every plugin skill's cheap metadata across every discovered plugin,
/// namespaced `<plugin>:<local-name>`.
///
/// Why: the additive `plugin` tier `skills::protocol::skills_list` and
/// `skills::FsSkillResolver`'s cached catalog both need.
/// What: for each [`PluginRoot`], scans immediate subdirectories of
/// `skills_dir` for a readable `SKILL.md`; a subdirectory without one is
/// skipped (mirrors `skills::discover_skill_metadata`'s graceful-skip
/// convention, never an error). `name`/`description` come from frontmatter,
/// falling back to the skill's directory name when `name:` is absent.
/// Sorted by the final namespaced name.
/// Test: `tests::discover_plugin_skills_namespaces_entries`,
/// `tests::discover_plugin_skills_skips_dir_without_skill_md`,
/// `tests::discover_plugin_skills_missing_plugins_dir_is_empty`.
/// Scan a single plugin's `skills_dir` for its `<name>/SKILL.md` skills.
///
/// Why: factored out of [`discover_plugin_skills`] so the per-plugin scan
/// (mirroring `skills::load_metadata_from_skill_dir`'s shape) is
/// independently readable. This is the ONE place a plugin skill's
/// `SKILL.md` content is read for listing purposes, so it is also the
/// enforcement point for the leaf-file-identity guard (code-critic PR
/// #3547 re-review, CRITICAL 5, CWE-59) — see [`super::path_is_contained`].
/// What: a missing/unreadable `skills_dir` yields an empty `Vec` (not an
/// error — most plugins ship agents only, or vice versa). `path.is_dir()`
/// FOLLOWS symlinks (so a symlinked `<name>` directory is not rejected by
/// this check alone), but every candidate's full `<name>/SKILL.md` leaf
/// path is additionally required to canonicalize within `skills_dir`'s own
/// canonicalization before its content is read — this single check catches
/// a symlinked `<name>` directory, a symlinked `SKILL.md` leaf file inside
/// a real `<name>` directory, or both, uniformly (see
/// [`super::path_is_contained`]'s doc). A rejected candidate is skipped
/// with a `WARN`, never surfacing the escaped target's content.
/// Test: covered via `discover_plugin_skills`'s tests above,
/// `tests::discover_plugin_skills_excludes_symlinked_skill_md`.
/// Lazily load one namespaced plugin skill's full body (frontmatter
/// stripped), or `None` when the plugin or the skill is unknown.
///
/// Why: `skills::FsSkillResolver::resolve`'s namespaced-name entry point —
/// the "on invoke" half of progressive disclosure for a plugin skill,
/// mirroring `skills::load_skill_body`'s lazy-load contract. This is the
/// LLM-reachable path — `tools::skill::UseSkillTool::execute` forwards the
/// model's raw `name` argument straight through
/// `skills::FsSkillResolver::resolve` to here — so `skill_name` is HOSTILE
/// input (code-critic PR #3547 review, CRITICAL 2).
/// What: validates `plugin:skill_name` via `plugins::is_valid_namespaced_name`
/// FIRST, before any path is built — rejects a traversal/unsafe-charset
/// payload like `skill_name = "../../../../x"` outright (this alone makes
/// escaping `skills_dir` syntactically impossible, since the accepted
/// charset contains no `/` or `.`). Then finds the [`PluginRoot`] whose
/// resolved `name` equals `plugin`; as a second, independent guard,
/// additionally requires `<plugin>:<skill_name>` to be a name
/// [`discover_plugin_skills`] actually found on disk this scan — mirroring
/// `skills::load_skill_body`'s "reject anything not in `known`" contract —
/// which also closes the residual gap where a plugin's own `SKILL.md`
/// frontmatter could declare a spoofed `name:` that passed the charset
/// check but was never a real, scanned skill. Because
/// [`discover_one_plugin_skills`] itself now rejects a symlinked
/// `<name>`/`SKILL.md` (code-critic PR #3547 re-review, CRITICAL 5), a
/// symlinked skill is never "found on disk" and this membership check
/// alone already excludes it — a THIRD, independent
/// [`super::path_is_contained`] check is still applied immediately before
/// the read below as defense in depth, so the guard holds even if the two
/// callers of [`discover_one_plugin_skills`] ever drift. Only once all
/// guards pass does it read `<skills_dir>/<skill_name>/SKILL.md` and strip
/// the frontmatter fence via `skills::frontmatter::strip_frontmatter` (the
/// same generic stripper project/embedded skill bodies use). `None` on any
/// miss (failed validation, unknown plugin, unknown/symlinked skill,
/// unreadable file) — the resolver trait's `Option`-returning API has no
/// separate error channel, matching `FsSkillResolver::resolve`'s existing
/// unknown-name handling.
/// Test: `tests::resolve_plugin_skill_body_returns_body`,
/// `tests::resolve_plugin_skill_body_unknown_plugin_is_none`,
/// `tests::resolve_plugin_skill_body_unknown_skill_is_none`,
/// `tests::resolve_plugin_skill_body_rejects_traversal_name`,
/// `tests::resolve_plugin_skill_body_rejects_symlinked_file`.