refactor(canister): range-check untrusted amounts without copying them - #242
Open
mbjorkqvist wants to merge 2 commits into
Open
refactor(canister): range-check untrusted amounts without copying them#242mbjorkqvist wants to merge 2 commits into
mbjorkqvist wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the canister’s handling of untrusted Nat amounts by allowing constant-time range checks to operate on borrowed values (avoiding copies of oversized magnitudes) and by ensuring only validated amounts are used for ledger calls, with new tests pinning the intended validation/authorization ordering.
Changes:
- Add
TryFrom<&Nat>fororder::Quantityand refactor callers to range-check through references instead of cloning untrustedNats. - Ensure deposit/withdraw use the already-validated amount for downstream logic (withdraw zero-check; deposit ledger transfer), and apply the same borrowing range-check to
add_trading_pairnotional bounds. - Add tests covering (1) range-check precedence over other request-error branches and (2) restricted-mode allowlist rejection occurring before any expensive work.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| integration_tests/tests/tests.rs | Adds restricted-mode integration coverage ensuring non-allowlisted callers are rejected via the allowlist path even with oversized amounts. |
| canister/src/tests.rs | Adds unit tests asserting oversized amounts are rejected before unsupported-token branches (for both deposit and withdraw). |
| canister/src/order/mod.rs | Introduces TryFrom<&Nat> for Quantity to enable constant-time range checks without copying untrusted magnitudes; retains TryFrom<Nat> for compatibility. |
| canister/src/lib.rs | Refactors deposit/withdraw and trading-pair notional validation to use borrowed range checks and to pass validated amounts onward (including to the ledger). |
| canister/src/ledger/mod.rs | Changes ledger::deposit to accept an already-validated amount value, preventing any unvalidated magnitude from reaching the ledger call. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
|
`Quantity::try_from(request.amount.clone())` copied the whole unbounded magnitude before the O(1) `bits()` check that rejects it, so the handlers still made one full copy of unvalidated input. Add a borrowing `TryFrom<&Nat> for Quantity` (the owned impl now delegates to it) and range-check through the reference instead. Also use the validated value where the raw request field was still being read back: `amount.is_zero()` for the withdraw zero-check, and `ledger::deposit` now takes the range-checked amount rather than the whole request, mirroring `ledger::withdraw`. The values are equal on every path that reaches them, so behavior is unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`add_trading_pair` cloned `min_notional` and `max_notional` only to hand them to `Quantity::try_from`, which now accepts a reference. The error payload still needs owned copies, so `invalid_notional` keeps its clones — but they are only built on the failure path. This endpoint is controller-gated, so there is no DoS exposure here; it is the same needless copy the borrowing conversion was added to remove. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
mbjorkqvist
force-pushed
the
mathias/DEFI-2959-followup-validate-amount-nits
branch
from
July 31, 2026 12:49
7862493 to
8b6fdb3
Compare
mbjorkqvist
marked this pull request as ready for review
July 31, 2026 13:05
|
✅ No security or compliance issues detected. Reviewed everything up to 8b6fdb3. Security Overview
Detected Code Changes
|
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.
Cleans up the needless copies of untrusted
Natamounts left behind by #237. Behaviour-preserving throughout — no path that reaches the ledger changes.Quantitygains a borrowing conversion, and the deposit/withdraw handlers — plusadd_trading_pair's notional bounds — range-check through a reference instead. The owned conversion stays as a delegating impl, since other callers still pass owned values.No new tests: the conversion's boundary is already covered by proptests in
canister/src/order/tests.rs, and the owned impl delegating to the borrowing one means they exercise both paths.The regression tests for #237's ordering guarantees are split out into #243, since they stand on their own and don't depend on anything here.