-
Notifications
You must be signed in to change notification settings - Fork 38
Enforce explicit https addresses for TLS connections #780
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,15 +26,25 @@ use crate::{ | |
| error::ConnectionError, | ||
| }; | ||
|
|
||
| #[derive(Clone, Hash, PartialEq, Eq)] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've actually just stolen a part of my code from the cluster branch. |
||
| #[derive(Clone, Hash, PartialEq, Eq, Default)] | ||
| pub struct Address { | ||
| uri: Uri, | ||
| } | ||
|
|
||
| impl Address { | ||
| const DEFAULT_SCHEME: &'static str = "http"; | ||
|
|
||
| pub(crate) fn into_uri(self) -> Uri { | ||
| self.uri | ||
| } | ||
|
|
||
| pub(crate) fn uri_scheme(&self) -> Option<&http::uri::Scheme> { | ||
| self.uri.scheme() | ||
| } | ||
|
|
||
| pub(crate) fn is_https(&self) -> bool { | ||
| self.uri_scheme().map_or(false, |scheme| scheme == &http::uri::Scheme::HTTPS) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for Address { | ||
|
|
@@ -44,7 +54,7 @@ impl FromStr for Address { | |
| let uri = if address.contains("://") { | ||
| address.parse::<Uri>()? | ||
| } else { | ||
| format!("http://{address}").parse::<Uri>()? | ||
| format!("{}://{}", Self::DEFAULT_SCHEME, address).parse::<Uri>()? | ||
| }; | ||
| if uri.port().is_none() { | ||
| return Err(Error::Connection(ConnectionError::MissingPort { address: address.to_owned() })); | ||
|
|
@@ -61,6 +71,6 @@ impl fmt::Display for Address { | |
|
|
||
| impl fmt::Debug for Address { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(self, f) | ||
| write!(f, "{:?}", self.uri) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,8 @@ impl ServerConnection { | |
| driver_lang: &str, | ||
| driver_version: &str, | ||
| ) -> crate::Result<(Self, Vec<DatabaseInfo>)> { | ||
| Self::validate_tls(&address, &driver_options)?; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We validate each connection (if there can be multiple) separately, which is correct. |
||
|
|
||
| let username = credentials.username().to_string(); | ||
| let request_transmitter = | ||
| Arc::new(RPCTransmitter::start(address, credentials.clone(), driver_options, &background_runtime)?); | ||
|
|
@@ -324,6 +326,25 @@ impl ServerConnection { | |
| other => Err(InternalError::UnexpectedResponseType { response_type: format!("{other:?}") }.into()), | ||
| } | ||
| } | ||
|
|
||
| fn validate_tls(address: &Address, driver_options: &DriverOptions) -> crate::Result { | ||
| match driver_options.is_tls_enabled() { | ||
| true => { | ||
| if driver_options.tls_config().is_none() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not happen, but the model allows it, so why not. |
||
| return Err(ConnectionError::AbsentTlsConfigForTlsConnection {}.into()); | ||
| } | ||
| if !address.is_https() { | ||
| return Err(ConnectionError::TlsConnectionWithoutHttps {}.into()); | ||
| } | ||
| } | ||
| false => { | ||
| if address.is_https() { | ||
| return Err(ConnectionError::NonTlsConnectionWithHttps {}.into()); | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for ServerConnection { | ||
|
|
||
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.
Tests started failing without this flag. Which is good.