@@ -1272,14 +1272,27 @@ fn normalize_endpoint_ports(data: &mut serde_json::Value) {
12721272 continue ;
12731273 } ;
12741274
1275- // If "ports" already exists and is non-empty, keep it.
1275+ // A present "ports" array takes precedence over scalar "port".
1276+ // Record whether it existed before filtering so an all-zero array
1277+ // does not silently fall back to the scalar.
1278+ let ports_was_present = ep_obj. contains_key ( "ports" ) ;
1279+
1280+ // If "ports" already exists, filter out numeric zero values so
1281+ // OPA never sees a zero port. Non-numeric entries are left intact
1282+ // so downstream validation can fail closed on malformed input
1283+ // rather than silently dropping it.
1284+ if let Some ( ports) = ep_obj. get_mut ( "ports" ) . and_then ( |v| v. as_array_mut ( ) ) {
1285+ ports. retain ( |p| p. as_u64 ( ) . is_none_or ( |n| n != 0 ) ) ;
1286+ }
1287+
12761288 let has_ports = ep_obj
12771289 . get ( "ports" )
12781290 . and_then ( |v| v. as_array ( ) )
12791291 . is_some_and ( |a| !a. is_empty ( ) ) ;
12801292
1281- if !has_ports {
1282- // Promote scalar "port" to "ports" array.
1293+ if !ports_was_present && !has_ports {
1294+ // Promote scalar "port" to "ports" array only when no
1295+ // "ports" key was originally present.
12831296 let port = ep_obj
12841297 . get ( "port" )
12851298 . and_then ( serde_json:: Value :: as_u64)
@@ -1647,10 +1660,12 @@ fn proto_to_opa_data_json(proto: &ProtoSandboxPolicy, entrypoint_pid: u32) -> St
16471660 . endpoints
16481661 . iter ( )
16491662 . map ( |e| {
1650- // Normalize port/ports: ports takes precedence, then
1651- // single port promoted to array. Rego always sees "ports".
1663+ // Normalize port/ports: filter zero ports first so OPA
1664+ // never sees them, then a present `ports` array takes
1665+ // precedence over the scalar `port`. Rego always sees "ports".
1666+ let filtered: Vec < u32 > = e. ports . iter ( ) . copied ( ) . filter ( |& p| p > 0 ) . collect ( ) ;
16521667 let ports: Vec < u32 > = if !e. ports . is_empty ( ) {
1653- e . ports . clone ( )
1668+ filtered
16541669 } else if e. port > 0 {
16551670 vec ! [ e. port]
16561671 } else {
@@ -7964,4 +7979,175 @@ network_policies:
79647979 cmdline_paths : vec ! [ ] ,
79657980 }
79667981 }
7982+
7983+ #[ test]
7984+ fn normalize_endpoint_ports_filters_zero_values ( ) {
7985+ let mut data = serde_json:: json!( {
7986+ "network_policies" : {
7987+ "p" : {
7988+ "endpoints" : [
7989+ { "host" : "h1.test" , "ports" : [ 0 , 443 ] } ,
7990+ { "host" : "h2.test" , "ports" : [ 0 ] } ,
7991+ { "host" : "h3.test" , "port" : 0 } ,
7992+ { "host" : "h4.test" , "port" : 8080 } ,
7993+ ]
7994+ }
7995+ }
7996+ } ) ;
7997+ normalize_endpoint_ports ( & mut data) ;
7998+ let endpoints = data[ "network_policies" ] [ "p" ] [ "endpoints" ]
7999+ . as_array ( )
8000+ . unwrap ( ) ;
8001+
8002+ // Mixed array: zero removed, positive kept.
8003+ assert_eq ! ( endpoints[ 0 ] [ "ports" ] , serde_json:: json!( [ 443 ] ) ) ;
8004+ // All-zero array: becomes empty, no fallback port.
8005+ assert_eq ! ( endpoints[ 1 ] [ "ports" ] , serde_json:: json!( [ ] ) ) ;
8006+ assert ! ( endpoints[ 1 ] . get( "port" ) . is_none( ) ) ;
8007+ // Zero scalar port: not promoted, removed.
8008+ assert ! ( endpoints[ 2 ] . get( "ports" ) . is_none( ) ) ;
8009+ assert ! ( endpoints[ 2 ] . get( "port" ) . is_none( ) ) ;
8010+ // Positive scalar port: promoted to ports array.
8011+ assert_eq ! ( endpoints[ 3 ] [ "ports" ] , serde_json:: json!( [ 8080 ] ) ) ;
8012+ assert ! ( endpoints[ 3 ] . get( "port" ) . is_none( ) ) ;
8013+ }
8014+
8015+ #[ test]
8016+ fn normalize_endpoint_ports_skips_non_object_endpoints ( ) {
8017+ let mut data = serde_json:: json!( {
8018+ "network_policies" : {
8019+ "p" : {
8020+ "endpoints" : [
8021+ "not-an-object" ,
8022+ { "host" : "h.test" , "port" : 443 } ,
8023+ 42 ,
8024+ ]
8025+ }
8026+ }
8027+ } ) ;
8028+ normalize_endpoint_ports ( & mut data) ;
8029+ let endpoints = data[ "network_policies" ] [ "p" ] [ "endpoints" ]
8030+ . as_array ( )
8031+ . unwrap ( ) ;
8032+
8033+ // Non-object entries are left untouched rather than panicking.
8034+ assert_eq ! ( endpoints[ 0 ] , serde_json:: json!( "not-an-object" ) ) ;
8035+ assert_eq ! ( endpoints[ 1 ] [ "ports" ] , serde_json:: json!( [ 443 ] ) ) ;
8036+ assert_eq ! ( endpoints[ 2 ] , serde_json:: json!( 42 ) ) ;
8037+ }
8038+
8039+ #[ test]
8040+ fn normalize_endpoint_ports_empty_array_after_filtering ( ) {
8041+ let mut data = serde_json:: json!( {
8042+ "network_policies" : {
8043+ "p" : {
8044+ "endpoints" : [
8045+ { "host" : "h.test" , "ports" : [ 0 ] , "port" : 8080 } ,
8046+ ]
8047+ }
8048+ }
8049+ } ) ;
8050+ normalize_endpoint_ports ( & mut data) ;
8051+ let endpoints = data[ "network_policies" ] [ "p" ] [ "endpoints" ]
8052+ . as_array ( )
8053+ . unwrap ( ) ;
8054+ // A present "ports" array takes precedence over scalar "port", even
8055+ // when filtering leaves the array empty.
8056+ assert_eq ! ( endpoints[ 0 ] [ "ports" ] , serde_json:: json!( [ ] ) ) ;
8057+ assert ! ( endpoints[ 0 ] . get( "port" ) . is_none( ) ) ;
8058+ }
8059+
8060+ #[ test]
8061+ fn normalize_endpoint_ports_preserves_non_numeric_entries ( ) {
8062+ let mut data = serde_json:: json!( {
8063+ "network_policies" : {
8064+ "p" : {
8065+ "endpoints" : [
8066+ { "host" : "h.test" , "ports" : [ 0 , "bad" , 443 ] } ,
8067+ ]
8068+ }
8069+ }
8070+ } ) ;
8071+ normalize_endpoint_ports ( & mut data) ;
8072+ let endpoints = data[ "network_policies" ] [ "p" ] [ "endpoints" ]
8073+ . as_array ( )
8074+ . unwrap ( ) ;
8075+ // Numeric zeros are removed; non-numeric entries are preserved so
8076+ // downstream validation can fail closed on malformed input.
8077+ assert_eq ! ( endpoints[ 0 ] [ "ports" ] , serde_json:: json!( [ "bad" , 443 ] ) ) ;
8078+ }
8079+
8080+ fn proto_with_endpoint_ports ( port : u32 , ports : Vec < u32 > ) -> ProtoSandboxPolicy {
8081+ let mut network_policies = std:: collections:: HashMap :: new ( ) ;
8082+ network_policies. insert (
8083+ "p" . to_string ( ) ,
8084+ NetworkPolicyRule {
8085+ name : "p" . to_string ( ) ,
8086+ endpoints : vec ! [ NetworkEndpoint {
8087+ host: "api.example.com" . to_string( ) ,
8088+ port,
8089+ ports,
8090+ ..Default :: default ( )
8091+ } ] ,
8092+ binaries : vec ! [ ] ,
8093+ } ,
8094+ ) ;
8095+ ProtoSandboxPolicy {
8096+ version : 1 ,
8097+ filesystem : None ,
8098+ landlock : None ,
8099+ process : None ,
8100+ network_policies,
8101+ network_middlewares : std:: collections:: HashMap :: default ( ) ,
8102+ }
8103+ }
8104+
8105+ #[ test]
8106+ fn proto_to_opa_data_json_filters_zero_ports_in_production_path ( ) {
8107+ // Mixed array: zero removed, positive kept.
8108+ let proto = proto_with_endpoint_ports ( 0 , vec ! [ 0 , 443 ] ) ;
8109+ let parsed: serde_json:: Value =
8110+ serde_json:: from_str ( & proto_to_opa_data_json ( & proto, 0 ) ) . unwrap ( ) ;
8111+ assert_eq ! (
8112+ parsed[ "network_policies" ] [ "p" ] [ "endpoints" ] [ 0 ] [ "ports" ] ,
8113+ serde_json:: json!( [ 443 ] )
8114+ ) ;
8115+
8116+ // Zero-only array: becomes empty, no fallback to scalar port.
8117+ let proto = proto_with_endpoint_ports ( 0 , vec ! [ 0 ] ) ;
8118+ let parsed: serde_json:: Value =
8119+ serde_json:: from_str ( & proto_to_opa_data_json ( & proto, 0 ) ) . unwrap ( ) ;
8120+ assert_eq ! (
8121+ parsed[ "network_policies" ] [ "p" ] [ "endpoints" ] [ 0 ] [ "ports" ] ,
8122+ serde_json:: json!( [ ] )
8123+ ) ;
8124+
8125+ // Zero scalar port: not promoted.
8126+ let proto = proto_with_endpoint_ports ( 0 , vec ! [ ] ) ;
8127+ let parsed: serde_json:: Value =
8128+ serde_json:: from_str ( & proto_to_opa_data_json ( & proto, 0 ) ) . unwrap ( ) ;
8129+ assert_eq ! (
8130+ parsed[ "network_policies" ] [ "p" ] [ "endpoints" ] [ 0 ] [ "ports" ] ,
8131+ serde_json:: json!( [ ] )
8132+ ) ;
8133+
8134+ // Positive scalar port with zero-only array: present `ports` wins,
8135+ // so the scalar port is NOT promoted.
8136+ let proto = proto_with_endpoint_ports ( 8080 , vec ! [ 0 ] ) ;
8137+ let parsed: serde_json:: Value =
8138+ serde_json:: from_str ( & proto_to_opa_data_json ( & proto, 0 ) ) . unwrap ( ) ;
8139+ assert_eq ! (
8140+ parsed[ "network_policies" ] [ "p" ] [ "endpoints" ] [ 0 ] [ "ports" ] ,
8141+ serde_json:: json!( [ ] )
8142+ ) ;
8143+
8144+ // Positive scalar port with positive array: array wins.
8145+ let proto = proto_with_endpoint_ports ( 8080 , vec ! [ 443 ] ) ;
8146+ let parsed: serde_json:: Value =
8147+ serde_json:: from_str ( & proto_to_opa_data_json ( & proto, 0 ) ) . unwrap ( ) ;
8148+ assert_eq ! (
8149+ parsed[ "network_policies" ] [ "p" ] [ "endpoints" ] [ 0 ] [ "ports" ] ,
8150+ serde_json:: json!( [ 443 ] )
8151+ ) ;
8152+ }
79678153}
0 commit comments