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

feat(libs): add support for any origin in case of cors wildcard #985

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
17 changes: 15 additions & 2 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,12 +84,16 @@ 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(", "))
Expand All @@ -97,9 +102,17 @@ impl CorsSettings {
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 allowed_origin.split(", ") {
cors = cors.allowed_origin(origin)
}
}
};
cors
}
}
Expand Down
Loading