Skip to content

Commit

Permalink
feat(libs): add support for any origin in case of cors wildcard (#985)
Browse files Browse the repository at this point in the history
* feat: add support any origin in case of wildcard

* chore: change default to `false`

* chore: change default to `true`

* chore: add option to write vector without space after comma
  • Loading branch information
sevenzing authored Jul 17, 2024
1 parent 79daab7 commit db4e91b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libs/blockscout-service-launcher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blockscout-service-launcher"
version = "0.12.1"
version = "0.13.0"
description = "Allows to launch blazingly fast blockscout rust services"
license = "MIT"
repository = "https://github.com/blockscout/blockscout-rs"
Expand Down
23 changes: 20 additions & 3 deletions libs/blockscout-service-launcher/src/launcher/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct CorsSettings {
pub allowed_credentials: bool,
pub max_age: usize,
pub block_on_origin_mismatch: bool,
pub send_wildcard: bool,
}

impl Default for CorsSettings {
Expand All @@ -83,23 +84,35 @@ impl Default for CorsSettings {
allowed_credentials: true,
max_age: 3600,
block_on_origin_mismatch: false,
send_wildcard: false,
}
}
}

impl CorsSettings {
pub fn build(self) -> Cors {
if !self.enabled {
return Cors::default();
}
let mut cors = Cors::default()
.allow_any_header()
.allowed_methods(self.allowed_methods.split(", "))
.allowed_methods(split_string(&self.allowed_methods))
.max_age(Some(self.max_age))
.block_on_origin_mismatch(self.block_on_origin_mismatch);
if self.allowed_credentials {
cors = cors.supports_credentials()
}
for origin in self.allowed_origin.split(", ") {
cors = cors.allowed_origin(origin)
if self.send_wildcard {
cors = cors.send_wildcard()
}
match self.allowed_origin.as_str() {
"*" => cors = cors.allow_any_origin(),
allowed_origin => {
for origin in split_string(allowed_origin) {
cors = cors.allowed_origin(origin)
}
}
};
cors
}
}
Expand Down Expand Up @@ -137,3 +150,7 @@ impl Default for MetricsSettings {
}
}
}

fn split_string(s: &str) -> Vec<&str> {
s.split(',').map(|s| s.trim()).collect()
}

0 comments on commit db4e91b

Please sign in to comment.