Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(p2p,rpc): limit Cairo 0 class definition size #2475

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Use aggregate Bloom filters for `starknet_getEvents` to improve performance.
- Cairo 0 class definition size is now capped at 4 MiB.

## [0.15.2] - 2024-12-04

Expand Down
2 changes: 2 additions & 0 deletions crates/common/src/class_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde_with::serde_as;

use crate::{ByteCodeOffset, EntryPoint};

pub const CLASS_DEFINITION_MAX_ALLOWED_SIZE: u64 = 4 * 1024 * 1024;

#[derive(Debug, Deserialize, Dummy)]
pub enum ClassDefinition<'a> {
Sierra(Sierra<'a>),
Expand Down
4 changes: 2 additions & 2 deletions crates/p2p/src/client/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,10 @@ impl TryFromDto<p2p_proto::class::Cairo0Class> for CairoDefinition {
let abi = dto.abi;

let compressed_program = base64::decode(dto.program)?;
let mut gzip_decoder =
flate2::read::GzDecoder::new(std::io::Cursor::new(compressed_program));
let gzip_decoder = flate2::read::GzDecoder::new(std::io::Cursor::new(compressed_program));
let mut program = Vec::new();
gzip_decoder
.take(pathfinder_common::class_definition::CLASS_DEFINITION_MAX_ALLOWED_SIZE)
.read_to_end(&mut program)
.context("Decompressing program JSON")?;

Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ impl CairoContractClass {

pub fn serialize_to_json(&self) -> anyhow::Result<Vec<u8>> {
// decode program
let mut decompressor =
let decompressor =
flate2::read::GzDecoder::new(Cursor::new(base64::decode(&self.program).unwrap()));
let mut program = Vec::new();
decompressor
.take(pathfinder_common::class_definition::CLASS_DEFINITION_MAX_ALLOWED_SIZE)
.read_to_end(&mut program)
.context("Decompressing program")?;

Expand Down
Loading