0

I am trying to learn solana development[native solana] and when reading some example programs I saw they all use the "accounts: &[AccountInfo]" in order to figure out the accounts involved.

Now since this array is created and sent by the user, I have doubts about how secure this method is. I even see some programs using this to get other pda/system programs for calls. So my question is what exactly can be trusted in this data? Can I assume that the caller is the first account? And is it secure to get system\pda addresses that way? Basically I want to understand which parts of this array I need to preform checks in my program and which parts I can assume that are correct.

Any help will be appreciated.

1 Answer 1

3

Accounts array is as reliable as you make it!

Note: Even though the user sends the accounts array, it is the program which decides the sequence of the accounts and communicates that to the user. The user is then required to provide the necessary accounts in the mentioned sequence to make the transaction work.

But you are right, the user can decide to ignore the sequence and pretty much send any accounts in the transaction. That's where proper validation comes into picture.

Consider the following function:

pub fn process_my_ix(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    let account_info_iter = &mut accounts.iter();

    let first_acc_info = next_account_info(account_info_iter)?; // 0
....

As you can see, first_acc_info is the first account we expect the user to provide us. The user can provide us any account but we want the first account to be the signer of the transaction. So, we will step forward and validate this account within the function:

...
if !first_acc_info.is_signer {
    msg!("Missing required signature");
    return Err(ProgramError::MissingRequiredSignature)
}
...

Now, it isn't possible for the user to enter a non-signer account as the first account in the array. This is how you must validate every account according to the logic of the program to avoid any kind of malicious activity from the user. A proper validation is the basic pillar of a program (smart contract).

PS: Anchor makes such checks much easier.

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.