[verified] feat(wallet): require RPC digest login and cap reads
All checks were successful
ci / test (pull_request) Successful in 3m8s
All checks were successful
ci / test (pull_request) Successful in 3m8s
Refuse unauthenticated ONIONWIRE_WALLET_RPC, speak HTTP Digest like monero-wallet-rpc --rpc-login, cap responses at 1 MiB, and reject .onion RPC URLs instead of half-dialing them over TCP.
This commit is contained in:
parent
2b42864eba
commit
125aafb816
6 changed files with 351 additions and 46 deletions
250
src/wallet.rs
250
src/wallet.rs
|
|
@ -1,5 +1,6 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use rand::RngCore;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
|
|
@ -16,11 +17,25 @@ impl std::fmt::Display for Error {
|
|||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Clone)]
|
||||
struct Endpoint {
|
||||
host: String,
|
||||
port: u16,
|
||||
path: String,
|
||||
user: String,
|
||||
pass: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Endpoint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Endpoint")
|
||||
.field("host", &self.host)
|
||||
.field("port", &self.port)
|
||||
.field("path", &self.path)
|
||||
.field("user", &self.user)
|
||||
.field("pass", &"<redacted>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -44,20 +59,28 @@ impl Wallet {
|
|||
|
||||
pub fn from_env() -> Self {
|
||||
match std::env::var("ONIONWIRE_WALLET_RPC") {
|
||||
Ok(s) if !s.trim().is_empty() => match Self::from_url(s.trim()) {
|
||||
Ok(w) => w,
|
||||
Err(e) => {
|
||||
eprintln!("ONIONWIRE_WALLET_RPC: {e}");
|
||||
Self::disabled()
|
||||
Ok(s) if !s.trim().is_empty() => {
|
||||
let login = std::env::var("ONIONWIRE_WALLET_RPC_LOGIN")
|
||||
.ok()
|
||||
.map(|v| v.trim().to_string())
|
||||
.filter(|v| !v.is_empty());
|
||||
match parse_http_url(s.trim(), login.as_deref()) {
|
||||
Ok(endpoint) => Self {
|
||||
endpoint: Some(endpoint),
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("ONIONWIRE_WALLET_RPC: {e}");
|
||||
Self::disabled()
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Self::disabled(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_url(url: &str) -> Result<Self> {
|
||||
Ok(Self {
|
||||
endpoint: Some(parse_http_url(url)?),
|
||||
endpoint: Some(parse_http_url(url, None)?),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -126,15 +149,26 @@ impl Wallet {
|
|||
"params": params,
|
||||
})
|
||||
.to_string();
|
||||
let host_hdr = host_header(&ep.host, ep.port);
|
||||
let req = format!(
|
||||
"POST {} HTTP/1.1\r\nHost: {host_hdr}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
ep.path,
|
||||
body.len()
|
||||
);
|
||||
let raw = tokio::time::timeout(RPC_TIMEOUT, http_post(ep, req.as_bytes()))
|
||||
.await
|
||||
.map_err(|_| Error("wallet RPC timed out".into()))??;
|
||||
let raw = post_timeout(ep, &build_req(ep, &body, None)).await?;
|
||||
let status = http_status(&raw).unwrap_or(0);
|
||||
if status == 200 {
|
||||
return Err(Error(
|
||||
"wallet RPC requires digest auth (open RPC refused)".into(),
|
||||
));
|
||||
}
|
||||
if status != 401 {
|
||||
return parse_json_rpc(&raw);
|
||||
}
|
||||
let challenge = www_authenticate(&raw).ok_or_else(|| {
|
||||
Error("wallet RPC digest required (--rpc-login / HTTP Digest)".into())
|
||||
})?;
|
||||
if !challenge.trim().to_ascii_lowercase().starts_with("digest") {
|
||||
return Err(Error(
|
||||
"wallet RPC digest required (--rpc-login / HTTP Digest)".into(),
|
||||
));
|
||||
}
|
||||
let auth = digest_authorization(ep, &challenge)?;
|
||||
let raw = post_timeout(ep, &build_req(ep, &body, Some(&auth))).await?;
|
||||
parse_json_rpc(&raw)
|
||||
}
|
||||
}
|
||||
|
|
@ -148,13 +182,36 @@ pub fn transfers_match(rows: &[TransferRow], txid: &str, amount: &str, address:
|
|||
})
|
||||
}
|
||||
|
||||
fn parse_http_url(url: &str) -> Result<Endpoint> {
|
||||
fn credentials_required() -> Error {
|
||||
Error("wallet RPC requires credentials (user:pass in URL or ONIONWIRE_WALLET_RPC_LOGIN)".into())
|
||||
}
|
||||
|
||||
fn parse_login(login: &str) -> Result<(String, String)> {
|
||||
let (user, pass) = login.split_once(':').ok_or_else(credentials_required)?;
|
||||
if user.is_empty() || pass.is_empty() {
|
||||
return Err(credentials_required());
|
||||
}
|
||||
Ok((user.to_string(), pass.to_string()))
|
||||
}
|
||||
|
||||
fn parse_http_url(url: &str, extra_login: Option<&str>) -> Result<Endpoint> {
|
||||
let rest = url
|
||||
.strip_prefix("http://")
|
||||
.ok_or_else(|| Error("wallet RPC must be http:// (no TLS)".into()))?;
|
||||
if rest.contains("://") {
|
||||
return Err(Error("wallet RPC must be http:// (no TLS)".into()));
|
||||
}
|
||||
let (userinfo, rest) = match rest.rsplit_once('@') {
|
||||
Some((ui, hostpart)) => (Some(ui), hostpart),
|
||||
None => (None, rest),
|
||||
};
|
||||
let (user, pass) = match userinfo {
|
||||
Some(ui) => parse_login(ui)?,
|
||||
None => match extra_login {
|
||||
Some(login) => parse_login(login)?,
|
||||
None => return Err(credentials_required()),
|
||||
},
|
||||
};
|
||||
let (hostport, path) = match rest.split_once('/') {
|
||||
Some((hp, p)) => (hp, format!("/{p}")),
|
||||
None => (rest, "/json_rpc".into()),
|
||||
|
|
@ -165,10 +222,21 @@ fn parse_http_url(url: &str) -> Result<Endpoint> {
|
|||
path
|
||||
};
|
||||
let (host, port) = parse_hostport(hostport)?;
|
||||
if !allowed_host(&host) {
|
||||
return Err(Error("wallet RPC host must be loopback or .onion".into()));
|
||||
if host.trim().to_ascii_lowercase().ends_with(".onion") {
|
||||
return Err(Error(
|
||||
"wallet RPC over .onion is not supported (loopback only; no Arti dial)".into(),
|
||||
));
|
||||
}
|
||||
Ok(Endpoint { host, port, path })
|
||||
if !allowed_host(&host) {
|
||||
return Err(Error("wallet RPC host must be loopback".into()));
|
||||
}
|
||||
Ok(Endpoint {
|
||||
host,
|
||||
port,
|
||||
path,
|
||||
user,
|
||||
pass,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_hostport(hostport: &str) -> Result<(String, u16)> {
|
||||
|
|
@ -201,9 +269,6 @@ fn parse_port(p: &str) -> Result<u16> {
|
|||
|
||||
fn allowed_host(host: &str) -> bool {
|
||||
let h = host.trim();
|
||||
if h.to_ascii_lowercase().ends_with(".onion") {
|
||||
return true;
|
||||
}
|
||||
if h.eq_ignore_ascii_case("localhost") {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -220,6 +285,31 @@ fn host_header(host: &str, port: u16) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
const MAX_RPC_BYTES: usize = 1024 * 1024;
|
||||
|
||||
async fn post_timeout(ep: &Endpoint, req: &str) -> Result<Vec<u8>> {
|
||||
tokio::time::timeout(RPC_TIMEOUT, http_post(ep, req.as_bytes()))
|
||||
.await
|
||||
.map_err(|_| Error("wallet RPC timed out".into()))?
|
||||
}
|
||||
|
||||
fn build_req(ep: &Endpoint, body: &str, authorization: Option<&str>) -> String {
|
||||
let host_hdr = host_header(&ep.host, ep.port);
|
||||
let mut req = format!(
|
||||
"POST {} HTTP/1.1\r\nHost: {host_hdr}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n",
|
||||
ep.path,
|
||||
body.len()
|
||||
);
|
||||
if let Some(auth) = authorization {
|
||||
req.push_str("Authorization: ");
|
||||
req.push_str(auth);
|
||||
req.push_str("\r\n");
|
||||
}
|
||||
req.push_str("\r\n");
|
||||
req.push_str(body);
|
||||
req
|
||||
}
|
||||
|
||||
async fn http_post(ep: &Endpoint, req: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut stream = TcpStream::connect((ep.host.as_str(), ep.port))
|
||||
.await
|
||||
|
|
@ -229,13 +319,117 @@ async fn http_post(ep: &Endpoint, req: &[u8]) -> Result<Vec<u8>> {
|
|||
.await
|
||||
.map_err(|e| Error(format!("wallet write: {e}")))?;
|
||||
let mut buf = Vec::new();
|
||||
stream
|
||||
.read_to_end(&mut buf)
|
||||
.await
|
||||
.map_err(|e| Error(format!("wallet read: {e}")))?;
|
||||
let mut tmp = [0u8; 8192];
|
||||
loop {
|
||||
let n = stream
|
||||
.read(&mut tmp)
|
||||
.await
|
||||
.map_err(|e| Error(format!("wallet read: {e}")))?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
if buf.len().saturating_add(n) > MAX_RPC_BYTES {
|
||||
return Err(Error("wallet RPC response too large".into()));
|
||||
}
|
||||
buf.extend_from_slice(&tmp[..n]);
|
||||
}
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn http_status(raw: &[u8]) -> Option<u16> {
|
||||
let text = std::str::from_utf8(raw).ok()?;
|
||||
let line = text.lines().next()?;
|
||||
let mut parts = line.split_whitespace();
|
||||
let _http = parts.next()?;
|
||||
parts.next()?.parse().ok()
|
||||
}
|
||||
|
||||
fn www_authenticate(raw: &[u8]) -> Option<String> {
|
||||
let text = std::str::from_utf8(raw).ok()?;
|
||||
let (head, _) = text
|
||||
.split_once("\r\n\r\n")
|
||||
.or_else(|| text.split_once("\n\n"))?;
|
||||
for line in head.lines().skip(1) {
|
||||
let (k, v) = match line.split_once(':') {
|
||||
Some(kv) => kv,
|
||||
None => continue,
|
||||
};
|
||||
if k.eq_ignore_ascii_case("www-authenticate") {
|
||||
return Some(v.trim().to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn digest_param(challenge: &str, key: &str) -> Option<String> {
|
||||
let t = challenge.trim();
|
||||
let rest = if t.len() >= 6 && t[..6].eq_ignore_ascii_case("digest") {
|
||||
t[6..].trim()
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
for part in rest.split(',') {
|
||||
let part = part.trim();
|
||||
let (k, v) = match part.split_once('=') {
|
||||
Some(kv) => kv,
|
||||
None => continue,
|
||||
};
|
||||
if k.eq_ignore_ascii_case(key) {
|
||||
return Some(v.trim().trim_matches('"').to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn md5_hex(s: &str) -> String {
|
||||
use md5::{Digest, Md5};
|
||||
hex_lower(&Md5::digest(s.as_bytes()))
|
||||
}
|
||||
|
||||
fn hex_lower(bytes: &[u8]) -> String {
|
||||
const H: &[u8; 16] = b"0123456789abcdef";
|
||||
let mut out = String::with_capacity(bytes.len() * 2);
|
||||
for &b in bytes {
|
||||
out.push(H[(b >> 4) as usize] as char);
|
||||
out.push(H[(b & 0xf) as usize] as char);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn digest_authorization(ep: &Endpoint, challenge: &str) -> Result<String> {
|
||||
if let Some(alg) = digest_param(challenge, "algorithm")
|
||||
&& !alg.eq_ignore_ascii_case("MD5")
|
||||
{
|
||||
return Err(Error("wallet RPC digest algorithm not MD5".into()));
|
||||
}
|
||||
let realm = digest_param(challenge, "realm").unwrap_or_default();
|
||||
let nonce = digest_param(challenge, "nonce")
|
||||
.ok_or_else(|| Error("wallet RPC digest required (--rpc-login / HTTP Digest)".into()))?;
|
||||
let qop = digest_param(challenge, "qop");
|
||||
let ha1 = md5_hex(&format!("{}:{realm}:{}", ep.user, ep.pass));
|
||||
let ha2 = md5_hex(&format!("POST:{}", ep.path));
|
||||
let (qop_part, resp) = if qop
|
||||
.as_deref()
|
||||
.is_some_and(|q| q.split(',').any(|x| x.trim() == "auth"))
|
||||
{
|
||||
let mut cnonce_bytes = [0u8; 8];
|
||||
rand::thread_rng().fill_bytes(&mut cnonce_bytes);
|
||||
let cnonce = hex_lower(&cnonce_bytes);
|
||||
let nc = "00000001";
|
||||
let response = md5_hex(&format!("{ha1}:{nonce}:{nc}:{cnonce}:auth:{ha2}"));
|
||||
(
|
||||
format!(", qop=auth, nc={nc}, cnonce=\"{cnonce}\""),
|
||||
response,
|
||||
)
|
||||
} else {
|
||||
(String::new(), md5_hex(&format!("{ha1}:{nonce}:{ha2}")))
|
||||
};
|
||||
Ok(format!(
|
||||
"Digest username=\"{}\", realm=\"{realm}\", nonce=\"{nonce}\", uri=\"{}\", algorithm=MD5, response=\"{resp}\"{qop_part}",
|
||||
ep.user, ep.path
|
||||
))
|
||||
}
|
||||
|
||||
fn parse_json_rpc(raw: &[u8]) -> Result<serde_json::Value> {
|
||||
let text = std::str::from_utf8(raw).map_err(|_| Error("wallet RPC not UTF-8".into()))?;
|
||||
let (head, body) = text
|
||||
|
|
|
|||
Loading…
Reference in a new issue