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, )
| 198 | // The relativePath is validated to prevent directory traversal and |
| 199 | // access to hidden files. |
| 200 | func 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, |