Skip to content
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

Added comments to 5-arbitrary-cpi in secure, recommended, and insecure versions #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion programs/5-arbitrary-cpi/insecure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
pub mod arbitrary_cpi_insecure {
use super::*;

// Insecure CPI invocation without program ID verification
pub fn cpi(ctx: Context<Cpi>, amount: u64) -> ProgramResult {
// Directly invokes the `transfer` instruction from the token program without checking the legitimacy of the CPI
solana_program::program::invoke(
&spl_token::instruction::transfer(
ctx.accounts.token_program.key,
ctx.accounts.token_program.key, // Unsafe: token program isn't verified
ctx.accounts.source.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
Expand All @@ -28,6 +30,7 @@ pub mod arbitrary_cpi_insecure {

#[derive(Accounts)]
pub struct Cpi<'info> {
// Uses generic AccountInfo, lacking token-specific account validation
source: AccountInfo<'info>,
destination: AccountInfo<'info>,
authority: AccountInfo<'info>,
Expand Down
7 changes: 7 additions & 0 deletions programs/5-arbitrary-cpi/recommended/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
pub mod arbitrary_cpi_recommended {
use super::*;

// Safe CPI invocation using verified token accounts
pub fn cpi(ctx: Context<Cpi>, amount: u64) -> ProgramResult {
// Uses anchor_spl's `transfer` helper, which performs more comprehensive checks on the accounts
token::transfer(ctx.accounts.transfer_ctx(), amount)
}
}

#[derive(Accounts)]
pub struct Cpi<'info> {
// Strong typing for TokenAccount ensures proper token program interactions
source: Account<'info, TokenAccount>,
destination: Account<'info, TokenAccount>,
// Verifies authority as a signer
authority: Signer<'info>,
// Program type enforces the correct program being used
token_program: Program<'info, Token>,
}

impl<'info> Cpi<'info> {
// Creates the context required for invoking the token transfer CPI
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.source.to_account_info(),
to: self.destination.to_account_info(),
authority: self.authority.to_account_info(),
};
// Builds the CPI context for safe execution
CpiContext::new(program, accounts)
}
}
6 changes: 5 additions & 1 deletion programs/5-arbitrary-cpi/secure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
pub mod arbitrary_cpi_secure {
use super::*;

// Secure CPI invocation with program ID validation
pub fn cpi_secure(ctx: Context<Cpi>, amount: u64) -> ProgramResult {
// Verifies the token program is indeed the spl_token program
if &spl_token::ID != ctx.accounts.token_program.key {
return Err(ProgramError::IncorrectProgramId);
return Err(ProgramError::IncorrectProgramId); // Fail if not the correct program
}
// Safely invoke the transfer instruction
solana_program::program::invoke(
&spl_token::instruction::transfer(
ctx.accounts.token_program.key,
Expand All @@ -31,6 +34,7 @@ pub mod arbitrary_cpi_secure {

#[derive(Accounts)]
pub struct Cpi<'info> {
// Same as the insecure version but with added program ID validation
source: AccountInfo<'info>,
destination: AccountInfo<'info>,
authority: AccountInfo<'info>,
Expand Down