1

The anchor documentation recommends not using the UncheckedAccount type unless you are sure that there are no security vulnerabilities.

Explicit wrapper for AccountInfo types to emphasize that no checks are performed

In the following scenario where I make a transfer from one account to another

#[derive(Accounts)]
pub struct Transfer<'info> {
    #[account(mut)]
    pub from: Signer<'info>,
    #[account(mut)]
    /// CHECK: Fine to be unsafe, we only need to send the funds to the winner
    pub to: UncheckedAccount<'info>
}

is it safe to mark the to account as UncheckedAccount? What are the security implications?

1 Answer 1

3

Anchor includes a SystemAccount type that you can use if the expected to account will always be a system program owned account (ex. a user's wallet address).

#[derive(Accounts)]
pub struct Transfer<'info> {
    pub from: Signer<'info>,
    #[account(mut)]
    pub to: SystemAccount<'info>
}

Using the UncheckedAccount type would allow the recipient of the transfer to be any account (such as program account, or an account owned by other program).

You would need to specify that the from account is mutable with #[account(mut)]. Maybe add a test to see if the balances after the transfer actually changes.

3
  • Why is it bad if the recipient account can be any account? What if it is the intended behaviour that both PDAs and system owned accounts can enter the lottery and be a recipient of the prize? Commented Jan 4 at 20:11
  • If it fits your use case and the PDA is owned by a program other than the system program, you could leave it as UncheckedAccount type
    – john
    Commented Jan 4 at 20:16
  • 1
    You can also include other checks in your instruction. IMO just leaving UncheckedAccount with no other verification for a transfer_to function just risks SOL going somewhere and getting stranded if somebody passes a bad/incorrect account param.
    – AMilz
    Commented Jan 4 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.