mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-23 03:11:32 +00:00
Fixing cors origin wildcard. (#5194)
* Fixing cors origin wildcard. - Fixes #5185 * Add other allows to specified origin block. * Fix clippy.
This commit is contained in:
parent
f916309df8
commit
faf62de4e3
3 changed files with 18 additions and 10 deletions
|
@ -122,5 +122,5 @@
|
||||||
}
|
}
|
||||||
# Sets a response Access-Control-Allow-Origin CORS header
|
# Sets a response Access-Control-Allow-Origin CORS header
|
||||||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
||||||
cors_origin: "*"
|
cors_origin: "lemmy.tld"
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ pub struct Settings {
|
||||||
/// Sets a response Access-Control-Allow-Origin CORS header
|
/// Sets a response Access-Control-Allow-Origin CORS header
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
||||||
#[default(None)]
|
#[default(None)]
|
||||||
#[doku(example = "*")]
|
#[doku(example = "lemmy.tld")]
|
||||||
cors_origin: Option<String>,
|
cors_origin: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
src/lib.rs
24
src/lib.rs
|
@ -339,23 +339,31 @@ fn create_http_server(
|
||||||
fn cors_config(settings: &Settings) -> Cors {
|
fn cors_config(settings: &Settings) -> Cors {
|
||||||
let self_origin = settings.get_protocol_and_hostname();
|
let self_origin = settings.get_protocol_and_hostname();
|
||||||
let cors_origin_setting = settings.cors_origin();
|
let cors_origin_setting = settings.cors_origin();
|
||||||
|
|
||||||
|
// A default setting for either wildcard, or None
|
||||||
|
let cors_default = Cors::default()
|
||||||
|
.allow_any_origin()
|
||||||
|
.allow_any_method()
|
||||||
|
.allow_any_header()
|
||||||
|
.expose_any_header()
|
||||||
|
.max_age(3600);
|
||||||
|
|
||||||
match (cors_origin_setting.clone(), cfg!(debug_assertions)) {
|
match (cors_origin_setting.clone(), cfg!(debug_assertions)) {
|
||||||
(Some(origin), false) => {
|
(Some(origin), false) => {
|
||||||
// Need to call send_wildcard() explicitly, passing this into allowed_origin() results in
|
// Need to call send_wildcard() explicitly, passing this into allowed_origin() results in
|
||||||
// error
|
// error
|
||||||
if cors_origin_setting.as_deref() == Some("*") {
|
if origin == "*" {
|
||||||
Cors::default().allow_any_origin().send_wildcard()
|
cors_default
|
||||||
} else {
|
} else {
|
||||||
Cors::default()
|
Cors::default()
|
||||||
.allowed_origin(&origin)
|
.allowed_origin(&origin)
|
||||||
.allowed_origin(&self_origin)
|
.allowed_origin(&self_origin)
|
||||||
|
.allow_any_method()
|
||||||
|
.allow_any_header()
|
||||||
|
.expose_any_header()
|
||||||
|
.max_age(3600)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Cors::default()
|
_ => cors_default,
|
||||||
.allow_any_origin()
|
|
||||||
.allow_any_method()
|
|
||||||
.allow_any_header()
|
|
||||||
.expose_any_header()
|
|
||||||
.max_age(3600),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue