Skip to content

Commit 7c121bb

Browse files
committed
fix(lazer): verify account size before applying migration
1 parent 2faddf9 commit 7c121bb

File tree

2 files changed

+61
-3
lines changed
  • lazer/contracts/solana/programs/pyth-lazer-solana-contract

2 files changed

+61
-3
lines changed

lazer/contracts/solana/programs/pyth-lazer-solana-contract/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ pub mod pyth_lazer_solana_contract {
103103
if old_data[0..ANCHOR_DISCRIMINATOR_BYTES] != Storage::DISCRIMINATOR {
104104
return Err(ProgramError::InvalidAccountData.into());
105105
}
106+
if old_data.len() != StorageV010::SERIALIZED_LEN + ANCHOR_DISCRIMINATOR_BYTES {
107+
return Err(ProgramError::InvalidAccountData.into());
108+
}
106109
let old_storage = StorageV010::deserialize(&mut &old_data[ANCHOR_DISCRIMINATOR_BYTES..])?;
107110
if old_storage.top_authority != ctx.accounts.top_authority.key() {
108111
return Err(ProgramError::MissingRequiredSignature.into());

lazer/contracts/solana/programs/pyth-lazer-solana-contract/tests/test1.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use {
22
anchor_lang::{prelude::AccountMeta, InstructionData},
33
pyth_lazer_solana_contract::{ed25519_program_args, ANCHOR_DISCRIMINATOR_BYTES},
4-
solana_program_test::{BanksClient, ProgramTest},
4+
solana_program_test::{BanksClient, BanksClientError, ProgramTest},
55
solana_sdk::{
66
account::Account,
77
ed25519_program,
88
hash::Hash,
9-
instruction::Instruction,
9+
instruction::{Instruction, InstructionError},
1010
pubkey::{Pubkey, PUBKEY_BYTES},
1111
signature::Keypair,
1212
signer::Signer,
1313
system_instruction, system_program, system_transaction, sysvar,
14-
transaction::Transaction,
14+
transaction::{Transaction, TransactionError},
1515
},
1616
std::env,
1717
};
@@ -277,3 +277,58 @@ async fn test_migrate_from_0_1_0() {
277277
// because it was present in the original storage PDA data.
278278
setup.verify_message(&message, treasury).await;
279279
}
280+
281+
#[tokio::test]
282+
async fn test_disallows_extra_migrate() {
283+
let mut setup = Setup::new().await;
284+
let treasury = setup.create_treasury().await;
285+
286+
let mut transaction_init_contract = Transaction::new_with_payer(
287+
&[Instruction::new_with_bytes(
288+
pyth_lazer_solana_contract::ID,
289+
&pyth_lazer_solana_contract::instruction::Initialize {
290+
top_authority: setup.payer.pubkey(),
291+
treasury,
292+
}
293+
.data(),
294+
vec![
295+
AccountMeta::new(setup.payer.pubkey(), true),
296+
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
297+
AccountMeta::new_readonly(system_program::ID, false),
298+
],
299+
)],
300+
Some(&setup.payer.pubkey()),
301+
);
302+
transaction_init_contract.sign(&[&setup.payer], setup.recent_blockhash);
303+
setup
304+
.banks_client
305+
.process_transaction(transaction_init_contract)
306+
.await
307+
.unwrap();
308+
309+
let mut transaction_migrate_contract = Transaction::new_with_payer(
310+
&[Instruction::new_with_bytes(
311+
pyth_lazer_solana_contract::ID,
312+
&pyth_lazer_solana_contract::instruction::MigrateFrom010 { treasury }.data(),
313+
vec![
314+
AccountMeta::new(setup.payer.pubkey(), true),
315+
AccountMeta::new(pyth_lazer_solana_contract::STORAGE_ID, false),
316+
AccountMeta::new_readonly(system_program::ID, false),
317+
],
318+
)],
319+
Some(&setup.payer.pubkey()),
320+
);
321+
transaction_migrate_contract.sign(&[&setup.payer], setup.recent_blockhash);
322+
let err = setup
323+
.banks_client
324+
.process_transaction(transaction_migrate_contract)
325+
.await
326+
.unwrap_err();
327+
assert!(matches!(
328+
err,
329+
BanksClientError::TransactionError(TransactionError::InstructionError(
330+
0,
331+
InstructionError::InvalidAccountData
332+
))
333+
));
334+
}

0 commit comments

Comments
 (0)