-
Notifications
You must be signed in to change notification settings - Fork 91
fix: enforce symlink containment in file discovery and resolution #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| """Tests for symlink containment enforcement across APM subsystems. | ||
|
|
||
| Validates that symlinked primitive files are rejected at discovery and | ||
| resolution time, preventing arbitrary local file reads. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import tempfile | ||
| import shutil | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _try_symlink(link: Path, target: Path): | ||
| """Create a symlink or skip the test on platforms that don't support it.""" | ||
| try: | ||
| link.symlink_to(target) | ||
| except OSError: | ||
| raise unittest.SkipTest("Symlinks not supported on this platform") | ||
|
|
||
|
|
||
| class TestPromptCompilerSymlinkContainment(unittest.TestCase): | ||
| """PromptCompiler._resolve_prompt_file rejects external symlinks.""" | ||
|
|
||
| def setUp(self): | ||
| self.tmpdir = tempfile.mkdtemp() | ||
| self.project = Path(self.tmpdir) / "project" | ||
| self.project.mkdir() | ||
| self.outside = Path(self.tmpdir) / "outside" | ||
| self.outside.mkdir() | ||
| # Create a file outside the project | ||
| self.secret = self.outside / "secret.txt" | ||
| self.secret.write_text("sensitive-data", encoding="utf-8") | ||
| # Create apm.yml so the project is valid | ||
| (self.project / "apm.yml").write_text( | ||
| "name: test\nversion: 1.0.0\n", encoding="utf-8" | ||
| ) | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.tmpdir, ignore_errors=True) | ||
|
|
||
| def test_symlinked_prompt_outside_project_rejected(self): | ||
| """Symlinked .prompt.md is rejected with clear error message.""" | ||
| from apm_cli.core.script_runner import PromptCompiler | ||
|
|
||
| prompts_dir = self.project / ".apm" / "prompts" | ||
| prompts_dir.mkdir(parents=True) | ||
| symlink = prompts_dir / "evil.prompt.md" | ||
| _try_symlink(symlink, self.secret) | ||
|
|
||
| compiler = PromptCompiler() | ||
| old_cwd = os.getcwd() | ||
| try: | ||
| os.chdir(self.project) | ||
| with self.assertRaises(FileNotFoundError) as ctx: | ||
| compiler._resolve_prompt_file(".apm/prompts/evil.prompt.md") | ||
| self.assertIn("symlink", str(ctx.exception).lower()) | ||
| finally: | ||
| os.chdir(old_cwd) | ||
|
|
||
| def test_normal_prompt_within_project_allowed(self): | ||
| """Non-symlinked prompt files within the project are allowed.""" | ||
| from apm_cli.core.script_runner import PromptCompiler | ||
|
|
||
| prompts_dir = self.project / ".apm" / "prompts" | ||
| prompts_dir.mkdir(parents=True) | ||
| prompt = prompts_dir / "safe.prompt.md" | ||
| prompt.write_text("# Safe prompt", encoding="utf-8") | ||
|
|
||
| compiler = PromptCompiler() | ||
| old_cwd = os.getcwd() | ||
| try: | ||
| os.chdir(self.project) | ||
| result = compiler._resolve_prompt_file(".apm/prompts/safe.prompt.md") | ||
| self.assertTrue(result.exists()) | ||
| finally: | ||
| os.chdir(old_cwd) | ||
|
|
||
|
|
||
| class TestPrimitiveDiscoverySymlinkContainment(unittest.TestCase): | ||
| """find_primitive_files rejects symlinks outside base directory.""" | ||
|
|
||
| def setUp(self): | ||
| self.tmpdir = tempfile.mkdtemp() | ||
| self.project = Path(self.tmpdir) / "project" | ||
| self.project.mkdir() | ||
| self.outside = Path(self.tmpdir) / "outside" | ||
| self.outside.mkdir() | ||
| self.secret = self.outside / "leak.instructions.md" | ||
| self.secret.write_text("---\napplyTo: '**'\n---\nLeaked!", encoding="utf-8") | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.tmpdir, ignore_errors=True) | ||
|
|
||
| def test_symlinked_instruction_outside_base_rejected(self): | ||
| """Symlinked .instructions.md outside base_dir is filtered out.""" | ||
| from apm_cli.primitives.discovery import find_primitive_files | ||
|
|
||
| instructions_dir = self.project / ".github" / "instructions" | ||
| instructions_dir.mkdir(parents=True) | ||
| symlink = instructions_dir / "evil.instructions.md" | ||
| _try_symlink(symlink, self.secret) | ||
|
|
||
| # Also add a normal file | ||
| normal = instructions_dir / "safe.instructions.md" | ||
| normal.write_text("---\napplyTo: '**'\n---\nSafe", encoding="utf-8") | ||
|
|
||
| results = find_primitive_files( | ||
| str(self.project), | ||
| [".github/instructions/*.instructions.md"], | ||
| ) | ||
| names = [f.name for f in results] | ||
| self.assertIn("safe.instructions.md", names) | ||
| self.assertNotIn("evil.instructions.md", names) | ||
|
|
||
|
|
||
| class TestBaseIntegratorSymlinkContainment(unittest.TestCase): | ||
| """BaseIntegrator.find_files_by_glob rejects external symlinks.""" | ||
|
|
||
| def setUp(self): | ||
| self.tmpdir = tempfile.mkdtemp() | ||
| self.pkg = Path(self.tmpdir) / "pkg" | ||
| self.pkg.mkdir() | ||
| self.outside = Path(self.tmpdir) / "outside" | ||
| self.outside.mkdir() | ||
| self.secret = self.outside / "leak.agent.md" | ||
| self.secret.write_text("# Leaked agent", encoding="utf-8") | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.tmpdir, ignore_errors=True) | ||
|
|
||
| def test_symlinked_agent_outside_package_rejected(self): | ||
| """Symlinked .agent.md outside package dir is filtered out.""" | ||
| from apm_cli.integration.base_integrator import BaseIntegrator | ||
|
|
||
| agents_dir = self.pkg / ".apm" / "agents" | ||
| agents_dir.mkdir(parents=True) | ||
| symlink = agents_dir / "evil.agent.md" | ||
| _try_symlink(symlink, self.secret) | ||
|
|
||
| normal = agents_dir / "safe.agent.md" | ||
| normal.write_text("# Safe agent", encoding="utf-8") | ||
|
|
||
| results = BaseIntegrator.find_files_by_glob( | ||
| self.pkg, "*.agent.md", subdirs=[".apm/agents"], | ||
| ) | ||
| names = [f.name for f in results] | ||
| self.assertIn("safe.agent.md", names) | ||
| self.assertNotIn("evil.agent.md", names) | ||
|
|
||
|
|
||
| class TestHookIntegratorSymlinkContainment(unittest.TestCase): | ||
| """HookIntegrator.find_hook_files rejects external symlinks.""" | ||
|
|
||
| def setUp(self): | ||
| self.tmpdir = tempfile.mkdtemp() | ||
| self.pkg = Path(self.tmpdir) / "pkg" | ||
| self.pkg.mkdir() | ||
| self.outside = Path(self.tmpdir) / "outside" | ||
| self.outside.mkdir() | ||
| self.secret = self.outside / "evil.json" | ||
| self.secret.write_text(json.dumps({"hooks": {}}), encoding="utf-8") | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.tmpdir, ignore_errors=True) | ||
|
|
||
| def test_symlinked_hook_json_outside_package_rejected(self): | ||
| """Symlinked hook JSON outside package dir is filtered out.""" | ||
| from apm_cli.integration.hook_integrator import HookIntegrator | ||
|
|
||
| hooks_dir = self.pkg / ".apm" / "hooks" | ||
| hooks_dir.mkdir(parents=True) | ||
| symlink = hooks_dir / "evil.json" | ||
| _try_symlink(symlink, self.secret) | ||
|
|
||
| normal = hooks_dir / "safe.json" | ||
| normal.write_text(json.dumps({"hooks": {}}), encoding="utf-8") | ||
|
|
||
| integrator = HookIntegrator() | ||
| results = integrator.find_hook_files(self.pkg) | ||
| names = [f.name for f in results] | ||
| self.assertIn("safe.json", names) | ||
| self.assertNotIn("evil.json", names) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs drift: docs/src/content/docs/enterprise/security.md currently states "Symlinks are never followed" and references rejecting symlinks via not .is_symlink(), but this function now permits symlinks as long as the resolved target stays within base_dir. Please update the security docs to describe the actual policy (allow contained symlinks; reject paths that resolve outside).