fix: security hardening - CEI pattern, over-withdrawal guard, debt settlement#13
Open
giwaov wants to merge 1 commit intocirclefin:masterfrom
Open
fix: security hardening - CEI pattern, over-withdrawal guard, debt settlement#13giwaov wants to merge 1 commit intocirclefin:masterfrom
giwaov wants to merge 1 commit intocirclefin:masterfrom
Conversation
…ttlement - Fix reentrancy risk in _executeRefund by setting refunded=true before external transfer call (checks-effects-interactions pattern) (circlefin#12) - Add cumulative withdrawal check in earlyWithdrawByArbiter to prevent over-withdrawal via repeated calls (circlefin#11) - Add _settleDebt call in refundByRecipient to prevent debt bypass (circlefin#10)
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.
Summary
This PR addresses three security vulnerabilities in RefundProtocol.sol:
1. Reentrancy risk in _executeRefund (fixes #12)
The _executeRefund\ function performed an external \iatToken.transfer()\ call before updating \payments[paymentID].refunded = true. This violates the checks-effects-interactions (CEI) pattern and opens a reentrancy vector if the token contract is malicious or upgradeable.
Fix: Move the state update (\payments[paymentID].refunded = true) before the external transfer call.
2. Over-withdrawal via repeated earlyWithdrawByArbiter calls (fixes #11)
The \earlyWithdrawByArbiter\ function checks \withdrawalAmount > payment.amount\ per payment but never validates the cumulative withdrawn amount. An arbiter could call this function multiple times to withdraw more than the payment amount.
Fix: Add a cumulative check: \payment.withdrawnAmount + withdrawalAmount > payment.amount.
3. Debt bypass in refundByRecipient (fixes #10)
The
efundByRecipient\ function does not call _settleDebt(msg.sender)\ before checking and deducting the recipient's balance, unlike \withdraw()\ which does settle debt first. This means a recipient with outstanding debt can get a full refund without settling.
Fix: Add _settleDebt(msg.sender)\ call before the balance check, matching the pattern in \withdraw().
Testing
All 36 existing tests pass:
\
forge test -vv
36 passed, 0 failed, 0 skipped
\\
Changes