Skip to content

Commit 2d66414

Browse files
committed
fix(integration): harden Kimi legacy migration against symlinked paths
1 parent 26e660a commit 2d66414

2 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎src/specify_cli/integrations/kimi/__init__.py‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,32 @@ def _is_safe_legacy_dir(path: Path, project_root: Path) -> bool:
144144
Legacy migration and cleanup ``shutil.move()`` and ``shutil.rmtree()``
145145
directories, so a symlinked ``.kimi``/``.kimi/skills`` (or one reached
146146
through a symlinked parent) must never be followed: doing so could
147-
relocate or delete content living outside the project tree. We reject
148-
the path when it is itself a symlink, when it is not a directory, or
149-
when resolving every symlink lands outside *project_root*.
147+
relocate or delete content living outside the project tree — or operate
148+
on an unrelated in-tree directory (e.g. ``.kimi -> .`` makes
149+
``.kimi/skills`` resolve to ``./skills``).
150+
151+
Checking only the fully-resolved path is insufficient, because a symlink
152+
pointing elsewhere *inside* the project still resolves to a location under
153+
*project_root*. We therefore reject the path when it is not a directory,
154+
when any component between *project_root* and *path* is a symlink
155+
(including the final component), or when the resolved path escapes the
156+
resolved *project_root*.
150157
"""
151-
if path.is_symlink() or not path.is_dir():
158+
if not path.is_dir():
152159
return False
160+
161+
# Reject if any path component below project_root is a symlink. We trust
162+
# project_root itself, so only components strictly under it are checked.
163+
try:
164+
relative = path.relative_to(project_root)
165+
except ValueError:
166+
return False
167+
current = project_root
168+
for part in relative.parts:
169+
current = current / part
170+
if current.is_symlink():
171+
return False
172+
153173
try:
154174
resolved = path.resolve()
155175
root = project_root.resolve()
@@ -293,9 +313,19 @@ def _migrate_legacy_kimi_context_file(
293313
remaining content is appended to ``AGENTS.md``. The legacy file is
294314
deleted if it becomes empty. Returns ``True`` if ``KIMI.md`` existed
295315
and was processed.
316+
317+
Both files are checked for symlinks first: a symlinked ``KIMI.md`` is not
318+
followed (its target could be read from outside the project), and a
319+
symlinked ``AGENTS.md`` is never written through (it could redirect the
320+
write to an arbitrary file outside the project root). In either case the
321+
migration is skipped and ``KIMI.md`` is left untouched.
296322
"""
297323
legacy_path = project_root / "KIMI.md"
298-
if not legacy_path.is_file():
324+
if legacy_path.is_symlink() or not legacy_path.is_file():
325+
return False
326+
327+
target_path = project_root / "AGENTS.md"
328+
if target_path.is_symlink():
299329
return False
300330

