feat(rpc): scrub the raw response body for secret-bearing calls (B7)

The secret exports (z_exportmnemonic / z_exportkey / dumpprivkey) already scrub
the parsed value at the call site, but the raw HTTP response string those RPCs
build — the curl write buffer, which holds the same secret in the clear — was
freed without zeroing. That's the "fuller fix belongs in the RPC layer" the
identity-fetch comment flagged.

Add RPCClient::callSecret(), a call() variant that sodium_memzeros the raw
response body after parsing (on success and on throw). NRVO makes the returned
string the very buffer curl wrote into, so one wipe covers it. Route every
secret-bearing export through it: chat identity (mnemonic + z_exportkey
fallback), Settings seed-phrase + single-key export, Export-all-keys, and the
migrate-to-seed isolated-node mnemonic export. Purely additive — the parsed
result is byte-identical, so no behavior change (safe for the fund-critical
migrate path).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-16 16:45:41 -05:00
parent bce69362eb
commit fecc9015fb
5 changed files with 39 additions and 7 deletions

View File

@@ -2632,7 +2632,7 @@ void App::exportPrivateKey(const std::string& address, std::function<void(const
std::string err; std::string err;
try { try {
rpc::RPCClient::TraceScope trace("Settings / Export private key"); rpc::RPCClient::TraceScope trace("Settings / Export private key");
key = rpc_->call(method, {address}).get<std::string>(); key = rpc_->callSecret(method, {address}).get<std::string>(); // zero the raw body too (B7)
} catch (const std::exception& e) { } catch (const std::exception& e) {
err = e.what(); err = e.what();
} }
@@ -2758,7 +2758,7 @@ void App::maybeProvisionChatIdentity()
bool unavailable = false; // no usable secret — stop retrying this session bool unavailable = false; // no usable secret — stop retrying this session
rpc::RPCClient::TraceScope trace("HushChat / identity"); rpc::RPCClient::TraceScope trace("HushChat / identity");
try { try {
auto response = rpc_->call("z_exportmnemonic"); auto response = rpc_->callSecret("z_exportmnemonic"); // zero the raw body too (B7)
if (response.contains("mnemonic") && response["mnemonic"].is_string()) { if (response.contains("mnemonic") && response["mnemonic"].is_string()) {
// Scrub the json's own copy of the seed after taking ours (the rest of the RPC // Scrub the json's own copy of the seed after taking ours (the rest of the RPC
// response chain is unmanaged — a fuller fix belongs in the RPC layer). // response chain is unmanaged — a fuller fix belongs in the RPC layer).
@@ -2774,7 +2774,7 @@ void App::maybeProvisionChatIdentity()
try { try {
// Mirror the mnemonic path: take our copy, then scrub the json's own copy of the // Mirror the mnemonic path: take our copy, then scrub the json's own copy of the
// spending key so it isn't left in freed heap (B4). z_exportkey returns a bare string. // spending key so it isn't left in freed heap (B4). z_exportkey returns a bare string.
auto keyResp = rpc_->call("z_exportkey", {fallbackZaddr}); auto keyResp = rpc_->callSecret("z_exportkey", {fallbackZaddr}); // zero the raw body too (B7)
if (keyResp.is_string()) { if (keyResp.is_string()) {
auto& key = keyResp.get_ref<std::string&>(); auto& key = keyResp.get_ref<std::string&>();
secret = key; secret = key;
@@ -3374,7 +3374,7 @@ void App::exportSeedPhrase(std::function<void(bool, bool, const std::string&, co
bool noMnemonic = false; bool noMnemonic = false;
rpc::RPCClient::TraceScope trace("Settings / Export seed phrase"); rpc::RPCClient::TraceScope trace("Settings / Export seed phrase");
try { try {
auto response = rpc_->call("z_exportmnemonic"); auto response = rpc_->callSecret("z_exportmnemonic"); // zero the raw body too (B7)
if (response.contains("mnemonic") && response["mnemonic"].is_string()) { if (response.contains("mnemonic") && response["mnemonic"].is_string()) {
auto& m = response["mnemonic"].get_ref<std::string&>(); auto& m = response["mnemonic"].get_ref<std::string&>();
phrase = m; phrase = m;

View File

@@ -110,7 +110,7 @@ SeedWalletResult SeedWalletCreator::create(bool keepDatadir,
// 6. Export the new seed phrase + a fresh shielded receive address (the future sweep target). // 6. Export the new seed phrase + a fresh shielded receive address (the future sweep target).
try { try {
auto m = cli.call("z_exportmnemonic"); auto m = cli.callSecret("z_exportmnemonic"); // zero the raw body too (B7)
if (m.contains("mnemonic") && m["mnemonic"].is_string()) if (m.contains("mnemonic") && m["mnemonic"].is_string())
r.seedPhrase = m["mnemonic"].get<std::string>(); r.seedPhrase = m["mnemonic"].get<std::string>();
r.destAddress = cli.call("z_getnewaddress").get<std::string>(); r.destAddress = cli.call("z_getnewaddress").get<std::string>();

View File

@@ -370,6 +370,27 @@ json RPCClient::call(const std::string& method, const json& params)
return parseRpcResult(http_code, response_data); return parseRpcResult(http_code, response_data);
} }
json RPCClient::callSecret(const std::string& method, const json& params)
{
std::lock_guard<std::recursive_mutex> lk(curl_mutex_);
if (!impl_->curl) {
throw std::runtime_error("Not connected");
}
// Zero the raw response body whether parsing succeeds or throws — it holds the secret in the
// clear (the curl write buffer performCall returns), and would otherwise be freed un-wiped (B7).
long http_code = 0;
std::string response_data = performCall(method, params, http_code);
try {
json result = parseRpcResult(http_code, response_data);
if (!response_data.empty()) sodium_memzero(&response_data[0], response_data.size());
return result;
} catch (...) {
if (!response_data.empty()) sodium_memzero(&response_data[0], response_data.size());
throw;
}
}
json RPCClient::call(const std::string& method, const json& params, long timeoutSec) json RPCClient::call(const std::string& method, const json& params, long timeoutSec)
{ {
std::lock_guard<std::recursive_mutex> lk(curl_mutex_); std::lock_guard<std::recursive_mutex> lk(curl_mutex_);

View File

@@ -133,6 +133,17 @@ public:
*/ */
json call(const std::string& method, const json& params = json::array()); json call(const std::string& method, const json& params = json::array());
/**
* @brief Like call(), but zeroes the raw HTTP response body after parsing (B7).
*
* For RPCs whose response carries a secret (z_exportmnemonic / z_exportkey), the parsed
* value is scrubbed by the caller — but the raw response string this method builds also
* holds that secret and is otherwise freed without zeroing. Use this variant so the largest,
* longest-lived plaintext copy doesn't linger in freed heap. Slightly slower (an extra wipe);
* only worth it for secret-bearing calls.
*/
json callSecret(const std::string& method, const json& params = json::array());
/** /**
* @brief Make a raw RPC call with a custom timeout * @brief Make a raw RPC call with a custom timeout
* @param method RPC method name * @param method RPC method name

View File

@@ -180,7 +180,7 @@ void ExportAllKeysDialog::render(App* app)
for (const auto& addr : z_addrs) { for (const auto& addr : z_addrs) {
try { try {
rpc::RPCClient::TraceScope trace("Settings / Export all keys"); rpc::RPCClient::TraceScope trace("Settings / Export all keys");
auto result = rpc->call("z_exportkey", {addr}); auto result = rpc->callSecret("z_exportkey", {addr}); // zero the raw body too (B7)
if (result.is_string()) { if (result.is_string()) {
keys += "# Address: " + addr + "\n"; keys += "# Address: " + addr + "\n";
keys += result.get<std::string>() + "\n\n"; keys += result.get<std::string>() + "\n\n";
@@ -196,7 +196,7 @@ void ExportAllKeysDialog::render(App* app)
for (const auto& addr : t_addrs) { for (const auto& addr : t_addrs) {
try { try {
rpc::RPCClient::TraceScope trace("Settings / Export all keys"); rpc::RPCClient::TraceScope trace("Settings / Export all keys");
auto result = rpc->call("dumpprivkey", {addr}); auto result = rpc->callSecret("dumpprivkey", {addr}); // zero the raw body too (B7)
if (result.is_string()) { if (result.is_string()) {
keys += "# Address: " + addr + "\n"; keys += "# Address: " + addr + "\n";
keys += result.get<std::string>() + "\n\n"; keys += result.get<std::string>() + "\n\n";