wallet: answer the Digest challenge on the connection that issued it
monero-wallet-rpc (epee) keeps its Digest nonce per TCP connection: the `http_server_auth` lives inside `http_custom_handler`, which is created per connection, so its nonce starts empty and any request without a valid `Authorization` field mints a fresh one. The client opened a *second* connection for the authenticated retry, presenting a nonce that socket never issued — so a real wallet RPC answered 401 forever, however correct the login was. That is the `wallet RPC HTTP HTTP/1.1 401 Unauthorized` the TUI and Android showed; the canned mocks replayed the challenge on any socket, so they stayed green. - `post_digest`/`http_digest`: one `TcpStream` for the challenge and the retry, no `Connection: close` on the challenge (epee closes the socket with the 401 when the request asks for it), `read_one_response` honouring `Content-Length` (a keep-alive response never ends in EOF; with no length the body ends at EOF, which is what keeps the 1 MiB cap meaningful for an open RPC). - A 401 after our *own* challenge now reads: the login does not match the child's `--rpc-login`. - Mocks in `tests/wallet.rs` and `src/node.rs` model the per-connection nonce: `serve_one_conn`/`serve_rpc_conns`. Two tests pin it — one RPC call uses one connection, and a nonce presented on another connection is refused. - `docs/WALLET.md`: what a 401 means and why it is not the flow. Verified end-to-end against `monero-wallet-rpc` v0.18.5.1 (`--rpc-login`, `--offline`, real wallet open): unfixed tree → `wallet RPC HTTP HTTP/1.1 401 Unauthorized`; fixed tree → `connected: true` with the wallet's real address.
This commit is contained in:
parent
b805bd4ad9
commit
cb411da813
4 changed files with 435 additions and 161 deletions
159
src/node.rs
159
src/node.rs
|
|
@ -2205,59 +2205,80 @@ mod tests {
|
|||
.to_string()
|
||||
}
|
||||
|
||||
/// A wallet child that clears the Digest challenge and answers one
|
||||
/// `transfer` — the two-connection dance `tests/wallet.rs` mocks.
|
||||
/// Serve a mock wallet RPC whose Digest state is **per connection**, the way
|
||||
/// epee does it: the nonce is minted by the socket that answered 401 and is
|
||||
/// valid nowhere else, so a client that retries on a fresh socket is refused
|
||||
/// forever. `reply` maps a request to its JSON-RPC body. The socket closes
|
||||
/// after an authenticated reply — the client asks for `Connection: close`
|
||||
/// there and the real server honours it.
|
||||
async fn serve_mock_rpc(
|
||||
listener: tokio::net::TcpListener,
|
||||
reply: Arc<dyn Fn(&str) -> String + Send + Sync>,
|
||||
) {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
static NEXT_NONCE: AtomicUsize = AtomicUsize::new(1);
|
||||
|
||||
while let Ok((mut sock, _)) = listener.accept().await {
|
||||
let reply = Arc::clone(&reply);
|
||||
tokio::spawn(async move {
|
||||
let mut nonce: Option<String> = None;
|
||||
loop {
|
||||
let mut buf = vec![0u8; 8192];
|
||||
let Ok(n) = sock.read(&mut buf).await else {
|
||||
return;
|
||||
};
|
||||
if n == 0 {
|
||||
return;
|
||||
}
|
||||
let req = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
let answered = req.contains("Authorization:")
|
||||
&& nonce.as_deref().is_some_and(|held| req.contains(held));
|
||||
if !answered {
|
||||
let fresh = format!("nonce{}", NEXT_NONCE.fetch_add(1, Ordering::Relaxed));
|
||||
let challenge = format!(
|
||||
"HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Digest realm=\"monero-rpc\", nonce=\"{fresh}\", qop=\"auth\", algorithm=MD5\r\nContent-Length: 0\r\n\r\n"
|
||||
);
|
||||
if sock.write_all(challenge.as_bytes()).await.is_err() {
|
||||
return;
|
||||
}
|
||||
nonce = Some(fresh);
|
||||
continue;
|
||||
}
|
||||
let body = reply(&req);
|
||||
let ok = format!(
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{body}",
|
||||
body.len()
|
||||
);
|
||||
let _ = sock.write_all(ok.as_bytes()).await;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// A loopback wallet RPC that answers what the tip path actually asks for:
|
||||
/// a digest challenge, then a synced `get_info` for the readiness gate and
|
||||
/// `tx_hash` for the transfer. The old single-answer mock stopped covering
|
||||
/// the path when `Wallet::transfer` started probing the chain tip first —
|
||||
/// it answered `tx_hash` to `get_info` and every tip was refused as unsynced.
|
||||
async fn mock_wallet() -> Wallet {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
|
||||
.await
|
||||
.expect("bind mock wallet");
|
||||
let port = listener.local_addr().expect("mock wallet addr").port();
|
||||
tokio::spawn(async move {
|
||||
// Every call starts unauthenticated, so every call gets one
|
||||
// challenge — the client retries each request with digest auth.
|
||||
for _ in 0..8 {
|
||||
let Ok((mut sock, _)) = listener.accept().await else {
|
||||
return;
|
||||
};
|
||||
let mut buf = vec![0u8; 8192];
|
||||
let Ok(n) = sock.read(&mut buf).await else {
|
||||
return;
|
||||
};
|
||||
let req = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
if !req.contains("Authorization:") {
|
||||
let _ = sock
|
||||
.write_all(
|
||||
b"HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Digest realm=\"monero-rpc\", nonce=\"abcnonce\", qop=\"auth\", algorithm=MD5\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
|
||||
)
|
||||
.await;
|
||||
continue;
|
||||
}
|
||||
let body = if req.contains("get_info") {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"height":100,"target_height":100,"synchronized":true}}"#
|
||||
} else {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"tx_hash":"abc123"}}"#
|
||||
};
|
||||
let _ = sock
|
||||
.write_all(
|
||||
format!(
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.await;
|
||||
let reply: Arc<dyn Fn(&str) -> String + Send + Sync> = Arc::new(|req: &str| {
|
||||
if req.contains("get_info") {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"height":100,"target_height":100,"synchronized":true}}"#
|
||||
.to_string()
|
||||
} else {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"tx_hash":"abc123"}}"#.to_string()
|
||||
}
|
||||
});
|
||||
tokio::spawn(serve_mock_rpc(listener, reply));
|
||||
Wallet::from_url(&format!("http://ow:[email protected]:{port}")).expect("wallet url")
|
||||
}
|
||||
|
||||
/// The payer of a tip must see it in their own chat: `receipt_chat_line` is
|
||||
/// the recipient's view of the frame that arrived, `tip_chat_line` is what
|
||||
/// the sender just spent.
|
||||
|
|
@ -2522,60 +2543,34 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
/// A loopback wallet RPC that answers the *status* probe: a digest
|
||||
/// challenge, then `get_address` / `get_balance` / `get_info`, with the
|
||||
/// chain numbers a syncing daemon reports. [`mock_wallet`] above answers the
|
||||
/// *transfer* path; this one answers a refresh, which never transfers.
|
||||
/// A loopback wallet RPC that answers the *status* probe: a digest
|
||||
/// challenge, then `get_address` / `get_balance` / `get_info`, with the
|
||||
/// chain numbers a syncing daemon reports. [`mock_wallet`] above answers the
|
||||
/// *transfer* path; this one answers a refresh, which never transfers.
|
||||
async fn mock_syncing_wallet(height: u64, target: u64) -> Wallet {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
|
||||
.await
|
||||
.expect("bind mock wallet");
|
||||
let port = listener.local_addr().expect("mock wallet addr").port();
|
||||
tokio::spawn(async move {
|
||||
// Every call starts unauthenticated, so every call gets one
|
||||
// challenge — the client retries each request with digest auth.
|
||||
for _ in 0..8 {
|
||||
let Ok((mut sock, _)) = listener.accept().await else {
|
||||
return;
|
||||
};
|
||||
let mut buf = vec![0u8; 8192];
|
||||
let Ok(n) = sock.read(&mut buf).await else {
|
||||
return;
|
||||
};
|
||||
let req = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
if !req.contains("Authorization:") {
|
||||
let _ = sock
|
||||
.write_all(
|
||||
b"HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Digest realm=\"monero-rpc\", nonce=\"abcnonce\", qop=\"auth\", algorithm=MD5\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
|
||||
)
|
||||
.await;
|
||||
continue;
|
||||
}
|
||||
let body = if req.contains("get_info") {
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":"0","result":{{"height":{height},"target_height":{target},"synchronized":true}}}}"#
|
||||
)
|
||||
} else if req.contains("get_address") {
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":"0","result":{{"address":"{addr}"}}}}"#,
|
||||
addr = xmr_addr()
|
||||
)
|
||||
} else {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"balance":"0"}}"#.to_string()
|
||||
};
|
||||
let _ = sock
|
||||
.write_all(
|
||||
format!(
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.await;
|
||||
let reply: Arc<dyn Fn(&str) -> String + Send + Sync> = Arc::new(move |req: &str| {
|
||||
if req.contains("get_info") {
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":"0","result":{{"height":{height},"target_height":{target},"synchronized":true}}}}"#
|
||||
)
|
||||
} else if req.contains("get_address") {
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":"0","result":{{"address":"{addr}"}}}}"#,
|
||||
addr = xmr_addr()
|
||||
)
|
||||
} else {
|
||||
r#"{"jsonrpc":"2.0","id":"0","result":{"balance":"0"}}"#.to_string()
|
||||
}
|
||||
});
|
||||
tokio::spawn(serve_mock_rpc(listener, reply));
|
||||
Wallet::from_url(&format!("http://ow:[email protected]:{port}")).expect("wallet url")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue