Skip to content

Conversation

@inureyes
Copy link
Member

Summary

  • Create src/shared/ module with validation, rate_limit, auth_types, and error submodules
  • Generalize RateLimiter<K> to work with any hashable key type
  • Add shared authentication types (AuthResult, UserInfo, ServerCheckMethod)
  • Add shared error types (ValidationError, AuthError, ConnectionError, RateLimitError)
  • Update existing modules to re-export from shared for backward compatibility

Test plan

  • All 752 existing tests pass (1 unrelated pre-existing failure)
  • All 18 new shared module tests pass
  • All 3 backward-compatible rate_limiter tests pass
  • All 9 validation tests pass (both shared and security modules)
  • Cargo clippy passes without warnings
  • Cargo fmt applied

Closes #124

- Add src/shared/ module with validation, rate_limit, auth_types, and error submodules
- Generalize RateLimiter<K> to work with any hashable key type
- Add AuthResult, UserInfo, and ServerCheckMethod shared auth types
- Add ValidationError, AuthError, ConnectionError, RateLimitError shared error types
- Update security and jump modules to re-export from shared for backward compatibility
- All existing tests pass with re-exported APIs

Closes #124
@inureyes inureyes added type:enhancement New feature or request priority:high High priority issue labels Jan 22, 2026
@inureyes
Copy link
Member Author

Security & Performance Review

Analysis Summary

  • PR Branch: feature/issue-124-shared-module-structure
  • Scope: changed-files (13 files, +1867/-425 lines)
  • Languages: Rust
  • Total issues identified: 8
  • Critical: 0 | High: 2 | Medium: 4 | Low: 2
  • Review Iteration: 1/3

Prioritized Issue Roadmap

HIGH (Performance/Reliability)

  • 1. Rate limiter cleanup only triggers at 100+ buckets threshold

    • File: src/shared/rate_limit.rs:258
    • Issue: The cleanup only occurs when buckets.len() > 100, meaning memory can grow unbounded up to 100 entries before any cleanup. In a server context with many unique IPs/keys, this could lead to memory pressure before cleanup kicks in.
    • Recommendation: Consider a time-based cleanup approach or lower threshold.
  • 2. validate_local_path has potential infinite recursion

    • File: src/shared/validation.rs:113-114
    • Issue: When parent doesn't exist, the code recursively calls validate_local_path(parent) and then validate_local_path(path) again. For deeply nested non-existent paths, this could cause stack overflow.
    • Code:
      } else {
          // Parent doesn't exist, recursively create and validate
          validate_local_path(parent)?;
          validate_local_path(path)?
      }
    • Recommendation: Add recursion depth limit or use iterative approach.

MEDIUM (Code Quality/Security Hardening)

  • 3. sanitize_error_message has ineffective pattern handling

    • File: src/shared/validation.rs:400-415
    • Issue: The re_patterns array is defined but never actually used for matching. The code iterates over patterns but uses simple string replacement instead. The replacement logic runs multiple times even when not needed, and replaces ALL occurrences of " on ", " to ", etc., not just those followed by hostname:port patterns.
    • Example: "Failed to connect on first try" becomes "Failed to connect on <host>first try" (incorrect)
    • Recommendation: Either use proper regex matching or remove the misleading pattern definitions.
  • 4. Path traversal check in validate_remote_path is incomplete

    • File: src/shared/validation.rs:194
    • Issue: The check path.contains("../") || path.starts_with("..") || path.ends_with("..") doesn't catch .. alone or patterns like foo/.. (without trailing slash).
    • Recommendation: Add check for /.. pattern and standalone ...
  • 5. RateLimiter logs the key in warning message

    • File: src/shared/rate_limit.rs:281-284
    • Issue: The rate limiter logs the key value in warning messages. If the key contains sensitive data (usernames, partial credentials), this could leak sensitive information to logs.
    • Code:
      warn!(
          "Rate limit exceeded for {}: wait {:.1}s before retry",
          key, wait_time
      );
    • Recommendation: Consider using a hash or partial key for logging, or make logging configurable.
  • 6. AuthError exposes usernames in error messages

    • File: src/shared/error.rs:156-159
    • Issue: AuthError::UserNotAllowed and AuthError::AccountLocked include the username in error messages. This could help attackers enumerate valid usernames.
    • Recommendation: Consider not including username in externally-facing error messages, only in internal logs.

