F: try every resolved peer address instead of the first
A peer whose name resolves to both families but which listens on only one of them was reported unreachable, which the documented 0.0.0.0 binding produces on a dual-stack LAN and in the NixOS test network. resolve_target now returns the whole address set (still refusing the name if any address is denied), the HTTP client pins it with resolve_to_addrs so the connector falls through to the next address, and tcp_check tries each address in order with a per-attempt share of the remaining deadline. ClientKey keys on the set. Regression cover: a client test that resolves localhost against a single-family listener, a tests/peers.rs case declaring the peer as http://localhost:<port>, and the VM test's TLS peer gamma now binds 0.0.0.0 (IPv4 only) while alpha and beta stay dual stack. Docs and the contracts section 10 address note describe the new behaviour.
This commit is contained in:
1 parent
3ae8e74a1d
commit
dde6a12ce4
5 files changed
+146
-34
No files matched your search
+108
-22
@@ -66,14 +66,14 @@ enum CaKey {
|
||||
Extra(Vec<u8>),
|
||||
}
|
||||
|
||||
/// One client per CA plus pinned address. Pinning the resolved address both
|
||||
/// keeps DNS rebinding from slipping past the deny list and makes the check
|
||||
/// and the connection use the same address.
|
||||
/// One client per CA plus pinned address set. Pinning every address that
|
||||
/// passed the deny check both keeps DNS rebinding from slipping past it and
|
||||
/// lets the connector try the next address when the first one does not answer.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
struct ClientKey {
|
||||
ca: CaKey,
|
||||
host: Option<String>,
|
||||
addr: SocketAddr,
|
||||
addrs: Vec<SocketAddr>,
|
||||
}
|
||||
|
||||
struct Permits {
|
||||
@@ -151,7 +151,7 @@ impl ReqwestTransport {
|
||||
&self,
|
||||
ca_pem: Option<Vec<u8>>,
|
||||
host: Option<&str>,
|
||||
addr: SocketAddr,
|
||||
addrs: &[SocketAddr],
|
||||
) -> Result<reqwest::Client, PeerError> {
|
||||
let key = ClientKey {
|
||||
ca: match &ca_pem {
|
||||
@@ -159,7 +159,7 @@ impl ReqwestTransport {
|
||||
Some(pem) => CaKey::Extra(pem.clone()),
|
||||
},
|
||||
host: host.map(str::to_owned),
|
||||
addr,
|
||||
addrs: addrs.to_vec(),
|
||||
};
|
||||
let mut cache = Self::lock(&self.clients);
|
||||
if let Some(client) = cache.get(&key) {
|
||||
@@ -168,7 +168,7 @@ impl ReqwestTransport {
|
||||
if cache.len() >= crate::peers::types::MAX_PEERS {
|
||||
cache.clear();
|
||||
}
|
||||
let client = build_client(ca_pem.as_deref(), host, addr)?;
|
||||
let client = build_client(ca_pem.as_deref(), host, addrs)?;
|
||||
cache.insert(key, client.clone());
|
||||
Ok(client)
|
||||
}
|
||||
@@ -222,12 +222,14 @@ fn denied(ip: IpAddr, deny: &[IpNet]) -> bool {
|
||||
}
|
||||
|
||||
/// Resolve the peer host and reject denied addresses before any connection is
|
||||
/// made. The returned address is pinned into the HTTP client; an IP literal
|
||||
/// needs no pin because there is no name to rebind.
|
||||
/// made. Every address is returned so a peer that listens on one family is not
|
||||
/// reported as down when its name also resolves to the other; the set is
|
||||
/// pinned into the HTTP client. An IP literal needs no pin because there is no
|
||||
/// name to rebind.
|
||||
async fn resolve_target(
|
||||
peer: &PeerConfig,
|
||||
deny: &[IpNet],
|
||||
) -> Result<(Option<String>, SocketAddr), PeerError> {
|
||||
) -> Result<(Option<String>, Vec<SocketAddr>), PeerError> {
|
||||
validate_target(peer)?;
|
||||
let port = peer
|
||||
.url
|
||||
@@ -242,9 +244,11 @@ async fn resolve_target(
|
||||
.collect(),
|
||||
None => return Err(PeerError::Unreachable),
|
||||
};
|
||||
let Some(addr) = addrs.first().copied() else {
|
||||
if addrs.is_empty() {
|
||||
return Err(PeerError::Dns);
|
||||
};
|
||||
}
|
||||
// One denied address refuses the whole name: picking a "safe" address out
|
||||
// of the set would let a hostile resolver keep the denied one around.
|
||||
if addrs.iter().any(|addr| denied(addr.ip(), deny)) {
|
||||
return Err(PeerError::Unreachable);
|
||||
}
|
||||
@@ -252,14 +256,14 @@ async fn resolve_target(
|
||||
Some(url::Host::Domain(name)) => Some(name.to_owned()),
|
||||
_ => None,
|
||||
};
|
||||
Ok((pin, addr))
|
||||
Ok((pin, addrs))
|
||||
}
|
||||
|
||||
/// Ring is passed in so this process never calls `install_default`.
|
||||
fn build_client(
|
||||
extra_ca: Option<&[u8]>,
|
||||
host: Option<&str>,
|
||||
addr: SocketAddr,
|
||||
addrs: &[SocketAddr],
|
||||
) -> Result<reqwest::Client, PeerError> {
|
||||
let tls = rustls_config(extra_ca)?;
|
||||
let mut builder = reqwest::Client::builder()
|
||||
@@ -268,7 +272,10 @@ fn build_client(
|
||||
.http1_only()
|
||||
.tls_backend_preconfigured(tls);
|
||||
if let Some(host) = host {
|
||||
builder = builder.resolve(host, addr);
|
||||
// The connector walks the pinned list, so the address the deny check
|
||||
// approved is the only kind that can be dialled, and an address family
|
||||
// the peer does not listen on no longer hides one it does.
|
||||
builder = builder.resolve_to_addrs(host, addrs);
|
||||
}
|
||||
builder.build().map_err(|_| PeerError::Tls)
|
||||
}
|
||||
@@ -524,11 +531,26 @@ impl PeerTransport for ReqwestTransport {
|
||||
let deadline = tokio::time::Instant::now() + timeout;
|
||||
within(deadline, async {
|
||||
let deny = self.deny_list();
|
||||
let (_, addr) = resolve_target(peer, &deny).await?;
|
||||
let started = tokio::time::Instant::now();
|
||||
let stream = tokio::net::TcpStream::connect(addr).await.map_err(map_io)?;
|
||||
drop(stream);
|
||||
Ok(started.elapsed())
|
||||
let (_, addrs) = resolve_target(peer, &deny).await?;
|
||||
let mut left = addrs.len();
|
||||
let mut last = PeerError::Unreachable;
|
||||
for addr in addrs {
|
||||
// Each address gets its share of what remains, so a blackholed
|
||||
// first address cannot eat the deadline the others need.
|
||||
let now = tokio::time::Instant::now();
|
||||
let share = deadline.saturating_duration_since(now) / left as u32;
|
||||
left -= 1;
|
||||
let started = tokio::time::Instant::now();
|
||||
match tokio::time::timeout(share, tokio::net::TcpStream::connect(addr)).await {
|
||||
Ok(Ok(stream)) => {
|
||||
drop(stream);
|
||||
return Ok(started.elapsed());
|
||||
}
|
||||
Ok(Err(err)) => last = map_io(err),
|
||||
Err(_) => last = PeerError::Timeout,
|
||||
}
|
||||
}
|
||||
Err(last)
|
||||
})
|
||||
.await
|
||||
}
|
||||
@@ -546,7 +568,7 @@ impl PeerTransport for ReqwestTransport {
|
||||
let (resp, permits, max) = within(deadline, async {
|
||||
let url = request_url(peer, &req)?;
|
||||
let deny = self.deny_list();
|
||||
let (pin, addr) = resolve_target(peer, &deny).await?;
|
||||
let (pin, addrs) = resolve_target(peer, &deny).await?;
|
||||
self.warn_plain_http(peer);
|
||||
let token_path = peer.token_file.clone();
|
||||
let secret = tokio::task::spawn_blocking(move || load_token(&token_path))
|
||||
@@ -570,7 +592,7 @@ impl PeerTransport for ReqwestTransport {
|
||||
} else {
|
||||
Some(self.acquire(&peer.name)?)
|
||||
};
|
||||
let client = self.client_for(ca, pin.as_deref(), addr)?;
|
||||
let client = self.client_for(ca, pin.as_deref(), &addrs)?;
|
||||
let max = self.max_bytes();
|
||||
let mut builder = client.get(url);
|
||||
for (name, value) in &req.headers {
|
||||
@@ -1196,6 +1218,70 @@ mod tests {
|
||||
let _ = std::fs::remove_file(&token);
|
||||
}
|
||||
|
||||
/// A name is resolved to every address, and one denied address refuses
|
||||
/// the name rather than being skipped in favour of a permitted one.
|
||||
#[tokio::test]
|
||||
async fn every_resolved_address_is_returned_and_checked() {
|
||||
let token = write_token("t", 0o600);
|
||||
let mut p = peer(9, &token, None);
|
||||
p.url = url("http://localhost:9");
|
||||
let (pin, addrs) = resolve_target(&p, &[]).await.unwrap();
|
||||
assert_eq!(pin.as_deref(), Some("localhost"));
|
||||
assert!(!addrs.is_empty());
|
||||
|
||||
// `localhost` may resolve to both loopback families. Denying one of
|
||||
// them must refuse the whole name.
|
||||
let denied_family = if addrs.iter().any(|a| a.is_ipv4()) {
|
||||
"127.0.0.0/8"
|
||||
} else {
|
||||
"::1/128"
|
||||
};
|
||||
let deny = deny_networks(&[denied_family.to_owned()]).unwrap();
|
||||
assert!(matches!(
|
||||
resolve_target(&p, &deny).await,
|
||||
Err(PeerError::Unreachable)
|
||||
));
|
||||
let _ = std::fs::remove_file(&token);
|
||||
}
|
||||
|
||||
/// A peer declared by name must be reached on the family it listens on:
|
||||
/// the listener takes one family only, so whichever family the resolver
|
||||
/// puts first, the other one has to be tried as well.
|
||||
#[tokio::test]
|
||||
async fn tcp_check_tries_every_resolved_address() {
|
||||
let token = write_token("t", 0o600);
|
||||
let transport = ReqwestTransport::new(&limits(4, 8, 1024)).unwrap();
|
||||
let mut tried = Vec::new();
|
||||
for bind in ["127.0.0.1", "[::1]"] {
|
||||
let Ok(listener) = TcpListener::bind(format!("{bind}:0")).await else {
|
||||
continue; // this environment has no IPv6 loopback
|
||||
};
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(async move {
|
||||
while let Ok((mut sock, _)) = listener.accept().await {
|
||||
let _ = read_req(&mut sock).await;
|
||||
let _ = sock
|
||||
.write_all(
|
||||
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
let mut p = peer(0, &token, None);
|
||||
p.url = url(&format!("http://localhost:{port}"));
|
||||
assert!(
|
||||
transport
|
||||
.tcp_check(&p, Duration::from_secs(2))
|
||||
.await
|
||||
.is_ok(),
|
||||
"{bind}"
|
||||
);
|
||||
tried.push(bind);
|
||||
}
|
||||
assert!(!tried.is_empty(), "no loopback family available");
|
||||
let _ = std::fs::remove_file(&token);
|
||||
}
|
||||
|
||||
/// The request timeout bounds preparation, not a follow stream: chunks
|
||||
/// keep arriving long after it would have expired.
|
||||
#[tokio::test]
|
||||
|
||||
Reference in new issue
Block a user