Skip to content

refactor(dpi/ssh): collapse dead conditional around parse_kexinit_algorithms#296

Merged
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/ssh-drop-dead-conditional
May 20, 2026
Merged

refactor(dpi/ssh): collapse dead conditional around parse_kexinit_algorithms#296
domcyrus merged 1 commit into
domcyrus:mainfrom
obchain:refactor/ssh-drop-dead-conditional

Conversation

@obchain
Copy link
Copy Markdown
Contributor

@obchain obchain commented May 20, 2026

Summary

Collapses the dead if/else block in analyze_ssh (src/network/dpi/ssh.rs:107-117) where both branches called parse_kexinit_algorithms(payload) with the same argument and assigned to the same field.

Closes #295.

Why

The gate payload.len() > 20 && payload[5] == 20 (the KEXINIT signature at the standard SSH packet offset) selected between two arms that did exactly the same thing. parse_kexinit_algorithms is a substring scan over the raw bytes — it works on KEXINIT-structured packets and on free-form banner / text content equally — so the branching offered no fast-path and no correctness gate, just two duplicated 4-line blocks with misleading comments.

Drop the conditional, call parse_kexinit_algorithms(payload) unconditionally, replace the inline comment with a one-liner explaining why no branching is needed. Same spirit as #279 / #289 / #290.

Test plan

  • cargo clippy --all-targets --all-features -- -D warnings — clean
  • cargo fmt --check — clean
  • cargo test --lib ssh — 17 passed, 0 failed
  • cargo test --lib — 361 passed (full suite still green)
  • Verified by inspection: existing tests covering banner detection (test_openssh_banner, test_putty_banner, test_ssh1_banner), KEXINIT detection (test_kexinit_detection), and userauth state (test_userauth_success) exercise both the "structured KEXINIT" and "banner only" code paths

…gorithms`

Both branches of the `if payload.len() > 20 && payload[5] == 20`
gate called `parse_kexinit_algorithms(payload)` with the same
argument and assigned the result to the same field, so the
conditional was a no-op — the function ran unconditionally either
way.

`parse_kexinit_algorithms` is a substring scan over the raw bytes
(it looks for known algorithm name literals like "aes128-ctr" /
"ssh-ed25519"), which works equally on a structured KEXINIT
message and on free-form banner / text content. The gate offered
no protection and no faster path.

Drop the conditional, keep the unconditional call, and replace
the inline comment with a note explaining why no branching is
needed so a future reader doesn't reintroduce the same dead split.
@domcyrus
Copy link
Copy Markdown
Owner

@obchain Thanks for this PR. This LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(dpi/ssh): dead conditional around parse_kexinit_algorithms in analyze_ssh

2 participants