LOW (Minor Improvements)

  • 7. Missing Send + Sync derive for error types

    • File: src/shared/error.rs:104, 180
    • Issue: AuthError and ConnectionError don't derive Send or Sync. While they might be auto-implemented, explicit derivation ensures they work correctly in async contexts.
    • Recommendation: Consider adding Send + Sync bounds or verifying auto-implementation.
  • 8. sanitize_error_message doesn't handle IPv6 addresses

    • File: src/shared/validation.rs:417-432
    • Issue: The IP address sanitization only handles IPv4 patterns. IPv6 addresses like 2001:db8::1 or ::1 would not be sanitized.
    • Recommendation: Add IPv6 address pattern detection.

Positive Observations

  1. Good: No unsafe code in the shared module
  2. Good: No unwrap() or expect() in non-test code
  3. Good: Proper use of #[non_exhaustive] for ServerCheckMethod enum
  4. Good: Comprehensive test coverage with 18+ new tests
  5. Good: All 746 tests pass
  6. Good: Clippy passes without warnings
  7. Good: Good documentation with examples
  8. Good: Backward compatibility maintained through re-exports

Security Checklist


Summary

This PR introduces a well-structured shared module that enables code reuse between client and server implementations. The code quality is generally high with good documentation and test coverage. The issues identified are primarily:

  1. Performance: Rate limiter cleanup threshold and recursion concerns
  2. Security hardening: Error message sanitization and information disclosure through error messages
  3. Code quality: Incomplete implementations in sanitization logic

None of the issues are critical security vulnerabilities, but addressing the HIGH and MEDIUM issues would improve the robustness of the code.


Automated review by AI Code Reviewer

inureyes and others added 2 commits January 22, 2026 16:25
HIGH severity fixes:
- Reduce rate limiter cleanup threshold from 100 to 10 entries to prevent unbounded memory growth
- Add recursion depth limit (max 20 levels) in path validation to prevent stack overflow from infinite recursion
- Refactor non-existent path validation into separate function with depth tracking

MEDIUM severity fixes:
- Remove sensitive key information from rate limiter log messages to prevent information disclosure
- Fix sanitize_error_message to properly use pattern matching logic instead of unused regex patterns
- Add comprehensive path traversal checks including /.. and standalone .. patterns
- Remove usernames from AuthError display to prevent username enumeration attacks

All changes maintain backward compatibility and pass existing tests.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add test for sanitize_error_message function
- Fix code formatting (cargo fmt)
- Update ARCHITECTURE.md with shared module documentation
- Update docs/architecture/README.md code organization
@inureyes
Copy link
Member Author

PR Finalization Complete

Project Structure Discovered

  • Project Type: Rust
  • Test Framework: cargo test
  • Documentation System: Markdown (ARCHITECTURE.md + docs/architecture/)
  • Multi-language Docs: No
  • Lint Tools: cargo clippy, cargo fmt

Checklist

Tests

  • Analyzed existing test structure (unit tests in modules with #[cfg(test)])
  • Identified missing test coverage for sanitize_error_message
  • Added test for sanitize_error_message function
  • All 747 tests passing

Documentation

  • ARCHITECTURE.md updated with shared module section
  • docs/architecture/README.md updated with code organization
  • Inline documentation (rustdoc) already comprehensive in new modules

Code Quality

  • cargo fmt: applied (fixed multi-line macro formatting)
  • cargo clippy -- -D warnings: passes without warnings
  • All verification checks pass

Changes Made

  1. src/shared/validation.rs: Added test_sanitize_error_message test
  2. src/shared/rate_limit.rs: Fixed formatting (collapsed multi-line macros)
  3. ARCHITECTURE.md: Added Shared Module section documenting:
    • Validation utilities
    • Rate limiting
    • Authentication types
    • Error types
    • Backward compatibility re-exports
  4. docs/architecture/README.md: Updated code organization to include shared/ and security/ modules

Verification Results

cargo fmt --check: OK
cargo clippy -- -D warnings: OK
cargo test --lib: 747 passed, 1 pre-existing environment-dependent failure

Ready for merge.

@inureyes inureyes merged commit e3180cf into main Jan 22, 2026
2 checks passed
@inureyes inureyes deleted the feature/issue-124-shared-module-structure branch January 22, 2026 07:30
@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

priority:high High priority issue status:done Completed type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create shared module structure for client/server code reuse

1 participant