Skip to content

Commit

Permalink
Panic if * is used with AllowOrigin::list (#285)
Browse files Browse the repository at this point in the history
* Handle `*` in `AllowOrigin::list`

* changelog

* just panic
  • Loading branch information
davidpdrsn committed Jul 15, 2022
1 parent 77d7148 commit 913683b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tower-http/CHANGELOG.md
Expand Up @@ -22,8 +22,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Fixed

- **trace:** Correctly identify gRPC requests in default `on_response` callback ([#278])
- **cors:** Panic if a wildcard (`*`) is passed to `AllowOrigin::list`. Use
`AllowOrigin::any()` instead ([#285])

[#278]: https://github.com/tower-rs/tower-http/pull/278
[#285]: https://github.com/tower-rs/tower-http/pull/285

# 0.3.4 (June 06, 2022)

Expand Down
12 changes: 11 additions & 1 deletion tower-http/src/cors/allow_origin.rs
Expand Up @@ -40,12 +40,22 @@ impl AllowOrigin {
///
/// See [`CorsLayer::allow_origin`] for more details.
///
/// # Panics
///
/// If the iterator contains a wildcard (`*`).
///
/// [`CorsLayer::allow_origin`]: super::CorsLayer::allow_origin
#[allow(clippy::borrow_interior_mutable_const)]
pub fn list<I>(origins: I) -> Self
where
I: IntoIterator<Item = HeaderValue>,
{
Self(OriginInner::List(origins.into_iter().collect()))
let origins = origins.into_iter().collect::<Vec<_>>();
if origins.iter().any(|o| o == WILDCARD) {
panic!("Wildcard origin (`*`) cannot be passed to `AllowOrigin::list`. Use `AllowOrigin::any()` instead");
} else {
Self(OriginInner::List(origins))
}
}

/// Set the allowed origins from a predicate
Expand Down

0 comments on commit 913683b

Please sign in to comment.