Skip to content

Fix critical security issues #3, #5, #17, #20 - #234

Merged
chonilius merged 2 commits into
MergeFi:mainfrom
gideononiru:fix/comprehensive-security-and-testing-improvements
Aug 30, 2026
Merged

Fix critical security issues #3, #5, #17, #20#234
chonilius merged 2 commits into
MergeFi:mainfrom
gideononiru:fix/comprehensive-security-and-testing-improvements

Conversation

@gideononiru

Copy link
Copy Markdown

Summary

This PR addresses four critical security and testing issues identified in the MergeFi contracts audit.

Changes

Issue #3: Token Accounting Protection

Problem: Trusting caller-supplied amounts could lead to accounting desync with fee-on-transfer, rebasing, or malicious tokens.

Solution:

  • Implemented measure_transfer_delta() in common lib to measure actual balance changes
  • Updated fund() and contribute() in both escrow and milestones contracts
  • Prevents bookkeeping from diverging from actual token holdings

Files Changed:

  • contracts/common/src/lib.rs
  • contracts/escrow/src/lib.rs
  • contracts/milestones/src/lib.rs

Issue #5: State Machine Bug Fix

Problem: release_issue() could be called after cancel_milestone(), leading to inconsistent state.

Solution:

  • Added milestone.closed check to release_issue()
  • Returns Error::MilestoneClosed if milestone is already closed
  • Updated deallocate() documentation for proper cancellation workflow

Files Changed:

  • contracts/milestones/src/lib.rs

Issue #20: Fee Update Mechanism with Safety Limits

Problem: No mechanism to update protocol fees, and unlimited changes could harm sponsors.

Solution:

  • Added set_fee_bps() admin function to escrow and milestones contracts
  • Implemented validate_fee_change() with 5% (500 bps) max change per call
  • Point-in-time fee locking: sponsors know the fee when committing funds
  • New escrows/milestones use current fee; existing ones retain their original fee

Files Changed:

  • contracts/common/src/lib.rs (validation logic)
  • contracts/escrow/src/lib.rs
  • contracts/milestones/src/lib.rs

Issue #17: Property-Based Fuzz Testing Infrastructure

Problem: Need more rigorous testing to catch edge cases in split calculations.

Solution:

  • Added proptest dependency to workspace
  • Created test_fuzz.rs with comprehensive property tests
  • 7 property tests verify mathematical invariants:
    • Sum preservation (total - fee)
    • Zero fee behavior
    • 100% fee behavior
    • Proportional distribution
    • Monotonic BPS (increasing share never decreases amount)
    • No amount exceeds total
    • All amounts non-negative
  • 3 edge case tests for boundary conditions

Files Changed:

  • Cargo.toml
  • contracts/common/Cargo.toml
  • contracts/common/src/lib.rs
  • contracts/common/src/test_fuzz.rs (new)

Testing

  • ✅ All 131 tests pass
    • 58 escrow tests
    • 40 milestones tests
    • 23 maintenance-pool tests
    • 10 property-based fuzz tests
  • Fixed MockPanicToken in test files to support balance tracking
  • All existing functionality preserved

Closes

…i#20

This commit addresses four critical security and testing issues:

Issue MergeFi#3: Token Accounting Protection
- Implemented balance-delta measurement in measure_transfer_delta()
- Protects against fee-on-transfer, rebasing, and malicious tokens
- Updated fund() and contribute() in escrow and milestones contracts
- Prevents accounting desync between bookkeeping and actual holdings

Issue MergeFi#5: State Machine Bug Fix
- Added milestone.closed check to release_issue()
- Blocks release after cancel_milestone is called
- Updated deallocate() workflow documentation for proper cancellation
- Prevents inconsistent state transitions

Issue MergeFi#20: Fee Update Mechanism
- Added set_fee_bps() to escrow and milestones contracts
- Implemented validate_fee_change() with 5% max change limit
- Point-in-time fee locking: sponsors know fee when committing funds
- Prevents accidental or malicious fee spikes

Issue MergeFi#17: Property-Based Fuzz Testing
- Added proptest dependency to workspace
- Created test_fuzz.rs with 7 property tests + 3 edge case tests
- Tests verify mathematical invariants: sum preservation, proportionality,
  monotonicity, bounds checking across wide input ranges
- Catches edge cases that unit tests might miss

Testing:
- All 131 tests pass (58 escrow, 40 milestones, 23 maintenance-pool, 10 fuzz)
- Fixed MockPanicToken in tests to support balance tracking
- Fixed doctest examples

Closes MergeFi#3, MergeFi#5, MergeFi#17, MergeFi#20
@vercel

vercel Bot commented Aug 30, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the chonilius' projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@gideononiru Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@chonilius
chonilius merged commit f324dc1 into MergeFi:main Aug 30, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment