Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(supervisor-network): L7 endpoint validation edge cases#2464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewwhitecdw
wants to merge
1
commit into
NVIDIA:mainChoose a base branch
from
andrewwhitecdw:fix-l7-policy-validation-edge-cases/aw
base:main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+335
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -958,6 +958,11 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec<String>, Vec< | ||
| }; | ||
| for (i, ep) in endpoints.iter().enumerate() { | ||
| let loc = format!("{name}.endpoints[{i}]"); | ||
| if !ep.is_object() { | ||
| errors.push(format!("{loc}: endpoint entry must be an object")); | ||
| continue; | ||
| } | ||
| let protocol = ep.get("protocol").and_then(|v| v.as_str()).unwrap_or(""); | ||
| let l7_protocol = L7Protocol::parse(protocol); | ||
| let jsonrpc_family = l7_protocol.is_some_and(L7Protocol::is_jsonrpc_family); | ||
| @@ -982,9 +987,13 @@ pub fn validate_l7_policies(data_json: &serde_json::Value) -> (Vec<String>, Vec< | ||
| .into_iter() | ||
| .collect() | ||
| }, | ||
| |arr| arr.iter().filter_map(serde_json::Value::as_u64).collect(), | ||
| |arr| { | ||
| arr.iter() | ||
| .filter_map(serde_json::Value::as_u64) | ||
| .filter(|p| *p > 0) | ||
| .collect() | ||
| }, | ||
| ); | ||
| let loc = format!("{name}.endpoints[{i}]"); | ||
| if protocol == "mcp" { | ||
| if host.trim().is_empty() { | ||
| @@ -1537,7 +1546,13 @@ pub fn expand_access_presets(data: &mut serde_json::Value) -> Vec<String> { | ||
| && !has_rules | ||
| && mcp_allow_all_known_mcp_methods | ||
| { | ||
| ep.as_object_mut().unwrap().insert( | ||
| let Some(obj) = ep.as_object_mut() else { | ||
| warnings.push(format!( | ||
| "{name}.endpoints[{i}]: endpoint entry is not an object; skipping access preset expansion" | ||
| )); | ||
| continue; | ||
| }; | ||
| obj.insert( | ||
| "rules".to_string(), | ||
| serde_json::Value::Array(vec![jsonrpc_rule_json("*")]), | ||
| ); | ||
| @@ -1562,9 +1577,13 @@ pub fn expand_access_presets(data: &mut serde_json::Value) -> Vec<String> { | ||
| continue; | ||
| }; | ||
| ep.as_object_mut() | ||
| .unwrap() | ||
| .insert("rules".to_string(), serde_json::Value::Array(rules)); | ||
| if let Some(obj) = ep.as_object_mut() { | ||
| obj.insert("rules".to_string(), serde_json::Value::Array(rules)); | ||
| } else { | ||
| warnings.push(format!( | ||
| "{name}.endpoints[{i}]: endpoint entry is not an object; skipping access preset expansion" | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| @@ -1609,6 +1628,34 @@ fn graphql_rule_json(operation_type: &str) -> serde_json::Value { | ||
| mod tests { | ||
| use super::*; | ||
| #[test] | ||
| fn validate_l7_policies_rejects_non_object_endpoint() { | ||
johntmyers marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let data = serde_json::json!({ | ||
| "network_policies": { | ||
| "test": { | ||
| "endpoints": [ | ||
| "not-an-object", | ||
| {"host": "api.example.com", "port": 443, "protocol": "rest"}, | ||
| 42, | ||
| ], | ||
| "binaries": [] | ||
| } | ||
| } | ||
| }); | ||
| let (errors, _warnings) = validate_l7_policies(&data); | ||
| assert!( | ||
| errors | ||
| .iter() | ||
| .any(|e| e.contains("endpoint entry must be an object")), | ||
| "expected non-object endpoint error: {errors:?}" | ||
| ); | ||
| // The valid object endpoint should not produce an error. | ||
| assert!( | ||
| !errors.iter().any(|e| e.contains("api.example.com")), | ||
| "valid endpoint should not be blamed: {errors:?}" | ||
| ); | ||
| } | ||
| #[test] | ||
| fn parse_l7_config_rest_enforce() { | ||
| let val = regorus::Value::from_json_str( | ||
| @@ -2830,6 +2877,43 @@ mod tests { | ||
| ); | ||
| } | ||
| #[test] | ||
| fn expand_access_presets_skips_non_object_endpoint_and_expands_valid() { | ||
| let mut data = serde_json::json!({ | ||
| "network_policies": { | ||
| "test": { | ||
| "endpoints": [ | ||
| "not-an-object", | ||
| { | ||
| "host": "api.example.com", | ||
| "port": 80, | ||
| "protocol": "rest", | ||
| "access": "read-only" | ||
| }, | ||
| ], | ||
| "binaries": [] | ||
| } | ||
| } | ||
| }); | ||
| let warnings = expand_access_presets(&mut data); | ||
| let endpoints = data["network_policies"]["test"]["endpoints"] | ||
| .as_array() | ||
| .unwrap(); | ||
| // Invalid entry is left untouched. | ||
| assert_eq!(endpoints[0], serde_json::json!("not-an-object")); | ||
| // Valid preset endpoint is expanded. | ||
| let rules = endpoints[1]["rules"].as_array().unwrap(); | ||
| assert_eq!(rules.len(), 3); | ||
| let methods: Vec<&str> = rules | ||
| .iter() | ||
| .map(|r| r["allow"]["method"].as_str().unwrap()) | ||
| .collect(); | ||
| assert!(methods.contains(&"GET")); | ||
| assert!(methods.contains(&"HEAD")); | ||
| assert!(methods.contains(&"OPTIONS")); | ||
| assert!(warnings.is_empty(), "expected no warnings: {warnings:?}"); | ||
| } | ||
| #[test] | ||
| fn expand_graphql_readonly_preset() { | ||
| let mut data = serde_json::json!({ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1272,14 +1272,27 @@ fn normalize_endpoint_ports(data: &mut serde_json::Value) { | ||
| continue; | ||
| }; | ||
| // If "ports" already exists and is non-empty, keep it. | ||
| // A present "ports" array takes precedence over scalar "port". | ||
| // Record whether it existed before filtering so an all-zero array | ||
| // does not silently fall back to the scalar. | ||
| let ports_was_present = ep_obj.contains_key("ports"); | ||
| // If "ports" already exists, filter out numeric zero values so | ||
| // OPA never sees a zero port. Non-numeric entries are left intact | ||
| // so downstream validation can fail closed on malformed input | ||
| // rather than silently dropping it. | ||
| if let Some(ports) = ep_obj.get_mut("ports").and_then(|v| v.as_array_mut()) { | ||
johntmyers marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ports.retain(|p| p.as_u64().is_none_or(|n| n != 0)); | ||
| } | ||
| let has_ports = ep_obj | ||
| .get("ports") | ||
| .and_then(|v| v.as_array()) | ||
| .is_some_and(|a| !a.is_empty()); | ||
| if !has_ports { | ||
| // Promote scalar "port" to "ports" array. | ||
| if !ports_was_present && !has_ports { | ||
| // Promote scalar "port" to "ports" array only when no | ||
| // "ports" key was originally present. | ||
| let port = ep_obj | ||
| .get("port") | ||
| .and_then(serde_json::Value::as_u64) | ||
| @@ -1647,10 +1660,12 @@ fn proto_to_opa_data_json(proto: &ProtoSandboxPolicy, entrypoint_pid: u32) -> St | ||
| .endpoints | ||
| .iter() | ||
| .map(|e| { | ||
| // Normalize port/ports: ports takes precedence, then | ||
| // single port promoted to array. Rego always sees "ports". | ||
| // Normalize port/ports: filter zero ports first so OPA | ||
| // never sees them, then a present `ports` array takes | ||
| // precedence over the scalar `port`. Rego always sees "ports". | ||
| let filtered: Vec<u32> = e.ports.iter().copied().filter(|&p| p > 0).collect(); | ||
| let ports: Vec<u32> = if !e.ports.is_empty() { | ||
| e.ports.clone() | ||
| filtered | ||
| } else if e.port > 0 { | ||
| vec![e.port] | ||
| } else { | ||
| @@ -7964,4 +7979,175 @@ network_policies: | ||
| cmdline_paths: vec![], | ||
| } | ||
| } | ||
| #[test] | ||
| fn normalize_endpoint_ports_filters_zero_values() { | ||
| let mut data = serde_json::json!({ | ||
| "network_policies": { | ||
| "p": { | ||
| "endpoints": [ | ||
| {"host": "h1.test", "ports": [0, 443]}, | ||
| {"host": "h2.test", "ports": [0]}, | ||
| {"host": "h3.test", "port": 0}, | ||
| {"host": "h4.test", "port": 8080}, | ||
| ] | ||
| } | ||
| } | ||
| }); | ||
| normalize_endpoint_ports(&mut data); | ||
| let endpoints = data["network_policies"]["p"]["endpoints"] | ||
| .as_array() | ||
| .unwrap(); | ||
| // Mixed array: zero removed, positive kept. | ||
| assert_eq!(endpoints[0]["ports"], serde_json::json!([443])); | ||
| // All-zero array: becomes empty, no fallback port. | ||
| assert_eq!(endpoints[1]["ports"], serde_json::json!([])); | ||
| assert!(endpoints[1].get("port").is_none()); | ||
| // Zero scalar port: not promoted, removed. | ||
| assert!(endpoints[2].get("ports").is_none()); | ||
| assert!(endpoints[2].get("port").is_none()); | ||
| // Positive scalar port: promoted to ports array. | ||
| assert_eq!(endpoints[3]["ports"], serde_json::json!([8080])); | ||
| assert!(endpoints[3].get("port").is_none()); | ||
| } | ||
| #[test] | ||
| fn normalize_endpoint_ports_skips_non_object_endpoints() { | ||
| let mut data = serde_json::json!({ | ||
| "network_policies": { | ||
| "p": { | ||
| "endpoints": [ | ||
| "not-an-object", | ||
| {"host": "h.test", "port": 443}, | ||
| 42, | ||
| ] | ||
| } | ||
| } | ||
| }); | ||
| normalize_endpoint_ports(&mut data); | ||
| let endpoints = data["network_policies"]["p"]["endpoints"] | ||
| .as_array() | ||
| .unwrap(); | ||
| // Non-object entries are left untouched rather than panicking. | ||
| assert_eq!(endpoints[0], serde_json::json!("not-an-object")); | ||
| assert_eq!(endpoints[1]["ports"], serde_json::json!([443])); | ||
| assert_eq!(endpoints[2], serde_json::json!(42)); | ||
| } | ||
| #[test] | ||
| fn normalize_endpoint_ports_empty_array_after_filtering() { | ||
| let mut data = serde_json::json!({ | ||
| "network_policies": { | ||
| "p": { | ||
| "endpoints": [ | ||
| {"host": "h.test", "ports": [0], "port": 8080}, | ||
| ] | ||
| } | ||
| } | ||
| }); | ||
| normalize_endpoint_ports(&mut data); | ||
| let endpoints = data["network_policies"]["p"]["endpoints"] | ||
| .as_array() | ||
| .unwrap(); | ||
| // A present "ports" array takes precedence over scalar "port", even | ||
| // when filtering leaves the array empty. | ||
| assert_eq!(endpoints[0]["ports"], serde_json::json!([])); | ||
| assert!(endpoints[0].get("port").is_none()); | ||
| } | ||
| #[test] | ||
| fn normalize_endpoint_ports_preserves_non_numeric_entries() { | ||
| let mut data = serde_json::json!({ | ||
| "network_policies": { | ||
| "p": { | ||
| "endpoints": [ | ||
| {"host": "h.test", "ports": [0, "bad", 443]}, | ||
| ] | ||
| } | ||
| } | ||
| }); | ||
| normalize_endpoint_ports(&mut data); | ||
| let endpoints = data["network_policies"]["p"]["endpoints"] | ||
| .as_array() | ||
| .unwrap(); | ||
| // Numeric zeros are removed; non-numeric entries are preserved so | ||
| // downstream validation can fail closed on malformed input. | ||
| assert_eq!(endpoints[0]["ports"], serde_json::json!(["bad", 443])); | ||
| } | ||
| fn proto_with_endpoint_ports(port: u32, ports: Vec<u32>) -> ProtoSandboxPolicy { | ||
| let mut network_policies = std::collections::HashMap::new(); | ||
| network_policies.insert( | ||
| "p".to_string(), | ||
| NetworkPolicyRule { | ||
| name: "p".to_string(), | ||
| endpoints: vec![NetworkEndpoint { | ||
| host: "api.example.com".to_string(), | ||
| port, | ||
| ports, | ||
| ..Default::default() | ||
| }], | ||
| binaries: vec![], | ||
| }, | ||
| ); | ||
| ProtoSandboxPolicy { | ||
| version: 1, | ||
| filesystem: None, | ||
| landlock: None, | ||
| process: None, | ||
| network_policies, | ||
| network_middlewares: std::collections::HashMap::default(), | ||
| } | ||
| } | ||
| #[test] | ||
| fn proto_to_opa_data_json_filters_zero_ports_in_production_path() { | ||
| // Mixed array: zero removed, positive kept. | ||
| let proto = proto_with_endpoint_ports(0, vec![0, 443]); | ||
| let parsed: serde_json::Value = | ||
| serde_json::from_str(&proto_to_opa_data_json(&proto, 0)).unwrap(); | ||
| assert_eq!( | ||
| parsed["network_policies"]["p"]["endpoints"][0]["ports"], | ||
| serde_json::json!([443]) | ||
| ); | ||
| // Zero-only array: becomes empty, no fallback to scalar port. | ||
| let proto = proto_with_endpoint_ports(0, vec![0]); | ||
| let parsed: serde_json::Value = | ||
| serde_json::from_str(&proto_to_opa_data_json(&proto, 0)).unwrap(); | ||
| assert_eq!( | ||
| parsed["network_policies"]["p"]["endpoints"][0]["ports"], | ||
| serde_json::json!([]) | ||
| ); | ||
| // Zero scalar port: not promoted. | ||
| let proto = proto_with_endpoint_ports(0, vec![]); | ||
| let parsed: serde_json::Value = | ||
| serde_json::from_str(&proto_to_opa_data_json(&proto, 0)).unwrap(); | ||
| assert_eq!( | ||
| parsed["network_policies"]["p"]["endpoints"][0]["ports"], | ||
| serde_json::json!([]) | ||
| ); | ||
| // Positive scalar port with zero-only array: present `ports` wins, | ||
| // so the scalar port is NOT promoted. | ||
| let proto = proto_with_endpoint_ports(8080, vec![0]); | ||
| let parsed: serde_json::Value = | ||
| serde_json::from_str(&proto_to_opa_data_json(&proto, 0)).unwrap(); | ||
| assert_eq!( | ||
| parsed["network_policies"]["p"]["endpoints"][0]["ports"], | ||
| serde_json::json!([]) | ||
| ); | ||
| // Positive scalar port with positive array: array wins. | ||
| let proto = proto_with_endpoint_ports(8080, vec![443]); | ||
| let parsed: serde_json::Value = | ||
| serde_json::from_str(&proto_to_opa_data_json(&proto, 0)).unwrap(); | ||
| assert_eq!( | ||
| parsed["network_policies"]["p"]["endpoints"][0]["ports"], | ||
| serde_json::json!([443]) | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.