use winrm_rs::{AuthMethod, WinrmClient, WinrmConfig, WinrmCredentials};
use wiremock::matchers::method;
use wiremock::{Mock, MockServer, ResponseTemplate};
fn split_host_port(uri: &str) -> (String, u16) {
let stripped = uri.strip_prefix("http://").unwrap_or(uri);
let stripped = stripped.strip_suffix('/').unwrap_or(stripped);
let (host, port) = stripped.rsplit_once(':').expect("uri has port");
(host.to_string(), port.parse().expect("port parses"))
}
fn basic_client(uri: &str) -> (WinrmClient, String) {
let (host, port) = split_host_port(uri);
let config = WinrmConfig {
port,
use_tls: false,
accept_invalid_certs: false,
connect_timeout_secs: 5,
operation_timeout_secs: 5,
auth_method: AuthMethod::Basic,
max_retries: 0,
..Default::default()
};
let creds = WinrmCredentials::new("user", "pass", "");
let client = WinrmClient::new(config, creds).expect("client builds");
(client, host)
}
#[tokio::test]
async fn http_redirects_are_not_followed() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.respond_with(
ResponseTemplate::new(302).insert_header("location", "http://attacker.invalid/x"),
)
.mount(&server)
.await;
let (client, host) = basic_client(&server.uri());
let result = client.run_command(&host, "ipconfig", &[]).await;
assert!(
result.is_err(),
"expected error on 302, got Ok({:?})",
result.ok()
);
}
#[tokio::test]
async fn host_sanitization_intent_is_documented() {
}