This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
jsonrpsee: add host and origin filtering #9787
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,26 @@ 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={}, allowed origins={:?}", addr, cors); | ||
|
||
if let Some(cors) = cors { | ||
// Whitelist listening address. | ||
let host = Host::parse(&format!("localhost:{}", addr.port())); | ||
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); | ||
|
||
// Set allowed origins. | ||
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 +135,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 +145,24 @@ 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={}, allowed origins={:?}", 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()), | ||
])?; | ||
|
||
// Set allowed origins. | ||
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0" } | |
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee", branch = "master", features = ["server"] } | ||
serde_json = "1" | ||
serde = { version = "1.0.126", features = ["derive"] } | ||
log = "0.4" | ||
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 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" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we put
localhost
/127.0.0.1
on the allowlist even ifcorse
isNone
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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