fix(input): strip Unicode/CJK whitespace across address, key, and URL inputs

Follow-up to the key-import fix: an audit found the same ASCII-only-trim pattern
across the input layer, where a pasted value carrying CJK full-width (U+3000),
NBSP, zero-width, or BOM whitespace survived trimming and then failed validation
or an RPC. Most paths failed closed (same value validated and acted on → "won't
work" rather than a wrong send), but several mattered — above all the Lite
Import Key path, the exact recurrence of the reported bug on the Lite variant.

Single-token inputs (keys, addresses, URLs) now go through
util::stripAllWhitespaceForKey; path/label inputs use the new
util::trimUnicodeWhitespace (edge-trim, preserves internal spaces):

- Lite Import Key (settings_page) — strip-all before importKeyAsync. [fund recovery]
- Lite wallet path (open/restore) — edge-trim the request path AND the empty gate
  (they had diverged: gate trimmed, request used raw).
- Contacts — gate on and save the cleaned label/address; clean on paste; an
  address never persists with stray whitespace (which silently broke every later
  use + defeated duplicate detection).
- Send recipient — strip-all on paste and on field edit, so a CJK-pasted valid
  address validates and sends.
- Chat new-conversation z-address — validate and send the cleaned value.
- Chat restore-by-address — strip-all (the old trim even missed '\r'), so a
  padded address matches the deleted list instead of broadcasting a junk memo.
- Mining pool URL + payout — strip-all at persist and in the payout check.
- Validate-address dialog — strip-all before classify.
- Lite custom server URL — strip-all; its label edge-trimmed.
- Payment URI — strip-all the parsed address.
- Defense-in-depth: importPrivateKey / sweepPrivateKey strip-all the key;
  importWalletFile edge-trims the path.

Adds trimUnicodeWhitespace coverage to testKeyInputSanitization. Build + tests +
hygiene green. (Deferred, noted for follow-up: the console tokenizer treating
Unicode whitespace as a separator, and repairing already-polluted address-book
entries on load.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 12:11:54 -06:00
co-authored by Claude Opus 4.8
parent 0a957e9555
commit 6bb72f9878
16 changed files with 411 additions and 69 deletions
+11 -12
View File
@@ -71,6 +71,7 @@
#include "util/perf_log.h"
#include "util/i18n.h"
#include "util/address_validation.h" // isValidRecipientAddress — payout validation at every start path (M-01)
#include "util/text_format.h" // stripAllWhitespaceForKey / trimUnicodeWhitespace (input sanitization)
#include "util/secure_vault.h"
#include <nlohmann/json.hpp>
@@ -5810,11 +5811,10 @@ void App::importPrivateKey(const std::string& rawKey, int startHeight,
return;
}
// Trim whitespace/newlines — manual entry doesn't go through the dialog's Paste trimmer, and a
// stray character makes the daemon reject the key with a cryptic error.
std::string key(rawKey);
while (!key.empty() && (key.front()==' '||key.front()=='\t'||key.front()=='\n'||key.front()=='\r')) key.erase(key.begin());
while (!key.empty() && (key.back()==' '||key.back()=='\t'||key.back()=='\n'||key.back()=='\r')) key.pop_back();
// A key is a single token — strip ALL whitespace (ASCII + Unicode). Defense-in-depth: the import dialog
// already passes a cleaned key, but any other caller (manual / console / future) is covered here too,
// including the CJK full-width / zero-width characters an ASCII trim missed.
std::string key = dragonx::util::stripAllWhitespaceForKey(rawKey);
// Reject anything that isn't a recognized Z/T private key or shielded viewing key before handing
// it to the daemon (the dialog's indicator and this guard share isRecognizedImportKey).
@@ -5919,9 +5919,9 @@ void App::importWalletFile(const std::string& path, std::function<void(bool, con
if (callback) callback(false, TR("import_key_need_node"));
return;
}
std::string p(path);
while (!p.empty() && (p.front()==' '||p.front()=='\t'||p.front()=='\n'||p.front()=='\r')) p.erase(p.begin());
while (!p.empty() && (p.back()==' '||p.back()=='\t'||p.back()=='\n'||p.back()=='\r')) p.pop_back();
// Edge-trim ASCII + Unicode whitespace: a file path may contain legitimate internal spaces, so don't
// strip those — only the stray leading/trailing (incl. CJK/Unicode) characters a paste can carry.
std::string p = dragonx::util::trimUnicodeWhitespace(path);
if (p.empty()) { if (callback) callback(false, TR("import_wallet_file_need_path")); return; }
{
std::string why; const char* trKey = nullptr;
@@ -5964,10 +5964,9 @@ void App::sweepPrivateKey(const std::string& rawKey, int startHeight, int destMo
sweep_step_ = SweepStep::Error;
return;
}
// Trim (manual entry doesn't go through the dialog's Paste trimmer).
std::string key(rawKey);
while (!key.empty() && (key.front()==' '||key.front()=='\t'||key.front()=='\n'||key.front()=='\r')) key.erase(key.begin());
while (!key.empty() && (key.back()==' '||key.back()=='\t'||key.back()=='\n'||key.back()=='\r')) key.pop_back();
// A key is a single token — strip ALL whitespace (ASCII + Unicode); defense-in-depth, the dialog already
// passes a cleaned key.
std::string key = dragonx::util::stripAllWhitespaceForKey(rawKey);
// Sweeping requires a SPENDING key (transparent WIF or shielded z-spending key) — a viewing key
// can't sign, and this must never accidentally route to z_importviewingkey.