-
Notifications
You must be signed in to change notification settings - Fork 179
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
refactor: server host filtering #1174
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8b06300
refactor: server host filtering
niklasad1 856c005
fix some nits
niklasad1 19acee0
fix build again
niklasad1 e36d902
allow requests with/without default port
niklasad1 2b57544
switch to `route_recognizer` for URL recognition
niklasad1 c9e5a38
remove weird From impl
niklasad1 b349074
Update core/src/server/host_filtering.rs
niklasad1 b2fa273
Update tests/tests/integration_tests.rs
niklasad1 ab82820
refactor host filter API
niklasad1 9075984
Merge remote-tracking branch 'origin/na-refactor-host-filtering' into…
niklasad1 9a32333
address grumbles
niklasad1 b9ccf12
fix clippy
niklasad1 14f15aa
host filter: switch to TryFrom
niklasad1 809809c
Update server/src/server.rs
niklasad1 500b3f3
Update server/src/server.rs
niklasad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
// Copyright 2019-2023 Parity Technologies (UK) Ltd. | ||
// | ||
// Permission is hereby granted, free of charge, to any | ||
// person obtaining a copy of this software and associated | ||
// documentation files (the "Software"), to deal in the | ||
// Software without restriction, including without | ||
// limitation the rights to use, copy, modify, merge, | ||
// publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software | ||
// is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice | ||
// shall be included in all copies or substantial portions | ||
// of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
//! HTTP Host Header validation. | ||
|
||
use std::net::SocketAddr; | ||
|
||
use crate::Error; | ||
use http::uri::{InvalidUri, Uri}; | ||
use route_recognizer::Router; | ||
|
||
/// Port pattern | ||
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)] | ||
pub enum Port { | ||
/// No port specified (default port) | ||
Default, | ||
/// Port specified as a wildcard pattern (*). | ||
Any, | ||
/// Fixed numeric port | ||
Fixed(u16), | ||
} | ||
|
||
impl From<u16> for Port { | ||
fn from(port: u16) -> Port { | ||
Port::Fixed(port) | ||
} | ||
} | ||
|
||
/// Represent the http URI scheme that is returned by the HTTP host header | ||
/// | ||
/// Further information can be found: <https://www.rfc-editor.org/rfc/rfc7230#section-2.7.1> | ||
#[derive(Clone, Hash, PartialEq, Eq, Debug)] | ||
pub struct Authority { | ||
hostname: String, | ||
port: Port, | ||
} | ||
|
||
impl Authority { | ||
fn inner_from_str(value: &str) -> Result<Self, AuthorityError> { | ||
let uri: Uri = value.parse().map_err(AuthorityError::InvalidUri)?; | ||
let authority = uri.authority().ok_or(AuthorityError::MissingHost)?; | ||
let hostname = authority.host(); | ||
let maybe_port = &authority.as_str()[hostname.len()..]; | ||
|
||
// After the host segment, the authority may contain a port such as `fooo:33`, `foo:*` or `foo` | ||
let port = match maybe_port.split_once(':') { | ||
Some((_, "*")) => Port::Any, | ||
Some((_, p)) => { | ||
let port_u16: u16 = | ||
p.parse().map_err(|e: std::num::ParseIntError| AuthorityError::InvalidPort(e.to_string()))?; | ||
|
||
// Omit default port to allow both requests with and without the default port. | ||
match default_port(uri.scheme_str()) { | ||
Some(p) if p == port_u16 => Port::Default, | ||
_ => port_u16.into(), | ||
} | ||
} | ||
None => Port::Default, | ||
}; | ||
|
||
Ok(Self { hostname: hostname.to_string(), port }) | ||
} | ||
} | ||
|
||
/// Error that can happen when parsing an URI authority fails. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum AuthorityError { | ||
/// Invalid URI. | ||
#[error("{0}")] | ||
InvalidUri(InvalidUri), | ||
/// Invalid port. | ||
#[error("{0}")] | ||
InvalidPort(String), | ||
/// The host was not found. | ||
#[error("The host was not found")] | ||
MissingHost, | ||
} | ||
|
||
impl<'a> TryFrom<&'a str> for Authority { | ||
type Error = AuthorityError; | ||
|
||
fn try_from(value: &'a str) -> Result<Self, Self::Error> { | ||
Self::inner_from_str(value) | ||
} | ||
} | ||
|
||
impl TryFrom<String> for Authority { | ||
type Error = AuthorityError; | ||
|
||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
Self::inner_from_str(&value) | ||
} | ||
} | ||
|
||
impl TryFrom<std::net::SocketAddr> for Authority { | ||
type Error = AuthorityError; | ||
|
||
fn try_from(sockaddr: SocketAddr) -> Result<Self, Self::Error> { | ||
Self::inner_from_str(&sockaddr.to_string()) | ||
} | ||
} | ||
|
||
/// Represent the URL patterns that is whitelisted. | ||
#[derive(Default, Debug, Clone)] | ||
pub struct WhitelistedHosts(Router<Port>); | ||
|
||
impl<T> From<T> for WhitelistedHosts | ||
where | ||
T: IntoIterator<Item = Authority>, | ||
{ | ||
fn from(value: T) -> Self { | ||
let mut router = Router::new(); | ||
|
||
for auth in value.into_iter() { | ||
router.add(&auth.hostname, auth.port); | ||
} | ||
|
||
Self(router) | ||
} | ||
} | ||
|
||
impl WhitelistedHosts { | ||
fn recognize(&self, other: &Authority) -> bool { | ||
if let Ok(p) = self.0.recognize(&other.hostname) { | ||
let p = p.handler(); | ||
|
||
match (p, &other.port) { | ||
(Port::Any, _) => true, | ||
(Port::Default, Port::Default) => true, | ||
(Port::Fixed(p1), Port::Fixed(p2)) if p1 == p2 => true, | ||
_ => false, | ||
} | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
/// Policy for validating the `HTTP host header`. | ||
#[derive(Debug, Clone)] | ||
pub enum AllowHosts { | ||
/// Allow all hosts (no filter). | ||
Any, | ||
/// Allow only specified hosts. | ||
Only(WhitelistedHosts), | ||
} | ||
|
||
impl AllowHosts { | ||
/// Verify a host. | ||
pub fn verify(&self, value: &str) -> Result<(), Error> { | ||
let auth = Authority::try_from(value) | ||
.map_err(|_| Error::HttpHeaderRejected("host", format!("Invalid authority: {value}")))?; | ||
|
||
if let AllowHosts::Only(url_pat) = self { | ||
if !url_pat.recognize(&auth) { | ||
return Err(Error::HttpHeaderRejected("host", value.into())); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn default_port(scheme: Option<&str>) -> Option<u16> { | ||
match scheme { | ||
Some("http") | Some("ws") => Some(80), | ||
Some("https") | Some("wss") => Some(443), | ||
Some("ftp") => Some(21), | ||
_ => None, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{AllowHosts, Authority, Port}; | ||
|
||
fn authority(host: &str, port: Port) -> Authority { | ||
Authority { hostname: host.to_owned(), port } | ||
} | ||
|
||
#[test] | ||
fn should_parse_valid_authority() { | ||
assert_eq!(Authority::try_from("http://parity.io").unwrap(), authority("parity.io", Port::Default)); | ||
assert_eq!(Authority::try_from("https://parity.io:8443").unwrap(), authority("parity.io", Port::Fixed(8443))); | ||
assert_eq!(Authority::try_from("chrome-extension://124.0.0.1").unwrap(), authority("124.0.0.1", Port::Default)); | ||
assert_eq!(Authority::try_from("http://*.domain:*/somepath").unwrap(), authority("*.domain", Port::Any)); | ||
assert_eq!(Authority::try_from("parity.io").unwrap(), authority("parity.io", Port::Default)); | ||
assert_eq!(Authority::try_from("127.0.0.1:8845").unwrap(), authority("127.0.0.1", Port::Fixed(8845))); | ||
assert_eq!( | ||
Authority::try_from("http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:9933/").unwrap(), | ||
authority("[2001:db8:85a3:8d3:1319:8a2e:370:7348]", Port::Fixed(9933)) | ||
); | ||
assert_eq!( | ||
Authority::try_from("http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/").unwrap(), | ||
authority("[2001:db8:85a3:8d3:1319:8a2e:370:7348]", Port::Default) | ||
); | ||
assert_eq!( | ||
Authority::try_from("https://user:password@example.com/tmp/foo").unwrap(), | ||
authority("example.com", Port::Default) | ||
); | ||
} | ||
|
||
#[test] | ||
fn should_not_parse_invalid_authority() { | ||
assert!(Authority::try_from("/foo/bar").is_err()); | ||
assert!(Authority::try_from("user:password").is_err()); | ||
assert!(Authority::try_from("parity.io/somepath").is_err()); | ||
assert!(Authority::try_from("127.0.0.1:8545/somepath").is_err()); | ||
} | ||
|
||
#[test] | ||
fn should_allow_when_validation_is_disabled() { | ||
assert!((AllowHosts::Any).verify("any").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn should_reject_if_header_not_on_the_list() { | ||
assert!((AllowHosts::Only(vec![].into())).verify("parity.io").is_err()); | ||
} | ||
|
||
#[test] | ||
fn should_accept_if_on_the_list() { | ||
assert!(AllowHosts::Only(vec![Authority::try_from("parity.io").unwrap()].into()).verify("parity.io").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn should_accept_if_on_the_list_with_port() { | ||
assert!((AllowHosts::Only(vec![Authority::try_from("parity.io:443").unwrap()].into())) | ||
.verify("parity.io:443") | ||
.is_ok()); | ||
assert!(AllowHosts::Only(vec![Authority::try_from("parity.io").unwrap()].into()) | ||
.verify("parity.io:443") | ||
.is_err()); | ||
} | ||
|
||
#[test] | ||
fn should_support_wildcards() { | ||
assert!((AllowHosts::Only(vec![Authority::try_from("*.web3.site:*").unwrap()].into())) | ||
.verify("parity.web3.site:8180") | ||
.is_ok()); | ||
assert!((AllowHosts::Only(vec![Authority::try_from("*.web3.site:*").unwrap()].into())) | ||
.verify("parity.web3.site") | ||
.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn should_accept_with_and_without_default_port() { | ||
assert!(AllowHosts::Only(vec![Authority::try_from("https://parity.io:443").unwrap()].into()) | ||
.verify("https://parity.io") | ||
.is_ok()); | ||
|
||
assert!(AllowHosts::Only(vec![Authority::try_from("https://parity.io").unwrap()].into()) | ||
.verify("https://parity.io:443") | ||
.is_ok()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah; can't do
authority.port()
because port may bve eg*
I guess!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes and also
http::Uri::port
won't tell us whether it has a default port or not as well, so we need to do some manual stuff here.so a little bit hacky but this was cleanest I could come up with