@@ -826,6 +826,16 @@ impl RequestCorrelation {
826826 . state
827827 . lock ( )
828828 . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
829+ if is_middleware_invocation ( & incoming) {
830+ if incoming. action == Some ( ActionId :: Denied )
831+ && let Some ( OcsfEvent :: HttpActivity ( primary) ) = & mut state. primary
832+ {
833+ merge_middleware_denial ( primary, & incoming) ;
834+ }
835+ drop ( state) ;
836+ self . emit_correlated ( OcsfEvent :: HttpActivity ( incoming) ) ;
837+ return ;
838+ }
829839 if !state. seeded && state. primary . is_some ( ) {
830840 state. primary = Some ( OcsfEvent :: HttpActivity ( incoming) ) ;
831841 state. seeded = true ;
@@ -922,13 +932,16 @@ impl RequestCorrelation {
922932 if let Some ( code) = status {
923933 http. http_response = Some ( HttpResponse { code } ) ;
924934 }
925- if let Some ( upstream) = upstream_status. filter ( |upstream| Some ( * upstream) != status)
926- && let serde_json:: Value :: Object ( unmapped) = http
927- . base
928- . unmapped
929- . get_or_insert_with ( || serde_json:: json!( { } ) )
935+ if let serde_json:: Value :: Object ( unmapped) = http
936+ . base
937+ . unmapped
938+ . get_or_insert_with ( || serde_json:: json!( { } ) )
930939 {
931- unmapped. insert ( "upstream_response_code" . into ( ) , serde_json:: json!( upstream) ) ;
940+ unmapped. insert ( "http_event_kind" . into ( ) , "request" . into ( ) ) ;
941+ if let Some ( upstream) = upstream_status. filter ( |upstream| Some ( * upstream) != status)
942+ {
943+ unmapped. insert ( "upstream_response_code" . into ( ) , serde_json:: json!( upstream) ) ;
944+ }
932945 }
933946 if blocked || outcome == Some ( HttpOutcome :: Denied ) {
934947 if http. action != Some ( ActionId :: Denied ) {
@@ -978,7 +991,9 @@ impl RequestCorrelation {
978991 . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
979992 state. resources . enrich ( & mut event) ;
980993 state. trace . as_mut ( ) . map ( |trace| {
981- if let OcsfEvent :: HttpActivity ( http) = & event {
994+ if let OcsfEvent :: HttpActivity ( http) = & event
995+ && !is_middleware_invocation ( http)
996+ {
982997 trace. observe_http ( http) ;
983998 }
984999 trace. span ( )
@@ -1005,6 +1020,32 @@ fn is_failure(event: &HttpActivityEvent) -> bool {
10051020 event. action == Some ( ActionId :: Denied ) || event. base . status == Some ( StatusId :: Failure )
10061021}
10071022
1023+ fn is_middleware_invocation ( event : & HttpActivityEvent ) -> bool {
1024+ event
1025+ . base
1026+ . unmapped
1027+ . as_ref ( )
1028+ . and_then ( |fields| fields. get ( "http_event_kind" ) )
1029+ . and_then ( serde_json:: Value :: as_str)
1030+ == Some ( "middleware_invocation" )
1031+ }
1032+
1033+ fn merge_middleware_denial ( primary : & mut HttpActivityEvent , invocation : & HttpActivityEvent ) {
1034+ primary. action = invocation. action ;
1035+ primary. disposition = invocation. disposition ;
1036+ primary. base . status = Some ( StatusId :: Failure ) ;
1037+ raise_severity ( primary, invocation. base . severity ) ;
1038+ if invocation. base . message . is_some ( ) {
1039+ primary. base . message . clone_from ( & invocation. base . message ) ;
1040+ }
1041+ if invocation. base . status_detail . is_some ( ) {
1042+ primary
1043+ . base
1044+ . status_detail
1045+ . clone_from ( & invocation. base . status_detail ) ;
1046+ }
1047+ }
1048+
10081049fn raise_severity ( event : & mut HttpActivityEvent , severity : SeverityId ) {
10091050 if event. base . severity . as_u8 ( ) < severity. as_u8 ( ) {
10101051 event. base . severity = severity;
@@ -1169,17 +1210,19 @@ pub fn emit_phase_events(
11691210 } ;
11701211 for mut event in events {
11711212 if let OcsfEvent :: HttpActivity ( http) = & event {
1172- // Routine invocation records belong to the phase span. A late
1173- // failure is separate from the already-written primary record.
1174- let emitted = correlation
1175- . state
1176- . lock ( )
1177- . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
1178- . primary
1179- . is_none ( ) ;
1180- if !emitted || http. base . status != Some ( StatusId :: Failure ) {
1213+ if !is_middleware_invocation ( http) {
1214+ correlation. emit ( event) ;
11811215 continue ;
11821216 }
1217+ if http. action == Some ( ActionId :: Denied )
1218+ && let Some ( OcsfEvent :: HttpActivity ( primary) ) = & mut correlation
1219+ . state
1220+ . lock ( )
1221+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
1222+ . primary
1223+ {
1224+ merge_middleware_denial ( primary, http) ;
1225+ }
11831226 }
11841227 correlation
11851228 . state
0 commit comments