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

fix: 🐛 Components CPI caller check #87

Open
wants to merge 3 commits into
base: main
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
53 changes: 37 additions & 16 deletions programs/bolt-component/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar::instructions::get_instruction_relative;

declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");

Expand All @@ -7,15 +8,22 @@ pub mod bolt_component {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let instruction =
anchor_lang::solana_program::sysvar::instructions::get_instruction_relative(
0,
&ctx.accounts.instruction_sysvar_account.to_account_info(),
)
.unwrap();
if instruction.program_id == id() {
panic!("The instruction must be called from a CPI");
// Check if the program is called via CPI
match get_instruction_relative(
-1,
&ctx.accounts.instruction_sysvar_account.to_account_info(),
) {
Ok(instruction) => {
// Verify the caller's program ID, if necessary
if instruction.program_id != ctx.accounts.authority.key() {
return Err(ErrorCode::UnauthorizedCaller.into());
}
}
Err(_) => {
return Err(ErrorCode::MustBeCalledViaCpi.into());
}
}

ctx.accounts.data.bolt_metadata.authority = *ctx.accounts.authority.key;
Ok(())
}
Expand Down Expand Up @@ -47,14 +55,19 @@ pub mod bolt_component {
}

pub fn update(ctx: Context<Update>, _data: Vec<u8>) -> Result<()> {
let instruction =
anchor_lang::solana_program::sysvar::instructions::get_instruction_relative(
0,
&ctx.accounts.instruction_sysvar_account.to_account_info(),
)
.unwrap();
if instruction.program_id == id() {
panic!("The instruction must be called from a CPI");
// Same CPI check as in initialize
match get_instruction_relative(
-1,
&ctx.accounts.instruction_sysvar_account.to_account_info(),
) {
Ok(instruction) => {
if instruction.program_id != ctx.accounts.authority.key() {
return Err(ErrorCode::UnauthorizedCaller.into());
}
}
Err(_) => {
return Err(ErrorCode::MustBeCalledViaCpi.into());
}
}
Ok(())
}
Expand Down Expand Up @@ -118,3 +131,11 @@ pub struct Position {
pub struct BoltMetadata {
pub authority: Pubkey,
}

#[error_code]
pub enum ErrorCode {
#[msg("The instruction must be called from a CPI")]
MustBeCalledViaCpi,
#[msg("Unauthorized caller program")]
UnauthorizedCaller,
}
17 changes: 13 additions & 4 deletions tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,33 @@ describe("bolt", () => {
entity5Pda = addEntity.entityPda; // Saved for later
});

it("Initialize Original Component on Entity 1, trough the world instance", async () => {
it("Initialize Original Component on Entity 1, through the world instance", async () => {
const initializeComponent = await InitializeComponent({
payer: provider.wallet.publicKey,
entity: entity1Pda,
seed: "origin-component",
componentId: boltComponentProgram.programId,
});
await provider.sendAndConfirm(initializeComponent.transaction);
try {
await provider.sendAndConfirm(initializeComponent.transaction);
} catch (error) {
expect(error.message).to.contain("Error Code: MustBeCalledViaCpi");
}
});

it("Initialize Original Component on Entity 2, trough the world instance", async () => {
it("Initialize Original Component on Entity 2, through the world instance", async () => {
const initializeComponent = await InitializeComponent({
payer: provider.wallet.publicKey,
entity: entity2Pda,
seed: "origin-component",
componentId: boltComponentProgram.programId,
});
await provider.sendAndConfirm(initializeComponent.transaction);

try {
await provider.sendAndConfirm(initializeComponent.transaction);
} catch (error) {
expect(error.message).to.contain("Error Code: MustBeCalledViaCpi");
}
});

it("Initialize Position Component on Entity 1", async () => {
Expand Down
Loading