Enhance secure string handling and memory management in WASM - #4826
Enhance secure string handling and memory management in WASM#4826sergei-boiko-trustwallet wants to merge 7 commits into
Conversation
Summary by OctaneNew ContractsNo new contracts were added. Updated Contracts
🔗 Commit Hash: 80dc5e7 |
There was a problem hiding this comment.
Pull request overview
This PR hardens the WASM bindings’ handling of sensitive strings and byte buffers by ensuring buffers are copied into JS-owned values and the original WASM-heap memory is securely zeroed after use, while also refactoring the codegen template to wipe data parameters consistently.
Changes:
- Updated
TWStringconversion to return anemscripten::valJS string viaTextDecoder, and zeroed the intermediate JSUint8Array. - Updated
DataToVal/TWDataToValto take ownership, copy into a JSUint8Array, thenmemzerothe WASM-heap buffer. - Refactored the C++ codegen method-forward template to wipe
dataparameters after the underlying core call.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| wasm/src/WasmString.h | Renames string conversion helper to return emscripten::val and documents zeroization behavior. |
| wasm/src/WasmString.cpp | Implements JS-string conversion via TextDecoder with explicit zeroing of intermediate JS byte buffer. |
| wasm/src/WasmData.h | Changes DataToVal API to take ownership (Data&&) to enable post-copy zeroization. |
| wasm/src/WasmData.cpp | Moves/owns buffers, copies to JS Uint8Array, and securely zeroes WASM-heap memory. |
| wasm/src/AnySigner.cpp | Moves output buffers into DataToVal to ensure ownership transfer and zeroization. |
| wasm/src/HexCoding.cpp | Moves parsed hex data into DataToVal to ensure ownership transfer and zeroization. |
| wasm/src/CoinTypeExt.cpp | Switches string-returning APIs to TWStringToVal for consistent secure handling. |
| codegen/lib/templates/cpp/method_forward.erb | Adds parameter wiping for data parameters and refactors return handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Binary size comparison➡️ aarch64-apple-ios: 14.31 MB ➡️ aarch64-apple-ios-sim: 14.31 MB ➡️ aarch64-linux-android: 18.73 MB ➡️ armv7-linux-androideabi: 16.17 MB ➡️ wasm32-unknown-emscripten: 13.66 MB |
Overview
Detailed findings
|
This pull request introduces significant improvements to how sensitive data is handled in the WASM bindings, focusing on enhanced memory hygiene and security. The main changes include ensuring that data buffers are securely zeroed after use, updating conversion utilities to return JavaScript values instead of C++ strings, and refactoring method templates to consistently wipe sensitive parameters. These updates help prevent leaks of sensitive information in both WASM and JS heaps.
Memory hygiene and security improvements:
DataToValandTWDataToValto take ownership of data, copy it to a JSUint8Array, and securely zero the WASM-heap buffer after use (wasm/src/WasmData.cpp,wasm/src/WasmData.h). [1] [2] [3]TWStringToStdtoTWStringToVal, now returning a JS string and explicitly zeroing both the WASM-heap and the intermediate JS-heap byte buffers (wasm/src/WasmString.cpp,wasm/src/WasmString.h). [1] [2]Template and method call refactoring:
dataand, after method calls, securely wipe their buffers before returning results (codegen/lib/templates/cpp/method_forward.erb).AnySigner::signandAnySigner::planto usestd::movefor efficient transfer and zeroing of output buffers (wasm/src/AnySigner.cpp). [1] [2]HexCoding::parseHexto usestd::movefor proper ownership and zeroing of parsed data (wasm/src/HexCoding.cpp).API consistency:
CoinTypeExtmethods to useTWStringToValinstead ofTWStringToStd, ensuring consistent secure string handling across the API (wasm/src/CoinTypeExt.cpp). [1] [2]