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.
This commit is contained in:
Matthew Martin 2018-07-02 01:23:57 -07:00 committed by Tomasz Drwięga
parent 34bf2452c3
commit 67721f3413
5 changed files with 18 additions and 1 deletions

View File

@ -482,6 +482,10 @@ usage! {
"--jsonrpc-server-threads=[NUM]", "--jsonrpc-server-threads=[NUM]",
"Enables multiple threads handling incoming connections for HTTP JSON-RPC server.", "Enables multiple threads handling incoming connections for HTTP JSON-RPC server.",
ARG arg_jsonrpc_max_payload: (Option<usize>) = 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"] ["API and Console Options WebSockets"]
FLAG flag_no_ws: (bool) = false, or |c: &Config| c.websockets.as_ref()?.disable.clone(), FLAG flag_no_ws: (bool) = false, or |c: &Config| c.websockets.as_ref()?.disable.clone(),
"--no-ws", "--no-ws",
@ -1165,6 +1169,7 @@ struct Rpc {
hosts: Option<Vec<String>>, hosts: Option<Vec<String>>,
server_threads: Option<usize>, server_threads: Option<usize>,
processing_threads: Option<usize>, processing_threads: Option<usize>,
max_payload: Option<usize>,
} }
#[derive(Default, Debug, PartialEq, Deserialize)] #[derive(Default, Debug, PartialEq, Deserialize)]
@ -1613,6 +1618,7 @@ mod tests {
arg_jsonrpc_hosts: "none".into(), arg_jsonrpc_hosts: "none".into(),
arg_jsonrpc_server_threads: None, arg_jsonrpc_server_threads: None,
arg_jsonrpc_threads: 4, arg_jsonrpc_threads: 4,
arg_jsonrpc_max_payload: None,
// WS // WS
flag_no_ws: false, flag_no_ws: false,
@ -1880,6 +1886,7 @@ mod tests {
hosts: None, hosts: None,
server_threads: None, server_threads: None,
processing_threads: None, processing_threads: None,
max_payload: None,
}), }),
ipc: Some(Ipc { ipc: Some(Ipc {
disable: None, disable: None,

View File

@ -879,6 +879,10 @@ impl Configuration {
_ => 1, _ => 1,
}, },
processing_threads: self.args.arg_jsonrpc_threads, 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) Ok(conf)

View File

@ -45,6 +45,7 @@ pub struct HttpConfiguration {
pub hosts: Option<Vec<String>>, pub hosts: Option<Vec<String>>,
pub server_threads: usize, pub server_threads: usize,
pub processing_threads: usize, pub processing_threads: usize,
pub max_payload: usize,
} }
impl HttpConfiguration { impl HttpConfiguration {
@ -64,6 +65,7 @@ impl Default for HttpConfiguration {
hosts: Some(vec![]), hosts: Some(vec![]),
server_threads: 1, server_threads: 1,
processing_threads: 4, processing_threads: 4,
max_payload: 5,
} }
} }
} }
@ -232,6 +234,7 @@ pub fn new_http<D: rpc_apis::Dependencies>(
rpc::RpcExtractor, rpc::RpcExtractor,
middleware, middleware,
conf.server_threads, conf.server_threads,
conf.max_payload,
); );
match start_result { match start_result {

View File

@ -140,6 +140,7 @@ pub fn start_http<M, S, H, T, R>(
extractor: T, extractor: T,
middleware: Option<R>, middleware: Option<R>,
threads: usize, threads: usize,
max_payload: usize,
) -> ::std::io::Result<HttpServer> where ) -> ::std::io::Result<HttpServer> where
M: jsonrpc_core::Metadata, M: jsonrpc_core::Metadata,
S: jsonrpc_core::Middleware<M>, S: jsonrpc_core::Middleware<M>,
@ -152,7 +153,8 @@ pub fn start_http<M, S, H, T, R>(
.threads(threads) .threads(threads)
.event_loop_remote(remote) .event_loop_remote(remote)
.cors(cors_domains.into()) .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 { if let Some(dapps) = middleware {
builder = builder.request_middleware(dapps) builder = builder.request_middleware(dapps)

View File

@ -40,6 +40,7 @@ fn serve(handler: Option<MetaIoHandler<Metadata>>) -> Server<HttpServer> {
} }
}), }),
1, 1,
5,
).unwrap()) ).unwrap())
} }