-
Notifications
You must be signed in to change notification settings - Fork 90
fix: validate authority on self-transfer early return #2252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a179aef
fix: handle self-transfer in ctoken transfer and transfer_checked
ananas-block a9c0a0d
fix: simplify map_or to is_some_and per clippy
ananas-block f5eec03
fix: use pubkey_eq for self-transfer check
ananas-block 89d1653
refactor: extract self-transfer validation into shared function
ananas-block 54dc238
chore: format
ananas-block bfceac3
fix: deduplicate random metadata keys in test_random_mint_action
ananas-block File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| use anchor_compressed_token::ErrorCode; | ||
| use anchor_lang::solana_program::program_error::ProgramError; | ||
| use anchor_lang::solana_program::{msg, program_error::ProgramError}; | ||
| use light_program_profiler::profile; | ||
| use light_token_interface::{ | ||
| state::{Token, ZExtensionStructMut}, | ||
|
|
@@ -15,6 +15,45 @@ use crate::{ | |
| }, | ||
| }; | ||
|
|
||
| /// Validates self-transfer: if source == destination, checks authority is signer | ||
| /// and is owner or delegate of the token account. | ||
| /// Returns Ok(true) if self-transfer was validated (caller should return Ok(())), | ||
| /// Returns Ok(false) if not a self-transfer (caller should continue). | ||
| #[inline(always)] | ||
| pub fn validate_self_transfer( | ||
| source: &AccountInfo, | ||
| destination: &AccountInfo, | ||
| authority: &AccountInfo, | ||
| ) -> Result<bool, ProgramError> { | ||
| if !pubkey_eq(source.key(), destination.key()) { | ||
| return Ok(false); | ||
| } | ||
| validate_self_transfer_authority(source, authority)?; | ||
| Ok(true) | ||
| } | ||
|
|
||
| #[cold] | ||
| fn validate_self_transfer_authority( | ||
| source: &AccountInfo, | ||
| authority: &AccountInfo, | ||
| ) -> Result<(), ProgramError> { | ||
| if !authority.is_signer() { | ||
| return Err(ProgramError::MissingRequiredSignature); | ||
| } | ||
| let token = | ||
| Token::from_account_info_checked(source).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let is_owner = pubkey_eq(authority.key(), token.base.owner.array_ref()); | ||
| let is_delegate = token | ||
| .base | ||
| .delegate() | ||
| .is_some_and(|d| pubkey_eq(authority.key(), d.array_ref())); | ||
|
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In SPL, it will check if the delegate amount is greater than the transferred amount, even when self-transferring. So there's a bit of inconsistency here |
||
| if !is_owner && !is_delegate { | ||
| msg!("Self-transfer authority must be owner or delegate"); | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Extension information detected from a single account deserialization. | ||
| /// Uses `MintExtensionFlags` for T22 extension flags to avoid duplication. | ||
| #[derive(Debug, Default)] | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.