Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add event limits checks (#1250)
Browse files Browse the repository at this point in the history
* Add event limits constants

* Check event limits in BusinessLogicSyscallHandler

* Check event limits in NativeSyscallHandler

* fmt
  • Loading branch information
fmoletta authored Mar 15, 2024
1 parent b756567 commit d011ebe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/definitions/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ lazy_static! {
pub(crate) static ref QUERY_VERSION_2: Felt252 = Into::<Felt252>::into(2) + *QUERY_VERSION_BASE;
pub(crate) static ref QUERY_VERSION_3: Felt252 = Into::<Felt252>::into(3) + *QUERY_VERSION_BASE;
}

// Event Limits
pub(crate) const EVENT_MAX_DATA_LENGTH: usize = 300;
pub(crate) const EVENT_MAX_KEYS_LENGTH: usize = 50;
pub(crate) const MAX_N_EMITTED_EVENTS: u64 = 1000;
23 changes: 22 additions & 1 deletion src/syscalls/business_logic_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::{
core::errors::state_errors::StateError,
definitions::{
block_context::BlockContext,
constants::{BLOCK_HASH_CONTRACT_ADDRESS, CONSTRUCTOR_ENTRY_POINT_SELECTOR},
constants::{
BLOCK_HASH_CONTRACT_ADDRESS, CONSTRUCTOR_ENTRY_POINT_SELECTOR, EVENT_MAX_DATA_LENGTH,
EVENT_MAX_KEYS_LENGTH, MAX_N_EMITTED_EVENTS,
},
},
execution::{
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
Expand Down Expand Up @@ -654,6 +657,24 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
let order = self.tx_execution_context.n_emitted_events;
let keys: Vec<Felt252> = get_felt_range(vm, request.keys_start, request.keys_end)?;
let data: Vec<Felt252> = get_felt_range(vm, request.data_start, request.data_end)?;
// Check event limits
if order >= MAX_N_EMITTED_EVENTS {
return Err(SyscallHandlerError::MaxNumberOfEmittedEventsExceeded(
MAX_N_EMITTED_EVENTS,
));
}
if keys.len() > EVENT_MAX_KEYS_LENGTH {
return Err(SyscallHandlerError::EventMaxKeysLengthExceeded(
keys.len(),
EVENT_MAX_KEYS_LENGTH,
));
}
if data.len() > EVENT_MAX_DATA_LENGTH {
return Err(SyscallHandlerError::EventMaxKeysLengthExceeded(
data.len(),
EVENT_MAX_DATA_LENGTH,
));
}
self.events.push(OrderedEvent::new(order, keys, data));

// Update events count.
Expand Down
24 changes: 23 additions & 1 deletion src/syscalls/native_syscall_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::{
core::errors::state_errors::StateError,
definitions::{block_context::BlockContext, constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR},
definitions::{
block_context::BlockContext,
constants::{
CONSTRUCTOR_ENTRY_POINT_SELECTOR, EVENT_MAX_DATA_LENGTH, EVENT_MAX_KEYS_LENGTH,
MAX_N_EMITTED_EVENTS,
},
},
execution::{
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
CallInfo, CallResult, CallType, OrderedEvent, OrderedL2ToL1Message,
Expand Down Expand Up @@ -391,6 +397,22 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler
) -> SyscallResult<()> {
let order = self.tx_execution_context.n_emitted_events;
tracing::debug!("Called `emit_event(KEYS: {keys:?}, DATA: {data:?})` from Cairo Native");
// Check event limits
if order >= MAX_N_EMITTED_EVENTS {
return Err(vec![Felt252::from_bytes_be_slice(
"Max number of emitted events reached".as_bytes(),
)]);
}
if keys.len() > EVENT_MAX_KEYS_LENGTH {
return Err(vec![Felt252::from_bytes_be_slice(
"Event max keys length exceeded".as_bytes(),
)]);
}
if data.len() > EVENT_MAX_DATA_LENGTH {
return Err(vec![Felt252::from_bytes_be_slice(
"Event data keys length exceeded".as_bytes(),
)]);
}

self.handle_syscall_request(gas, "emit_event")?;

Expand Down
6 changes: 6 additions & 0 deletions src/syscalls/syscall_handler_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ pub enum SyscallHandlerError {
UnsupportedAddressDomain(String),
#[error("{0:?}")]
CustomError(String),
#[error("Event exceeded the maximum keys length, keys length: {0}, max keys length: {1}.")]
EventMaxKeysLengthExceeded(usize, usize),
#[error("Event exceeded the maximum data length, data length: {0}, max data length: {1}.")]
EventMaxDataLengthExceeded(usize, usize),
#[error("Maximum number of events reached: {0}, can't emit another event.")]
MaxNumberOfEmittedEventsExceeded(u64),
}

0 comments on commit d011ebe

Please sign in to comment.