Skip to content

feat: Implement authentication rate limiting (fail2ban-like)#155

Merged
inureyes merged 3 commits intomainfrom
feature/auth-rate-limiting
Jan 24, 2026
Merged

feat: Implement authentication rate limiting (fail2ban-like)#155
inureyes merged 3 commits intomainfrom
feature/auth-rate-limiting

Conversation

@inureyes
Copy link
Member

Summary

  • Add AuthRateLimiter with ban support to protect against brute-force attacks
  • Track failed authentication attempts per IP address with automatic banning
  • Add configuration options for auth window and IP whitelist
  • Integrate rate limiter with SSH handler for both public key and password auth

Changes

New Files

  • src/server/security/mod.rs - Security module root
  • src/server/security/rate_limit.rs - AuthRateLimiter implementation with 12 unit tests

Modified Files

  • src/server/config/types.rs - Add auth_window and whitelist_ips to SecurityConfig
  • src/server/handler.rs - Integrate AuthRateLimiter with auth methods
  • src/server/mod.rs - Add security module and background cleanup task

Features

  • Failure tracking: Counts failed authentication attempts per IP
  • Automatic banning: Bans IPs that exceed the configured threshold
  • Time-based window: Failures outside the window are not counted
  • Configurable ban duration: Bans expire after the configured time
  • IP whitelist: Whitelisted IPs are never banned
  • Automatic cleanup: Expired records are cleaned up every 60 seconds
  • Audit logging: Logs ban events for monitoring

Configuration

security:
  max_auth_attempts: 5    # Maximum failed attempts before ban
  auth_window: 300        # Time window in seconds (default: 5 minutes)
  ban_time: 300           # Ban duration in seconds (default: 5 minutes)
  whitelist_ips:          # IPs exempt from rate limiting
    - 127.0.0.1

Test Plan

  • Unit tests for failure counting
  • Unit tests for ban after max attempts
  • Unit tests for ban expiration
  • Unit tests for whitelist IPs
  • Unit tests for success resets failures
  • Unit tests for cleanup functionality
  • All 929 existing tests pass
  • Manual testing with SSH client

Closes #140

Add AuthRateLimiter with ban support to protect against brute-force attacks:

- Track failed authentication attempts per IP address
- Automatically ban IPs that exceed max attempts within time window
- Configurable max attempts, time window, and ban duration
- IP whitelist support for trusted addresses
- Automatic cleanup of expired bans and failure records
- Background cleanup task running every 60 seconds

Configuration options added to SecurityConfig:
- auth_window: Time window for counting attempts (default: 300s)
- whitelist_ips: IPs exempt from rate limiting

Integration with SSH handler:
- Check if IP is banned before authentication
- Record failures and trigger bans on threshold
- Record success to reset failure counter
- Logging for ban events

Closes #140
@inureyes inureyes added type:enhancement New feature or request type:security Security vulnerability or fix labels Jan 24, 2026
- Use configuration values instead of hardcoded values for auth_window and ban_time
- Integrate whitelist_ips from configuration with validation and logging
- Fix TOCTOU race condition in record_failure by removing entry atomically
- Add capacity limit (max_tracked_ips) to prevent memory exhaustion DoS
- Use HashSet for whitelist O(1) lookups instead of Vec O(n)
- Add auth rate limit config fields to ServerConfig
- Propagate security config from ServerFileConfig to ServerConfig
- Add test for capacity limit enforcement
- Fix code formatting (cargo fmt)
- Update ARCHITECTURE.md with Server Security Module documentation
- Update server-configuration.md with auth_window and whitelist_ips options
- All 930 tests passing, clippy clean
@inureyes
Copy link
Member Author

PR Finalization Report

Project Structure Discovered

  • Project Type: Rust (Cargo.toml)
  • Test Framework: cargo test (930 tests)
  • Documentation System: Markdown docs in docs/architecture/
  • Multi-language Docs: No
  • Lint Tools: cargo fmt, cargo clippy

Checklist

Tests

  • Analyzed existing test structure (src/server/security/rate_limit.rs)
  • Identified test coverage: 13 comprehensive unit tests already present
  • Test coverage includes:
    • Failure counting and tracking
    • Ban triggering after max attempts
    • Ban expiration timing
    • IP whitelist functionality
    • Success resets failure counter
    • Time window expiration
    • Cleanup of expired records
    • Manual ban/unban operations
    • Multiple IP isolation
    • Clone shares state (thread safety)
    • Capacity limits for memory safety
    • Configuration accessors
  • All 930 tests passing

Documentation

  • ARCHITECTURE.md updated with Server Security Module section
  • server-configuration.md updated with:
    • auth_window configuration option
    • whitelist_ips configuration option
    • Improved security section documentation

Code Quality

  • cargo fmt: Fixed formatting issues (5 files)
  • cargo clippy: Clean (no warnings)
  • All tests passing

Changes Made

  • Fixed code formatting in src/server/mod.rs and src/server/security/rate_limit.rs
  • Added Server Security Module documentation to ARCHITECTURE.md
  • Added auth_window and whitelist_ips documentation to docs/architecture/server-configuration.md
  • Updated SshHandler documentation to mention auth rate limiting with ban support

Summary

The AuthRateLimiter implementation is comprehensive with 13 unit tests covering all major functionality. The code follows Rust best practices with proper async/await patterns, thread-safe data structures, and memory-safe design with capacity limits. Documentation has been updated to reflect the new security features.

@inureyes inureyes merged commit 51dc329 into main Jan 24, 2026
1 of 2 checks passed
@inureyes inureyes deleted the feature/auth-rate-limiting branch January 24, 2026 03:39
@inureyes inureyes self-assigned this Jan 24, 2026
@inureyes inureyes added the status:done Completed label Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:done Completed type:enhancement New feature or request type:security Security vulnerability or fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement authentication rate limiting (fail2ban-like)

1 participant