From 1e9d392a2ec01db5e192ef5963b9aee7a617c069 Mon Sep 17 00:00:00 2001 From: Larko <59736843+Larkooo@users.noreply.github.com> Date: Tue, 30 Apr 2024 23:37:16 -0400 Subject: [PATCH] feat: cli arg disable cors torii proxy server (#1904) * feat: torii cli arg for no cors * feat: cors layer into optional layer * fmt * refactor: try to parse url * fix: remove default value for allowed origins * chore: unused imports --- bin/torii/src/main.rs | 4 ++-- crates/torii/server/src/proxy.rs | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/bin/torii/src/main.rs b/bin/torii/src/main.rs index a4f7923719..d3c85d63d7 100644 --- a/bin/torii/src/main.rs +++ b/bin/torii/src/main.rs @@ -87,9 +87,9 @@ struct Args { /// Specify allowed origins for api endpoints (comma-separated list of allowed origins, or "*" /// for all) - #[arg(long, default_value = "*")] + #[arg(long)] #[arg(value_delimiter = ',')] - allowed_origins: Vec, + allowed_origins: Option>, /// The external url of the server, used for configuring the GraphQL Playground in a hosted /// environment diff --git a/crates/torii/server/src/proxy.rs b/crates/torii/server/src/proxy.rs index 4d34fafa35..df9f4e26f5 100644 --- a/crates/torii/server/src/proxy.rs +++ b/crates/torii/server/src/proxy.rs @@ -54,7 +54,7 @@ lazy_static::lazy_static! { pub struct Proxy { addr: SocketAddr, - allowed_origins: Vec, + allowed_origins: Option>, grpc_addr: Option, graphql_addr: Arc>>, } @@ -62,7 +62,7 @@ pub struct Proxy { impl Proxy { pub fn new( addr: SocketAddr, - allowed_origins: Vec, + allowed_origins: Option>, grpc_addr: Option, graphql_addr: Option, ) -> Self { @@ -103,15 +103,23 @@ impl Proxy { .collect::>(), ); - let cors = match allowed_origins.as_slice() { - [origin] if origin == "*" => cors.allow_origin(AllowOrigin::mirror_request()), - origins => cors.allow_origin( - origins.iter().map(|o| o.parse().expect("valid origin")).collect::>(), - ), - }; + let cors = + 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::().expect("Invalid URI"); + + o.parse().expect("Invalid origin") + }) + .collect::>(), + ), + }); let graphql_addr_clone = graphql_addr.clone(); - let service = ServiceBuilder::new().layer(cors).service_fn(move |req| { + let service = ServiceBuilder::new().option_layer(cors).service_fn(move |req| { let graphql_addr = graphql_addr_clone.clone(); async move { let graphql_addr = graphql_addr.read().await;