mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-22 10:51:29 +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
|
||||
# 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
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
||||
#[default(None)]
|
||||
#[doku(example = "*")]
|
||||
#[doku(example = "lemmy.tld")]
|
||||
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 {
|
||||
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()
|
||||
.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 cors_origin_setting.as_deref() == Some("*") {
|
||||
Cors::default().allow_any_origin().send_wildcard()
|
||||
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)
|
||||
}
|
||||
}
|
||||
_ => Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_method()
|
||||
.allow_any_header()
|
||||
.expose_any_header()
|
||||
.max_age(3600),
|
||||
_ => cors_default,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue