Skip to content

Commit

Permalink
sdk: add max_num_tags limit
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 29, 2024
1 parent 153fcfe commit db6c87a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions crates/nostr-sdk/src/relay/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ impl Default for Limits {
messages: MessagesLimits {
max_size: 5_250_000,
},
events: EventsLimits { max_size: 70_000 },
events: EventsLimits {
max_size: 70_000,
max_num_tags: 2_000,
},
}
}
}
Expand All @@ -36,8 +39,8 @@ pub struct MessagesLimits {
pub struct EventsLimits {
/// Maximum size of normalised JSON, in bytes
pub max_size: u32,
// /// Maximum number of tags allowed
// pub max_num_tags: u16,
/// Maximum number of tags allowed
pub max_num_tags: u16,
// Maximum size for tag values, in bytes
// pub max_tag_val_size: u16,
}
24 changes: 24 additions & 0 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ pub enum Error {
/// Max event size
max_size: usize,
},
/// Too many tags
#[error("Received event with too many tags: tags={size}, max_tags={max_size}")]
TooManyTags {
/// Tags num
size: usize,
/// Max tags num
max_size: usize,
},
}

/// Relay connection status
Expand Down Expand Up @@ -795,11 +803,27 @@ impl Relay {
tracing::trace!("Received message from {}: {:?}", relay.url, msg);

if let RawRelayMessage::Event { event, .. } = &msg {
// Check event size
let size: usize = event.to_string().as_bytes().len();
let max_size: usize = relay.limits.events.max_size as usize;
if size > max_size {
return Err(Error::EventTooLarge { size, max_size });
}

// Check tags limit
if let Some(tags) = event.get("tags") {
if let Some(tags) = tags.as_array() {
let size: usize = tags.len();
let max_num_tags: usize =
relay.limits.events.max_num_tags as usize;
if size > max_num_tags {
return Err(Error::TooManyTags {
size,
max_size: max_num_tags,
});
}
}
}
}

if let Err(err) = relay
Expand Down

0 comments on commit db6c87a

Please sign in to comment.