Rules › Anchor (Rust)

CPI to a program the caller chooses

highAnchor (Rust)Rule anchor-arbitrary-cpi

invoke/invoke_signed with a program account that isn't Program<'info, T> or checked against a known id runs whatever program the caller passes.

Why it matters

A cross-program invocation to a program account the caller passed in runs whatever program they chose, with whatever accounts and signer seeds you hand it. An attacker substitutes a fake "token program" that reports success without moving anything, or steals PDA-signed authority. Use Program<'info, T> (which checks the program id) or compare the key to the expected id before invoking.

Vulnerable

let ix = Instruction { program_id: ctx.accounts.target.key(), accounts: vec![], data };
invoke(&ix, &[ctx.accounts.target.to_account_info()])?;

Fixed

// Anchor checks token_program.key() == spl_token::ID for Program<'info, Token>.
pub token_program: Program<'info, Token>,
// ...
token::transfer(CpiContext::new(ctx.accounts.token_program.to_account_info(), accounts), amount)?;

What other tools call it

ToolDetector or reference
Sealevel attacks5-arbitrary-cpi

Check your code

Paste a contract into the in-browser checker, or let the Claude Code plugin scan before every deploy. The quick check for this rule is a pattern match: treat a hit as "look here", and a miss as no guarantee.

Static analysis finds known bug classes; it can't prove code is safe and isn't an audit. Get a professional audit before deploying code that will hold value.