-
Notifications
You must be signed in to change notification settings - Fork 2.7k
jsonrpsee: add host and origin filtering #9787
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
| #![warn(missing_docs)] | ||
|
|
||
| use jsonrpsee::{ | ||
| http_server::{HttpServerBuilder, HttpStopHandle}, | ||
| http_server::{AccessControlBuilder, Host, HttpServerBuilder, HttpStopHandle}, | ||
| ws_server::{WsServerBuilder, WsStopHandle}, | ||
| RpcModule, | ||
| }; | ||
|
|
@@ -90,7 +90,7 @@ pub type WsServer = WsStopHandle; | |
| /// Start HTTP server listening on given address. | ||
| pub fn start_http<M: Send + Sync + 'static>( | ||
| addr: std::net::SocketAddr, | ||
| _cors: Option<&Vec<String>>, | ||
| cors: Option<&Vec<String>>, | ||
| maybe_max_payload_mb: Option<usize>, | ||
| module: RpcModule<M>, | ||
| rt: tokio::runtime::Handle, | ||
|
|
@@ -99,8 +99,24 @@ pub fn start_http<M: Send + Sync + 'static>( | |
| .map(|mb| mb.saturating_mul(MEGABYTE)) | ||
| .unwrap_or(RPC_MAX_PAYLOAD_DEFAULT); | ||
|
|
||
| let mut acl = AccessControlBuilder::new(); | ||
|
|
||
| log::info!("starting JSONRPC HTTP server: addr={}, cors={:?}", addr, cors); | ||
|
|
||
| if let Some(cors) = cors { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we put
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All hosts/origins are enabled by default: https://github.com/paritytech/jsonrpsee/blob/master/http-server/src/access_control/mod.rs#L115 We should probably document it clearly in jsonrpsee I guess |
||
| // Whitelist listening address. | ||
| let host = Host::parse(&format!("localhost:{}", addr.port())); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incredible ugly and annoying, we should fix this API. |
||
| acl = acl.allow_host(host); | ||
| let host = Host::parse(&format!("127.0.0.1:{}", addr.port())); | ||
| acl = acl.allow_host(host); | ||
| for origin in cors { | ||
| acl = acl.cors_allow_origin(origin.into()); | ||
| } | ||
| }; | ||
|
|
||
| let server = HttpServerBuilder::default() | ||
| .max_request_body_size(max_request_body_size as u32) | ||
| .set_access_control(acl.build()) | ||
| .build(addr)?; | ||
|
|
||
| let handle = server.stop_handle(); | ||
|
|
@@ -117,7 +133,7 @@ pub fn start_http<M: Send + Sync + 'static>( | |
| pub fn start_ws<M: Send + Sync + 'static>( | ||
| addr: std::net::SocketAddr, | ||
| max_connections: Option<usize>, | ||
| _cors: Option<&Vec<String>>, | ||
| cors: Option<&Vec<String>>, | ||
| maybe_max_payload_mb: Option<usize>, | ||
| module: RpcModule<M>, | ||
| rt: tokio::runtime::Handle, | ||
|
|
@@ -127,14 +143,19 @@ pub fn start_ws<M: Send + Sync + 'static>( | |
| .unwrap_or(RPC_MAX_PAYLOAD_DEFAULT); | ||
| let max_connections = max_connections.unwrap_or(WS_MAX_CONNECTIONS); | ||
|
|
||
| let server = tokio::task::block_in_place(|| { | ||
| rt.block_on( | ||
| WsServerBuilder::default() | ||
| .max_request_body_size(max_request_body_size as u32) | ||
| .max_connections(max_connections as u64) | ||
| .build(addr), | ||
| ) | ||
| })?; | ||
| let mut builder = WsServerBuilder::default() | ||
| .max_request_body_size(max_request_body_size as u32) | ||
| .max_connections(max_connections as u64); | ||
|
|
||
| log::info!("starting JSONRPC WS server: addr={}, cors={:?}", addr, cors); | ||
|
|
||
| if let Some(cors) = cors { | ||
| // Whitelist listening address. | ||
| builder = builder.set_allowed_hosts([format!("localhost:{}", addr.port()), format!("127.0.0.1:{}", addr.port())])?; | ||
| builder = builder.set_allowed_origins(cors)?; | ||
| } | ||
|
|
||
| let server = tokio::task::block_in_place(|| rt.block_on(builder.build(addr)))?; | ||
|
|
||
| let handle = server.stop_handle(); | ||
| let rpc_api = build_rpc_api(module); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,10 @@ targets = ["x86_64-unknown-linux-gnu"] | |
|
|
||
| [dependencies] | ||
| codec = { package = "parity-scale-codec", version = "2.0.0" } | ||
| jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee", branch = "master", features = ["server"] } | ||
| jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee", branch = "na-http-server-export-acl", features = ["server"] } | ||
| serde_json = "1" | ||
| serde = { version = "1.0.126", features = ["derive"] } | ||
| log = "0.4" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah so this is what you meant by the issue on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah :) |
||
|
|
||
| sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" } | ||
| sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.