fix(import): strip Unicode whitespace from pasted keys before import/sweep
Users (notably in the Chinese community) hit "Invalid spending key" when
importing/sweeping an otherwise-valid shielded spending key. Root cause: the
Import Key dialog classified the TRIMMED key (so the UI showed it as valid) but
handed the RAW import_key_input_ buffer to z_importkey/sweep — and the trim was
ASCII-only (' ' \t \n \r). CJK clipboards commonly inject whitespace that an
ASCII trim leaves behind — above all a trailing U+3000 ideographic space, plus
NBSP / zero-width / BOM — so a trailing such character passed the prefix
classifier (UI "valid") but reached the daemon's DecodeSpendingKey() verbatim,
which failed the Bech32 decode → "Invalid spending key". Native Ctrl+V also
bypassed the paste-button trim entirely, so even plain ASCII whitespace reached
the RPC. This is why it hit CJK users systematically and others rarely.
Add util::stripAllWhitespaceForKey (UTF-8 aware; removes every whitespace code
point, ASCII + Unicode, including internal ones — a key is a single token). The
dialog now classifies AND sends this cleaned key (keyTrim), the key Paste button
cleans the field with it, and trimmedClipboardText (addresses etc.) now
edge-trims Unicode whitespace too. Covers spending, viewing, and WIF imports and
the sweep path. Present in v2.0.2 and current dev (same gate).
Adds testKeyInputSanitization (trailing U+3000 / NBSP / BOM / zero-width /
internal newline → clean key; and the end-to-end classify-after-clean case).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3325,6 +3325,32 @@ void testInstalledDaemonInfoPosix()
|
||||
}
|
||||
#endif
|
||||
|
||||
// Regression: a pasted spending key carrying whitespace (notably the full-width/zero-width characters CJK
|
||||
// clipboards inject) must be fully cleaned before classification AND before it reaches z_importkey — else
|
||||
// the UI shows "valid" but the daemon rejects the raw key as "Invalid spending key". (Chinese-community
|
||||
// import bug, V2.0.2.)
|
||||
void testKeyInputSanitization()
|
||||
{
|
||||
using dragonx::util::stripAllWhitespaceForKey;
|
||||
using dragonx::services::WalletSecurityController;
|
||||
// DragonX mainnet Sapling spending key HRP is "secret-extended-key-main"; the import gate prefix-matches
|
||||
// it (the key exceeds the Bech32 length cap), so a well-formed-looking stand-in exercises the path.
|
||||
const std::string key = "secret-extended-key-main1qabcdefghijklmnop";
|
||||
|
||||
EXPECT_EQ(stripAllWhitespaceForKey(" " + key + " \n\r\t"), key); // ASCII edge whitespace
|
||||
EXPECT_EQ(stripAllWhitespaceForKey(key + "\xE3\x80\x80"), key); // trailing U+3000 (the reported CJK case)
|
||||
EXPECT_EQ(stripAllWhitespaceForKey("\xC2\xA0" + key), key); // leading U+00A0 NBSP
|
||||
EXPECT_EQ(stripAllWhitespaceForKey(key + "\xEF\xBB\xBF"), key); // trailing U+FEFF BOM/ZWNBSP
|
||||
EXPECT_EQ(stripAllWhitespaceForKey(key + "\xE2\x80\x8B"), key); // trailing U+200B zero-width space
|
||||
// Internal whitespace from a line-wrapped paste is removed too (keys are single tokens).
|
||||
EXPECT_EQ(stripAllWhitespaceForKey("secret-extended-key-main1qabc\ndefghijklmnop"), key);
|
||||
EXPECT_EQ(stripAllWhitespaceForKey(key), key); // clean key unchanged
|
||||
|
||||
// End-to-end: a trailing full-width space classified as valid only AFTER sanitizing — this is exactly
|
||||
// what the raw-buffer path got wrong (UI said valid, z_importkey saw the stray byte and refused).
|
||||
EXPECT_TRUE(WalletSecurityController::isRecognizedPrivateKey(stripAllWhitespaceForKey(key + "\xE3\x80\x80")));
|
||||
}
|
||||
|
||||
void testWalletSecurityController()
|
||||
{
|
||||
using dragonx::services::WalletSecurityController;
|
||||
@@ -20374,6 +20400,7 @@ int main()
|
||||
testSecureVaultWellFormed();
|
||||
testInstalledDaemonInfoPosix();
|
||||
#endif
|
||||
testKeyInputSanitization();
|
||||
testWalletSecurityController();
|
||||
testWalletSecurityWorkflow();
|
||||
testWalletSecurityWorkflowExecutor();
|
||||
|
||||
Reference in New Issue
Block a user