T09 · Insecure Skill Coding Practices
- Location
tests/test_core.py:52- Finding
Arbitrary External Python Code Execution Through Unsafe Test Paths
- Content
View full analysis
Vulnerability Details
File Location:
tests/test_core.py, lines 52-117
Vulnerability Type: Execution and import of Python files outside the project boundary
Risk Level: HighVulnerable Code
python def setUp(self): self.workspace_root = Path(__file__).parent.parent.parent self.script_path = self.workspace_root / 'scripts' / 'pilot_check_stage3.py' def test_pilot_check_execution(self): import subprocess result = subprocess.run( ['python3', str(self.script_path), '测试任务'], capture_output=True, text=True, timeout=10 )python def setUp(self): self.workspace_root = Path(__file__).parent.parent.parent self.script_path = self.workspace_root / 'scripts' / 'daily_memory_check.py' def test_daily_check_execution(self): import subprocess result = subprocess.run( ['python3', str(self.script_path)], capture_output=True, text=True, timeout=30 )python def setUp(self): self.workspace_root = Path(__file__).parent.parent.parent self.design_path = self.workspace_root / 'scripts' / 'smart_adaptive_mem0_design.py' def test_smart_router_initialization(self): try: sys.path.insert(0, str(self.workspace_root / 'scripts')) from smart_adaptive_mem0_design import SmartRouter router = SmartRouter() self.assertIsNotNone(router, "The smart router should initialize successfully") except ImportError as e: self.skipTest(f"The adaptive module is not fully implemented: {e}")Technical Analysis
test_core.pyresides under<project>/tests/. Consequently,Path(__file__).parent.parent.parentresolves to the parent directory of the project rather than the project root. The resulting script directory is therefore<project-parent>/scripts/, outside the audited package.The test suite ...[truncated 2124 chars]
- Remediation
View remediation
Remediation Suggestions
-
Resolve the actual project root instead of its parent:
python project_root = Path(__file__).resolve().parent.parent scripts_dir = project_root / "scripts" -
Do not execute or import scripts that are absent from and outside this project. Remove these tests if they target unrelated workspace components.
-
Before executing any path, resolve it and enforce project-root containment:
python project_root = Path(__file__).resolve().parent.parent script_path = (project_root / "scripts" / "pilot_check_stage3.py").resolve() if not script_path.is_relative_to(project_root): raise RuntimeError("Refusing to execute a file outside the project root") -
Avoid modifying
sys.pathto include shared or parent workspace directories. Package the intended module normally and import it through the installed project package. -
Replace external process tests with direct unit tests of
MemorySecretaryLite. Usetempfile.TemporaryDirectoryto construct isolated workspace and memory fixtures. -
If subprocess testing is necessary, execute only a version-controlled fixture inside the repository and use a sanitized environment, isolated working directory, and least-privileged test account.
-
Add a CI check that rejects test paths resolving outside the repository and verifies that every executed script is tracked by the project.
-
