-
Notifications
You must be signed in to change notification settings - Fork 11.4k
fix(network-proxy): recheck network proxy connect targets #19999
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,157 @@ | ||
| use crate::policy::is_non_public_ip; | ||
| use crate::state::NetworkProxyState; | ||
| use rama_core::Service; | ||
| use rama_core::error::BoxError; | ||
| use rama_core::error::ErrorExt as _; | ||
| use rama_core::error::OpaqueError; | ||
| use rama_core::extensions::ExtensionsMut; | ||
| use rama_net::address::ProxyAddress; | ||
| use rama_net::client::EstablishedClientConnection; | ||
| use rama_net::transport::TryRefIntoTransportContext; | ||
| use rama_tcp::TcpStream; | ||
| use rama_tcp::client::TcpStreamConnector; | ||
| use rama_tcp::client::service::TcpConnector; | ||
| use std::io; | ||
| use std::net::SocketAddr; | ||
| use std::sync::Arc; | ||
|
|
||
| #[derive(Clone)] | ||
| pub(crate) struct TargetCheckedTcpConnector { | ||
| policy: TargetPolicy, | ||
| } | ||
|
|
||
| impl TargetCheckedTcpConnector { | ||
| pub(crate) fn new(state: Arc<NetworkProxyState>) -> Self { | ||
| Self { | ||
| policy: TargetPolicy::State(state), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn from_allow_local_binding(allow_local_binding: bool) -> Self { | ||
| Self { | ||
| policy: TargetPolicy::Config { | ||
| allow_local_binding, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<Input> Service<Input> for TargetCheckedTcpConnector | ||
| where | ||
| Input: TryRefIntoTransportContext + Send + ExtensionsMut + 'static, | ||
| Input::Error: Into<BoxError> + Send + Sync + 'static, | ||
| { | ||
| type Output = EstablishedClientConnection<TcpStream, Input>; | ||
| type Error = BoxError; | ||
|
|
||
| async fn serve(&self, input: Input) -> Result<Self::Output, Self::Error> { | ||
| if input.extensions().get::<ProxyAddress>().is_some() { | ||
| return TcpConnector::new().serve(input).await; | ||
| } | ||
|
|
||
| TcpConnector::new() | ||
| .with_connector(TargetCheckedStreamConnector { | ||
| policy: self.policy.clone(), | ||
| }) | ||
| .serve(input) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct TargetCheckedStreamConnector { | ||
| policy: TargetPolicy, | ||
| } | ||
|
|
||
| impl TcpStreamConnector for TargetCheckedStreamConnector { | ||
| type Error = BoxError; | ||
|
|
||
| async fn connect(&self, addr: SocketAddr) -> Result<TcpStream, Self::Error> { | ||
| if !self.policy.allow_local_binding().await? && is_non_public_ip(addr.ip()) { | ||
| return Err(io::Error::new( | ||
| io::ErrorKind::PermissionDenied, | ||
| "network target rejected by policy", | ||
| ) | ||
| .into()); | ||
| } | ||
|
|
||
| tokio::net::TcpStream::connect(addr) | ||
| .await | ||
| .map(TcpStream::from) | ||
| .map_err(Into::into) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| enum TargetPolicy { | ||
| Config { allow_local_binding: bool }, | ||
| State(Arc<NetworkProxyState>), | ||
| } | ||
|
|
||
| impl TargetPolicy { | ||
| async fn allow_local_binding(&self) -> Result<bool, BoxError> { | ||
| match self { | ||
| Self::Config { | ||
| allow_local_binding, | ||
| } => Ok(*allow_local_binding), | ||
| Self::State(state) => state.allow_local_binding().await.map_err(|err| { | ||
| let err: BoxError = err.into(); | ||
| OpaqueError::from_boxed(err) | ||
| .context("read network proxy config") | ||
| .into_boxed() | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::config::NetworkProxySettings; | ||
| use crate::state::network_proxy_state_for_policy; | ||
| use rama_net::address::HostWithPort; | ||
| use std::net::Ipv4Addr; | ||
| use tokio::net::TcpListener; | ||
|
|
||
| #[tokio::test(flavor = "current_thread")] | ||
| async fn direct_connector_rejects_non_public_target_when_local_binding_disabled() { | ||
| let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) | ||
| .await | ||
| .expect("bind local listener"); | ||
| let target = listener.local_addr().expect("local addr"); | ||
| let connector = TargetCheckedTcpConnector::new(Arc::new(network_proxy_state_for_policy( | ||
| NetworkProxySettings::default(), | ||
| ))); | ||
|
|
||
| let request: rama_tcp::client::Request = | ||
| rama_tcp::client::Request::new(HostWithPort::from(target)); | ||
| let err = Service::serve(&connector, request) | ||
| .await | ||
| .expect_err("local target should be rejected"); | ||
|
|
||
| assert!( | ||
| format!("{err:?}").contains("network target rejected by policy"), | ||
| "unexpected error: {err:?}" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "current_thread")] | ||
| async fn direct_connector_allows_non_public_target_when_local_binding_enabled() { | ||
| let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) | ||
| .await | ||
| .expect("bind local listener"); | ||
| let target = listener.local_addr().expect("local addr"); | ||
| let connector = TargetCheckedTcpConnector::new(Arc::new(network_proxy_state_for_policy( | ||
| NetworkProxySettings { | ||
| allow_local_binding: true, | ||
| ..NetworkProxySettings::default() | ||
| }, | ||
| ))); | ||
|
|
||
| let request: rama_tcp::client::Request = | ||
| rama_tcp::client::Request::new(HostWithPort::from(target)); | ||
| let result = Service::serve(&connector, request).await; | ||
|
|
||
| assert!(result.is_ok(), "local target should be allowed: {result:?}"); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| mod certs; | ||
| mod config; | ||
| mod connect_policy; | ||
| mod http_proxy; | ||
| mod mitm; | ||
| mod network_policy; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Rejecting all non-public socket addresses when
allow_local_bindingis false regresses a supported policy path:NetworkProxyState::host_blockedintentionally permits explicitly allowlisted local literals. With this connector check, allowlisted targets likelocalhost/10.0.0.1pass host policy but are still denied at connect time (PermissionDenied), so valid configurations now fail.Useful? React with 👍 / 👎.