chore: cors followup changes (#1480)

This commit is contained in:
Matthias Seitz
2023-02-21 14:57:09 +01:00
committed by GitHub
parent 3c41807f0f
commit 93b1a7f49a
3 changed files with 210 additions and 36 deletions

160
Cargo.lock generated
View File

@ -96,6 +96,21 @@ dependencies = [
"memchr",
]
[[package]]
name = "alloc-no-stdlib"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3"
[[package]]
name = "alloc-stdlib"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece"
dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "android_system_properties"
version = "0.1.5"
@ -151,6 +166,20 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9"
[[package]]
name = "async-compression"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942c7cd7ae39e91bde4820d74132e9862e62c2f386c3aa90ccf55949f5bad63a"
dependencies = [
"brotli",
"flate2",
"futures-core",
"memchr",
"pin-project-lite",
"tokio",
]
[[package]]
name = "async-lock"
version = "2.6.0"
@ -492,6 +521,27 @@ dependencies = [
"generic-array 0.14.6",
]
[[package]]
name = "brotli"
version = "3.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a0b1dbcc8ae29329621f8d4f0d835787c1c38bb1401979b49d13b0b305ff68"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
"brotli-decompressor",
]
[[package]]
name = "brotli-decompressor"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b6561fd3f895a11e8f72af2cb7d22e08366bebc2b6b57f7744c4bda27034744"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
]
[[package]]
name = "bs58"
version = "0.4.0"
@ -930,6 +980,15 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cace84e55f07e7301bae1c519df89cdad8cc3cd868413d3fdbdeca9ff3db484"
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "criterion"
version = "0.4.0"
@ -1930,6 +1989,16 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "flate2"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -2313,6 +2382,16 @@ dependencies = [
"hashbrown 0.11.2",
]
[[package]]
name = "hdrhistogram"
version = "7.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f19b9f54f7c7f55e31401bb647626ce0cf0f67b0004982ce815b3ee72a02aa8"
dependencies = [
"byteorder",
"num-traits",
]
[[package]]
name = "heapless"
version = "0.7.16"
@ -2432,6 +2511,12 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-range-header"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfe8eed0a9285ef776bb792479ea3834e8b94e13d615c2f66d03dd50a435a29"
[[package]]
name = "httparse"
version = "1.8.0"
@ -2717,6 +2802,15 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "iri-string"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f0f7638c1e223529f1bfdc48c8b133b9e0b434094d1d28473161ee48b235f78"
dependencies = [
"nom",
]
[[package]]
name = "is-terminal"
version = "0.4.3"
@ -3204,6 +3298,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -4831,6 +4935,7 @@ dependencies = [
name = "reth-rpc-builder"
version = "0.1.0"
dependencies = [
"hyper",
"jsonrpsee",
"reth-ipc",
"reth-network-api",
@ -4843,7 +4948,10 @@ dependencies = [
"reth-transaction-pool",
"serde",
"strum",
"thiserror",
"tokio",
"tower",
"tower-http",
]
[[package]]
@ -6285,11 +6393,51 @@ version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"hdrhistogram",
"indexmap",
"pin-project",
"pin-project-lite",
"rand 0.8.5",
"slab",
"tokio",
"tokio-util 0.7.4",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-http"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f873044bf02dd1e8239e9c1293ea39dad76dc594ec16185d0a1bf31d8dc8d858"
dependencies = [
"async-compression",
"base64 0.13.1",
"bitflags",
"bytes",
"futures-core",
"futures-util",
"http",
"http-body",
"http-range-header",
"httpdate",
"iri-string",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"tokio",
"tokio-util 0.7.4",
"tower",
"tower-layer",
"tower-service",
"tracing",
"uuid 1.3.0",
]
[[package]]
name = "tower-layer"
version = "0.3.2"
@ -6605,6 +6753,15 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.10"
@ -6709,6 +6866,9 @@ name = "uuid"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
dependencies = [
"getrandom 0.2.8",
]
[[package]]
name = "valuable"

View File

@ -17,12 +17,13 @@ reth-rpc-types = { path = "../rpc-types" }
reth-transaction-pool = { path = "../../transaction-pool" }
jsonrpsee = { version = "0.16", features = ["server"] }
tower-http = { version = "0.3", features = ["full"] }
tower = {version = "0.4" , features = ["full"] }
hyper = "0.14"
strum = { version = "0.24", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
tower-http = { version = "0.3.4", features = ["full"] }
hyper = "0.14.24"
tower = {version = "0.4.13" , features = ["full"] }
thiserror = "1.0"
[dev-dependencies]
reth-tracing = { path = "../../tracing" }

View File

@ -54,7 +54,10 @@
use hyper::{http::HeaderValue, Method};
pub use jsonrpsee::server::ServerBuilder;
use jsonrpsee::{
core::{server::host_filtering::AllowHosts, server::rpc_module::Methods, Error as RpcError},
core::{
server::{host_filtering::AllowHosts, rpc_module::Methods},
Error as RpcError,
},
server::{Server, ServerHandle},
RpcModule,
};
@ -74,7 +77,7 @@ use std::{
};
use strum::{AsRefStr, EnumString, EnumVariantNames, ParseError, VariantNames};
use tower::layer::util::{Identity, Stack};
use tower_http::cors::{Any, CorsLayer};
use tower_http::cors::{AllowOrigin, Any, CorsLayer};
/// The default port for the http server
pub const DEFAULT_HTTP_RPC_PORT: u16 = 8545;
@ -618,8 +621,8 @@ impl RpcServerConfig {
)));
if let Some(builder) = self.http_server_config {
let cors = Self::create_cors_layer(self.http_cors_domains).unwrap();
if let Some(cors) = cors {
if let Some(cors) = self.http_cors_domains.as_deref().map(create_cors_layer) {
let cors = cors.map_err(|err| RpcError::Custom(err.to_string()))?;
let middleware = tower::ServiceBuilder::new().layer(cors);
let http_server =
builder.set_middleware(middleware).build(http_socket_addr).await?;
@ -653,38 +656,48 @@ impl RpcServerConfig {
Ok(server)
}
}
fn create_cors_layer(http_cors_domains: Option<String>) -> Result<Option<CorsLayer>, RpcError> {
let mut cors = None;
if let Some(domains) = http_cors_domains {
match domains.as_str() {
"*" => {
cors = Some(
CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers(Any),
);
}
"" => {}
_ => {
let origins = domains
.split(",")
.map(|domain| domain.parse::<HeaderValue>())
.collect::<Result<Vec<HeaderValue>, _>>();
if let Ok(origins) = origins {
cors = Some(
CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(origins)
.allow_headers(Any),
);
}
}
/// Error thrown when parsing cors domains went wrong
#[derive(Debug, thiserror::Error)]
enum CorsDomainError {
#[error("{domain} is an invalid header value")]
InvalidHeader { domain: String },
#[error("Wildcard origin (`*`) cannot be passed as part of a list: {input}")]
WildCardNotAllowed { input: String },
}
/// Creates a [CorsLayer] from the given domains
fn create_cors_layer(http_cors_domains: &str) -> Result<CorsLayer, CorsDomainError> {
let cors = match http_cors_domains.trim() {
"*" => CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers(Any),
_ => {
let iter = http_cors_domains.split(',');
if iter.clone().any(|o| o == "*") {
return Err(CorsDomainError::WildCardNotAllowed {
input: http_cors_domains.to_string(),
})
}
let origins = iter
.map(|domain| {
domain
.parse::<HeaderValue>()
.map_err(|_| CorsDomainError::InvalidHeader { domain: domain.to_string() })
})
.collect::<Result<Vec<HeaderValue>, _>>()?;
let origin = AllowOrigin::list(origins);
CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(origin)
.allow_headers(Any)
}
Ok(cors)
}
};
Ok(cors)
}
/// Holds modules to be installed per transport type