@@ -832,6 +832,16 @@ impl RequestCorrelation {
832832 . state
833833 . lock ( )
834834 . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
835+ if is_middleware_invocation ( & incoming) {
836+ if incoming. action == Some ( ActionId :: Denied )
837+ && let Some ( OcsfEvent :: HttpActivity ( primary) ) = & mut state. primary
838+ {
839+ merge_middleware_denial ( primary, & incoming) ;
840+ }
841+ drop ( state) ;
842+ self . emit_correlated ( OcsfEvent :: HttpActivity ( incoming) ) ;
843+ return ;
844+ }
835845 if !state. seeded && state. primary . is_some ( ) {
836846 state. primary = Some ( OcsfEvent :: HttpActivity ( incoming) ) ;
837847 state. seeded = true ;
@@ -929,13 +939,16 @@ impl RequestCorrelation {
929939 if let Some ( code) = status {
930940 http. http_response = Some ( HttpResponse { code } ) ;
931941 }
932- if let Some ( upstream) = upstream_status. filter ( |upstream| Some ( * upstream) != status)
933- && let serde_json:: Value :: Object ( unmapped) = http
934- . base
935- . unmapped
936- . get_or_insert_with ( || serde_json:: json!( { } ) )
942+ if let serde_json:: Value :: Object ( unmapped) = http
943+ . base
944+ . unmapped
945+ . get_or_insert_with ( || serde_json:: json!( { } ) )
937946 {
938- unmapped. insert ( "upstream_response_code" . into ( ) , serde_json:: json!( upstream) ) ;
947+ unmapped. insert ( "http_event_kind" . into ( ) , "request" . into ( ) ) ;
948+ if let Some ( upstream) = upstream_status. filter ( |upstream| Some ( * upstream) != status)
949+ {
950+ unmapped. insert ( "upstream_response_code" . into ( ) , serde_json:: json!( upstream) ) ;
951+ }
939952 }
940953 if blocked || outcome == Some ( HttpOutcome :: Denied ) {
941954 if http. action != Some ( ActionId :: Denied ) {
@@ -985,7 +998,9 @@ impl RequestCorrelation {
985998 . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
986999 state. resources . enrich ( & mut event) ;
9871000 state. trace . as_mut ( ) . map ( |trace| {
988- if let OcsfEvent :: HttpActivity ( http) = & event {
1001+ if let OcsfEvent :: HttpActivity ( http) = & event
1002+ && !is_middleware_invocation ( http)
1003+ {
9891004 trace. observe_http ( http) ;
9901005 }
9911006 trace. span ( )
@@ -1012,6 +1027,32 @@ fn is_failure(event: &HttpActivityEvent) -> bool {
10121027 event. action == Some ( ActionId :: Denied ) || event. base . status == Some ( StatusId :: Failure )
10131028}
10141029
1030+ fn is_middleware_invocation ( event : & HttpActivityEvent ) -> bool {
1031+ event
1032+ . base
1033+ . unmapped
1034+ . as_ref ( )
1035+ . and_then ( |fields| fields. get ( "http_event_kind" ) )
1036+ . and_then ( serde_json:: Value :: as_str)
1037+ == Some ( "middleware_invocation" )
1038+ }
1039+
1040+ fn merge_middleware_denial ( primary : & mut HttpActivityEvent , invocation : & HttpActivityEvent ) {
1041+ primary. action = invocation. action ;
1042+ primary. disposition = invocation. disposition ;
1043+ primary. base . status = Some ( StatusId :: Failure ) ;
1044+ raise_severity ( primary, invocation. base . severity ) ;
1045+ if invocation. base . message . is_some ( ) {
1046+ primary. base . message . clone_from ( & invocation. base . message ) ;
1047+ }
1048+ if invocation. base . status_detail . is_some ( ) {
1049+ primary
1050+ . base
1051+ . status_detail
1052+ . clone_from ( & invocation. base . status_detail ) ;
1053+ }
1054+ }
1055+
10151056fn raise_severity ( event : & mut HttpActivityEvent , severity : SeverityId ) {
10161057 if event. base . severity . as_u8 ( ) < severity. as_u8 ( ) {
10171058 event. base . severity = severity;
@@ -1176,17 +1217,19 @@ pub fn emit_phase_events(
11761217 } ;
11771218 for mut event in events {
11781219 if let OcsfEvent :: HttpActivity ( http) = & event {
1179- // Routine invocation records belong to the phase span. A late
1180- // failure is separate from the already-written primary record.
1181- let emitted = correlation
1182- . state
1183- . lock ( )
1184- . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
1185- . primary
1186- . is_none ( ) ;
1187- if !emitted || http. base . status != Some ( StatusId :: Failure ) {
1220+ if !is_middleware_invocation ( http) {
1221+ correlation. emit ( event) ;
11881222 continue ;
11891223 }
1224+ if http. action == Some ( ActionId :: Denied )
1225+ && let Some ( OcsfEvent :: HttpActivity ( primary) ) = & mut correlation
1226+ . state
1227+ . lock ( )
1228+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
1229+ . primary
1230+ {
1231+ merge_middleware_denial ( primary, http) ;
1232+ }
11901233 }
11911234 correlation
11921235 . state
0 commit comments