Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit dc85c98

Browse files
authored
token-2022: Prohibit self-delegated transfers with CPI Guard (#6724)
1 parent d1c03cc commit dc85c98

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

token/program-2022-test/tests/cpi_guard.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,27 @@ async fn test_cpi_guard_transfer() {
280280
let alice_state = token_obj.get_account_info(&alice.pubkey()).await.unwrap();
281281
assert_eq!(alice_state.base.amount, amount);
282282

283+
// delegate-auth cpi transfer to self does not work
284+
token_obj
285+
.approve(
286+
&alice.pubkey(),
287+
&alice.pubkey(),
288+
&alice.pubkey(),
289+
1,
290+
&[&alice],
291+
)
292+
.await
293+
.unwrap();
294+
295+
let error = token_obj
296+
.process_ixs(&[mk_transfer(alice.pubkey(), do_checked)], &[&alice])
297+
.await
298+
.unwrap_err();
299+
assert_eq!(error, client_error(TokenError::CpiGuardTransferBlocked));
300+
301+
let alice_state = token_obj.get_account_info(&alice.pubkey()).await.unwrap();
302+
assert_eq!(alice_state.base.amount, amount);
303+
283304
// delegate-auth cpi transfer with cpi guard works
284305
token_obj
285306
.approve(

token/program-2022/src/processor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ impl Processor {
402402
authority_info_data_len,
403403
account_info_iter.as_slice(),
404404
)?;
405+
if let Ok(cpi_guard) = source_account.get_extension::<CpiGuard>() {
406+
// If delegated to self, don't allow a transfer with CPI Guard
407+
if delegate == source_account.base.owner
408+
&& cpi_guard.lock_cpi.into()
409+
&& in_cpi()
410+
{
411+
return Err(TokenError::CpiGuardTransferBlocked.into());
412+
}
413+
}
405414
let delegated_amount = u64::from(source_account.base.delegated_amount);
406415
if delegated_amount < amount {
407416
return Err(TokenError::InsufficientFunds.into());

0 commit comments

Comments
 (0)