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

feat: prevent memory alloc dos attack #13

Merged
merged 2 commits into from
Aug 29, 2023
Merged
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
15 changes: 13 additions & 2 deletions y-octo/src/doc/codec/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::{
use super::*;
use crate::doc::StateVector;

const CLIENTS_SAFE_CAPACITY: u64 = 1024;
const STRUCTS_SAFE_CAPACITY: u64 = 10240;

#[derive(Debug, Default, Clone)]
pub struct Update {
pub(crate) structs: HashMap<u64, VecDeque<Node>>,
Expand All @@ -25,13 +28,21 @@ impl<R: CrdtReader> CrdtRead<R> for Update {
fn read(decoder: &mut R) -> JwstCodecResult<Self> {
let num_of_clients = decoder.read_var_u64()?;

let mut map = HashMap::with_capacity(num_of_clients as usize);
let mut map = HashMap::with_capacity(if num_of_clients > CLIENTS_SAFE_CAPACITY {
CLIENTS_SAFE_CAPACITY
} else {
num_of_clients
} as usize);
for _ in 0..num_of_clients {
let num_of_structs = decoder.read_var_u64()?;
let client = decoder.read_var_u64()?;
let mut clock = decoder.read_var_u64()?;

let mut structs = VecDeque::with_capacity(num_of_structs as usize);
let mut structs = VecDeque::with_capacity(if num_of_structs > STRUCTS_SAFE_CAPACITY {
STRUCTS_SAFE_CAPACITY
} else {
num_of_structs
} as usize);

for _ in 0..num_of_structs {
let struct_info = Node::read(decoder, Id::new(client, clock))?;
Expand Down
Loading