Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set CORS to any to accept RPC calls from web-based apps #1459

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion common/apm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ arc-swap = "1.6"
axum = "0.6"
beef = "0.5"
derive_more = "0.99"
hyper = { version = "0.14", features = ["http1", "server"] }
lazy_static = "1.4"
log = "0.4"
minstant = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions core/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ beef = "0.5"
ckb-jsonrpc-types = "0.111"
ckb-traits = "0.111"
ckb-types = "0.111"
hyper = "0.14"
jsonrpsee = { version = "0.20", features = ["macros", "server"] }
log = "0.4"
parking_lot = "0.12"
Expand All @@ -18,6 +19,8 @@ rlp = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = "0.25"
tower = "0.4"
tower-http = { version = "0.4", features = ["cors"] }

common-apm = { path = "../../common/apm" }
common-config-parser = { path = "../../common/config-parser" }
Expand Down
9 changes: 9 additions & 0 deletions core/api/src/jsonrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ mod ws_subscription;
use std::{collections::HashMap, sync::Arc};

use ckb_jsonrpc_types::{CellInfo, HeaderView as CkbHeaderView, OutPoint};
use hyper::{header::CONTENT_TYPE, Method};
use jsonrpsee::server::{ServerBuilder, ServerHandle};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use tower_http::cors::{Any as CorsAny, CorsLayer};

use common_config_parser::types::{spec::HardforkName, Config};
use protocol::traits::APIAdapter;
Expand Down Expand Up @@ -268,11 +270,18 @@ pub async fn run_jsonrpc_server<Adapter: APIAdapter + 'static>(
rpc.merge(ckb_light_client_rpc).unwrap();

if let Some(addr) = config.rpc.http_listening_address {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
.allow_origin(CorsAny)
.allow_headers([CONTENT_TYPE]);
let middleware = tower::ServiceBuilder::new().layer(cors);

let server = ServerBuilder::new()
.http_only()
.max_request_body_size(config.rpc.max_payload_size)
.max_response_body_size(config.rpc.max_payload_size)
.max_connections(config.rpc.maxconn)
.set_middleware(middleware)
.build(addr)
.await
.map_err(|e| APIError::HttpServer(e.to_string()))?;
Expand Down
Loading