Skip to content

Commit 5bca951

Browse files
authored
Merge pull request #729 from swimos/ws-protocol
Adds WS_NO_SUBPROTOCOL_CHECK
2 parents 17eb6dc + de73315 commit 5bca951

File tree

1 file changed

+34
-1
lines changed
  • server/swimos_server_app/src/server/http

1 file changed

+34
-1
lines changed

server/swimos_server_app/src/server/http/mod.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use ratchet::{
3838
WebSocketStream,
3939
};
4040
use ratchet_core::server::UpgradeRequestParts;
41+
use std::env::{var, VarError};
4142
use std::{
4243
marker::PhantomData,
4344
net::SocketAddr,
@@ -61,6 +62,7 @@ use tokio::{
6162
sync::mpsc,
6263
time::{sleep, Sleep},
6364
};
65+
use tracing::{debug, warn};
6466

6567
mod resolver;
6668
#[cfg(test)]
@@ -375,6 +377,37 @@ where
375377
}
376378
}
377379

380+
/// Returns whether to validate the upgrade's requested subprotocol. Users may disable this check
381+
/// by setting WS_NO_SUBPROTOCOL_CHECK=true if they are running a legacy version of Swim which would be
382+
/// incompatible with this version. If the environment variable has not been set or it is invalid
383+
/// then the subprotocol will still be verified and a log entry will be emitted.
384+
///
385+
/// Defaults to `true`.
386+
fn validate_subprotocol() -> bool {
387+
match var("WS_NO_SUBPROTOCOL_CHECK") {
388+
Ok(s) => match s.as_str() {
389+
"true" => {
390+
debug!("Skipping subprotocol check due to WS_NO_SUBPROTOCOL_CHECK=true");
391+
false
392+
}
393+
"false" => true,
394+
s => {
395+
warn!("WS_NO_SUBPROTOCOL_CHECK set to an invalid value. Ignoring. Should be 'true' or 'false': {}", s);
396+
true
397+
}
398+
},
399+
Err(VarError::NotPresent) => true,
400+
Err(VarError::NotUnicode(value)) => {
401+
let value = value.to_string_lossy();
402+
warn!(
403+
"WS_NO_SUBPROTOCOL_CHECK set to non-Unicode value. Ignoring: {}",
404+
value
405+
);
406+
true
407+
}
408+
}
409+
}
410+
378411
/// Perform the websocket negotiation and assign the upgrade future to the target parameter.
379412
fn perform_upgrade<Ext, Sock, B>(
380413
config: WebSocketConfig,
@@ -392,7 +425,7 @@ where
392425
{
393426
match result {
394427
Ok(parts) => {
395-
if parts.subprotocol.is_none() {
428+
if parts.subprotocol.is_none() && validate_subprotocol() {
396429
// We can only speak warp0 so fail the upgrade.
397430
let response = swimos_http::fail_upgrade("Failed to negotiate warp0 subprotocol");
398431
(response, None)

0 commit comments

Comments
 (0)