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

refactor: server host filtering #1174

Merged
merged 15 commits into from
Aug 9, 2023
Merged

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Aug 8, 2023

Cleans up the host filtering by supporting ipv6 addresses, fixes a bug when host filtering with * is configured when a request is missing the default port.

In addition the API on the server is stricter and hosts filtering with invalid authorities are now rejected which wasn't the case before.

Close #1075

Cleans up the host filtering by allowing `ipv6 addresses`, fixes a bug when
host filtering with `*` is configured when a request is missing the default port.

In addition the API on the server is stricter and hosts filtering with invalid
authorities are now rejected which wasn't the case before.
@niklasad1 niklasad1 requested a review from a team as a code owner August 8, 2023 11:18
let uri: Uri = s.parse().map_err(|e: InvalidUri| e.to_string())?;
let authority = uri.authority().ok_or_else(|| "HTTP Host must contain authority".to_owned())?;
let hostname = authority.host();
let maybe_port = &authority.as_str()[hostname.len()..];
Copy link
Collaborator

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!

Copy link
Member Author

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

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

Couple of small nits but looks cleaner to me :)

/// Enables host filtering and allow only the specified hosts.
///
/// Default: allow all.
pub fn host_filter<T: IntoIterator<Item = Authority>>(mut self, allow_only: T) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to copy my element comment:

It could accept anything that can TryFrom into the Authority type and then it'd have to return the error when host_filter is called if there is one. Prolly a bit nicer cos then you can just do

.host_filter([
    "foo.com",
    "something_else.com:*"
])?

for instance which would be quite cool.

(I think it should return an AuthorityError enum if the conversion fails personally rather than a String)

server/src/server.rs Outdated Show resolved Hide resolved
server/src/server.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 merged commit 38193f0 into master Aug 9, 2023
15 checks passed
@niklasad1 niklasad1 deleted the na-refactor-host-filtering branch August 9, 2023 12:29
@niklasad1 niklasad1 added the breaking change Breaking change in the public APIs label Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Breaking change in the public APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

host filtering: support ipv6 addresses
3 participants