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

fix(host filtering): allow hosts with multiple ports #1227

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions core/src/server/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ impl Subscription {
let raw = self.rx.recv().await?;

tracing::debug!("[Subscription::next]: rx {}", raw);

// clippy complains about this but it doesn't compile without the extra res binding.
#[allow(clippy::let_and_return)]
let res = match serde_json::from_str::<SubscriptionResponse<T>>(&raw) {
Ok(r) => Some(Ok((r.params.result, r.params.subscription.into_owned()))),
Err(e) => match serde_json::from_str::<SubscriptionError<serde_json::Value>>(&raw) {
Expand Down
29 changes: 23 additions & 6 deletions server/src/middleware/host_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ use crate::transport::http;
use futures_util::{Future, FutureExt, TryFutureExt};
use hyper::{Body, Request, Response};
use route_recognizer::Router;
use std::collections::BTreeMap;
use std::error::Error as StdError;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tower::{Layer, Service};

type Ports = Vec<Port>;

/// Middleware to enable host filtering.
#[derive(Debug)]
pub struct HostFilterLayer(Option<Arc<WhitelistedHosts>>);
Expand Down Expand Up @@ -125,17 +128,30 @@ where

/// Represent the URL patterns that is whitelisted.
#[derive(Default, Debug, Clone)]
pub struct WhitelistedHosts(Router<Port>);
pub struct WhitelistedHosts(Router<Ports>);

impl<T> From<T> for WhitelistedHosts
where
T: IntoIterator<Item = Authority>,
{
fn from(value: T) -> Self {
let mut router = Router::new();
let mut uniq_hosts: BTreeMap<String, Ports> = BTreeMap::new();

// Ensure that no ports "overwritten"
// since it's possible add the same hostname with
// several port numbers.
for auth in value.into_iter() {
router.add(&auth.host, auth.port);
uniq_hosts
.entry(auth.host)
.and_modify(|v| {
v.push(auth.port);
})
.or_insert_with(|| vec![auth.port]);
}

for (host, ports) in uniq_hosts.into_iter() {
router.add(&host, ports);
}

Self(router)
Expand All @@ -145,14 +161,14 @@ where
impl WhitelistedHosts {
fn recognize(&self, other: &Authority) -> bool {
if let Ok(p) = self.0.recognize(&other.host) {
let p = p.handler();
let ports = p.handler();

match (p, &other.port) {
ports.iter().any(|p| match (p, &other.port) {
Copy link
Member Author

@niklasad1 niklasad1 Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this list to be small and linear search should ok here, we could change this to a HashSet or BTreeSet though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you're right!

(Port::Any, _) => true,
(Port::Default, Port::Default) => true,
(Port::Fixed(p1), Port::Fixed(p2)) if p1 == p2 => true,
_ => false,
}
})
} else {
false
}
Expand Down Expand Up @@ -186,8 +202,9 @@ mod tests {

#[test]
fn should_accept_if_on_the_list_with_port() {
let filter = unwrap_filter(&["parity.io:443"]);
let filter = unwrap_filter(&["parity.io:443", "parity.io:9944"]);
assert!(filter.recognize(&unwrap_auth("parity.io:443")));
assert!(filter.recognize(&unwrap_auth("parity.io:9944")));
assert!(!filter.recognize(&unwrap_auth("parity.io")));
}

Expand Down