feat: Add --sudo-password flag for automated sudo authentication#78
feat: Add --sudo-password flag for automated sudo authentication#78
Conversation
Add secure sudo password handling for commands requiring elevated privileges: - Add -S/--sudo-password CLI flag to prompt for sudo password - Create security/sudo module with SudoPassword struct using zeroize - Implement sudo prompt detection patterns for various Linux distributions - Add execute_with_sudo method to SSH client with PTY support - Support both streaming and non-streaming execution paths - Support BSSH_SUDO_PASSWORD environment variable (with security warnings) Security features: - Password stored using zeroize crate for automatic memory clearing - Debug output redacts password content - PTY allocation ensures proper sudo interaction - Password never logged or printed in any output Closes #74
Security and Performance Review - PR #78: Sudo Password FeatureAnalysis Summary
Prioritized Fix RoadmapCRITICAL
HIGH
MEDIUM
Positive Security Observations
Test Results
Manual Review RequiredThe following items could not be automatically verified and require manual testing:
Recommendations
Code Quality
|
This commit addresses all security issues found in the PR review: 1. CRITICAL: Replace Arc with SecretString - Replaced Arc<SudoPasswordInner> with secrecy::SecretString crate - SecretString is specifically designed for handling secrets in memory - Works correctly with cloning in parallel execution contexts - Each clone is independent and properly zeroized 2. HIGH: Empty password validation - Added validation in SudoPassword::new() to reject empty passwords - Added validation in prompt_sudo_password() to reject empty input - Added validation in get_sudo_password_from_env() to reject empty env vars - Returns clear error messages for empty passwords 3. MEDIUM: Zeroizing copy in with_newline() - Changed return type from Vec<u8> to Zeroizing<Vec<u8>> - Ensures the copy with newline is also cleared from memory - Uses zeroize crate's Zeroizing wrapper for automatic cleanup 4. MEDIUM: Unbounded buffer growth protection - Added MAX_SUDO_PROMPT_BUFFER_SIZE constant (64KB limit) - Enforced buffer size limit in execute_with_sudo() for both stdout and stderr - Truncates buffer to last 64KB when limit exceeded - Prevents memory exhaustion on commands with large output All tests pass successfully.
Security Fixes Applied ✅All security issues identified in the review have been addressed in commit
Summary of Security Improvements
All 462 tests pass successfully. |
Add #[serial] attribute to environment-related tests to prevent race conditions when tests modify the BSSH_SUDO_PASSWORD env var. Also ensure environment is clean before each test.
- Add [Unreleased] section to CHANGELOG with sudo password feature details - Add -S/--sudo-password option documentation to manpage - Add BSSH_SUDO_PASSWORD environment variable documentation - Add usage examples for sudo commands in manpage
- Change from single boolean flag to counter-based tracking - Allow up to 10 sudo password sends per session (MAX_SUDO_PASSWORD_SENDS) - Support commands like 'sudo cmd1 && sudo cmd2' when sudo cache is disabled - Stop sending passwords after authentication failure is detected - Add detailed debug logging with attempt count This handles scenarios where: - sudo -k explicitly invalidates the cache - Server has Defaults timestamp_timeout=0 - Long-running commands where sudo cache expires
Prevents TUI layout corruption when sudo authentication fails. The warn-level logs were being output directly to terminal, interfering with the TUI display. Debug level ensures these messages only appear with -vv verbosity flag.
- Close SSH channel immediately when sudo authentication fails - Return exit code 1 to indicate authentication failure - Display clear error message: '[bssh] Sudo authentication failed' - Change log level from warn to debug to prevent TUI corruption - TUI now returns TuiExitReason to indicate user quit vs completion - Abort pending handles when user explicitly quits (q key) This fixes: 1. TUI layout corruption from warn-level log messages 2. Unable to quit TUI when sudo fails (q key not working) 3. Node appearing stuck in 'in progress' state after sudo failure
- Add ExitCode variant to CommandOutput enum for streaming exit codes - Send ExitCode(1) through channel when sudo authentication fails - Stream manager now processes exit code and sets Failed status when != 0 - TUI correctly shows ✗ (failed) instead of ✓ (completed) for sudo failures This ensures the TUI summary shows accurate success/failure counts.
completed_count() was using is_complete() which returns true for both Completed AND Failed status. This caused failed nodes to be counted in both success (✓) and failure (✗) columns. Now completed_count() only counts streams with ExecutionStatus::Completed, ensuring accurate TUI statistics: - ✓ = successfully completed (exit code 0) - ✗ = failed (exit code != 0 or error) - in progress = total - completed - failed
7ee2127 to
9effcda
Compare
Change status bar from '✓ N • ✗ N • N in progress' to 'Total: N • ✓ N • ✗ N • N in progress' for clarity. Also remove redundant '(N nodes)' from title since total is now shown.
Summary
-S/--sudo-passwordCLI flag to securely prompt for sudo password before command executionsecurity/sudomodule withSudoPasswordstruct using zeroize for automatic memory clearingexecute_with_sudomethod to SSH client with PTY support for proper sudo interactionBSSH_SUDO_PASSWORDenvironment variable for automation scenarios (with security warnings)Implementation Details
CLI Changes
-S/--sudo-passwordflag to CLI (boolean)Core Logic
rpasswordcrate for secure password input[sudo] password for {user}:,Password:, etc.Security Requirements
zeroizecrateFiles Modified
src/cli.rs- Add--sudo-passwordflagsrc/commands/exec.rs- Handle sudo password parametersrc/executor/connection_manager.rs- Pass password to execution logicsrc/executor/parallel.rs- Add sudo password to executorsrc/ssh/client/command.rs- Add connect_and_execute_with_sudo methodsrc/ssh/tokio_client/channel_manager.rs- Detect sudo prompts, inject passwordsrc/security/mod.rs- New security module structuresrc/security/sudo.rs- SudoPassword struct and prompt detectionREADME.md- Document new featureARCHITECTURE.md- Document implementation approachTest plan
-SflagCloses #74