Repository navigation
fix(security): wipe private keys parsed on the add-key, import and new-wallet screens - #1151
Conversation
The Add Key screen, the private-key import, the single-key wallet parsers and the new-wallet and import-wallet seeds kept private keys and seeds in plain arrays, Vecs and Strings that were freed without being erased. A shared model::key_input::decode_hex_secret decodes hex straight into a Zeroizing buffer of the final size. It accepts and rejects exactly what hex::decode does, with the same error, so every caller keeps its messages. Parsed keys, the imported WIF text and the BIP39 seeds are held in Zeroizing. The user-key list on identity load reserves its capacity up front, so growth does not leave key copies in freed heap. New tests pin the unchanged behaviour: decode_hex_secret against hex::decode, the import screen's hex rejections, SingleKeyWallet::from_hex messages, and the Key Info hex-or-WIF split by its banners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
⛔ Final review complete — 1 blocking finding(s) (commit f87a006) |
With no password, ClosedSingleKey.encrypted_private_key holds the raw private key, but only the open key was wiped on drop. The import preview rebuilds its SingleKeyWallet on every edit, so each replaced preview freed a plain copy of the key. ClosedSingleKey now implements Zeroize, wiping encrypted_private_key, and runs it on drop. The stored format is unchanged; the hash, salt and nonce are not secret and are kept. Also from review: a test for the 32-byte hex import success path, and a corrected comment on the 52-character Key Info case, which does not prove that hex is tried before WIF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q
|
Waiting for bot review — coderabbitai not yet · thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
thepastaclaw
left a comment
There was a problem hiding this comment.
Final validation — Phase 2 only (no Phase 1 for this repository)
The implementation changes are otherwise consistent with the stated zeroization goal, and the available CI checks passed. However, the PR adds deterministic 32-byte private-key material to repository tests, violating the repository's explicit prohibition on committing private keys outside the approved historical fixture; this must be removed before merge.
🔴 1 blocking
Review provenance
Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)
- Triage:
criticalbygpt-6.1-sol(effort low) — The diff is intricate and directly changes private-key and wallet-seed handling in functions such as decode_hex_secret, SingleKeyWallet::from_wif/from_hex, and identity loading, which are critical key-handling surfaces. - Phase 1 reviewers: not run (disabled for this repository)
- Fresh verifier:
gpt-6.1-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.1-sol— general (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— rust-quality (completed, effort xhigh); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.
In `src/ui/wallets/import_mnemonic_screen.rs`:
- [BLOCKING] src/ui/wallets/import_mnemonic_screen.rs:785-789: Do not commit hard-coded private-key material in tests
This test embeds `[0x2B; 32]` and verifies that it is accepted by `PrivateKey::from_byte_array`, making the bytes an explicitly valid private key committed to the repository. The same pattern appears in `src/model/wallet/single_key.rs:510` as `[0x2A; 32]` in the zeroization test. The repository policy prohibits plaintext private keys in tests and documentation except for the specifically approved historical migration fixture. Generate valid key bytes at runtime using the existing randomness facilities, or use a non-secret test helper, so no deterministic private-key material is stored in git.
Out-of-scope follow-up suggestions (1)
These are valid observations, but they are outside this PR's scope and should be handled in separate issues or author/maintainer-requested PRs rather than blocking this review.
- Upstream WIF decoding still retains additional transient copies — The pinned rust-dashcore implementation decodes WIF data into a plain buffer in
PrivateKey::from_wif, so this local PR cannot guarantee complete wiping of every upstream transient on the WIF path. The issue is tracked upstream as dashpay/rust-dashcore#1225 and should be addressed there before updating the dependency.- Follow-up: Track dashpay/rust-dashcore#1225 through its fix and update the pinned dependency afterward.
False positive - this is a placeholder, not a key.
|
Waiting for bot review — coderabbitai skipped after its own rate limit · thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
/skip-bots |
|
Policy satisfied — this can merge. |
TL;DR
The remaining places that parse a private key or derive a wallet seed now hold them in
Zeroizing, so they are wiped when dropped instead of being left in freed memory. This covers:SingleKeyWalletWIF and hex parsers;Identity load also reserves its user-key list up front, so growing the list leaves no key copies behind. Accepted inputs and messages do not change.
User story
As an Everyday User who pastes a private key or enters a recovery phrase, I want the app to wipe its working copies of that key or seed once it is done with them, so that a later memory dump or swap file does not reveal it.
Scenario
Vec, then copied into a plain[u8; 32], and the partly decoded bytes were dropped unwiped when a later character was bad;String;[u8; 64].Zeroizingand wiped when dropped. The only copies left are the ones the stored and task types take by value.The messages are unchanged: "Private key not 32 bytes", "Invalid hex string for private key.", "Hex private keys must be exactly 32 bytes; got N bytes.", and the rest.
Detailed discussion
This PR follows #1150, which did the same for
verify_key_input, the identity-load lookups and the Key Info screen. It covers SEC-005 from the #1150 review, plus SEC-001 and SEC-002. It is based onv1.0-devand merges cleanly with #1150.model::key_input::decode_hex_secret(new). It decodes hex straight into aZeroizing<Vec<u8>>of sizelen / 2usinghex::decode_to_slice.decode_to_slicechecks for an odd length first, and the buffer always has the right length. So it accepts and rejects exactly whathex::decodedoes, with the sameFromHexError:OddLength, thenInvalidHexCharacterat the same index.add_key_screen.rs): usesdecode_hex_secret, and the key is aZeroizing<[u8; 32]>. It is hex-encoded from a slice, so no plain temporary is made.import_mnemonic_screen.rs):decode_hex_secret, and the parsed key isZeroizing;Zeroizing<String>;Zeroizing<[u8; 64]>.SingleKeyWallet::from_wif/from_hex(model/wallet/single_key.rs): the WIF path keeps theZeroizingthatEcdsaSecretKey::to_bytes()returns, and the hex path usesdecode_hex_secret.model/wallet/single_key.rs): without a password,ClosedSingleKey.encrypted_private_keyholds the raw key, but only the open key was wiped on drop. The import preview rebuilds itsSingleKeyWalleton every edit, so each replaced preview freed a plain copy of the key.ClosedSingleKeynow implementsZeroize, which wipesencrypted_private_key, and runs it on drop. The stored format is unchanged.add_new_wallet_screen.rs): the seed isZeroizing<[u8; 64]>. This is the same one-line change as the import screen, kept consistent.Vec<PrivateKey>is built withVec::with_capacity(keys_input.len())in a plain loop.collectoverfilter_maphas a lower size hint of 0, so it reallocated and memcpy'd the keys, then freed the old buffer without runningDrop. The early return on the first bad key is unchanged.What remains, deliberately:
After fix(security): wipe parsed private-key copies from memory #1150 merges, Key Info still has its own hex pre-scan next to the shared
decode_hex_secret. The two behave the same. Routing Key Info throughdecode_hex_secretis a small follow-up once both PRs are in:Okwith 32 bytes reads the key, any otherOkgives "not 32 bytes", andErrfalls back to WIF.Copies into types taken by value stay. This covers
Wallet::new_from_seed([u8; 64]),SingleKeyWallet::new([u8; 32]),IdentityTask::AddKeyToIdentity(.., [u8; 32])and the stored key formats. Changing them is a wider type or storage change.Stack slots left behind when a
Zeroizingvalue moves are the usual residual ofZeroizing. That is SEC-003 in the fix(security): wipe parsed private-key copies from memory #1150 review.The recovery-phrase word fields and the egui text buffers are out of scope.
Upstream copies are reported for upstream fixes:
dashcore::signer::sign_hash,PrivateKey::from_wifviabase58ck::decode_check, and dppvalidate_private_key_bytes_for_key_data.Testing
New tests pin the unchanged behaviour:
model::key_input::tests::decode_hex_secret_matches_hex_decodecompares bytes and errors againsthex::decode. The inputs cover empty, valid, wrong length, odd length, non-hex, a bad character late in the string, and free text.ui::wallets::import_mnemonic_screen::tests::hex_private_key_rejections_are_unchangedchecks that 31-byte hex givesPrivateKeyWrongLength { byte_count: 31 }, that odd-length and non-hex input givePrivateKeyUnrecognized, and that nothing is imported.ui::wallets::import_mnemonic_screen::tests::a_32_byte_hex_private_key_is_importedcovers the success path: 32-byte hex is converted to WIF and imported, and the imported key's address is the one the hex names.model::wallet::single_key::tests::closed_key_wipes_its_stored_key_byteschecks thatzeroizeemptiesencrypted_private_keyand keeps the hash, salt and nonce. It also checks at compile time thatClosedSingleKeyisZeroizeOnDrop.model::wallet::single_key::tests::from_hex_rejections_keep_their_messageschecks thehex::decodeerror text and the wrong-length message.ui::identity::keys::key_info_screen::tests::a_paste_is_read_as_hex_when_it_is_hex_and_as_wif_otherwisepins the hex-or-WIF split by its banners (SEC-002):a_write_behind_the_screen_survives_a_paste.These are behaviour pins, so they pass before and after the change; there is no RED step for a refactor that keeps behaviour. Zeroization itself is not tested with memory-scraping tests, because those would be unreliable.
Run with
cargo test --all-features --lib -- model::key_input model::wallet::single_key ui::wallets::import_mnemonic_screen ui::identity::keys::key_info_screen backend_task::identity::load_identity wallet_backend::single_key backend_task::migration::single_key_restore: 127 passed.cargo clippy --all-features --lib --profile test -- -D warningsis clean.🤖 Generated with Claude Code
https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q
PR Hygiene ·
f87a006src/backend_task/identity/load_identity.rs,src/model/key_input.rs,src/model/wallet/single_key.rsand 4 more) — approved by lklimekWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.