@@ -860,6 +860,13 @@ async fn handle_tunnel_message_mk2(
860860 authorized_tunnel_routes : & HashMap < ( protocol:: mk2:: GatewayId , protocol:: mk2:: RequestId ) , ( ) > ,
861861 msg : protocol:: mk2:: ToServerTunnelMessage ,
862862) -> Result < ( ) > {
863+ let route = ( msg. message_id . gateway_id , msg. message_id . request_id ) ;
864+ let clear_route = matches ! (
865+ msg. message_kind,
866+ protocol:: mk2:: ToServerTunnelMessageKind :: ToServerResponseAbort
867+ | protocol:: mk2:: ToServerTunnelMessageKind :: ToServerWebSocketClose ( _)
868+ ) ;
869+
863870 // Extract inner data length before consuming msg
864871 let inner_data_len = tunnel_message_inner_data_len_mk2 ( & msg. message_kind ) ;
865872
@@ -868,10 +875,7 @@ async fn handle_tunnel_message_mk2(
868875 return Err ( errors:: WsError :: InvalidPacket ( "payload too large" . to_string ( ) ) . build ( ) ) ;
869876 }
870877
871- if !authorized_tunnel_routes
872- . contains_async ( & ( msg. message_id . gateway_id , msg. message_id . request_id ) )
873- . await
874- {
878+ if !authorized_tunnel_routes. contains_async ( & route) . await {
875879 return Err (
876880 errors:: WsError :: InvalidPacket ( "unauthorized tunnel message" . to_string ( ) ) . build ( ) ,
877881 ) ;
@@ -899,6 +903,10 @@ async fn handle_tunnel_message_mk2(
899903 )
900904 } ) ?;
901905
906+ if clear_route {
907+ authorized_tunnel_routes. remove_async ( & route) . await ;
908+ }
909+
902910 Ok ( ( ) )
903911}
904912
@@ -909,6 +917,13 @@ async fn handle_tunnel_message_mk1(
909917 authorized_tunnel_routes : & HashMap < ( protocol:: mk2:: GatewayId , protocol:: mk2:: RequestId ) , ( ) > ,
910918 msg : protocol:: ToServerTunnelMessage ,
911919) -> Result < ( ) > {
920+ let route = ( msg. message_id . gateway_id , msg. message_id . request_id ) ;
921+ let clear_route = matches ! (
922+ msg. message_kind,
923+ protocol:: ToServerTunnelMessageKind :: ToServerResponseAbort
924+ | protocol:: ToServerTunnelMessageKind :: ToServerWebSocketClose ( _)
925+ ) ;
926+
912927 // Ignore DeprecatedTunnelAck messages (used only for backwards compatibility)
913928 if matches ! (
914929 msg. message_kind,
@@ -925,10 +940,7 @@ async fn handle_tunnel_message_mk1(
925940 return Err ( errors:: WsError :: InvalidPacket ( "payload too large" . to_string ( ) ) . build ( ) ) ;
926941 }
927942
928- if !authorized_tunnel_routes
929- . contains_async ( & ( msg. message_id . gateway_id , msg. message_id . request_id ) )
930- . await
931- {
943+ if !authorized_tunnel_routes. contains_async ( & route) . await {
932944 return Err (
933945 errors:: WsError :: InvalidPacket ( "unauthorized tunnel message" . to_string ( ) ) . build ( ) ,
934946 ) ;
@@ -950,6 +962,10 @@ async fn handle_tunnel_message_mk1(
950962 )
951963 } ) ?;
952964
965+ if clear_route {
966+ authorized_tunnel_routes. remove_async ( & route) . await ;
967+ }
968+
953969 Ok ( ( ) )
954970}
955971
0 commit comments