fix(exec): remove ad-hoc shell comment stripping from _guard_command
- Removes match_text regex that stripped # comments before pattern matching (broke on quoted # inside strings) - allow_patterns now run re.fullmatch against the full lowercased command - deny_patterns search the original lowercased command - Replaces comment-stripping test with comment-tail bypass regression (touch canary # echo allowlisted must be blocked) - Adds Re-bin regression for quoted hash + blocked command (echo "#" followed by blocked command must be caught) - All 10 tests pass Signed-off-by: axelray-dev <110029405+axelray-dev@users.noreply.github.com>
This commit is contained in:
@@ -598,17 +598,16 @@ class ExecTool(Tool):
|
||||
"""Best-effort safety guard for potentially destructive commands."""
|
||||
cmd = command.strip()
|
||||
lower = cmd.lower()
|
||||
match_text = re.sub(r"(^|[^\\])#.*$", r"\1", lower).strip()
|
||||
|
||||
# allow_patterns take priority over deny_patterns so that users can
|
||||
# exempt specific commands (e.g. "rm -rf" inside a build directory)
|
||||
# from the hardcoded deny list via configuration.
|
||||
explicitly_allowed = bool(self.allow_patterns) and any(
|
||||
re.fullmatch(p, match_text) for p in self.allow_patterns
|
||||
re.fullmatch(p, lower) for p in self.allow_patterns
|
||||
)
|
||||
if not explicitly_allowed:
|
||||
for pattern in self.deny_patterns:
|
||||
if re.search(pattern, match_text):
|
||||
if re.search(pattern, lower):
|
||||
return "Error: Command blocked by deny pattern filter"
|
||||
|
||||
if self.allow_patterns:
|
||||
|
||||
Reference in New Issue
Block a user