Skip to content
Merged
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
21 changes: 15 additions & 6 deletions src/apm_cli/integration/hook_integrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def _rewrite_command_for_target(
Handles:
- ${CLAUDE_PLUGIN_ROOT}/path references (resolved from package root)
- ./path relative references (resolved from hook file's parent directory)
- Windows backslash variants of both (.\\ and ${CLAUDE_PLUGIN_ROOT}\\)

Args:
command: Original command string
Expand Down Expand Up @@ -232,10 +233,14 @@ def _rewrite_command_for_target(
scripts_base = f"{base_root}/hooks/{package_name}"

# Handle ${CLAUDE_PLUGIN_ROOT} references (always relative to package root)
plugin_root_pattern = r'\$\{CLAUDE_PLUGIN_ROOT\}(/[^\s]+)'
# Match both forward-slash and backslash separators (Windows hook JSON
# may use backslashes: ${CLAUDE_PLUGIN_ROOT}\scripts\scan.ps1)
plugin_root_pattern = r'\$\{CLAUDE_PLUGIN_ROOT\}([\\/][^\s]+)'
for match in re.finditer(plugin_root_pattern, command):
full_var = match.group(0)
rel_path = match.group(1).lstrip('/')
# Normalize backslashes to forward slashes before Path construction
# (on Unix, Path treats backslashes as literal filename chars)
rel_path = match.group(1).replace('\\', '/').lstrip('/')

source_file = (package_path / rel_path).resolve()
# Reject path traversal outside the package directory
Expand All @@ -246,14 +251,18 @@ def _rewrite_command_for_target(
scripts_to_copy.append((source_file, target_rel))
new_command = new_command.replace(full_var, target_rel)

# Handle relative ./path references (safe to run after ${CLAUDE_PLUGIN_ROOT}
# substitution since replacements produce paths like ".github/..." not "./...")
# Handle relative ./path and .\path references (safe to run after
# ${CLAUDE_PLUGIN_ROOT} substitution since replacements produce paths
# like ".github/..." not "./" or ".\")
# Match both forward-slash and backslash separators (Windows hook JSON
# may use backslashes: .\scripts\scan.ps1)
# Resolve from hook file's directory if available, else fall back to package root
resolve_base = hook_file_dir if hook_file_dir else package_path
rel_pattern = r'(\./[^\s]+)'
rel_pattern = r'(\.[\\/][^\s]+)'
for match in re.finditer(rel_pattern, new_command):
rel_ref = match.group(1)
rel_path = rel_ref[2:] # Strip ./
# Normalize to forward slashes for path resolution
rel_path = rel_ref[2:].replace('\\', '/')

source_file = (resolve_base / rel_path).resolve()
# Reject path traversal outside the package directory
Expand Down
Loading
Loading