fix(binarycodec): harden MPT Amount — bounds, sign-bit, 0X prefix, TryFrom guard - #339
Open
e-desouza wants to merge 2 commits into
Open
fix(binarycodec): harden MPT Amount — bounds, sign-bit, 0X prefix, TryFrom guard#339e-desouza wants to merge 2 commits into
e-desouza wants to merge 2 commits into
Conversation
This was referenced Jul 12, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #339 +/- ##
==========================================
+ Coverage 86.71% 86.74% +0.03%
==========================================
Files 252 252
Lines 32630 32683 +53
==========================================
+ Hits 28296 28352 +56
+ Misses 4334 4331 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the binary codec’s Amount handling for MPT and native XRP wire data to avoid panics on malformed buffers, enforce XLS-33’s “MPT amounts are unsigned” rule, and align MPT hex parsing with xrpl.js behavior.
Changes:
- Added length guards and error returns to prevent out-of-bounds reads during native/MPT serialization and in
is_*helpers. - Enforced MPT positivity by rejecting wire payloads with the positive bit (0x40) cleared, instead of emitting negative-prefixed strings.
- Tightened MPT value parsing to reject uppercase
"0X"prefixes and added regression tests for the new hardening behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/core/binarycodec/types/amount.rs |
Adds bounds checks, converts native decode to fallible, rejects invalid MPT sign bit and "0X" prefix, and adds new tests. |
src/core/binarycodec/test/tx_encode_decode_tests.rs |
Updates MPT negative-sign-bit test to expect serialization rejection instead of a negative string. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
404
to
+407
| let is_positive = leading & 0x40 != 0; | ||
| let sign = if is_positive { "" } else { "-" }; | ||
| if !is_positive { | ||
| return Err(S::Error::custom("MPT amount has negative sign bit set")); | ||
| } |
Comment on lines
+281
to
+285
| /// Returns true if the positive-sign bit (0x40) in byte 0 is set. | ||
| /// Returns false on an empty or structurally invalid buffer. Callers that need to | ||
| /// distinguish "not positive" from "invalid/empty" should check `!self.0.is_empty()` first. | ||
| pub fn is_positive(&self) -> bool { | ||
| self.0[0] & 0x40 > 0 | ||
| !self.0.is_empty() && self.0[0] & 0x40 > 0 |
Comment on lines
+469
to
+473
| if obj.contains_key("mpt_issuance_id") { | ||
| // Primary MPT discriminator — presence of mpt_issuance_id marks this as MPT. | ||
| if obj.contains_key("currency") || obj.contains_key("issuer") { | ||
| return Err(XRPLCoreException::SerdeJsonError( | ||
| XRPLSerdeJsonError::UnexpectedValueType { |
- Correct misleading error message: "negative sign bit set" → "positive-sign
bit (0x40) is not set", which accurately describes the failure condition
- Tighten is_positive() rustdoc: remove unsupported "structurally invalid"
claim; only an empty buffer is guarded against
- Enforce exact-key validation for MPT in TryFrom<serde_json::Value>: require
exactly {"mpt_issuance_id","value"} (len == 2 + both keys present), matching
xrpl.js isAmountObjectMPT and the strict shape in src/models/amount/mod.rs
e-desouza
force-pushed
the
fix/issue-259-262-mpt-amount
branch
from
July 15, 2026 16:45
04854a8 to
5fc169a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Amountin the binary codec had several gaps when processing MPT wire data:is_native,is_mpt, andis_positivepanicked on empty buffers — any caller could trigger a process crash by constructing a zero-lengthAmount._deserialize_native_amountsliced 8 bytes without a length guard; a short buffer panicked insideSerialize.Serializeaccessedbytes[1..9]andbytes[9..33]without a priorlen < 33check.TryFrom<serde_json::Value>for MPT amounts indexedobj["value"]viaMap::index, which panics on a missing key. An object carrying onlympt_issuance_id(no"value") crashed the process on any publicencode()call."0X"hex prefix was silently accepted; xrpl.js rejects it.Fixes #259, #262.
What changed
is_native,is_mpt,is_positive: guard with!self.0.is_empty()before byte 0 access._deserialize_native_amount: changed return toXRPLCoreResult<String>; addedlen < 8early return.Serializebranch: addedlen < 33check; returnsErrinstead of producing a negative string when positive bit is clear._serialize_mpt_amount: explicitErron"0X"uppercase prefix (matches xrpl.js); usesstrip_prefix("0x")only.TryFrom<serde_json::Value>MPT branch: replacedobj["value"]/obj["mpt_issuance_id"]indexing with.get(key).and_then(|v| v.as_str()).ok_or(...).How to validate
New tests:
test_mpt_negative_sign_bit_rejected_on_serialize,test_mpt_short_buffer_serialize_error,test_amount_bool_methods_empty_buffer,test_mpt_reject_uppercase_hex_prefix,test_native_short_buffer_serialize_error.