From 67721f3413cf2ecaff050c0a1bad1261cfd0b783 Mon Sep 17 00:00:00 2001 From: Matthew Martin Date: Mon, 2 Jul 2018 01:23:57 -0700 Subject: [PATCH] Add option for user to set max size limit for RPC requests (#9010) * Add option for user to set max size limit for RPC requests as requested in #8961. * Add max_payload to tests. * Change name for max payload option and change value from NUM to MB. * Fix broken test. * Fix incorrect indentation. --- parity/cli/mod.rs | 7 +++++++ parity/configuration.rs | 4 ++++ parity/rpc.rs | 3 +++ rpc/src/lib.rs | 4 +++- rpc/src/tests/rpc.rs | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index 751a5c10a..d325ad234 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -482,6 +482,10 @@ usage! { "--jsonrpc-server-threads=[NUM]", "Enables multiple threads handling incoming connections for HTTP JSON-RPC server.", + ARG arg_jsonrpc_max_payload: (Option) = None, or |c: &Config| c.rpc.as_ref()?.max_payload, + "--jsonrpc-max-payload=[MB]", + "Specify maximum size for RPC requests in megabytes.", + ["API and Console Options – WebSockets"] FLAG flag_no_ws: (bool) = false, or |c: &Config| c.websockets.as_ref()?.disable.clone(), "--no-ws", @@ -1165,6 +1169,7 @@ struct Rpc { hosts: Option>, server_threads: Option, processing_threads: Option, + max_payload: Option, } #[derive(Default, Debug, PartialEq, Deserialize)] @@ -1613,6 +1618,7 @@ mod tests { arg_jsonrpc_hosts: "none".into(), arg_jsonrpc_server_threads: None, arg_jsonrpc_threads: 4, + arg_jsonrpc_max_payload: None, // WS flag_no_ws: false, @@ -1880,6 +1886,7 @@ mod tests { hosts: None, server_threads: None, processing_threads: None, + max_payload: None, }), ipc: Some(Ipc { disable: None, diff --git a/parity/configuration.rs b/parity/configuration.rs index 9e258e426..ab9baac1f 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -879,6 +879,10 @@ impl Configuration { _ => 1, }, processing_threads: self.args.arg_jsonrpc_threads, + max_payload: match self.args.arg_jsonrpc_max_payload { + Some(max) if max > 0 => max as usize, + _ => 5usize, + }, }; Ok(conf) diff --git a/parity/rpc.rs b/parity/rpc.rs index 3e71cc629..b5733f0ce 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -45,6 +45,7 @@ pub struct HttpConfiguration { pub hosts: Option>, pub server_threads: usize, pub processing_threads: usize, + pub max_payload: usize, } impl HttpConfiguration { @@ -64,6 +65,7 @@ impl Default for HttpConfiguration { hosts: Some(vec![]), server_threads: 1, processing_threads: 4, + max_payload: 5, } } } @@ -232,6 +234,7 @@ pub fn new_http( rpc::RpcExtractor, middleware, conf.server_threads, + conf.max_payload, ); match start_result { diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 6356dc284..1caf383f0 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -140,6 +140,7 @@ pub fn start_http( extractor: T, middleware: Option, threads: usize, + max_payload: usize, ) -> ::std::io::Result where M: jsonrpc_core::Metadata, S: jsonrpc_core::Middleware, @@ -152,7 +153,8 @@ pub fn start_http( .threads(threads) .event_loop_remote(remote) .cors(cors_domains.into()) - .allowed_hosts(allowed_hosts.into()); + .allowed_hosts(allowed_hosts.into()) + .max_request_body_size(max_payload * 1024 * 1024); if let Some(dapps) = middleware { builder = builder.request_middleware(dapps) diff --git a/rpc/src/tests/rpc.rs b/rpc/src/tests/rpc.rs index 015c2764a..d0c0fafd9 100644 --- a/rpc/src/tests/rpc.rs +++ b/rpc/src/tests/rpc.rs @@ -40,6 +40,7 @@ fn serve(handler: Option>) -> Server { } }), 1, + 5, ).unwrap()) }