Skip to content

Commit 62ec711

Browse files
authored
HELP-68823 Fix invalid_me comparison and normalize host strings (#1319) (#1321)
1 parent 6ad6c6e commit 62ec711

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/client/options.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
convert::TryFrom,
1111
fmt::{self, Display, Formatter, Write},
1212
hash::{Hash, Hasher},
13-
net::Ipv6Addr,
13+
net::{Ipv4Addr, Ipv6Addr},
1414
path::PathBuf,
1515
str::FromStr,
1616
sync::Arc,
@@ -272,6 +272,14 @@ impl ServerAddress {
272272
.into());
273273
}
274274

275+
let normalized_hostname = if let Ok(v4) = hostname.parse::<Ipv4Addr>() {
276+
v4.to_string()
277+
} else if let Ok(v6) = hostname.parse::<Ipv6Addr>() {
278+
v6.to_string()
279+
} else {
280+
hostname.to_lowercase()
281+
};
282+
275283
let port = if let Some(port) = port {
276284
match u16::from_str(port) {
277285
Ok(0) | Err(_) => {
@@ -291,7 +299,7 @@ impl ServerAddress {
291299
};
292300

293301
Ok(Self::Tcp {
294-
host: hostname.to_lowercase(),
302+
host: normalized_hostname,
295303
port,
296304
})
297305
}

src/sdam/description/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl ServerDescription {
334334
pub(crate) fn invalid_me(&self) -> Result<bool> {
335335
if let Some(ref reply) = self.reply.as_ref().map_err(Clone::clone)? {
336336
if let Some(ref me) = reply.command_response.me {
337-
return Ok(&self.address.to_string() != me);
337+
return Ok(self.address != ServerAddress::parse(me)?);
338338
}
339339
}
340340

src/sdam/test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,27 @@ async fn removed_server_monitor_stops() -> crate::error::Result<()> {
349349

350350
Ok(())
351351
}
352+
353+
#[test]
354+
fn ipv6_invalid_me() {
355+
let addr = ServerAddress::Tcp {
356+
host: "::1".to_string(),
357+
port: Some(8191),
358+
};
359+
let desc = ServerDescription {
360+
address: addr.clone(),
361+
server_type: super::ServerType::RsSecondary,
362+
last_update_time: None,
363+
average_round_trip_time: None,
364+
reply: Ok(Some(crate::hello::HelloReply {
365+
server_address: addr.clone(),
366+
command_response: crate::hello::HelloCommandResponse {
367+
me: Some("[::1]:8191".to_string()),
368+
..Default::default()
369+
},
370+
raw_command_response: bson::RawDocumentBuf::new(),
371+
cluster_time: None,
372+
})),
373+
};
374+
assert!(!desc.invalid_me().unwrap());
375+
}

0 commit comments

Comments
 (0)