Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Enforce limit on header range length in light client logs request.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Aug 1, 2018
1 parent 583ab3b commit f6fd170
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions rpc/src/v1/helpers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ pub fn request_rejected_limit() -> Error {
}
}

pub fn request_rejected_param_limit() -> Error {
Error {
code: ErrorCode::ServerError(codes::REQUEST_REJECTED_LIMIT),
message: "Request has been rejected because requested data size exceeds limit.".into(),
data: None,
}
}

pub fn account<T: fmt::Debug>(error: &str, details: T) -> Error {
Error {
code: ErrorCode::ServerError(codes::ACCOUNT_ERROR),
Expand Down
13 changes: 11 additions & 2 deletions rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ impl LightFetch {
use std::collections::BTreeMap;
use jsonrpc_core::futures::stream::{self, Stream};

const MAX_BLOCK_RANGE: u64 = 1000;

let fetcher: Self = self.clone();
self.headers_range_by_block_id(filter.from_block, filter.to_block)
self.headers_range_by_block_id(filter.from_block, filter.to_block, MAX_BLOCK_RANGE)
.and_then(move |mut headers| {
if headers.is_empty() {
return Either::A(future::ok(Vec::new()));
Expand Down Expand Up @@ -434,7 +436,12 @@ impl LightFetch {
}
}

fn headers_range_by_block_id(&self, from_block: BlockId, to_block: BlockId) -> impl Future<Item = Vec<encoded::Header>, Error = Error> {
fn headers_range_by_block_id(
&self,
from_block: BlockId,
to_block: BlockId,
max: u64
) -> impl Future<Item = Vec<encoded::Header>, Error = Error> {
let fetch_hashes = [from_block, to_block].iter()
.filter_map(|block_id| match block_id {
BlockId::Hash(hash) => Some(hash.clone()),
Expand All @@ -461,6 +468,8 @@ impl LightFetch {
if to_block_num < from_block_num {
// early exit for "to" block before "from" block.
return Either::A(future::ok(Vec::new()));
} else if to_block_num - from_block_num >= max {
return Either::A(future::err(errors::request_rejected_param_limit()));
}

let to_header_hint = match to_block {
Expand Down

0 comments on commit f6fd170

Please sign in to comment.