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 safety check to IDL write instruction handler. #1215

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum ErrorCode {
/// 1001 - Invalid program given to the IDL instruction
#[msg("Invalid program given to the IDL instruction")]
IdlInstructionInvalidProgram,
/// 1002 - Canonical IDL account cannot be written to, must write to an IDL buffer
#[msg("Canonical IDL account cannot be written to, must write to an IDL buffer")]
IdlInstructionWritingToCanonicalAccount,

// Constraints
/// 2000 - A mut constraint was violated
Expand Down
13 changes: 10 additions & 3 deletions lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
Err(anchor_lang::__private::ErrorCode::IdlInstructionStub.into())
}

// One time IDL account initializer. Will faill on subsequent
// One time IDL account initializer. Will fail on subsequent
// invocations.
#[inline(never)]
pub fn __idl_create_account(
Expand Down Expand Up @@ -152,8 +152,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlWrite");

let mut idl = &mut accounts.idl;
idl.data.extend(idl_data);
let mut buffer = &mut accounts.idl;

// Ensure that the instruction is writing to a buffer account, not the canonical IDL
// account.
if buffer.key == IdlAccount::address(program_id) {
return Err(anchor_lang::__private::ErrorCode::IdlInstructionWritingToCanonicalAccount.into());
}

buffer.data.extend(idl_data);
Ok(())
}

Expand Down