spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

security: alias path traversal in CommandRegistrar.register_commands() for flat agents

Open mbachorik opened this issue 7 months ago • 3 comments

Summary

Free-form aliases (permitted since #2125 / 4deb90f) can contain path separators that escape the agent commands directory when registered by flat-file agents (claude, gemini, copilot, etc.).

Root cause

`CommandRegistrar._compute_output_name()` returns the alias name unchanged for non-SKILL.md agents:

```python if agent_config["extension"] != "/SKILL.md": return cmd_name # alias returned as-is ```

A manifest alias like `../foo` produces:

``` commands_dir / "../foo.md" → escapes commands_dir ```

`alias_file.parent.mkdir(parents=True, exist_ok=True)` will even create the parent directories.

Not affected

SKILL.md agents (codex, kimi) are safe — `_compute_output_name` replaces `.` with `-` and prepends `speckit-`, neutralising traversal sequences.

Suggested fix

Validate alias strings against a safe identifier pattern before writing, or resolve `alias_file` and assert it stays within `commands_dir` before writing.

Discovery

Identified during review of PR #2027.

mbachorik avatar Apr 15 '26 14:04 mbachorik

Hi @mbachorik and @mnriem — I'm preparing a PR for this and wanted to confirm one design choice before opening it.

While reproducing #2229 on current main, I found two remaining unguarded write paths in addition to the alias case discussed in the issue:

  1. The primary command write path in CommandRegistrar.register_commands()
  2. write_copilot_prompt(), which writes {cmd_name}.prompt.md under .github/prompts/

My current patch applies the same containment check pattern to all three write sites.

The only design question is symlink handling:

  • A lexical path check would block .. / absolute-path traversal, but still allow writes through pre-planted symlinks under the commands directory.
  • A resolved-path containment check (resolve() + is_relative_to()) also blocks symlink escapes, but disallows intentionally symlinked subdirectories such as .claude/skills/shared -> /team/shared-skills.

My default is the resolved-path check, because it matches the alias guard already merged in b67b285 and closes the symlink escape vector as well.

Would you like this fix to block symlink escapes too, or should it preserve intentionally symlinked subdirectories and only block lexical traversal?

I can implement either version cleanly; I just wanted to confirm the intended policy before sending the PR.

chordpli avatar Apr 21 '26 01:04 chordpli

Pick whichever one is safer, but the most backwards compatible. There are plenty of extensions now so we should not disrupt them unless we have to

mnriem avatar Apr 21 '26 12:04 mnriem

I'm sorry to be late to the party (was offline in plane). Thanks for taking care of this!

mbachorik avatar Apr 22 '26 05:04 mbachorik