MCPcopy Index your code
hub / github.com/coder/coder / LoadSkillFile

Function LoadSkillFile

coderd/x/chatd/chattool/skill.go:200–233  ·  view source on GitHub ↗

LoadSkillFile reads a supporting file from a skill's directory. The relativePath is validated to prevent directory traversal and access to hidden files.

(
	ctx context.Context,
	conn workspacesdk.AgentConn,
	skill SkillMeta,
	relativePath string,
)

Source from the content-addressed store, hash-verified

198// The relativePath is validated to prevent directory traversal and
199// access to hidden files.
200func LoadSkillFile(
201 ctx context.Context,
202 conn workspacesdk.AgentConn,
203 skill SkillMeta,
204 relativePath string,
205) (string, error) {
206 if err := validateSkillFilePath(relativePath); err != nil {
207 return "", err
208 }
209
210 fullPath := path.Join(skill.Dir, relativePath)
211
212 reader, _, err := conn.ReadFile(
213 ctx, fullPath, 0, maxSkillFileBytes+1,
214 )
215 if err != nil {
216 return "", xerrors.Errorf(
217 "read skill file: %w", err,
218 )
219 }
220 raw, err := io.ReadAll(io.LimitReader(reader, maxSkillFileBytes+1))
221 reader.Close()
222 if err != nil {
223 return "", xerrors.Errorf(
224 "read skill file bytes: %w", err,
225 )
226 }
227
228 if int64(len(raw)) > maxSkillFileBytes {
229 raw = raw[:maxSkillFileBytes]
230 }
231
232 return string(raw), nil
233}
234
235// validateSkillFilePath rejects paths that could escape the skill
236// directory or access hidden files. Only forward-relative,

Callers 2

TestLoadSkillFileFunction · 0.92
ReadSkillFileFunction · 0.85

Calls 5

validateSkillFilePathFunction · 0.85
ReadFileMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45
ReadAllMethod · 0.45

Tested by 1

TestLoadSkillFileFunction · 0.74