mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: fix cors check (#2540)
This commit is contained in:
@ -74,8 +74,13 @@ impl RpcError {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum WsHttpSamePortError {
|
||||
/// Ws and http server configured on same port but with different cors domains.
|
||||
#[error("CORS domains for http and ws are different, but they are on the same port")]
|
||||
ConflictingCorsDomains,
|
||||
#[error("CORS domains for http and ws are different, but they are on the same port: http: {http_cors_domains:?}, ws: {ws_cors_domains:?}")]
|
||||
ConflictingCorsDomains {
|
||||
/// Http cors domains.
|
||||
http_cors_domains: Option<String>,
|
||||
/// Ws cors domains.
|
||||
ws_cors_domains: Option<String>,
|
||||
},
|
||||
/// Ws and http server configured on same port but with different modules.
|
||||
#[error("Different api modules for http and ws on the same port is currently not supported: http: {http_modules:?}, ws: {ws_modules:?}")]
|
||||
ConflictingModules {
|
||||
|
||||
@ -1010,8 +1010,15 @@ impl RpcServerConfig {
|
||||
self.ws_server_config.is_some()
|
||||
{
|
||||
let cors = match (self.ws_cors_domains.as_ref(), self.http_cors_domains.as_ref()) {
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(WsHttpSamePortError::ConflictingCorsDomains.into())
|
||||
(Some(ws_cors), Some(http_cors)) => {
|
||||
if ws_cors.trim() != http_cors.trim() {
|
||||
return Err(WsHttpSamePortError::ConflictingCorsDomains {
|
||||
http_cors_domains: Some(http_cors.clone()),
|
||||
ws_cors_domains: Some(ws_cors.clone()),
|
||||
}
|
||||
.into())
|
||||
}
|
||||
Some(ws_cors)
|
||||
}
|
||||
(None, cors @ Some(_)) => cors,
|
||||
(cors @ Some(_), None) => cors,
|
||||
|
||||
@ -72,3 +72,49 @@ async fn test_launch_same_port_different_modules() {
|
||||
RpcError::WsHttpSamePortError(WsHttpSamePortError::ConflictingModules { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_launch_same_port_same_cors() {
|
||||
let builder = test_rpc_builder();
|
||||
let server = builder.build(
|
||||
TransportRpcModuleConfig::set_ws(vec![RethRpcModule::Eth])
|
||||
.with_http(vec![RethRpcModule::Eth]),
|
||||
);
|
||||
let addr = test_address();
|
||||
let res = server
|
||||
.start_server(
|
||||
RpcServerConfig::ws(Default::default())
|
||||
.with_ws_address(addr)
|
||||
.with_http(Default::default())
|
||||
.with_cors(Some("*".to_string()))
|
||||
.with_http_cors(Some("*".to_string()))
|
||||
.with_http_address(addr),
|
||||
)
|
||||
.await;
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_launch_same_port_different_cors() {
|
||||
let builder = test_rpc_builder();
|
||||
let server = builder.build(
|
||||
TransportRpcModuleConfig::set_ws(vec![RethRpcModule::Eth])
|
||||
.with_http(vec![RethRpcModule::Eth]),
|
||||
);
|
||||
let addr = test_address();
|
||||
let res = server
|
||||
.start_server(
|
||||
RpcServerConfig::ws(Default::default())
|
||||
.with_ws_address(addr)
|
||||
.with_http(Default::default())
|
||||
.with_cors(Some("*".to_string()))
|
||||
.with_http_cors(Some("example".to_string()))
|
||||
.with_http_address(addr),
|
||||
)
|
||||
.await;
|
||||
let err = res.unwrap_err();
|
||||
assert!(matches!(
|
||||
err,
|
||||
RpcError::WsHttpSamePortError(WsHttpSamePortError::ConflictingCorsDomains { .. })
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user