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

🐛 Change CORS layer construction to allow wildcard #538

Merged
merged 1 commit into from
Dec 26, 2024
Merged
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
61 changes: 39 additions & 22 deletions apps/server/src/config/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,47 @@
pub fn get_cors_layer(config: StumpConfig) -> CorsLayer {
let is_debug = config.is_debug();

let mut allowed_origins = Vec::new();
for origin in config.allowed_origins {
if let Ok(val) = origin.parse::<HeaderValue>() {
allowed_origins.push(val)
} else {
tracing::error!("Failed to parse allowed origin: {:?}", origin);
}
// Create CORS layer
let mut cors_layer = CorsLayer::new();
cors_layer = cors_layer
.allow_methods([
Method::GET,
Method::PUT,
Method::POST,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
Method::CONNECT,
])
.allow_headers([ACCEPT, AUTHORIZATION, CONTENT_TYPE])
.allow_credentials(true);

// If allowed origins include the general wildcard ("*") then we can return a permissive CORS layer and exit early.
if config.allowed_origins.contains(&"*".to_string()) {
cors_layer = cors_layer.allow_origin(AllowOrigin::any());

#[cfg(debug_assertions)]
tracing::trace!(

Check warning on line 55 in apps/server/src/config/cors.rs

View check run for this annotation

Codecov / codecov/patch

apps/server/src/config/cors.rs#L35-L55

Added lines #L35 - L55 were not covered by tests
?cors_layer,
"Cors configuration completed (allowing any origin)"

Check warning on line 57 in apps/server/src/config/cors.rs

View check run for this annotation

Codecov / codecov/patch

apps/server/src/config/cors.rs#L57

Added line #L57 was not covered by tests
);

return cors_layer;

Check warning on line 60 in apps/server/src/config/cors.rs

View check run for this annotation

Codecov / codecov/patch

apps/server/src/config/cors.rs#L60

Added line #L60 was not covered by tests
}

// Convert allowed origins from config into `HeaderValue`s for CORS layer.
let allowed_origins: Vec<_> = config
.allowed_origins
.into_iter()
.filter_map(|origin| match origin.parse::<HeaderValue>() {
Ok(val) => Some(val),
Err(e) => {
tracing::error!("Failed to parse allowed origin: {origin:?}: {e}");
None

Check warning on line 71 in apps/server/src/config/cors.rs

View check run for this annotation

Codecov / codecov/patch

apps/server/src/config/cors.rs#L63-L71

Added lines #L63 - L71 were not covered by tests
},
})
.collect();

Check warning on line 75 in apps/server/src/config/cors.rs

View check run for this annotation

Codecov / codecov/patch

apps/server/src/config/cors.rs#L73-L75

Added lines #L73 - L75 were not covered by tests
let local_ip = local_ip()
.map_err(|e| {
tracing::error!("Failed to get local ip: {:?}", e);
Expand Down Expand Up @@ -70,8 +102,6 @@
base
};

let mut cors_layer = CorsLayer::new();

let defaults = if is_debug {
DEBUG_ALLOWED_ORIGINS
} else {
Expand All @@ -87,19 +117,6 @@
.collect::<Vec<HeaderValue>>(),
));

cors_layer = cors_layer
.allow_methods([
Method::GET,
Method::PUT,
Method::POST,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
Method::CONNECT,
])
.allow_headers([ACCEPT, AUTHORIZATION, CONTENT_TYPE])
.allow_credentials(true);

#[cfg(debug_assertions)]
tracing::trace!(?cors_layer, "Cors configuration complete");

Expand Down
Loading