@@ -939,9 +939,18 @@ fn build_forward_policy_deny_ocsf_event(
939939 . build ( )
940940}
941941
942+ fn destination_denial_detail ( kind : DestinationDenialKind ) -> & ' static str {
943+ match kind {
944+ DestinationDenialKind :: TrustedGateway => "trusted-gateway check failed" ,
945+ DestinationDenialKind :: InvalidAllowedIps => "invalid allowed_ips in policy" ,
946+ DestinationDenialKind :: AllowedIps => "allowed_ips check failed" ,
947+ DestinationDenialKind :: DeclaredEndpoint => "declared endpoint check failed" ,
948+ DestinationDenialKind :: InternalAddress => "internal address" ,
949+ }
950+ }
951+
942952#[ allow( clippy:: too_many_arguments) ]
943- async fn deny_connect_destination (
944- client : & mut TcpStream ,
953+ fn build_connect_destination_deny_ocsf_event (
945954 denial : & DestinationDenial ,
946955 peer_addr : SocketAddr ,
947956 host : & str ,
@@ -950,24 +959,15 @@ async fn deny_connect_destination(
950959 pid : & str ,
951960 ancestors : & str ,
952961 cmdline : & str ,
953- decision : & EgressDecision ,
954- denial_tx : & Option < mpsc:: UnboundedSender < DenialEvent > > ,
955- activity_tx : & Option < ActivitySender > ,
956- ) -> Result < ( ) > {
957- let detail = match denial. kind {
958- DestinationDenialKind :: TrustedGateway => "trusted-gateway check failed" ,
959- DestinationDenialKind :: InvalidAllowedIps => "invalid allowed_ips in policy" ,
960- DestinationDenialKind :: AllowedIps => "allowed_ips check failed" ,
961- DestinationDenialKind :: DeclaredEndpoint => "declared endpoint check failed" ,
962- DestinationDenialKind :: InternalAddress => "internal address" ,
963- } ;
962+ ) -> openshell_ocsf:: OcsfEvent {
963+ let detail = destination_denial_detail ( denial. kind ) ;
964964 let message = if denial. kind == DestinationDenialKind :: InternalAddress {
965965 format ! ( "CONNECT blocked: internal address {host}:{port}" )
966966 } else {
967967 format ! ( "CONNECT blocked: {detail} for {host}:{port}" )
968968 } ;
969969
970- let event = NetworkActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
970+ NetworkActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
971971 . activity ( ActivityId :: Open )
972972 . action ( ActionId :: Denied )
973973 . disposition ( DispositionId :: Blocked )
@@ -979,8 +979,68 @@ async fn deny_connect_destination(
979979 . firewall_rule ( "-" , "ssrf" )
980980 . message ( message)
981981 . status_detail ( & denial. reason )
982- . build ( ) ;
983- ocsf_emit ! ( event) ;
982+ . build ( )
983+ }
984+
985+ #[ allow( clippy:: too_many_arguments) ]
986+ fn build_forward_destination_deny_ocsf_event (
987+ denial : & DestinationDenial ,
988+ peer_addr : SocketAddr ,
989+ method : & str ,
990+ host : & str ,
991+ port : u16 ,
992+ path : & str ,
993+ binary : & str ,
994+ pid : & str ,
995+ ancestors : & str ,
996+ cmdline : & str ,
997+ policy : & str ,
998+ ) -> openshell_ocsf:: OcsfEvent {
999+ let detail = destination_denial_detail ( denial. kind ) ;
1000+ let log_detail = if denial. kind == DestinationDenialKind :: InternalAddress {
1001+ "internal IP without allowed_ips"
1002+ } else {
1003+ detail
1004+ } ;
1005+
1006+ HttpActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
1007+ . activity ( ActivityId :: Other )
1008+ . action ( ActionId :: Denied )
1009+ . disposition ( DispositionId :: Blocked )
1010+ . severity ( SeverityId :: Medium )
1011+ . status ( StatusId :: Failure )
1012+ . http_request ( HttpRequest :: new (
1013+ method,
1014+ OcsfUrl :: new ( "http" , host, path, port) ,
1015+ ) )
1016+ . dst_endpoint ( Endpoint :: from_domain ( host, port) )
1017+ . src_endpoint ( Endpoint :: from_ip ( peer_addr. ip ( ) , peer_addr. port ( ) ) )
1018+ . actor_process ( Process :: from_bypass ( binary, pid, ancestors) . with_cmd_line ( cmdline) )
1019+ . firewall_rule ( policy, "ssrf" )
1020+ . message ( format ! ( "FORWARD blocked: {log_detail} for {host}:{port}" ) )
1021+ . status_detail ( & denial. reason )
1022+ . build ( )
1023+ }
1024+
1025+ #[ allow( clippy:: too_many_arguments) ]
1026+ async fn deny_connect_destination (
1027+ client : & mut TcpStream ,
1028+ denial : & DestinationDenial ,
1029+ peer_addr : SocketAddr ,
1030+ host : & str ,
1031+ port : u16 ,
1032+ binary : & str ,
1033+ pid : & str ,
1034+ ancestors : & str ,
1035+ cmdline : & str ,
1036+ decision : & EgressDecision ,
1037+ denial_tx : & Option < mpsc:: UnboundedSender < DenialEvent > > ,
1038+ activity_tx : & Option < ActivitySender > ,
1039+ ) -> Result < ( ) > {
1040+ let detail = destination_denial_detail ( denial. kind ) ;
1041+ ocsf_emit ! ( build_connect_destination_deny_ocsf_event(
1042+ denial, peer_addr, host, port, binary, pid, ancestors, cmdline,
1043+ ) ) ;
9841044
9851045 emit_denial (
9861046 denial_tx,
@@ -1026,37 +1086,10 @@ async fn deny_forward_destination(
10261086 denial_tx : Option < & mpsc:: UnboundedSender < DenialEvent > > ,
10271087 activity_tx : Option < & ActivitySender > ,
10281088) -> Result < ( ) > {
1029- let detail = match denial. kind {
1030- DestinationDenialKind :: TrustedGateway => "trusted-gateway check failed" ,
1031- DestinationDenialKind :: InvalidAllowedIps => "invalid allowed_ips in policy" ,
1032- DestinationDenialKind :: AllowedIps => "allowed_ips check failed" ,
1033- DestinationDenialKind :: DeclaredEndpoint => "declared endpoint check failed" ,
1034- DestinationDenialKind :: InternalAddress => "internal address" ,
1035- } ;
1036- let log_detail = if denial. kind == DestinationDenialKind :: InternalAddress {
1037- "internal IP without allowed_ips"
1038- } else {
1039- detail
1040- } ;
1041-
1042- let event = HttpActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
1043- . activity ( ActivityId :: Other )
1044- . action ( ActionId :: Denied )
1045- . disposition ( DispositionId :: Blocked )
1046- . severity ( SeverityId :: Medium )
1047- . status ( StatusId :: Failure )
1048- . http_request ( HttpRequest :: new (
1049- method,
1050- OcsfUrl :: new ( "http" , host, path, port) ,
1051- ) )
1052- . dst_endpoint ( Endpoint :: from_domain ( host, port) )
1053- . src_endpoint ( Endpoint :: from_ip ( peer_addr. ip ( ) , peer_addr. port ( ) ) )
1054- . actor_process ( Process :: from_bypass ( binary, pid, ancestors) . with_cmd_line ( cmdline) )
1055- . firewall_rule ( policy, "ssrf" )
1056- . message ( format ! ( "FORWARD blocked: {log_detail} for {host}:{port}" ) )
1057- . status_detail ( & denial. reason )
1058- . build ( ) ;
1059- ocsf_emit ! ( event) ;
1089+ let detail = destination_denial_detail ( denial. kind ) ;
1090+ ocsf_emit ! ( build_forward_destination_deny_ocsf_event(
1091+ denial, peer_addr, method, host, port, path, binary, pid, ancestors, cmdline, policy,
1092+ ) ) ;
10601093
10611094 emit_denial_simple (
10621095 denial_tx,
0 commit comments