301331
content = legacy_path.read_text(encoding="utf-8-sig")
@@ -319,7 +349,6 @@ def _migrate_legacy_kimi_context_file(
319349
legacy_path.unlink()
320350
return True
321351

322-
target_path = project_root / "AGENTS.md"
323352
if target_path.is_file():
324353
existing = target_path.read_text(encoding="utf-8-sig")
325354
existing = existing.replace("\r\n", "\n").replace("\r", "\n")

‎tests/integrations/test_integration_kimi.py‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""Tests for KimiIntegration — skills integration with legacy migration."""
22

33
from specify_cli.integrations import get_integration
4-
from specify_cli.integrations.kimi import _migrate_legacy_kimi_dotted_skills
4+
from specify_cli.integrations.kimi import (
5+
_migrate_legacy_kimi_context_file,
6+
_migrate_legacy_kimi_dotted_skills,
7+
)
58
from specify_cli.integrations.manifest import IntegrationManifest
69

710
from .test_integration_base_skills import SkillsIntegrationTests
@@ -344,6 +347,88 @@ def test_teardown_skips_symlinked_legacy_skills_dir(self, tmp_path):
344347
# The symlink target and its contents must survive teardown.
345348
assert keep.exists()
346349

350+
def test_migrate_skips_symlinked_legacy_parent_dir(self, tmp_path):
351+
# `.kimi` is itself a symlink to the project root, so `.kimi/skills`
352+
# resolves to `./skills` — an unrelated in-tree directory. Even though
353+
# the resolved path stays inside the project, migration must not
354+
# operate on it because a path component is a symlink.
355+
project = tmp_path / "project"
356+
unrelated = project / "skills" / "speckit-evillegacy"
357+
unrelated.mkdir(parents=True)
358+
(unrelated / "SKILL.md").write_text("# unrelated\n")
359+
# .kimi -> project root, so .kimi/skills == ./skills.
360+
(project / ".kimi").symlink_to(project, target_is_directory=True)
361+
362+
i = get_integration("kimi")
363+
m = IntegrationManifest("kimi", project)
364+
i.setup(project, m, parsed_options={"migrate_legacy": True})
365+
366+
# The unrelated ./skills content must be untouched.
367+
assert (unrelated / "SKILL.md").exists()
368+
assert not (
369+
project / ".kimi-code" / "skills" / "speckit-evillegacy"
370+
).exists()
371+
372+
def test_teardown_skips_symlinked_legacy_parent_dir(self, tmp_path):
373+
project = tmp_path / "project"
374+
project.mkdir()
375+
# Looks Speckit-generated, so only the symlink check protects it.
376+
unrelated = project / "skills" / "speckit-evillegacy"
377+
unrelated.mkdir(parents=True)
378+
(unrelated / "SKILL.md").write_text(
379+
"---\nmetadata:\n author: github-spec-kit\n---\n# x\n"
380+
)
381+
(project / ".kimi").symlink_to(project, target_is_directory=True)
382+
383+
i = get_integration("kimi")
384+
m = IntegrationManifest("kimi", project)
385+
i.teardown(project, m)
386+
387+
# The unrelated ./skills content must survive teardown.
388+
assert (unrelated / "SKILL.md").exists()
389+
390+
def test_context_migration_does_not_write_through_symlinked_agents_md(
391+
self, tmp_path
392+
):
393+
# A sensitive file outside the project that a malicious AGENTS.md
394+
# symlink points at. Migration must never overwrite it.
395+
outside = tmp_path / "outside"
396+
outside.mkdir()
397+
secret = outside / "secret.txt"
398+
secret.write_text("original secret\n")
399+
400+
project = tmp_path / "project"
401+
project.mkdir()
402+
(project / "AGENTS.md").symlink_to(secret)
403+
(project / "KIMI.md").write_text("# Notes\n\nKeep this.\n")
404+
405+
result = _migrate_legacy_kimi_context_file(project)
406+
407+
# The outside file must not be overwritten through the symlink.
408+
assert secret.read_text() == "original secret\n"
409+
# KIMI.md is preserved so the user can migrate manually.
410+
assert (project / "KIMI.md").is_file()
411+
assert result is False
412+
413+
def test_context_migration_does_not_follow_symlinked_kimi_md(self, tmp_path):
414+
# A symlinked KIMI.md (source) must not be followed/consumed.
415+
outside = tmp_path / "outside"
416+
outside.mkdir()
417+
external = outside / "external.md"
418+
external.write_text("# external\n")
419+
420+
project = tmp_path / "project"
421+
project.mkdir()
422+
(project / "KIMI.md").symlink_to(external)
423+
424+
result = _migrate_legacy_kimi_context_file(project)
425+
426+
assert result is False
427+
# The external file and the symlink are left intact.
428+
assert external.read_text() == "# external\n"
429+
assert (project / "KIMI.md").is_symlink()
430+
assert not (project / "AGENTS.md").exists()
431+
347432

348433
class TestKimiNextSteps:
349434
"""CLI output tests for kimi next-steps display."""

0 commit comments

Comments
 (0)