-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix/v2 metadata deserialize cursor #4839
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
base: anchor-next
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ use { | |
| require, AccountDeserialize, AnchorAccount, CpiContext, CpiHandle, CpiHandleMut, Id, | ||
| IdlAccountType, Result, ToCpiAccounts, | ||
| }, | ||
| borsh::BorshDeserialize, | ||
| core::ops::Deref, | ||
| pinocchio::account::AccountView, | ||
| solana_address::Address, | ||
|
|
@@ -715,9 +716,74 @@ pub struct MetadataAccount { | |
|
|
||
| impl MetadataAccount { | ||
| #[inline] | ||
| fn parse(data: &[u8]) -> Result<mpl_token_metadata::accounts::Metadata> { | ||
| mpl_token_metadata::accounts::Metadata::safe_deserialize(data) | ||
| .map_err(|_| ProgramError::InvalidAccountData) | ||
| fn parse(buf: &mut &[u8]) -> Result<mpl_token_metadata::accounts::Metadata> { | ||
| use mpl_token_metadata::types::{ | ||
| Collection, CollectionDetails, Data, Key, ProgrammableConfig, TokenStandard, Uses, | ||
| }; | ||
|
|
||
| if buf.is_empty() || buf[0] != Key::MetadataV1 as u8 { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| let key: Key = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let update_authority: Pubkey = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let mint: Pubkey = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let data: Data = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let primary_sale_happened: bool = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let is_mutable: bool = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let edition_nonce: Option<u8> = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
|
|
||
| let token_standard_res: core::result::Result<Option<TokenStandard>, borsh::io::Error> = | ||
| BorshDeserialize::deserialize(buf); | ||
| let collection_res: core::result::Result<Option<Collection>, borsh::io::Error> = | ||
| BorshDeserialize::deserialize(buf); | ||
| let uses_res: core::result::Result<Option<Uses>, borsh::io::Error> = | ||
| BorshDeserialize::deserialize(buf); | ||
| let collection_details_res: core::result::Result< | ||
| Option<CollectionDetails>, | ||
| borsh::io::Error, | ||
| > = BorshDeserialize::deserialize(buf); | ||
| let programmable_config_res: core::result::Result< | ||
| Option<ProgrammableConfig>, | ||
| borsh::io::Error, | ||
| > = BorshDeserialize::deserialize(buf); | ||
|
|
||
| let (token_standard, collection, uses) = | ||
| match (token_standard_res, collection_res, uses_res) { | ||
| (Ok(token_standard), Ok(collection), Ok(uses)) => { | ||
| (token_standard, collection, uses) | ||
| } | ||
| _ => (None, None, None), | ||
| }; | ||
|
Comment on lines
+758
to
+764
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The custom let (token_standard, collection, uses) =
match (token_standard_res, collection_res, uses_res) {
(Ok(token_standard), Ok(collection), Ok(uses)) => {
(token_standard, collection, uses)
}
_ => (None, None, None),
};If any of these three fields fails to deserialize (for example, if an older metadata account is shorter and does not contain the This causes successfully deserialized Attack Scenario & Impact
Steps to ReproduceFix with AITriage: Reply
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !fp The grouped fallback in |
||
|
|
||
| let collection_details = collection_details_res.unwrap_or(None); | ||
| let programmable_config = programmable_config_res.unwrap_or(None); | ||
|
|
||
| Ok(mpl_token_metadata::accounts::Metadata { | ||
| key, | ||
| update_authority, | ||
| mint, | ||
| name: data.name, | ||
| symbol: data.symbol, | ||
| uri: data.uri, | ||
| seller_fee_basis_points: data.seller_fee_basis_points, | ||
| creators: data.creators, | ||
| primary_sale_happened, | ||
| is_mutable, | ||
| edition_nonce, | ||
| token_standard, | ||
| collection, | ||
| uses, | ||
| collection_details, | ||
| programmable_config, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -738,7 +804,7 @@ impl AnchorAccount for MetadataAccount { | |
| fn load(view: AccountView) -> Result<Self> { | ||
| require!(view.owned_by(&ID), ProgramError::IllegalOwner); | ||
| let data_ref = view.try_borrow()?; | ||
| let data = Self::parse(&data_ref)?; | ||
| let data = Self::parse(&mut &data_ref[..])?; | ||
| drop(data_ref); | ||
| Ok(Self { | ||
| view: Some(view), | ||
|
|
@@ -771,8 +837,12 @@ pub struct MasterEditionAccount { | |
|
|
||
| impl MasterEditionAccount { | ||
| #[inline] | ||
| fn parse(data: &[u8]) -> Result<mpl_token_metadata::accounts::MasterEdition> { | ||
| mpl_token_metadata::accounts::MasterEdition::safe_deserialize(data) | ||
| fn parse(buf: &mut &[u8]) -> Result<mpl_token_metadata::accounts::MasterEdition> { | ||
| if buf.is_empty() || buf[0] != mpl_token_metadata::types::Key::MasterEditionV2 as u8 { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| mpl_token_metadata::accounts::MasterEdition::deserialize(buf) | ||
| .map_err(|_| ProgramError::InvalidAccountData) | ||
| } | ||
| } | ||
|
|
@@ -794,7 +864,7 @@ impl AnchorAccount for MasterEditionAccount { | |
| fn load(view: AccountView) -> Result<Self> { | ||
| require!(view.owned_by(&ID), ProgramError::IllegalOwner); | ||
| let data_ref = view.try_borrow()?; | ||
| let data = Self::parse(&data_ref)?; | ||
| let data = Self::parse(&mut &data_ref[..])?; | ||
| drop(data_ref); | ||
| Ok(Self { | ||
| view: Some(view), | ||
|
|
@@ -829,9 +899,43 @@ impl TokenRecordAccount { | |
| pub const LEN: usize = mpl_token_metadata::accounts::TokenRecord::LEN; | ||
|
|
||
| #[inline] | ||
| fn parse(data: &[u8]) -> Result<mpl_token_metadata::accounts::TokenRecord> { | ||
| mpl_token_metadata::accounts::TokenRecord::safe_deserialize(data) | ||
| .map_err(|_| ProgramError::InvalidAccountData) | ||
| fn parse(buf: &mut &[u8]) -> Result<mpl_token_metadata::accounts::TokenRecord> { | ||
| const LOCKED_TRANSFER_SIZE: i64 = 33; | ||
|
|
||
| let length = Self::LEN as i64 - buf.len() as i64; | ||
| if !(length == 0 || length == LOCKED_TRANSFER_SIZE) | ||
| || buf.first().copied() != Some(mpl_token_metadata::types::Key::TokenRecord as u8) | ||
| { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| let key = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let bump = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let state = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let rule_set_revision = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let delegate = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let delegate_role = | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)?; | ||
| let locked_transfer = if length == 0 { | ||
| BorshDeserialize::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)? | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| Ok(mpl_token_metadata::accounts::TokenRecord { | ||
| key, | ||
| bump, | ||
| state, | ||
| rule_set_revision, | ||
| delegate, | ||
| delegate_role, | ||
| locked_transfer, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -852,7 +956,7 @@ impl AnchorAccount for TokenRecordAccount { | |
| fn load(view: AccountView) -> Result<Self> { | ||
| require!(view.owned_by(&ID), ProgramError::IllegalOwner); | ||
| let data_ref = view.try_borrow()?; | ||
| let data = Self::parse(&data_ref)?; | ||
| let data = Self::parse(&mut &data_ref[..])?; | ||
| drop(data_ref); | ||
| Ok(Self { | ||
| view: Some(view), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit — document the on-chain order in here while deserializing.