Skip to content

Commit 0ed13b0

Browse files
committed
fix: CORS config
1 parent 607a575 commit 0ed13b0

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,13 @@ impl Config {
176176
self.server
177177
.cors_allowed_origins
178178
.iter()
179-
.map(|o| o.parse().unwrap())
179+
.filter_map(|o| match o.parse() {
180+
Ok(v) => Some(v),
181+
Err(e) => {
182+
tracing::warn!("Skipping invalid CORS origin {:?}: {}", o, e);
183+
None
184+
}
185+
})
180186
.collect()
181187
}
182188

src/http_server.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use axum::{middleware, response::Json, routing::get, Router};
2+
use axum::http::Method;
23
use rusx::{PkceCodeVerifier, TwitterGateway};
34
use serde::{Deserialize, Serialize};
45
use std::{
@@ -7,7 +8,10 @@ use std::{
78
};
89
use tower::ServiceBuilder;
910
use tower_cookies::CookieManagerLayer;
10-
use tower_http::{cors::CorsLayer, trace::TraceLayer};
11+
use tower_http::{
12+
cors::{AllowHeaders, CorsLayer},
13+
trace::TraceLayer,
14+
};
1115

1216
use crate::{
1317
db_persistence::DbPersistence,
@@ -58,9 +62,15 @@ pub fn create_router(state: AppState) -> Router {
5862
.layer(
5963
ServiceBuilder::new()
6064
.layer(TraceLayer::new_for_http())
61-
.layer(CorsLayer::permissive().allow_origin(state.config.get_cors_allowed_origins())),
65+
.layer(
66+
CorsLayer::new()
67+
.allow_origin(state.config.get_cors_allowed_origins())
68+
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE, Method::OPTIONS])
69+
.allow_headers(AllowHeaders::mirror_request())
70+
.allow_credentials(true),
71+
),
6272
)
63-
.layer(CookieManagerLayer::new()) // Enable Cookie support
73+
.layer(CookieManagerLayer::new())
6474
.with_state(state)
6575
}
6676

0 commit comments

Comments
 (0)