From 750abe97327db83262ac6b1f791e4dab3842c09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 21 Sep 2022 14:26:18 +0200 Subject: [PATCH] pallet-utility: Disallow none origin --- frame/utility/src/lib.rs | 6 +++--- frame/utility/src/tests.rs | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index bc19b5625a71a..819314f3d8454 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -203,7 +203,7 @@ pub mod pallet { origin: OriginFor, calls: Vec<::RuntimeCall>, ) -> DispatchResultWithPostInfo { - let is_root = ensure_root(origin.clone()).is_ok(); + let is_root = ensure_signed_or_root(origin.clone())?.is_none(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); @@ -319,7 +319,7 @@ pub mod pallet { origin: OriginFor, calls: Vec<::RuntimeCall>, ) -> DispatchResultWithPostInfo { - let is_root = ensure_root(origin.clone()).is_ok(); + let is_root = ensure_signed_or_root(origin.clone())?.is_none(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); @@ -426,7 +426,7 @@ pub mod pallet { origin: OriginFor, calls: Vec<::RuntimeCall>, ) -> DispatchResultWithPostInfo { - let is_root = ensure_root(origin.clone()).is_ok(); + let is_root = ensure_signed_or_root(origin.clone())?.is_none(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index 65d9c966ebdfd..ebd8dda81adbc 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -25,6 +25,7 @@ use crate as utility; use frame_support::{ assert_err_ignore_postinfo, assert_noop, assert_ok, dispatch::{DispatchError, DispatchErrorWithPostInfo, Dispatchable, Pays}, + error::BadOrigin, parameter_types, storage, traits::{ConstU32, ConstU64, Contains}, weights::Weight, @@ -651,7 +652,7 @@ fn force_batch_works() { call_transfer(2, 10), call_transfer(2, 5), ] - ),); + )); System::assert_last_event(utility::Event::BatchCompletedWithErrors.into()); System::assert_has_event( utility::Event::ItemFailed { error: DispatchError::Other("") }.into(), @@ -662,10 +663,19 @@ fn force_batch_works() { assert_ok!(Utility::force_batch( RuntimeOrigin::signed(2), vec![call_transfer(1, 5), call_transfer(1, 5),] - ),); + )); System::assert_last_event(utility::Event::BatchCompleted.into()); assert_ok!(Utility::force_batch(RuntimeOrigin::signed(1), vec![call_transfer(2, 50),]),); System::assert_last_event(utility::Event::BatchCompletedWithErrors.into()); }); } + +#[test] +fn none_origin_does_not_work() { + new_test_ext().execute_with(|| { + assert_noop!(Utility::force_batch(RuntimeOrigin::none(), vec![]), BadOrigin); + assert_noop!(Utility::batch(RuntimeOrigin::none(), vec![]), BadOrigin); + assert_noop!(Utility::batch_all(RuntimeOrigin::none(), vec![]), BadOrigin); + }) +}