|
| 1 | +use std::sync::Arc; |
| 2 | +use std::time::Duration; |
| 3 | + |
| 4 | +use russh::client; |
| 5 | +use russh::keys::PrivateKey; |
| 6 | +use russh::{MethodKind, MethodSet, server}; |
| 7 | + |
| 8 | +struct AcceptTestServerKey; |
| 9 | + |
| 10 | +impl client::Handler for AcceptTestServerKey { |
| 11 | + type Error = russh::Error; |
| 12 | + |
| 13 | + async fn check_server_key( |
| 14 | + &mut self, |
| 15 | + _server_public_key: &russh::keys::ssh_key::PublicKey, |
| 16 | + ) -> Result<bool, Self::Error> { |
| 17 | + Ok(true) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +struct RemainingMethodsUserSwitchServer; |
| 22 | + |
| 23 | +impl server::Handler for RemainingMethodsUserSwitchServer { |
| 24 | + type Error = russh::Error; |
| 25 | + |
| 26 | + async fn auth_none(&mut self, user: &str) -> Result<server::Auth, Self::Error> { |
| 27 | + if user == "alice" { |
| 28 | + Ok(server::Auth::Reject { |
| 29 | + proceed_with_methods: Some(MethodSet::from(&[MethodKind::Password][..])), |
| 30 | + partial_success: true, |
| 31 | + }) |
| 32 | + } else { |
| 33 | + Ok(server::Auth::reject()) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[tokio::test] |
| 39 | +async fn auth_does_not_carry_remaining_methods_across_username_change() { |
| 40 | + let mut server_config = server::Config::default(); |
| 41 | + server_config.inactivity_timeout = None; |
| 42 | + server_config.auth_rejection_time = Duration::from_millis(1); |
| 43 | + server_config.auth_rejection_time_initial = Some(Duration::from_millis(1)); |
| 44 | + server_config.keys.push( |
| 45 | + PrivateKey::random(&mut rand::rng(), russh::keys::ssh_key::Algorithm::Ed25519).unwrap(), |
| 46 | + ); |
| 47 | + let server_config = Arc::new(server_config); |
| 48 | + |
| 49 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 50 | + let addr = listener.local_addr().unwrap(); |
| 51 | + let server = tokio::spawn(async move { |
| 52 | + let (socket, _) = listener.accept().await.unwrap(); |
| 53 | + let running = server::run_stream(server_config, socket, RemainingMethodsUserSwitchServer) |
| 54 | + .await |
| 55 | + .unwrap(); |
| 56 | + running.await |
| 57 | + }); |
| 58 | + |
| 59 | + let mut session = client::connect( |
| 60 | + Arc::new(client::Config::default()), |
| 61 | + addr, |
| 62 | + AcceptTestServerKey, |
| 63 | + ) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + let alice = session.authenticate_none("alice").await.unwrap(); |
| 68 | + assert!( |
| 69 | + matches!( |
| 70 | + alice, |
| 71 | + client::AuthResult::Failure { |
| 72 | + ref remaining_methods, |
| 73 | + .. |
| 74 | + } if *remaining_methods == MethodSet::from(&[MethodKind::Password][..]) |
| 75 | + ), |
| 76 | + "unexpected Alice auth result: {alice:?}" |
| 77 | + ); |
| 78 | + |
| 79 | + let bob = session.authenticate_none("bob").await.unwrap(); |
| 80 | + if let client::AuthResult::Failure { |
| 81 | + remaining_methods, .. |
| 82 | + } = bob |
| 83 | + { |
| 84 | + assert!( |
| 85 | + remaining_methods.contains(&MethodKind::PublicKey), |
| 86 | + "server reused Alice's narrowed remaining methods for Bob: {remaining_methods:?}" |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + server.abort(); |
| 91 | +} |
0 commit comments