wallet: answer the Digest challenge on the connection that issued it #137

Merged
sirius merged 1 commit from fix/wallet-rpc-digest-same-connection into main 2026-09-20 13:30:21 +00:00
Owner

The wallet RPC Digest handshake never worked against a real monero-wallet-rpc: the authenticated retry went out on a new TCP connection, and epee binds its Digest nonce to the connection that issued the challenge.

Symptom (TUI and Android, unchanged by chain-source or creds):

wallet RPC HTTP HTTP/1.1 401 Unauthorized

Why

http_custom_handler owns http_server_auth and is constructed per connection; its session starts with an empty nonce and counter 0, and any request without a valid Authorization field mints a fresh nonce (contrib/epee/src/http_auth.cpp, http_protocol_handler.h:186,223). A retry on a second socket therefore presents a nonce that socket never issued → kStale → 401 again, forever, however correct the login is. curl and monero's own client work because they retry on the same connection.

src/wallet.rs's rpc_with did the challenge through one post_timeout (fresh TcpStream, Connection: close) and the retry through a second — and Connection: close on the challenge made epee hang up right after the 401 anyway.

Fix

  • post_digest / http_digest: one TcpStream for the challenge and the retry, no Connection: close on the challenge, read_one_response honouring Content-Length (a keep-alive response never ends in EOF; epee always sends the header, and with none the body ends at EOF, which keeps the 1 MiB cap meaningful for an open RPC), authenticated request on that same stream.
  • A 401 after our own challenge now reports the honest cause: the login does not match the child's --rpc-login.
  • Mocks stop being able to hide it: tests/wallet.rs gains serve_one_conn/serve_rpc_conns (per-connection nonce) and src/node.rs's two wallet mocks use serve_mock_rpc on the same model. Two new tests pin the behaviour: one RPC call uses exactly one connection, and a nonce presented on another connection is refused.
  • docs/WALLET.md says what a 401 means and why it is not the flow.

Evidence

Real monero-wallet-rpc v0.18.5.1 (--rpc-login owuser:owpass --offline --wallet-dir /tmp/..., wallet created and open), dialed at http://owuser:[email protected]:18999:

  • unfixed tree (HEAD): WalletStatus { connected: false, last_error: Some("wallet RPC HTTP HTTP/1.1 401 Unauthorized") } — the user's exact string
  • fixed tree: WalletStatus { connected: true, last_error: None, receive_address: "44vivHYS1E25H75tpoXnLGHep9FUeHS1ZiRrFuMneo8VHQPaKRHvGjo6nd8Qvo7hHVKBCXK2bipUWQNmbxYUFtmaCADMsqD" }, ping ok: true

Local: cargo clippy --locked --all-targets --target x86_64-unknown-linux-gnu -- -D warnings clean; cargo test --locked --target x86_64-unknown-linux-gnu all targets green (wallet 42 passed).

Not verified

Nothing was run on a device: the on-device Cuprate node's own "not verified" bounds in android/CUPRATE_DAEMON.md are unchanged. This fixes the wallet-RPC auth handshake only — an in-process Cuprate chain source that has not reached the tip still reports syncing, not ready.

The wallet RPC Digest handshake never worked against a real `monero-wallet-rpc`: the authenticated retry went out on a **new TCP connection**, and epee binds its Digest nonce to the connection that issued the challenge. Symptom (TUI and Android, unchanged by chain-source or creds): ``` wallet RPC HTTP HTTP/1.1 401 Unauthorized ``` ## Why `http_custom_handler` owns `http_server_auth` and is constructed per connection; its session starts with an empty nonce and counter 0, and any request without a valid `Authorization` field mints a fresh nonce (`contrib/epee/src/http_auth.cpp`, `http_protocol_handler.h:186,223`). A retry on a second socket therefore presents a nonce that socket never issued → `kStale` → 401 again, forever, however correct the login is. curl and monero's own client work because they retry on the same connection. `src/wallet.rs`'s `rpc_with` did the challenge through one `post_timeout` (fresh `TcpStream`, `Connection: close`) and the retry through a second — and `Connection: close` on the challenge made epee hang up right after the 401 anyway. ## Fix - `post_digest` / `http_digest`: one `TcpStream` for the challenge and the retry, no `Connection: close` on the challenge, `read_one_response` honouring `Content-Length` (a keep-alive response never ends in EOF; epee always sends the header, and with none the body ends at EOF, which keeps the 1 MiB cap meaningful for an open RPC), authenticated request on that same stream. - A 401 after our own challenge now reports the honest cause: the login does not match the child's `--rpc-login`. - Mocks stop being able to hide it: `tests/wallet.rs` gains `serve_one_conn`/`serve_rpc_conns` (per-connection nonce) and `src/node.rs`'s two wallet mocks use `serve_mock_rpc` on the same model. Two new tests pin the behaviour: one RPC call uses exactly one connection, and a nonce presented on another connection is refused. - `docs/WALLET.md` says what a 401 means and why it is not the flow. ## Evidence Real `monero-wallet-rpc` v0.18.5.1 (`--rpc-login owuser:owpass --offline --wallet-dir /tmp/...`, wallet created and open), dialed at `http://owuser:[email protected]:18999`: - unfixed tree (`HEAD`): `WalletStatus { connected: false, last_error: Some("wallet RPC HTTP HTTP/1.1 401 Unauthorized") }` — the user's exact string - fixed tree: `WalletStatus { connected: true, last_error: None, receive_address: "44vivHYS1E25H75tpoXnLGHep9FUeHS1ZiRrFuMneo8VHQPaKRHvGjo6nd8Qvo7hHVKBCXK2bipUWQNmbxYUFtmaCADMsqD" }`, `ping ok: true` Local: `cargo clippy --locked --all-targets --target x86_64-unknown-linux-gnu -- -D warnings` clean; `cargo test --locked --target x86_64-unknown-linux-gnu` all targets green (wallet 42 passed). ## Not verified Nothing was run on a device: the on-device Cuprate node's own "not verified" bounds in `android/CUPRATE_DAEMON.md` are unchanged. This fixes the wallet-RPC auth handshake only — an in-process Cuprate chain source that has not reached the tip still reports `syncing`, not `ready`.
wallet: answer the Digest challenge on the connection that issued it
All checks were successful
ci / test (pull_request) Successful in 5m37s
ci / sdk (pull_request) Successful in 1m21s
ci / fuzz (pull_request) Successful in 4m52s
android-ci / android (pull_request) Successful in 12m15s
cb411da813
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.
sirius merged commit 3769bd9a42 into main 2026-09-20 13:30:21 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!137
No description provided.