Skip to content

Commit 37de4d9

Browse files
Merge pull request #41 from hashed-io/feature/benchmark/ticket/29
Out of bounds marketplace application
2 parents f90a8a9 + ae9340d commit 37de4d9

File tree

4 files changed

+112
-16
lines changed

4 files changed

+112
-16
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<T: Config> Pallet<T> {
135135
// ensure the origin is owner or admin
136136
Self::is_authorized(authority.clone(), &marketplace_id, Permission::Enroll)?;
137137

138-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
138+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
139139

140140
let application = Application::<T> {
141141
status: ApplicationStatus::default(),
@@ -794,11 +794,12 @@ impl<T: Config> Pallet<T> {
794794
}
795795

796796
/* ---- Helper functions ---- */
797-
798797
pub fn set_up_application(
799798
fields: Fields<T>,
800799
custodian_fields: Option<CustodianFields<T>>,
801-
) -> (Option<T::AccountId>, BoundedVec<ApplicationField, T::MaxFiles>) {
800+
) -> Result<(Option<T::AccountId>, BoundedVec<ApplicationField, T::MaxFiles>), DispatchError> {
801+
ensure!(!fields.is_empty(), Error::<T>::FieldsNotProvided);
802+
802803
let mut f: Vec<ApplicationField> = fields
803804
.iter()
804805
.map(|tuple| ApplicationField {
@@ -807,17 +808,23 @@ impl<T: Config> Pallet<T> {
807808
custodian_cid: None,
808809
})
809810
.collect();
811+
810812
let custodian = match custodian_fields {
811813
Some(c_fields) => {
814+
if fields.len() != c_fields.1.len() {
815+
return Err(Error::<T>::InsufficientCustodianFields.into())
816+
}
812817
for (i, field) in f.iter_mut().enumerate() {
813818
field.custodian_cid = Some(c_fields.1[i].clone());
814819
}
815-
816820
Some(c_fields.0)
817821
},
818822
_ => None,
819823
};
820-
(custodian, BoundedVec::<ApplicationField, T::MaxFiles>::try_from(f).unwrap_or_default())
824+
825+
let fields = BoundedVec::<ApplicationField, T::MaxFiles>::try_from(f).
826+
map_err(|_| Error::<T>::ExceedMaxFilesApplication)?;
827+
Ok((custodian, fields))
821828
}
822829

823830
fn insert_in_auth_market_lists(

pallets/gated-marketplace/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ pub mod pallet {
338338
OwnerNotInMarketplace,
339339
/// MappedAssetId not found
340340
AssetNotFound,
341+
/// Not enough custodian fields provided
342+
InsufficientCustodianFields,
343+
/// Fields not provided for the application
344+
FieldsNotProvided,
345+
/// Exceeds the maximum number of files
346+
ExceedMaxFilesApplication,
341347
}
342348

343349
#[pallet::call]
@@ -435,7 +441,7 @@ pub mod pallet {
435441
) -> DispatchResult {
436442
let who = ensure_signed(origin)?;
437443

438-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
444+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
439445

440446
let application = Application::<T> {
441447
status: ApplicationStatus::default(),
@@ -473,7 +479,7 @@ pub mod pallet {
473479
) -> DispatchResult {
474480
let who = ensure_signed(origin)?;
475481

476-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
482+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
477483

478484
let application = Application::<T> {
479485
status: ApplicationStatus::default(),

pallets/gated-marketplace/src/mock.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate as pallet_gated_marketplace;
2-
use frame_system::RawOrigin;
32
use sp_runtime::traits::Lookup;
43

54
use frame_support::{
@@ -22,6 +21,7 @@ use sp_runtime::{
2221
use system::EnsureSigned;
2322
type AccountId = u64;
2423
type AssetId = u32;
24+
2525
// Configure a mock runtime to test the pallet.
2626
frame_support::construct_runtime!(
2727
pub enum Test
@@ -163,24 +163,25 @@ impl pallet_balances::Config for Test {
163163
}
164164

165165
parameter_types! {
166-
pub const MaxScopesPerPallet: u32 = 2;
167-
pub const MaxRolesPerPallet: u32 = 6;
168-
pub const RoleMaxLen: u32 = 25;
169-
pub const PermissionMaxLen: u32 = 25;
170-
pub const MaxPermissionsPerRole: u32 = 30;
171-
pub const MaxRolesPerUser: u32 = 2;
172-
pub const MaxUsersPerRole: u32 = 2;
166+
pub const MaxScopesPerPallet: u32 = 2;
167+
pub const MaxRolesPerPallet: u32 = 6;
168+
pub const RoleMaxLen: u32 = 25;
169+
pub const PermissionMaxLen: u32 = 25;
170+
pub const MaxPermissionsPerRole: u32 = 30;
171+
pub const MaxRolesPerUser: u32 = 2;
172+
pub const MaxUsersPerRole: u32 = 2;
173173
}
174174
impl pallet_rbac::Config for Test {
175175
type RuntimeEvent = RuntimeEvent;
176+
type RemoveOrigin = EnsureRoot<Self::AccountId>;
176177
type MaxScopesPerPallet = MaxScopesPerPallet;
177178
type MaxRolesPerPallet = MaxRolesPerPallet;
178179
type RoleMaxLen = RoleMaxLen;
179180
type PermissionMaxLen = PermissionMaxLen;
180181
type MaxPermissionsPerRole = MaxPermissionsPerRole;
181182
type MaxRolesPerUser = MaxRolesPerUser;
182183
type MaxUsersPerRole = MaxUsersPerRole;
183-
type RemoveOrigin = EnsureRoot<Self::AccountId>;
184+
type WeightInfo = ();
184185
}
185186

186187
// Build genesis storage according to the mock runtime.

pallets/gated-marketplace/src/tests.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,33 @@ fn apply_to_marketplace_works() {
244244
});
245245
}
246246

247+
#[test]
248+
fn apply_to_marketplace_without_fields_shouldnt_work() {
249+
new_test_ext().execute_with(|| {
250+
Balances::make_free_balance_be(&1, 100);
251+
// Dispatch a signed extrinsic.
252+
let m_label = create_label("my marketplace");
253+
assert_ok!(GatedMarketplace::create_marketplace(
254+
RuntimeOrigin::signed(1),
255+
2,
256+
m_label.clone(),
257+
500,
258+
600,
259+
1,
260+
));
261+
let m_id = get_marketplace_id("my marketplace", 500, 600, 1);
262+
assert_noop!(
263+
GatedMarketplace::apply(
264+
RuntimeOrigin::signed(3),
265+
m_id,
266+
create_application_fields(0),
267+
None
268+
),
269+
Error::<Test>::FieldsNotProvided
270+
);
271+
});
272+
}
273+
247274
#[test]
248275
fn apply_with_custodian_works() {
249276
new_test_ext().execute_with(|| {
@@ -273,6 +300,60 @@ fn apply_with_custodian_works() {
273300
});
274301
}
275302

303+
#[test]
304+
fn apply_with_mismatched_number_of_fields_and_custodian_fields_shouldnt_work() {
305+
new_test_ext().execute_with(|| {
306+
Balances::make_free_balance_be(&1, 100);
307+
// Dispatch a signed extrinsic.
308+
let m_label = create_label("my marketplace");
309+
assert_ok!(GatedMarketplace::create_marketplace(
310+
RuntimeOrigin::signed(1),
311+
2,
312+
m_label.clone(),
313+
500,
314+
600,
315+
1,
316+
));
317+
let m_id = get_marketplace_id("my marketplace", 500, 600, 1);
318+
assert_noop!(
319+
GatedMarketplace::apply(
320+
RuntimeOrigin::signed(3),
321+
m_id,
322+
create_application_fields(2),
323+
create_custodian_fields(4, 1)
324+
),
325+
Error::<Test>::InsufficientCustodianFields
326+
);
327+
});
328+
}
329+
330+
#[test]
331+
fn apply_with_custodian_but_no_custodian_fields_shouldnt_work() {
332+
new_test_ext().execute_with(|| {
333+
Balances::make_free_balance_be(&1, 100);
334+
// Dispatch a signed extrinsic.
335+
let m_label = create_label("my marketplace");
336+
assert_ok!(GatedMarketplace::create_marketplace(
337+
RuntimeOrigin::signed(1),
338+
2,
339+
m_label.clone(),
340+
500,
341+
600,
342+
1,
343+
));
344+
let m_id = get_marketplace_id("my marketplace", 500, 600, 1);
345+
assert_noop!(
346+
GatedMarketplace::apply(
347+
RuntimeOrigin::signed(3),
348+
m_id,
349+
create_application_fields(2),
350+
create_custodian_fields(4, 0)
351+
),
352+
Error::<Test>::InsufficientCustodianFields
353+
);
354+
});
355+
}
356+
276357
#[test]
277358
fn apply_with_same_account_as_custodian_shouldnt_work() {
278359
new_test_ext().execute_with(|| {
@@ -378,6 +459,7 @@ fn apply_twice_shouldnt_work() {
378459
1,
379460
));
380461
let m_id = get_marketplace_id("my marketplace", 500, 600, 1);
462+
381463
assert_ok!(GatedMarketplace::apply(
382464
RuntimeOrigin::signed(3),
383465
m_id,

0 commit comments

Comments
 (0)