Refactor proxy and transport modules for improved safety and performance

- Enhanced linting rules in `src/proxy/mod.rs` to enforce stricter code quality checks in production.
- Updated hash functions in `src/proxy/middle_relay.rs` for better efficiency.
- Added new security tests in `src/proxy/tests/middle_relay_stub_completion_security_tests.rs` to validate desynchronization behavior.
- Removed ignored test stubs in `src/proxy/tests/middle_relay_security_tests.rs` to clean up the test suite.
- Improved error handling and code readability in various transport modules, including `src/transport/middle_proxy/config_updater.rs` and `src/transport/middle_proxy/pool.rs`.
- Introduced new padding functions in `src/stream/frame_stream_padding_security_tests.rs` to ensure consistent behavior across different implementations.
- Adjusted TLS stream validation in `src/stream/tls_stream.rs` for better boundary checking.
- General code cleanup and dead code elimination across multiple files to enhance maintainability.
This commit is contained in:
David Osipov
2026-03-21 20:05:07 +04:00
parent a6c298b633
commit 4c32370b25
35 changed files with 794 additions and 174 deletions
@@ -0,0 +1,56 @@
fn old_padding_round_up_to_4(len: usize) -> Option<usize> {
len.checked_add(3)
.map(|sum| sum / 4)
.and_then(|words| words.checked_mul(4))
}
fn new_padding_round_up_to_4(len: usize) -> Option<usize> {
len.div_ceil(4).checked_mul(4)
}
#[test]
fn padding_rounding_equivalent_for_extensive_safe_domain() {
for len in 0usize..=200_000usize {
let old = old_padding_round_up_to_4(len).expect("old expression must be safe");
let new = new_padding_round_up_to_4(len).expect("new expression must be safe");
assert_eq!(old, new, "mismatch for len={len}");
assert!(new >= len, "rounded length must not shrink: len={len}, out={new}");
assert_eq!(new % 4, 0, "rounded length must stay 4-byte aligned");
}
}
#[test]
fn padding_rounding_equivalent_near_usize_limit_when_old_is_defined() {
let candidates = [
usize::MAX - 3,
usize::MAX - 4,
usize::MAX - 5,
usize::MAX - 6,
usize::MAX - 7,
usize::MAX - 8,
usize::MAX - 15,
usize::MAX / 2,
(usize::MAX / 2) + 1,
];
for len in candidates {
let old = old_padding_round_up_to_4(len);
let new = new_padding_round_up_to_4(len);
if let Some(old_val) = old {
assert_eq!(Some(old_val), new, "safe-domain mismatch for len={len}");
}
}
}
#[test]
fn padding_rounding_documents_overflow_boundary_behavior() {
// For very large lengths, arithmetic round-up may overflow regardless of spelling.
// This documents the boundary so future changes do not assume universal safety.
assert_eq!(old_padding_round_up_to_4(usize::MAX), None);
assert_eq!(old_padding_round_up_to_4(usize::MAX - 1), None);
assert_eq!(old_padding_round_up_to_4(usize::MAX - 2), None);
// The div_ceil form avoids `len + 3` overflow, but final `* 4` can still overflow.
assert_eq!(new_padding_round_up_to_4(usize::MAX), None);
assert_eq!(new_padding_round_up_to_4(usize::MAX - 1), None);
}