Skip to content

feat: add reentrancy guard to release, refund, and cancel_invoice - #541

Merged
Kingsman-99 merged 2 commits into
Stellar-split:mainfrom
Keengfk:feat/reentrancy-guard
Jul 28, 2026
Merged

feat: add reentrancy guard to release, refund, and cancel_invoice#541
Kingsman-99 merged 2 commits into
Stellar-split:mainfrom
Keengfk:feat/reentrancy-guard

Conversation

@Keengfk

@Keengfk Keengfk commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

feat: reentrancy guard for release, refund, and cancel_invoice

Summary

Token transfer callbacks in Soroban can re-enter the contract if the token
contract is malicious or misconfigured. This PR protects all fund-moving entry
points with a reentrancy lock backed by temporary storage.

Changes

error.rs

  • Added ReentrantCall = 33 to ContractError. Discriminant is append-only
    (stable).

lib.rs

  • Added reentrancy_lock_key() — returns a Symbol ("re_lock") used as the
    temporary storage key for the lock.
  • Added with_reentrancy_guard() — a closure-based helper that sets the flag on
    entry, runs the body, and clears it on exit.
  • Wrapped release_invoice, refund, and cancel_invoice with an inline guard:
    1. If re_lock is present in temporary storage → panic! with ReentrantCall
      error code.
    2. Set re_lock in temporary storage.
    3. Execute the function body.
    4. Remove re_lock on normal exit.

Why temporary storage

Temporary storage is scoped to a single transaction and is automatically
discarded on abort. This means:

  • A stale lock can never block a future independent call across transaction
    boundaries.
  • No migration or cleanup logic is needed.
  • If the function panics (transaction aborts), the lock is dropped with the
    transaction state.

Acceptance criteria coverage

┌───────────┬────────┐
│ Criterion │ Status │
├──────────────────────────────────────────────────────┼────────┤
│ Nested calls to release_invoice return ReentrantCall │
└────────────────────────
closes #479

- Add ReentrantCall = 33 error variant to ContractError (stable discriminant)
- Add reentrancy_lock_key() using temporary storage symbol "re_lock"
- Add with_reentrancy_guard() closure helper
- Wrap release_invoice, refund, and cancel_invoice with inline guard:
  check lock -> set lock -> run body -> clear lock on normal exit
- Temporary storage ensures the lock is never persisted across transactions
@drips-wave

drips-wave Bot commented Jul 27, 2026

Copy link
Copy Markdown

@Keengfk 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

@Kingsman-99
Kingsman-99 merged commit 9e848fb into Stellar-split:main Jul 28, 2026
1 of 4 checks passed
@grantfox-oss grantfox-oss Bot mentioned this pull request Jul 28, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reentrancy Guard for Payout Functions

2 participants