Skip to content
Merged
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
Next Next commit
fix(node): bind a peer row to its DID at the upsert_peer boundary (#273)
upsert_peer ended in an unconditional ON CONFLICT DO UPDATE SET http_url,
so any unauthenticated caller could repoint any peer. A repointed row
steers the sync worker's git remote, the post-receive notify fan-out,
trigger_sync, and the public GET /api/v1/resolve/{did}, none of which
consult the reachability flag that #270's stopgap resets.

An unsigned write may now insert an unseen DID but may never change an
existing row's http_url; changing one requires a verified signature from
that row's own DID. The gate is at the db boundary rather than in the
announce handler because there is a second writer: the bootstrap
announce-back in main.rs reads a peer's own JSON response body with no
proof of anything, and now declares itself unproven, which makes it
insert-only by construction.

The proven variant carries the DID it proves and upsert_peer compares it
against the row being written. A bare proven/unproven flag would say that
something was proven without saying which DID, leaving the real check in
the handler and reproducing RUSTSEC-2022-0009, where a valid signature was
accepted without checking it derived the claimed identity.

An unproven insert is restricted to did:key. Only did:key can ever
authenticate here, so a did:web or did:gitlawb row created unsigned would
become unwritable by anyone once this rule lands.

The unproven path derives its refusal from rows_affected, because an
ON CONFLICT DO UPDATE with a guarding WHERE reports zero rows rather than
raising, and a silent no-op is the fail-open this gate exists to prevent.
The refusal is a typed error the handler maps to 403, downcast out of the
anyhow chain the way sqlx errors already are, so it does not render as a
500.

Observed at c83cbc5 before the fix: an unsigned announce naming an
existing peer returned 200 OK and left the attacker's URL stored. That
test is the RED this change turns green.

Two of #270's reachability tests now drive the proven path. Under this
rule an unproven repoint is refused, so they can no longer express a URL
change through it; they assert the same behavior as before.
  • Loading branch information
beardthelion committed Aug 4, 2026
commit 14481df288336ac82624cd02ebfc9643df42614b
104 changes: 100 additions & 4 deletions crates/gitlawb-node/src/api/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,32 @@ use gitlawb_core::did::Did;
use serde::{Deserialize, Serialize};

use crate::auth::AuthenticatedDid;
use crate::db::{PeerWriteAuthority, PeerWriteDenied};
use crate::error::{AppError, Result};
use crate::state::AppState;

/// Map an `upsert_peer` failure onto its response class.
///
/// The boundary's denials arrive inside an anyhow chain, so they are downcast
/// back out the way `From<anyhow::Error> for AppError` already recovers sqlx
/// errors. Without this a refusal renders as a 500, which is the mis-rendered
/// denial this gate exists to avoid. An authority refusal is 403; naming a DID
/// method that can never authenticate is a fact about the input's form, so it
/// is the 400 validation class.
fn peer_write_error(err: anyhow::Error) -> AppError {
match err.downcast::<PeerWriteDenied>() {
Ok(denied) => match &denied {
PeerWriteDenied::UnsupportedDidMethod { .. } => {
AppError::BadRequest(denied.to_string())
}
PeerWriteDenied::UnprovenRepoint { .. } | PeerWriteDenied::ProofDidMismatch { .. } => {
AppError::Forbidden(denied.to_string())
}
},
Err(other) => AppError::from(other),
}
}

#[derive(Debug, Deserialize)]
pub struct AnnounceRequest {
/// The DID of the announcing node
Expand Down Expand Up @@ -187,18 +210,23 @@ pub async fn announce(
.did
.parse()
.map_err(|e: gitlawb_core::Error| AppError::BadRequest(e.to_string()))?;
if let Some(Extension(auth)) = auth {
// The extension's DID is cryptographically derived, never body-supplied:
// auth/mod.rs resolves the verifying key FROM the keyid DID itself, so what
// is carried through to the upsert boundary below is a proof, not a claim.
let proven_did: Option<String> = if let Some(Extension(auth)) = auth {
if auth.0 != announced_did.to_string() {
return Err(AppError::BadRequest(
"Signature keyid must match announced DID".into(),
));
}
Some(auth.0)
} else {
tracing::warn!(
did = %announced_did,
"accepted unsigned peer announce; set GITLAWB_REQUIRE_SIGNED_PEER_WRITES=true after all peers upgrade"
);
}
None
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Validate the URL is a public http(s) endpoint. The announce route is
// reachable unauthenticated (until all peers sign), so without this an
Expand Down Expand Up @@ -228,7 +256,20 @@ pub async fn announce(
));
}

state.db.upsert_peer(&req.did, &req.http_url).await?;
// The authority declaration, not a re-statement of the check above: the
// boundary carries the proven DID and re-checks it against the row itself,
// so the keyid comparison above is an early rejection rather than the only
// gate. The other writer (the bootstrap announce-back in main.rs) never
// reaches this handler at all.
let authority = match proven_did.as_deref() {
Some(did) => PeerWriteAuthority::Proven(did),
None => PeerWriteAuthority::Unproven,
};
state
.db
.upsert_peer(&req.did, &req.http_url, authority)
.await
.map_err(peer_write_error)?;

tracing::info!(did = %req.did, url = %req.http_url, "peer announced");

Expand Down Expand Up @@ -770,7 +811,11 @@ mod tests {
let did = Keypair::generate().did().to_string();
state
.db
.upsert_peer(&did, "https://peer.example.com")
.upsert_peer(
&did,
"https://peer.example.com",
crate::db::PeerWriteAuthority::Unproven,
)
.await
.unwrap();
did
Expand Down Expand Up @@ -1155,6 +1200,57 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}

/// U1 (#273). An unsigned announce must not repoint an existing peer's row.
///
/// Kills the unconditional `ON CONFLICT(did) DO UPDATE SET http_url = $2` in
/// `Db::upsert_peer`. The failure message carries BOTH observed values,
/// because the gate is the observed rewrite (2xx AND the attacker URL
/// stored), not merely a failing assertion: a 429 from the peer-write brake,
/// a fixture URL `is_public_http_url` rejects, or a DB error would all read
/// as "assertion failed" while proving nothing about this defect.
#[sqlx::test]
async fn unsigned_announce_cannot_repoint_an_existing_peer(pool: PgPool) {
const HONEST: &str = "https://honest-peer.example.com";
const ATTACKER: &str = "https://attacker.example.com";

let state = test_state(pool).await;
let did = Keypair::generate().did().to_string();
state
.db
.upsert_peer(&did, HONEST, crate::db::PeerWriteAuthority::Unproven)
.await
.unwrap();

let stored_url = |peers: Vec<crate::db::PeerRecord>| {
peers.into_iter().find(|p| p.did == did).map(|p| p.http_url)
};
assert_eq!(
stored_url(state.db.list_peers().await.unwrap()).as_deref(),
Some(HONEST),
"seed did not take; the case below would pass vacuously"
);

// Clone before build_router consumes state, so the row can be read back.
let db = state.db.clone();
let body = format!(r#"{{"did":"{did}","http_url":"{ATTACKER}"}}"#);
let resp = crate::server::build_router(state)
.oneshot(unsigned_post(
"/api/v1/peers/announce",
&body,
"203.0.113.77:5000",
))
.await
.unwrap();
let status = resp.status();
let stored = stored_url(db.list_peers().await.unwrap())
.expect("peer row vanished; neither the allowed nor the denied outcome");

assert_eq!(
stored, HONEST,
"unsigned announce repointed an existing peer (observed status {status}, stored http_url {stored})"
);
}

// Build a request carrying a REAL RFC 9421 signature (not just an injected
// extension), so it passes require_signature on the full production router.
fn real_signed_trigger(peer: &str) -> Request<Body> {
Expand Down
Loading