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

Commit d011ebe

Browse files
authored
Add event limits checks (#1250)
* Add event limits constants * Check event limits in BusinessLogicSyscallHandler * Check event limits in NativeSyscallHandler * fmt
1 parent b756567 commit d011ebe

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/definitions/constants.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ lazy_static! {
111111
pub(crate) static ref QUERY_VERSION_2: Felt252 = Into::<Felt252>::into(2) + *QUERY_VERSION_BASE;
112112
pub(crate) static ref QUERY_VERSION_3: Felt252 = Into::<Felt252>::into(3) + *QUERY_VERSION_BASE;
113113
}
114+
115+
// Event Limits
116+
pub(crate) const EVENT_MAX_DATA_LENGTH: usize = 300;
117+
pub(crate) const EVENT_MAX_KEYS_LENGTH: usize = 50;
118+
pub(crate) const MAX_N_EMITTED_EVENTS: u64 = 1000;

src/syscalls/business_logic_syscall_handler.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515
core::errors::state_errors::StateError,
1616
definitions::{
1717
block_context::BlockContext,
18-
constants::{BLOCK_HASH_CONTRACT_ADDRESS, CONSTRUCTOR_ENTRY_POINT_SELECTOR},
18+
constants::{
19+
BLOCK_HASH_CONTRACT_ADDRESS, CONSTRUCTOR_ENTRY_POINT_SELECTOR, EVENT_MAX_DATA_LENGTH,
20+
EVENT_MAX_KEYS_LENGTH, MAX_N_EMITTED_EVENTS,
21+
},
1922
},
2023
execution::{
2124
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
@@ -654,6 +657,24 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
654657
let order = self.tx_execution_context.n_emitted_events;
655658
let keys: Vec<Felt252> = get_felt_range(vm, request.keys_start, request.keys_end)?;
656659
let data: Vec<Felt252> = get_felt_range(vm, request.data_start, request.data_end)?;
660+
// Check event limits
661+
if order >= MAX_N_EMITTED_EVENTS {
662+
return Err(SyscallHandlerError::MaxNumberOfEmittedEventsExceeded(
663+
MAX_N_EMITTED_EVENTS,
664+
));
665+
}
666+
if keys.len() > EVENT_MAX_KEYS_LENGTH {
667+
return Err(SyscallHandlerError::EventMaxKeysLengthExceeded(
668+
keys.len(),
669+
EVENT_MAX_KEYS_LENGTH,
670+
));
671+
}
672+
if data.len() > EVENT_MAX_DATA_LENGTH {
673+
return Err(SyscallHandlerError::EventMaxKeysLengthExceeded(
674+
data.len(),
675+
EVENT_MAX_DATA_LENGTH,
676+
));
677+
}
657678
self.events.push(OrderedEvent::new(order, keys, data));
658679

659680
// Update events count.

src/syscalls/native_syscall_handler.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use crate::{
22
core::errors::state_errors::StateError,
3-
definitions::{block_context::BlockContext, constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR},
3+
definitions::{
4+
block_context::BlockContext,
5+
constants::{
6+
CONSTRUCTOR_ENTRY_POINT_SELECTOR, EVENT_MAX_DATA_LENGTH, EVENT_MAX_KEYS_LENGTH,
7+
MAX_N_EMITTED_EVENTS,
8+
},
9+
},
410
execution::{
511
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
612
CallInfo, CallResult, CallType, OrderedEvent, OrderedL2ToL1Message,
@@ -391,6 +397,22 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler
391397
) -> SyscallResult<()> {
392398
let order = self.tx_execution_context.n_emitted_events;
393399
tracing::debug!("Called `emit_event(KEYS: {keys:?}, DATA: {data:?})` from Cairo Native");
400+
// Check event limits
401+
if order >= MAX_N_EMITTED_EVENTS {
402+
return Err(vec![Felt252::from_bytes_be_slice(
403+
"Max number of emitted events reached".as_bytes(),
404+
)]);
405+
}
406+
if keys.len() > EVENT_MAX_KEYS_LENGTH {
407+
return Err(vec![Felt252::from_bytes_be_slice(
408+
"Event max keys length exceeded".as_bytes(),
409+
)]);
410+
}
411+
if data.len() > EVENT_MAX_DATA_LENGTH {
412+
return Err(vec![Felt252::from_bytes_be_slice(
413+
"Event data keys length exceeded".as_bytes(),
414+
)]);
415+
}
394416

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

src/syscalls/syscall_handler_errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,10 @@ pub enum SyscallHandlerError {
7373
UnsupportedAddressDomain(String),
7474
#[error("{0:?}")]
7575
CustomError(String),
76+
#[error("Event exceeded the maximum keys length, keys length: {0}, max keys length: {1}.")]
77+
EventMaxKeysLengthExceeded(usize, usize),
78+
#[error("Event exceeded the maximum data length, data length: {0}, max data length: {1}.")]
79+
EventMaxDataLengthExceeded(usize, usize),
80+
#[error("Maximum number of events reached: {0}, can't emit another event.")]
81+
MaxNumberOfEmittedEventsExceeded(u64),
7682
}

0 commit comments

Comments
 (0)