@@ -38,6 +38,7 @@ use ratchet::{
38
38
WebSocketStream ,
39
39
} ;
40
40
use ratchet_core:: server:: UpgradeRequestParts ;
41
+ use std:: env:: { var, VarError } ;
41
42
use std:: {
42
43
marker:: PhantomData ,
43
44
net:: SocketAddr ,
@@ -61,6 +62,7 @@ use tokio::{
61
62
sync:: mpsc,
62
63
time:: { sleep, Sleep } ,
63
64
} ;
65
+ use tracing:: { debug, warn} ;
64
66
65
67
mod resolver;
66
68
#[ cfg( test) ]
@@ -375,6 +377,37 @@ where
375
377
}
376
378
}
377
379
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
+
378
411
/// Perform the websocket negotiation and assign the upgrade future to the target parameter.
379
412
fn perform_upgrade < Ext , Sock , B > (
380
413
config : WebSocketConfig ,
@@ -392,7 +425,7 @@ where
392
425
{
393
426
match result {
394
427
Ok ( parts) => {
395
- if parts. subprotocol . is_none ( ) {
428
+ if parts. subprotocol . is_none ( ) && validate_subprotocol ( ) {
396
429
// We can only speak warp0 so fail the upgrade.
397
430
let response = swimos_http:: fail_upgrade ( "Failed to negotiate warp0 subprotocol" ) ;
398
431
( response, None )
0 commit comments