Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions local/src/devflow_cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess
import sys
import tempfile
from pathlib import Path
from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import Dict, List, Optional, Tuple

from . import __version__
Expand Down Expand Up @@ -74,6 +74,17 @@ def asset_root() -> Path:
raise DevFlowError("找不到 DevFlow 安装资产;请从源码目录运行或重新安装 loopforge CLI 包")


def is_safe_state_path(value: object) -> bool:
# Install state can be moved between Windows and POSIX projects.
if not isinstance(value, str) or not value or "\0" in value:
return False
for parser in (PurePosixPath, PureWindowsPath):
parsed = parser(value)
if parsed.anchor or not parsed.parts or ".." in parsed.parts:
return False
return True


def load_state(project: Path) -> dict:
path = project / STATE_PATH
if not path.is_file():
Expand All @@ -82,8 +93,19 @@ def load_state(project: Path) -> dict:
data = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
raise DevFlowError(f"安装状态损坏: {path}: {exc}") from exc
if data.get("version") != STATE_VERSION or not isinstance(data.get("installations"), dict):
if (
not isinstance(data, dict)
or data.get("version") != STATE_VERSION
or not isinstance(data.get("installations"), dict)
):
raise DevFlowError(f"不支持的安装状态版本: {path}")
for item in data["installations"].values():
if not isinstance(item, dict) or not isinstance(item.get("files"), list):
raise DevFlowError(f"安装状态损坏: {path}")
for record in item["files"]:
relative = record.get("path") if isinstance(record, dict) else None
if not is_safe_state_path(relative):
raise DevFlowError(f"安装状态包含非法路径: {path}: {relative!r}")
return data


Expand Down
28 changes: 28 additions & 0 deletions local/tests/test_installer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hashlib
import json
import os
import stat
import subprocess
Expand Down Expand Up @@ -83,6 +85,32 @@ def test_force_replaces_conflicting_skill_symlink_without_touching_target(self):
self.assertEqual("external content\n", external_skill.read_text(encoding="utf-8"))
self.assertNotEqual("external content\n", (link / "SKILL.md").read_text(encoding="utf-8"))

def test_state_rejects_paths_that_are_not_inside_the_project(self):
with tempfile.TemporaryDirectory() as temporary:
project = self.project(temporary)
apply_install(project, "claude", "classic")
state_path = project / ".devflow/install-state.json"
state = json.loads(state_path.read_text(encoding="utf-8"))
victim = project.parent / "keep.txt"
content = b"keep me\n"
victim.write_bytes(content)

for unsafe in ("../keep.txt", r"..\keep.txt", str(victim),
r"C:keep.txt", r"C:\keep.txt", r"\keep.txt", ".", ""):
with self.subTest(path=unsafe):
state["installations"]["classic:claude"]["files"] = [{
"path": unsafe,
"sha256": hashlib.sha256(content).hexdigest(),
}]
state_path.write_text(json.dumps(state), encoding="utf-8")
with self.assertRaisesRegex(DevFlowError, "安装状态包含非法路径"):
apply_install(project, "claude", "classic", update_only=True, force=True)
with self.assertRaisesRegex(DevFlowError, "安装状态包含非法路径"):
uninstall(project, "claude")
with self.assertRaisesRegex(DevFlowError, "安装状态包含非法路径"):
status_rows(project)
self.assertEqual(content, victim.read_bytes())

def test_update_rejects_modified_managed_file(self):
with tempfile.TemporaryDirectory() as temporary:
project = self.project(temporary)
Expand Down