From 79ecc100d3d7c41b87908a83c54d2b1b8c581fc4 Mon Sep 17 00:00:00 2001 From: noah Date: Tue, 22 Sep 2026 19:18:27 -0600 Subject: [PATCH] review wave 3: log-fetch credential leak + JSON escaping + column-scan cost Second review round (over the wave 1/2 fixes) surfaced further, mostly new-path findings; this clears the substantive ones: - HIGH log_scan.rs passed miner admin credentials on the curl command line (same class as the access-probe leak fixed in wave 1). Its curl() now takes optional stdin: the Antminer digest login goes through a curl config on stdin (-K -), the VNish unlock password through the POST body on stdin (-d @-), and the timeout path reaps the child so it no longer leaks a zombie. - MEDIUM JSON injection/breakage: the VNish unlock body and the LuxOS uninstall command spliced a password / miner-supplied SessionID into JSON with format!. Both now build the JSON with serde_json, which escapes correctly. - MEDIUM column scan cost: fill_rows computed table_columns() twice (my wave-2 built_columns check added one), and with auto-hide-empty on that scans every cell of every row each time. It is now computed once and reused. Accepted, documented tradeoffs from this round: miner_probe's setpools takes the pool password as a CLI arg (inherent to a diagnostic CLI; it is the low-sensitivity mining worker password), MinerRow::upsert_into's linear scan is fine at the tool's fleet scale (hundreds), and bulk config-action writes stay serial deliberately. 96 tests + clippy-clean + selftest green. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Djv3oncM4LxTS5tb61Ako6 --- crates/alkane-gui/src/log_scan.rs | 104 +++++++++++++++++++----------- crates/alkane-gui/src/restore.rs | 9 ++- crates/alkane-gui/src/ui.rs | 7 +- 3 files changed, 79 insertions(+), 41 deletions(-) diff --git a/crates/alkane-gui/src/log_scan.rs b/crates/alkane-gui/src/log_scan.rs index b5ff859f..ce2520bd 100644 --- a/crates/alkane-gui/src/log_scan.rs +++ b/crates/alkane-gui/src/log_scan.rs @@ -122,35 +122,45 @@ fn summarize_log(log: &str) -> Vec { /// VNish: unlock -> bearer -> the full miner log. fn fetch_vnish_log(ip: &str, pass: &str) -> Result { - let unlock = curl(&[ - "-s", - "--connect-timeout", - "5", - "--max-time", - "9", - "-X", - "POST", - "-H", - "Content-Type: application/json", - "-d", - &format!("{{\"pw\":\"{pass}\"}}"), - &format!("http://{ip}/api/v1/unlock"), - ])?; + // The password goes in the POST body read from stdin (-d @-), never on argv, + // and is JSON-encoded by serde so it is both un-leakable and correctly escaped + // (round 18 #12). + let body = serde_json::json!({ "pw": pass }).to_string(); + let unlock = curl( + &[ + "-s", + "--connect-timeout", + "5", + "--max-time", + "9", + "-X", + "POST", + "-H", + "Content-Type: application/json", + "-d", + "@-", + &format!("http://{ip}/api/v1/unlock"), + ], + Some(body.as_bytes()), + )?; let token = serde_json::from_str::(&unlock) .ok() .and_then(|v| v.get("token").and_then(|t| t.as_str()).map(str::to_string)) .filter(|t| !t.is_empty()) .ok_or_else(|| "VNish unlock rejected (check the VNish password in scan credentials)".to_string())?; - let log = curl(&[ - "-s", - "--connect-timeout", - "5", - "--max-time", - "12", - "-H", - &format!("Authorization: Bearer {token}"), - &format!("http://{ip}/api/v1/logs/miner"), - ])?; + let log = curl( + &[ + "-s", + "--connect-timeout", + "5", + "--max-time", + "12", + "-H", + &format!("Authorization: Bearer {token}"), + &format!("http://{ip}/api/v1/logs/miner"), + ], + None, + )?; if log.trim().is_empty() { return Err("VNish returned an empty log".to_string()); } @@ -159,17 +169,23 @@ fn fetch_vnish_log(ip: &str, pass: &str) -> Result { /// Stock Antminer web log (digest auth). Absent on older models (404). fn fetch_antminer_log(ip: &str, user: &str, pass: &str) -> Result { - let body = curl(&[ - "-s", - "--digest", - "-u", - &format!("{user}:{pass}"), - "--connect-timeout", - "5", - "--max-time", - "12", - &format!("http://{ip}/cgi-bin/log.cgi"), - ])?; + // Credentials go through a curl config on stdin (-K -), never on argv, so they + // can't be read from the process list (round 18 #12). + let config = format!("user = \"{}\"\n", curl_config_quote(&format!("{user}:{pass}"))); + let body = curl( + &[ + "-s", + "--digest", + "--connect-timeout", + "5", + "--max-time", + "12", + "-K", + "-", + &format!("http://{ip}/cgi-bin/log.cgi"), + ], + Some(config.as_bytes()), + )?; if body.contains("404 - Not Found") || body.trim().is_empty() { return Err("this firmware does not expose a log over its web API".to_string()); } @@ -186,13 +202,21 @@ fn fetch_whatsminer_errors(ip: &str) -> Result { /// Run curl and return stdout, with a hard wall-clock cap. stdout is drained on a /// separate thread so a large log (hundreds of KB) never deadlocks on a full pipe /// -- the reason a naive "wait then read" times out on the VNish log. -fn curl(args: &[&str]) -> Result { - use std::io::Read; +fn curl(args: &[&str], stdin_data: Option<&[u8]>) -> Result { + use std::io::{Read, Write}; let mut cmd = Command::new("curl"); cmd.args(args); cmd.stdout(std::process::Stdio::piped()); cmd.stderr(std::process::Stdio::null()); + if stdin_data.is_some() { + cmd.stdin(std::process::Stdio::piped()); + } let mut child = cmd.spawn().map_err(|e| format!("spawn curl: {e}"))?; + if let Some(data) = stdin_data { + if let Some(mut si) = child.stdin.take() { + let _ = si.write_all(data); + } // dropping `si` closes the pipe so curl stops reading the config/body + } let mut stdout = child.stdout.take().ok_or("no curl stdout")?; let reader = std::thread::spawn(move || { let mut buf = Vec::new(); @@ -208,6 +232,7 @@ fn curl(args: &[&str]) -> Result { None => { if start.elapsed() >= timeout { let _ = child.kill(); + let _ = child.wait(); // reap so we never leave a zombie (round 18 #12) timed_out = true; break; } @@ -222,6 +247,11 @@ fn curl(args: &[&str]) -> Result { Ok(String::from_utf8_lossy(&buf).into_owned()) } +/// Escape a value for a curl `--config` file (double-quoted string). +fn curl_config_quote(s: &str) -> String { + s.replace('\\', "\\\\").replace('"', "\\\"") +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/alkane-gui/src/restore.rs b/crates/alkane-gui/src/restore.rs index 298a71ce..ac89ed48 100644 --- a/crates/alkane-gui/src/restore.rs +++ b/crates/alkane-gui/src/restore.rs @@ -110,10 +110,13 @@ fn restore_one(t: &RestoreTarget, vnish_pass: &str, ssh_user: &str, ssh_pass: &s /// VNish: unlock -> bearer -> firmware/remove (self-restores + reboots ~3 s). fn restore_vnish(ip: &str, pass: &str) -> (bool, String) { + // serde-encode the body so a password with a quote/backslash can't break or + // inject into the JSON (round 18 #12). + let body = serde_json::json!({ "pw": pass }).to_string(); let unlock = match curl(&[ "-s", "--connect-timeout", "5", "--max-time", "9", "-X", "POST", "-H", "Content-Type: application/json", - "-d", &format!("{{\"pw\":\"{pass}\"}}"), + "-d", &body, &format!("http://{ip}/api/v1/unlock"), ]) { Ok(s) => s, @@ -155,7 +158,9 @@ fn restore_luxos(ip: &str) -> (bool, String) { let Some(session) = session.filter(|s| !s.is_empty()) else { return (false, format!("no SessionID in logon response: {}", logon.trim())); }; - let cmd = format!("{{\"command\":\"uninstallluxos\",\"parameter\":\"{session}\"}}"); + // serde-encode so the miner-supplied SessionID can't break or inject into the + // JSON-RPC command (round 18 #12). + let cmd = serde_json::json!({ "command": "uninstallluxos", "parameter": session }).to_string(); match lux_rpc(ip, &cmd) { // Code 350 "Uninstalling LuxOS"; the miner reboots + reverts in background. Ok(resp) if resp.contains("350") || resp.to_lowercase().contains("uninstall") => { diff --git a/crates/alkane-gui/src/ui.rs b/crates/alkane-gui/src/ui.rs index d9f143ad..2b304638 100644 --- a/crates/alkane-gui/src/ui.rs +++ b/crates/alkane-gui/src/ui.rs @@ -1229,12 +1229,16 @@ impl App { pub fn fill_rows(&mut self) { let Some(t) = self.table else { return }; + // Compute the shown columns once and reuse it everywhere below (with + // auto-hide-empty on, table_columns() scans every cell of every row, so it + // must not be called per section of this function -- round 18 #12). + let cols = self.table_columns(); // If the set of shown columns has changed since the widget was built (only // possible with auto-hide-empty on, when a scan makes a column non-empty), // the widget's columns are stale: rendering now would misalign cells and // header clicks. Rebuild instead -- deferred, because rebuild_table // destroys this control (round 18 #12). - if self.table_columns() != self.built_columns { + if cols != self.built_columns { wx_rs::call_after(DEFER_TABLE); return; } @@ -1243,7 +1247,6 @@ impl App { let top = t.dvlc_get_top_row(); t.dvlc_clear(); self.view_idx = self.view.view(&self.rows); - let cols = self.table_columns(); for &ri in &self.view_idx { let r = &self.rows[ri]; let cells: Vec = cols.iter().map(|c| c.cell(r)).collect();