Skip to content

Commit

Permalink
Fix behavior when connecting wss url without TLS support (#437)
Browse files Browse the repository at this point in the history
* Fail before connecting if TLS support is not present

* Add test for wss:// explicit failure when TLS support is not compiled in

* Fix: do not compile irrelevant test when  feature is not enabled
  • Loading branch information
maugier committed Aug 13, 2024
1 parent bef231d commit fb83cd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ pub fn connect_with_config<Req: IntoClientRequest>(
) -> Result<(WebSocket<MaybeTlsStream<TcpStream>>, Response)> {
let uri = request.uri();
let mode = uri_mode(uri)?;

#[cfg(not(any(feature = "native-tls", feature = "__rustls-tls")))]
if let Mode::Tls = mode {
return Err(Error::Url(UrlError::TlsFeatureNotEnabled));
}

let host = request.uri().host().ok_or(Error::Url(UrlError::NoHostName))?;
let host = if host.starts_with('[') { &host[1..host.len() - 1] } else { host };
let port = uri.port_u16().unwrap_or(match mode {
Expand Down
10 changes: 10 additions & 0 deletions tests/wss_fails_when_no_tls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![cfg(all(feature = "handshake", not(any(feature = "native-tls", feature = "__rustls-tls"))))]

use tungstenite::{connect, error::UrlError, Error};

#[test]
fn wss_url_fails_when_no_tls_support() {
let ws = connect("wss://127.0.0.1/ws");
eprintln!("{:?}", ws);
assert!(matches!(ws, Err(Error::Url(UrlError::TlsFeatureNotEnabled))))
}

0 comments on commit fb83cd1

Please sign in to comment.