Skip to content

Commit

Permalink
policy hash + patches (#38)
Browse files Browse the repository at this point in the history
* generic security fixes

* use hash to identify policies (#39)

* use hash to identify policies

* use policy account in hash
  • Loading branch information
Bhargavamacha authored Apr 10, 2024
1 parent 4d2d276 commit 6f59119
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 22 deletions.
114 changes: 114 additions & 0 deletions programs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions programs/asset_controller/src/instructions/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::state::*;
#[instruction(amount: u64)]
pub struct ExecuteTransferHook<'info> {
#[account(
token::mint = asset_mint,
token::authority = owner_delegate,
token::token_program = anchor_spl::token_interface::spl_token_2022::id(),
associated_token::token_program = anchor_spl::token_interface::spl_token_2022::id(),
associated_token::authority = owner_delegate,
associated_token::mint = asset_mint,
)]
pub source_account: Box<InterfaceAccount<'info, TokenAccount>>,
#[account(
Expand All @@ -24,8 +24,10 @@ pub struct ExecuteTransferHook<'info> {
token::mint = asset_mint,
token::token_program = anchor_spl::token_interface::spl_token_2022::id(),
)]
// can be any token account, user must make sure it is an associated token account with relevant identity permissions
pub destination_account: Box<InterfaceAccount<'info, TokenAccount>>,
pub owner_delegate: SystemAccount<'info>,
/// CHECK: can be any account
pub owner_delegate: UncheckedAccount<'info>,
/// CHECK: meta list account
#[account(
seeds = [META_LIST_ACCOUNT_SEED, asset_mint.key().as_ref()],
Expand Down
2 changes: 2 additions & 0 deletions programs/data_registry/src/instructions/account/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct CreateDataAccount<'info> {
signer,
space = 8 + DataAccount::INIT_SPACE,
payer = payer,
constraint = args.name.len() <= MAX_NAME_LEN,
constraint = args.uri.len() <= MAX_URI_LEN,
)]
pub data_account: Box<Account<'info, DataAccount>>,
pub system_program: Program<'info, System>,
Expand Down
3 changes: 1 addition & 2 deletions programs/data_registry/src/instructions/registry/delegate.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token_interface::Mint;

#[derive(Accounts)]
#[instruction()]
pub struct DelegateDataRegistry<'info> {
#[account(
constraint = authority.key() == data_registry.authority,
)]
pub authority: Box<InterfaceAccount<'info, Mint>>,
pub authority: Signer<'info>,
#[account(mut)]
pub data_registry: Box<Account<'info, DataRegistryAccount>>,
}
Expand Down
9 changes: 5 additions & 4 deletions programs/data_registry/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use anchor_lang::prelude::*;

use crate::DataRegistryErrors;

#[account()]
#[derive(InitSpace)]
pub struct DataRegistryAccount {
Expand Down Expand Up @@ -42,6 +40,9 @@ pub enum DataAccountType {
Miscellaneous,
}

pub const MAX_NAME_LEN: usize = 32;
pub const MAX_URI_LEN: usize = 255;

#[account()]
#[derive(InitSpace)]
pub struct DataAccount {
Expand All @@ -51,10 +52,10 @@ pub struct DataAccount {
/// type of the data account
pub _type: DataAccountType,
/// used by creator to store name of the document
#[max_len(32)]
#[max_len(MAX_NAME_LEN)]
pub name: String,
/// uri pointing to the data stored in the document
#[max_len(255)]
#[max_len(MAX_URI_LEN)]
pub uri: String,
}

Expand Down
1 change: 1 addition & 0 deletions programs/policy_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ default = []
anchor-lang = { git = "https://[email protected]/bridgesplit/anchor" }
anchor-spl = { git = "https://[email protected]/bridgesplit/anchor" }
num_enum = "0.7.2"
sha256 = "1.5.0"
2 changes: 2 additions & 0 deletions programs/policy_engine/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ pub enum PolicyEngineErrors {
IdentityFilterFailed,
#[msg("Unauthorized signer")]
UnauthorizedSigner,
#[msg("Policy already exists")]
PolicyAlreadyExists,
}
3 changes: 2 additions & 1 deletion programs/policy_engine/src/instructions/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ pub fn handler(
identity_filter: IdentityFilter,
policy_type: PolicyType,
) -> Result<()> {
let policy_account_address = ctx.accounts.policy_account.key();
ctx.accounts
.policy_account
.attach(policy_type, identity_filter);
.attach(policy_account_address, policy_type, identity_filter)?;
ctx.accounts.policy_engine.update_max_timeframe(policy_type);
Ok(())
}
2 changes: 2 additions & 0 deletions programs/policy_engine/src/instructions/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub fn handler(
identity_filter: IdentityFilter,
policy_type: PolicyType,
) -> Result<()> {
let policy_account_address = ctx.accounts.policy_account.key();
ctx.accounts.policy_account.new(
policy_account_address,
ctx.accounts.policy_engine.key(),
identity_filter,
policy_type,
Expand Down
4 changes: 2 additions & 2 deletions programs/policy_engine/src/instructions/detach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct DetachFromPolicyAccount<'info> {
pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<DetachFromPolicyAccount>, policy_type: PolicyType) -> Result<()> {
ctx.accounts.policy_account.detach(policy_type);
pub fn handler(ctx: Context<DetachFromPolicyAccount>, hash: String) -> Result<()> {
let policy_type = ctx.accounts.policy_account.detach(hash)?;
// update max timeframe if detached policy was the max timeframe

let mut max_timeframe = match policy_type {
Expand Down
4 changes: 2 additions & 2 deletions programs/policy_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub mod policy_engine {
/// remove policy
pub fn detach_from_policy_account(
ctx: Context<DetachFromPolicyAccount>,
policy_type: PolicyType,
hash: String,
) -> Result<()> {
instructions::detach::handler(ctx, policy_type)
instructions::detach::handler(ctx, hash)
}
}
Loading

0 comments on commit 6f59119

Please sign in to comment.