Skip to content

Commit

Permalink
feat: add allowed-origins to katana cli (#1909)
Browse files Browse the repository at this point in the history
* feat: add allowed-origins to katana cli

* fix: testsequencer config
  • Loading branch information
Larkooo authored May 2, 2024
1 parent 6ff5247 commit 048d6dc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions bin/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub struct ServerOptions {
#[arg(default_value = "100")]
#[arg(help = "Maximum number of concurrent connections allowed.")]
pub max_connections: u32,

#[arg(long)]
#[arg(help = "Enables the CORS layer and sets the allowed origins.")]
pub allowed_origins: Option<Vec<String>>,
}

#[derive(Debug, Args, Clone)]
Expand Down Expand Up @@ -236,6 +240,7 @@ impl KatanaArgs {
port: self.server.port,
host: self.server.host.clone().unwrap_or("0.0.0.0".into()),
max_connections: self.server.max_connections,
allowed_origins: self.server.allowed_origins.clone(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/dojo-test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl TestSequencer {
port: 0,
host: "127.0.0.1".into(),
max_connections: 100,
allowed_origins: None,
apis: vec![
ApiKind::Starknet,
ApiKind::Katana,
Expand Down
1 change: 1 addition & 0 deletions crates/katana/rpc/rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct ServerConfig {
pub port: u16,
pub host: String,
pub max_connections: u32,
pub allowed_origins: Option<Vec<String>>,
pub apis: Vec<ApiKind>,
}

Expand Down
23 changes: 18 additions & 5 deletions crates/katana/rpc/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::time::Duration;

use anyhow::Result;
use config::ServerConfig;
use hyper::Method;
use hyper::{Method, Uri};
use jsonrpsee::server::middleware::proxy_get_request::ProxyGetRequestLayer;
use jsonrpsee::server::{AllowHosts, ServerBuilder, ServerHandle};
use jsonrpsee::RpcModule;
Expand All @@ -27,7 +27,7 @@ use katana_rpc_api::starknet::StarknetApiServer;
use katana_rpc_api::torii::ToriiApiServer;
use katana_rpc_api::ApiKind;
use metrics::RpcServerMetrics;
use tower_http::cors::{Any, CorsLayer};
use tower_http::cors::{AllowOrigin, CorsLayer};

use crate::dev::DevApi;
use crate::katana::KatanaApi;
Expand Down Expand Up @@ -65,12 +65,25 @@ pub async fn spawn<EF: ExecutorFactory>(
let cors = CorsLayer::new()
// Allow `POST` when accessing the resource
.allow_methods([Method::POST, Method::GET])
// Allow requests from any origin
.allow_origin(Any)
.allow_headers([hyper::header::CONTENT_TYPE]);

let cors =
config.allowed_origins.clone().map(|allowed_origins| match allowed_origins.as_slice() {
[origin] if origin == "*" => cors.allow_origin(AllowOrigin::mirror_request()),
origins => cors.allow_origin(
origins
.iter()
.map(|o| {
let _ = o.parse::<Uri>().expect("Invalid URI");

o.parse().expect("Invalid origin")
})
.collect::<Vec<_>>(),
),
});

let middleware = tower::ServiceBuilder::new()
.layer(cors)
.option_layer(cors)
.layer(ProxyGetRequestLayer::new("/", "health")?)
.timeout(Duration::from_secs(20));

Expand Down

0 comments on commit 048d6dc

Please sign in to comment.