Skip to content

Commit

Permalink
add some validation of remaining accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Feb 13, 2025
1 parent f7a30fb commit 3a8aa41
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions programs/gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,8 @@ pub mod gateway {
.pack();

// account metas for remaining accounts
let account_metas: Vec<AccountMeta> = ctx
.remaining_accounts
.iter()
.map(|account_info| {
// Check if the account is writable, then create the appropriate AccountMeta
if account_info.is_writable {
AccountMeta::new(*account_info.key, account_info.is_signer)
} else {
AccountMeta::new_readonly(*account_info.key, account_info.is_signer)
}
})
.collect();
let account_metas =
prepare_account_metas(ctx.remaining_accounts, &ctx.accounts.signer, pda)?;

let ix = Instruction {
program_id: ctx.accounts.destination_program.key(),
Expand Down Expand Up @@ -314,18 +304,8 @@ pub mod gateway {
.pack();

// account metas for remaining accounts
let account_metas: Vec<AccountMeta> = ctx
.remaining_accounts
.iter()
.map(|account_info| {
// Check if the account is writable, then create the appropriate AccountMeta
if account_info.is_writable {
AccountMeta::new(*account_info.key, account_info.is_signer)
} else {
AccountMeta::new_readonly(*account_info.key, account_info.is_signer)
}
})
.collect();
let account_metas =
prepare_account_metas(ctx.remaining_accounts, &ctx.accounts.signer, pda)?;

let ix = Instruction {
program_id: ctx.accounts.destination_program.key(),
Expand Down Expand Up @@ -937,6 +917,36 @@ fn validate_whitelist_tss_signature(
Ok(())
}

// Prepares account metas for withdraw and call, revert if unallowed account is passed
// TODO: this might be extended
fn prepare_account_metas(
remaining_accounts: &[AccountInfo],
signer: &Signer,
pda: &Account<Pda>,
) -> Result<Vec<AccountMeta>> {
let mut account_metas = Vec::new();

for account_info in remaining_accounts.iter() {
let account_key = account_info.key;

// Prevent signer from being included
require!(account_key != signer.key, Errors::InvalidInstructionData);

// Gateway pda can be added as not writable
if *account_key == pda.key() {
account_metas.push(AccountMeta::new_readonly(*account_key, false));
} else {

Check warning on line 938 in programs/gateway/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] programs/gateway/src/lib.rs#L938

warning: this `else { if .. }` block can be collapsed --> programs/gateway/src/lib.rs:938:16 | 938 | } else { | ________________^ 939 | | if account_info.is_writable { 940 | | account_metas.push(AccountMeta::new(*account_key, false)); 941 | | } else { 942 | | account_metas.push(AccountMeta::new_readonly(*account_key, false)); 943 | | } 944 | | } | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if = note: `#[warn(clippy::collapsible_else_if)]` on by default help: collapse nested if block | 938 ~ } else if account_info.is_writable { 939 + account_metas.push(AccountMeta::new(*account_key, false)); 940 + } else { 941 + account_metas.push(AccountMeta::new_readonly(*account_key, false)); 942 + } |
Raw output
programs/gateway/src/lib.rs:938:16:w:warning: this `else { if .. }` block can be collapsed
   --> programs/gateway/src/lib.rs:938:16
    |
938 |           } else {
    |  ________________^
939 | |             if account_info.is_writable {
940 | |                 account_metas.push(AccountMeta::new(*account_key, false));
941 | |             } else {
942 | |                 account_metas.push(AccountMeta::new_readonly(*account_key, false));
943 | |             }
944 | |         }
    | |_________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if
    = note: `#[warn(clippy::collapsible_else_if)]` on by default
help: collapse nested if block
    |
938 ~         } else if account_info.is_writable {
939 +             account_metas.push(AccountMeta::new(*account_key, false));
940 +         } else {
941 +             account_metas.push(AccountMeta::new_readonly(*account_key, false));
942 +         }
    |


__END__
if account_info.is_writable {
account_metas.push(AccountMeta::new(*account_key, false));
} else {
account_metas.push(AccountMeta::new_readonly(*account_key, false));
}
}
}

Ok(account_metas)
}

/// Instruction context for initializing the program.
#[derive(Accounts)]
pub struct Initialize<'info> {
Expand Down

0 comments on commit 3a8aa41

Please sign in to comment.