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

Allow setting multiple values for cors_origin (fixes #5198) #5353

Merged
merged 3 commits into from
Jan 27, 2025
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
8 changes: 6 additions & 2 deletions config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@
bind: "127.0.0.1"
port: 10002
}
# Sets a response Access-Control-Allow-Origin CORS header
# Sets a response Access-Control-Allow-Origin CORS header. Can also be set via environment:
# `LEMMY_CORS_ORIGIN=example.org,site.com`
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
cors_origin: "lemmy.tld"
cors_origin: [
"lemmy.tld"
/* ... */
]
}
27 changes: 8 additions & 19 deletions crates/routes/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,19 @@ pub fn cors_config(settings: &Settings) -> Cors {
let self_origin = settings.get_protocol_and_hostname();
let cors_origin_setting = settings.cors_origin();

// A default setting for either wildcard, or None
let cors_default = Cors::default()
.allow_any_origin()
let mut cors = Cors::default()
.allow_any_method()
.allow_any_header()
.expose_any_header()
.max_age(3600);

match (cors_origin_setting.clone(), cfg!(debug_assertions)) {
(Some(origin), false) => {
// Need to call send_wildcard() explicitly, passing this into allowed_origin() results in
// error
if origin == "*" {
cors_default
} else {
Cors::default()
.allowed_origin(&origin)
.allowed_origin(&self_origin)
.allow_any_method()
.allow_any_header()
.expose_any_header()
.max_age(3600)
}
if cfg!(debug_assertions) || cors_origin_setting.contains(&"*".to_string()) {
cors = cors.allow_any_origin();
} else {
cors = cors.allowed_origin(&self_origin);
for c in cors_origin_setting {
cors = cors.allowed_origin(&c);
}
_ => cors_default,
}
cors
}
10 changes: 6 additions & 4 deletions crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ pub struct Settings {
// Prometheus configuration.
#[doku(example = "Some(Default::default())")]
pub prometheus: Option<PrometheusConfig>,
/// Sets a response Access-Control-Allow-Origin CORS header
/// Sets a response Access-Control-Allow-Origin CORS header. Can also be set via environment:
/// `LEMMY_CORS_ORIGIN=example.org,site.com`
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
#[doku(example = "lemmy.tld")]
cors_origin: Option<String>,
cors_origin: Vec<String>,
}

impl Settings {
pub fn cors_origin(&self) -> Option<String> {
pub fn cors_origin(&self) -> Vec<String> {
env::var("LEMMY_CORS_ORIGIN")
.ok()
.or(self.cors_origin.clone())
.map(|e| e.split(',').map(ToString::to_string).collect())
.unwrap_or(self.cors_origin.clone())
}
}

Expand Down