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

fix: validate subprotocol mqtt #724

Merged
merged 5 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions rumqttc/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::tls;

#[cfg(feature = "websocket")]
use {
crate::websockets::{split_url, UrlError},
crate::websockets::{split_url, validate_response_headers, UrlError},
async_tungstenite::tungstenite::client::IntoClientRequest,
ws_stream_tungstenite::WsStream,
};
Expand Down Expand Up @@ -63,6 +63,9 @@ pub enum ConnectionError {
#[cfg(feature = "proxy")]
#[error("Proxy Connect: {0}")]
Proxy(#[from] ProxyError),
#[cfg(feature = "websocket")]
#[error("Websocket response validation error: ")]
ResponseValidation(#[from] crate::websockets::ValidationError),
}

/// Eventloop with all the state of a connection
Expand Down Expand Up @@ -398,7 +401,10 @@ async fn network_connect(
.headers_mut()
.insert("Sec-WebSocket-Protocol", "mqtt".parse().unwrap());

let (socket, _) = async_tungstenite::tokio::client_async(request, tcp_stream).await?;
let (socket, response) =
async_tungstenite::tokio::client_async(request, tcp_stream).await?;

validate_response_headers(response)?;

Network::new(WsStream::new(socket), options.max_incoming_packet_size)
}
Expand All @@ -411,13 +417,15 @@ async fn network_connect(

let connector = tls::rustls_connector(&tls_config).await?;

let (socket, _) = async_tungstenite::tokio::client_async_tls_with_connector(
let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
request,
tcp_stream,
Some(connector),
)
.await?;

validate_response_headers(response)?;

Network::new(WsStream::new(socket), options.max_incoming_packet_size)
}
};
Expand Down
12 changes: 9 additions & 3 deletions rumqttc/src/v5/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use {std::path::Path, tokio::net::UnixStream};

#[cfg(feature = "websocket")]
use {
crate::websockets::{split_url, UrlError},
crate::websockets::{split_url, validate_response_headers, UrlError},
async_tungstenite::tungstenite::client::IntoClientRequest,
ws_stream_tungstenite::WsStream,
};
Expand Down Expand Up @@ -62,6 +62,9 @@ pub enum ConnectionError {
#[cfg(feature = "proxy")]
#[error("Proxy Connect: {0}")]
Proxy(#[from] ProxyError),
#[cfg(feature = "websocket")]
#[error("Websocket response validation error: ")]
ResponseValidation(#[from] crate::websockets::ValidationError),
}

/// Eventloop with all the state of a connection
Expand Down Expand Up @@ -336,7 +339,9 @@ async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionErr
request = request_modifier(request).await;
}

let (socket, _) = async_tungstenite::tokio::client_async(request, tcp_stream).await?;
let (socket, response) =
async_tungstenite::tokio::client_async(request, tcp_stream).await?;
validate_response_headers(response)?;

Network::new(WsStream::new(socket), max_incoming_pkt_size)
}
Expand All @@ -353,12 +358,13 @@ async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionErr

let connector = tls::rustls_connector(&tls_config).await?;

let (socket, _) = async_tungstenite::tokio::client_async_tls_with_connector(
let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
request,
tcp_stream,
Some(connector),
)
.await?;
validate_response_headers(response)?;

Network::new(WsStream::new(socket), max_incoming_pkt_size)
}
Expand Down
29 changes: 29 additions & 0 deletions rumqttc/src/websockets.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use http::Response;

#[derive(Debug, thiserror::Error)]
pub enum UrlError {
#[error("Invalid protocol specified inside url.")]
Expand All @@ -8,6 +10,33 @@ pub enum UrlError {
Parse(#[from] http::uri::InvalidUri),
}

#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
#[error("Websocket response does not contain subprotocol header")]
SubprotocolHeaderMissing,
#[error("Websocket subprotocol header: {0}")]
SubprotocolMqtt(String),
de-sh marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) fn validate_response_headers(
response: Response<Option<Vec<u8>>>,
) -> Result<(), ValidationError> {
let val = response
.headers()
.get("Sec-WebSocket-Protocol")
.ok_or(ValidationError::SubprotocolHeaderMissing)?;

let sub_protocol = val
.to_str()
.map_err(|_| ValidationError::SubprotocolHeaderMissing)?;
de-sh marked this conversation as resolved.
Show resolved Hide resolved

if sub_protocol != "mqtt" {
return Err(ValidationError::SubprotocolMqtt(sub_protocol.to_owned()));
}
de-sh marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

pub(crate) fn split_url(url: &str) -> Result<(String, u16), UrlError> {
let uri = url.parse::<http::Uri>()?;
let domain = domain(&uri).ok_or(UrlError::Protocol)?;
Expand Down
Loading