@@ -3367,6 +3367,10 @@ network_policies:
33673367 close_on_first_message : bool ,
33683368 message_received : Option < Arc < tokio:: sync:: Notify > > ,
33693369 release_message : Option < Arc < tokio:: sync:: Notify > > ,
3370+ // Opt-in capture of the preflight request context's workspace. Kept
3371+ // separate from `observed` so it does not perturb the session-event
3372+ // ordering the other tests assert on.
3373+ preflight_observed : Option < tokio:: sync:: mpsc:: UnboundedSender < String > > ,
33703374 }
33713375
33723376 #[ tonic:: async_trait]
@@ -3433,11 +3437,20 @@ network_policies:
34333437 let close_on_first_message = self . close_on_first_message ;
34343438 let message_received = self . message_received . clone ( ) ;
34353439 let release_message = self . release_message . clone ( ) ;
3440+ let preflight_observed = self . preflight_observed . clone ( ) ;
34363441 let ( responses_tx, responses_rx) = tokio:: sync:: mpsc:: channel ( 4 ) ;
34373442 tokio:: spawn ( async move {
34383443 while let Ok ( Some ( request) ) = requests. message ( ) . await {
34393444 let response = match request. event {
3440- Some ( web_socket_session_event:: Event :: Preflight ( _) ) => {
3445+ Some ( web_socket_session_event:: Event :: Preflight ( preflight) ) => {
3446+ if let Some ( preflight_observed) = & preflight_observed {
3447+ let workspace = preflight
3448+ . context
3449+ . as_ref ( )
3450+ . map ( |context| context. workspace . clone ( ) )
3451+ . unwrap_or_default ( ) ;
3452+ let _ = preflight_observed. send ( workspace) ;
3453+ }
34413454 Some ( WebSocketSessionEventResult {
34423455 result : Some (
34433456 web_socket_session_event_result:: Result :: PreflightDecision (
@@ -3599,6 +3612,7 @@ network_policies:
35993612 request_id : "request" . into ( ) ,
36003613 sandbox_id : "sandbox" . into ( ) ,
36013614 sandbox_name : "sandbox-name" . into ( ) ,
3615+ workspace : "workspace" . into ( ) ,
36023616 scheme : scheme. into ( ) ,
36033617 host : "api.openai.com" . into ( ) ,
36043618 port : if scheme == "wss" { 443 } else { 80 } ,
@@ -3617,6 +3631,80 @@ network_policies:
36173631 )
36183632 }
36193633
3634+ // Companion to the HTTP-path assertion in openshell-supervisor-middleware:
3635+ // proves the WebSocket preflight forwards the request context's workspace to
3636+ // the middleware service.
3637+ #[ tokio:: test]
3638+ async fn websocket_preflight_forwards_workspace_to_middleware ( ) {
3639+ use openshell_core:: proto:: SupervisorMiddlewareService ;
3640+ use openshell_supervisor_middleware:: { ChainEntry , MiddlewareRegistry , OnError } ;
3641+
3642+ let ( preflight_tx, mut preflight_rx) = tokio:: sync:: mpsc:: unbounded_channel ( ) ;
3643+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" )
3644+ . await
3645+ . expect ( "bind WebSocket middleware" ) ;
3646+ let address = listener. local_addr ( ) . expect ( "middleware address" ) ;
3647+ let ( shutdown_tx, shutdown_rx) = tokio:: sync:: oneshot:: channel ( ) ;
3648+ let server = tonic:: transport:: Server :: builder ( )
3649+ . add_service ( SupervisorMiddlewareServer :: new ( OpenAiWebSocketRedactor {
3650+ preflight_observed : Some ( preflight_tx) ,
3651+ ..Default :: default ( )
3652+ } ) )
3653+ . serve_with_incoming_shutdown ( TcpListenerStream :: new ( listener) , async {
3654+ let _ = shutdown_rx. await ;
3655+ } ) ;
3656+ let server_task = tokio:: spawn ( server) ;
3657+ let registry = MiddlewareRegistry :: connect_services (
3658+ Vec :: new ( ) ,
3659+ vec ! [ SupervisorMiddlewareService {
3660+ name: "openai-redactor" . into( ) ,
3661+ grpc_endpoint: format!( "http://{address}" ) ,
3662+ max_payload_bytes: openshell_supervisor_middleware:: MAX_MIDDLEWARE_PAYLOAD_BYTES
3663+ as u64 ,
3664+ timeout: "2s" . into( ) ,
3665+ tls_ca_cert_pem: Vec :: new( ) ,
3666+ audience: String :: new( ) ,
3667+ allow_insecure_transport: false ,
3668+ } ] ,
3669+ )
3670+ . await
3671+ . expect ( "connect middleware" ) ;
3672+ let runner = openshell_supervisor_middleware:: ChainRunner :: from_registry ( registry) ;
3673+ runner
3674+ . preflight_websocket (
3675+ & [ ChainEntry {
3676+ name : "redact-openai" . into ( ) ,
3677+ implementation : "openai-redactor" . into ( ) ,
3678+ order : 0 ,
3679+ config : prost_types:: Struct :: default ( ) ,
3680+ on_error : OnError :: FailClosed ,
3681+ } ] ,
3682+ openshell_supervisor_middleware:: WebSocketPreflightInput {
3683+ session_id : "session" . into ( ) ,
3684+ request_id : "request" . into ( ) ,
3685+ sandbox_id : "sandbox" . into ( ) ,
3686+ sandbox_name : "sandbox-name" . into ( ) ,
3687+ workspace : "team-a" . into ( ) ,
3688+ scheme : "wss" . into ( ) ,
3689+ host : "api.openai.com" . into ( ) ,
3690+ port : 443 ,
3691+ path : "/v1/responses" . into ( ) ,
3692+ requested_subprotocols : Vec :: new ( ) ,
3693+ } ,
3694+ )
3695+ . await
3696+ . expect ( "preflight" ) ;
3697+
3698+ assert_eq ! (
3699+ preflight_rx. recv( ) . await ,
3700+ Some ( "team-a" . to_string( ) ) ,
3701+ "WebSocket preflight must forward the workspace to the middleware"
3702+ ) ;
3703+
3704+ let _ = shutdown_tx. send ( ( ) ) ;
3705+ let _ = server_task. await ;
3706+ }
3707+
36203708 async fn assert_invalid_close_termination ( payload : Vec < u8 > , expected_close_code : u16 ) {
36213709 let ( mut session, mut observed, shutdown_tx, server_task) =
36223710 recording_middleware_session ( "wss" ) . await ;
@@ -4484,6 +4572,7 @@ network_policies:
44844572 request_id : "request" . into ( ) ,
44854573 sandbox_id : "sandbox" . into ( ) ,
44864574 sandbox_name : "sandbox-name" . into ( ) ,
4575+ workspace : "workspace" . into ( ) ,
44874576 scheme : "wss" . into ( ) ,
44884577 host : "api.openai.com" . into ( ) ,
44894578 port : 443 ,
@@ -4632,6 +4721,7 @@ network_policies:
46324721 request_id : "request" . into ( ) ,
46334722 sandbox_id : "sandbox" . into ( ) ,
46344723 sandbox_name : "sandbox-name" . into ( ) ,
4724+ workspace : "workspace" . into ( ) ,
46354725 scheme : "wss" . into ( ) ,
46364726 host : "api.openai.com" . into ( ) ,
46374727 port : 443 ,
@@ -4752,6 +4842,7 @@ network_policies:
47524842 request_id : "request" . into ( ) ,
47534843 sandbox_id : "sandbox" . into ( ) ,
47544844 sandbox_name : "sandbox-name" . into ( ) ,
4845+ workspace : "workspace" . into ( ) ,
47554846 scheme : "wss" . into ( ) ,
47564847 host : "api.openai.com" . into ( ) ,
47574848 port : 443 ,
0 commit comments