Commit 382f506
authored
feat: Implement command execution handler for server (#148)
* feat: Implement command execution handler for server
Implement the exec_request handler for bssh-server, enabling clients
to execute remote commands via SSH. Features include:
- CommandExecutor with configurable shell and timeout
- Stdout/stderr streaming to SSH channel
- Command allow/block list validation for security
- Exit code propagation to client
- Environment variable configuration (HOME, USER, SHELL, PATH)
- Working directory configuration
- Proper channel lifecycle (success, eof, close)
Closes #128
* security: Fix critical and high security issues in command execution
This commit addresses all CRITICAL and HIGH severity security
vulnerabilities identified in PR #148 code review.
CRITICAL Issues Fixed:
1. Command Injection via Blocklist Bypass
- Implement normalized validation (lowercase, whitespace collapse)
- Add regex-based pattern detection for variable expansion
- Detect all shell metacharacters (;, &&, ||, |, $(), etc.)
- Case-insensitive and normalized blocklist matching
2. Allowlist Validation Bypass
- Detect and reject command chaining in allowlist mode
- Change to exact match only (no prefix matching)
- Prevent "ls; rm -rf /" style bypasses
3. Process Privilege Control
- Add process group creation for better isolation
- Document OS-level privilege control requirements
- Use kill_on_drop for automatic cleanup
HIGH Issues Fixed:
4. Zombie Process Risk on Timeout
- Create new process group with process_group(0)
- Kill entire process group on timeout using kill(-pid, SIGKILL)
- Implement fallback to immediate child kill
5. Resource Limits
- Add documentation for OS-level resource limits
- Recommend systemd service limits and ulimit configuration
6. Environment Variable Security
- Block dangerous environment variables (LD_PRELOAD, LD_LIBRARY_PATH, etc.)
- Add runtime filtering with warning logs
- Prevent library injection and environment-based attacks
Implementation Details:
- Enhanced validation with multiple defense layers
- Comprehensive test suite with 17 passing tests
- Updated default blocklist to block command categories
- Added dangerous environment variable blocklist
- Process group management for Unix systems
- Updated dependencies: nix (process, signal features), libc
Breaking Changes:
- Default blocklist now blocks command names instead of full patterns
- Command chaining strictly blocked when allowlist is configured
- Case-insensitive command blocking (RM, Rm blocked along with rm)
- Dangerous environment variables are now filtered
Testing:
- All 17 exec module tests pass
- Comprehensive injection prevention tests added
- Blocklist normalization tests
- Allowlist bypass prevention tests
- Environment variable filtering tests
* chore: Fix formatting and dead code warning in exec handler
Apply cargo fmt formatting and add #[allow(dead_code)] for test helper1 parent cce72c6 commit 382f506
File tree
6 files changed
+1031
-10
lines changed- src/server
6 files changed
+1031
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| 201 | + | |
201 | 202 | | |
202 | 203 | | |
203 | 204 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
| 217 | + | |
216 | 218 | | |
217 | 219 | | |
218 | 220 | | |
219 | 221 | | |
220 | 222 | | |
221 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
222 | 233 | | |
223 | 234 | | |
224 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | | - | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
| |||
139 | 144 | | |
140 | 145 | | |
141 | 146 | | |
| 147 | + | |
142 | 148 | | |
143 | 149 | | |
144 | 150 | | |
| |||
282 | 288 | | |
283 | 289 | | |
284 | 290 | | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
285 | 309 | | |
286 | 310 | | |
287 | 311 | | |
| |||
413 | 437 | | |
414 | 438 | | |
415 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
416 | 473 | | |
0 commit comments