fix: add additional self transfer checks, add rent exemption create token account check, add additiona create ata idempotent check#2292
Conversation
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughAdds stricter validations: enforce exact account size and rent-exemption for non-compressible token accounts on creation; verify existing ATA's mint and owner match expected values; and extend self-transfer validation to accept instruction payload and check transfer amount against token balance. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Program
participant TokenAccount
participant RentSysvar
participant MintAccount
Client->>Program: Create token account / Create ATA / Transfer (with instruction_data)
alt Create non-compressible account
Program->>TokenAccount: check data.len() == BASE_TOKEN_ACCOUNT_SIZE
Program->>RentSysvar: verify rent-exempt
RentSysvar-->>Program: rent-exempt? (ok / error)
end
alt Idempotent ATA path (account exists && owned by signer)
Program->>TokenAccount: from_account_info_checked()
Program->>MintAccount: compare token.base.mint == expected_mint
Program->>TokenAccount: compare token.base.owner == expected_owner
end
alt Self-transfer path
Program->>TokenAccount: validate_self_transfer(source,dest,auth,instruction_data)
Note right of Program: parse amount from instruction_data (u64 LE)
Program->>TokenAccount: check source.balance >= amount
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
Verify each finding against the current code and only fix it if needed.
In `@programs/compressed-token/program/src/ctoken/transfer/shared.rs`:
- Around line 51-55: The code assumes instruction_data has at least 8 bytes when
parsing amount with u64::from_le_bytes(instruction_data[..8]), which can panic
if a future caller omits the length check; update the function
validate_self_transfer (in shared.rs) to defensively check
instruction_data.len() >= 8 and return ProgramError::InvalidInstructionData (or
appropriate error) before slicing, or alternatively add a clear doc comment on
validate_self_transfer stating the minimum byte length requirement and
referencing the 8-byte amount layout so all callers must validate length first.
Entire-Checkpoint: 2b4028368dbf
Entire-Checkpoint: c298aaf24a18
Summary
Add data length validation for non-compressible token accounts in create.rs
Add rent-exemption check for non-compressible token accounts in create.rs
Add mint and owner field validation in idempotent ATA creation (create_ata.rs)
Add frozen state check for self-transfers (shared.rs)
Add balance sufficiency check for self-transfers (shared.rs)
Thread instruction_data through self-transfer validation (default.rs, checked.rs)
Tests
Added failing tests for all new checks:
Self-transfer (transfer.rs):
test_ctoken_self_transfer_frozen- frozen account self-transfer returns InvalidAccountDatatest_ctoken_self_transfer_insufficient_funds- amount > balance returns InsufficientFunds (6154)test_ctoken_self_transfer_success- valid self-transfer succeeds, balance unchangedtest_ctoken_transfer_checked_self_transfer_frozen- frozen account via transfer_checkedtest_ctoken_transfer_checked_self_transfer_insufficient_funds- insufficient funds via transfer_checkedNon-compressible account creation (create.rs):
Idempotent ATA creation (create_ata.rs):
test_create_ata_idempotent_owner_mismatch- tampered owner field returns InvalidAccountDatatest_create_ata_idempotent_mint_mismatch- tampered mint field returns InvalidAccountDataSummary by CodeRabbit
Bug Fixes