Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(node): check literal peers before any request and keep mirror pin…
…s whole

Review on #612 found gaps around the peer fetch:

- The sync worker sent the withheld-paths request before the peer was
  prepared, so an IP-literal origin the address policy refuses still got
  an HTTP request. Preparation now runs first and a refusal sends nothing.
- The gossip ping round dialed stored IP-literal peer URLs the resolver
  policy never sees. It now classifies literals and counts a refused row
  as a failed probe.
- Every sync git spawn now goes through one scrubbed helper, and the
  scrub also drops GIT_COMMON_DIR, the object-directory variables and
  GIT_CONFIG_COUNT.
- The peer config blanks remote.origin.proxy, which outranks http.proxy.
- The curloptResolve pin drops the "+" prefix, which needs libcurl 7.75.
- An existing mirror gets the new pin before its origin URL changes.
- A transient git version read, or a local config write around a
  completed transfer, leaves the row pending instead of failing it.
- The operator doc states the address ranges and logging as implemented.
  • Loading branch information
beardthelion committed Nov 19, 2026
commit 8e94b3983e7bcff366f83211a7ac9c038ace75e5
2 changes: 2 additions & 0 deletions crates/twigpine-node/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19575,6 +19575,7 @@ mod peer_authority_tests {
/// | `seed_local_peer` (sync.rs) | excluded by test-module location: a deliberate `upsert_peer` bypass for `file://` fixtures, which the public-URL gate rejects |
/// | `a_legacy_row_can_still_refresh_its_liveness` (db/mod.rs) | test-only. Seeds a PRE-GATE row by raw SQL on purpose: `upsert_peer` cannot create one, since the gate it is testing refuses exactly that DID. The fixture models what a deployed table already holds |
/// | `manual_ping_uses_readiness_without_mutating_federation_gate` (api/peers.rs) | test-only fixture seed, same shape and same reason: the row under test must pre-exist so the assertion is about what the ping does NOT rewrite |
/// | `gossip_ping_round_never_dials_a_refused_ip_literal` (lib.rs) | test-only fixture seed for a legacy row holding an IP literal `upsert_peer` would refuse; the round must count it unready without dialing it |
/// | `gossip_ping_round_requires_two_failures_before_persisting_unreachable` (lib.rs) | test-only fixture seed for the gossip hysteresis round; the row must pre-exist so the assertion is about when `mark_peer_ping` fires |
/// | `seed_peer` (p2p/mod.rs) | test-only fixture seed. Raw SQL so the gossip ingest tests can drive the known-peer gate directly, including the did:web case `upsert_peer` would refuse |
/// | `federated_peer_query_is_bounded` (api/repos.rs) | test-only fixture seed for the bounded federation query; inserts reachable rows without exercising peer admission |
Expand Down Expand Up @@ -19677,6 +19678,7 @@ mod peers_table_writer_guard {
"manual_ping_uses_readiness_without_mutating_federation_gate",
1,
),
("gossip_ping_round_never_dials_a_refused_ip_literal", 1),
(
"gossip_ping_round_requires_two_failures_before_persisting_unreachable",
1,
Expand Down
107 changes: 105 additions & 2 deletions crates/twigpine-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,7 @@ async fn gossip_task(
client.as_ref(),
&mut failed_once,
peers,
crate::api::peers::mirror_upstream_ip_permitted,
)
.await;
}
Expand Down Expand Up @@ -2004,18 +2005,40 @@ fn load_or_create_keypair_at(key_path: &std::path::Path) -> Result<Keypair> {
Ok(kp)
}

// reqwest dials an IP-literal host without calling the resolver, so the
// resolver policy never sees it. Classify literals here; hostnames pass on to
// the policy. An unparseable URL is refused too.
fn peer_url_literal_permitted(http_url: &str, permit: fn(std::net::IpAddr) -> bool) -> bool {
let Ok(parsed) = url::Url::parse(http_url) else {
return false;
};
match parsed.host() {
Some(url::Host::Ipv4(v4)) => permit(std::net::IpAddr::V4(v4)),
Some(url::Host::Ipv6(v6)) => permit(std::net::IpAddr::V6(v6)),
Some(url::Host::Domain(_)) => true,
None => false,
}
}

async fn gossip_ping_round(
db: &Db,
client: &reqwest::Client,
failed_once: &mut HashSet<String>,
peers: Vec<db::PeerRecord>,
permit: fn(std::net::IpAddr) -> bool,
) {
{
let current_dids: HashSet<&str> = peers.iter().map(|peer| peer.did.as_str()).collect();
failed_once.retain(|did| current_dids.contains(did.as_str()));
}
for peer in peers {
let ok = ping_peer_readiness(client, &peer.http_url).await;
// A refused URL counts as a failed probe, like a connection error.
let ok = if peer_url_literal_permitted(&peer.http_url, permit) {
ping_peer_readiness(client, &peer.http_url).await
} else {
warn!(did = %peer.did, "peer url refused by address policy; not pinging");
false
};
match peer_ping_db_update(failed_once, &peer.did, ok) {
Some(reachable) => {
if let Err(error) = db.mark_peer_ping(&peer.did, reachable).await {
Expand Down Expand Up @@ -2124,7 +2147,7 @@ mod rate_limit_env_tests {
#[cfg(test)]
mod gossip_ssrf_tests {
use super::{
gossip_ping_round, peer_ping_db_update, ping_peer_readiness,
gossip_ping_round, peer_ping_db_update, peer_url_literal_permitted, ping_peer_readiness,
ping_peer_readiness_with_timeout,
};
use axum::{http::StatusCode, routing::get, Router};
Expand All @@ -2142,6 +2165,11 @@ mod gossip_ssrf_tests {
super::build_http_client().expect("failed to build production http client")
}

// Lets tests that use a loopback mock exercise the round itself.
fn permit_all(_: std::net::IpAddr) -> bool {
true
}

#[test]
fn peer_ping_requires_two_failures_before_marking_unreachable() {
let mut failed_once = HashSet::new();
Expand Down Expand Up @@ -2198,6 +2226,7 @@ mod gossip_ssrf_tests {
&production_http_client(),
&mut failed_once,
state.db.list_peers().await.unwrap(),
permit_all,
)
.await;
assert!(
Expand Down Expand Up @@ -2226,6 +2255,7 @@ mod gossip_ssrf_tests {
&production_http_client(),
&mut failed_once,
state.db.list_peers().await.unwrap(),
permit_all,
)
.await;
assert!(
Expand Down Expand Up @@ -2611,6 +2641,7 @@ mod gossip_ssrf_tests {
&client,
&mut failed_once,
state.db.list_peers().await.unwrap(),
crate::api::peers::mirror_upstream_ip_permitted,
)
.await;
}
Expand All @@ -2627,6 +2658,78 @@ mod gossip_ssrf_tests {
.unwrap();
assert!(!peer.last_ping_ok, "a refused peer must end up unreachable");
}

// A pre-gate row can hold an IP literal, which reqwest dials without the
// resolver. The round must refuse it before any request is sent.
#[sqlx::test]
async fn gossip_ping_round_never_dials_a_refused_ip_literal(pool: PgPool) {
let (port, accepted) = counting_listener().await;
let state = crate::test_support::test_state(pool.clone()).await;
let did = "did:key:z6MkGossipLiteralRefused";
let now = chrono::Utc::now().to_rfc3339();
sqlx::query(
"INSERT INTO peers (did, http_url, last_seen, last_ping_ok, announced_at)
VALUES ($1, $2, $3, TRUE, $3)",
)
.bind(did)
.bind(format!("http://127.0.0.1:{port}"))
.bind(now)
.execute(&pool)
.await
.unwrap();

let mut failed_once = HashSet::new();
for _ in 0..2 {
gossip_ping_round(
state.db.as_ref(),
&production_http_client(),
&mut failed_once,
state.db.list_peers().await.unwrap(),
crate::api::peers::mirror_upstream_ip_permitted,
)
.await;
}

assert_eq!(accepted.load(Ordering::SeqCst), 0);
let peer = state
.db
.list_peers()
.await
.unwrap()
.into_iter()
.find(|p| p.did == did)
.unwrap();
assert!(
!peer.last_ping_ok,
"a refused literal must end up unreachable"
);
}

#[test]
fn peer_url_literal_classifier_refuses_non_public_literals() {
let permit = crate::api::peers::mirror_upstream_ip_permitted;
for url in [
"http://127.0.0.1:8080",
"http://10.0.0.1",
"http://203.0.113.10",
"http://[::1]:8080",
"http://[fd00::1]",
"http://[::ffff:127.0.0.1]",
"not a url",
] {
assert!(
!peer_url_literal_permitted(url, permit),
"{url} must be refused"
);
}
for url in [
"http://8.8.8.8",
"http://[2606:4700::1111]",
"https://peer.example.com",
] {
assert!(peer_url_literal_permitted(url, permit), "{url} must pass");
}
}
}

#[cfg(test)]
Expand Down
Loading