Skip to content

Agent Mesh

fix: address PR review on Model Armor DNS SSRF and AllowedTargets peer ID canonicalization

Antonio Ojea7bd48bdb3b5d
diff --git a/api/policy_rules.go b/api/policy_rules.go
index b7b6b1a7..b66a2849 100644
--- a/api/policy_rules.go
+++ b/api/policy_rules.go
@@ -237,10 +237,19 @@ func BuildPolicyRules(roles []*PolicyRole, bindings []*PolicyBinding) (rules []P
warnings = append(warnings, fmt.Sprintf("Role %s target %q has invalid characters and is skipped", roleName, t))
continue
}
- if tFact, tVal := ParseServiceTarget(t); tVal == "" || (tFact != "" && ValidateLabelKey(tFact) != nil) {
+ tFact, tVal := ParseServiceTarget(t)
+ if tVal == "" || (tFact != "" && ValidateLabelKey(tFact) != nil) {
warnings = append(warnings, fmt.Sprintf("Role %s target %q has invalid format and is skipped", roleName, t))
continue
}
+ if tFact == FactNode && tVal != "*" {
+ pID, err := peer.Decode(tVal)
+ if err != nil {
+ warnings = append(warnings, fmt.Sprintf("Role %s target %q has invalid Peer ID and is skipped", roleName, t))
+ continue
+ }
+ t = tFact + ":" + pID.String()
+ }
nonWildcardTargets = append(nonWildcardTargets, t)
}
if hasUnrestricted {
diff --git a/api/policy_rules_test.go b/api/policy_rules_test.go
index 900708cb..bd0315de 100644
--- a/api/policy_rules_test.go
+++ b/api/policy_rules_test.go
@@ -39,7 +39,7 @@ func TestBuildPolicyRules(t *testing.T) {
roles := []*PolicyRole{
{
Name: "test-role",
- AllowedTargets: []string{"*", "node:peer-abc", "custom-fact:custom-val", "legacy-peer"},
+ AllowedTargets: []string{"*", "node:" + nodeCID, "node:not-a-peer-id", "custom-fact:custom-val", "legacy-peer"},
AllowedServices: []string{"*:*", "mcp:*", "mcp:*.suffix", "mcp:prefix.*", "mcp:exact"},
CustomDatalog: []string{
"custom_rule($x) <- fact($x), $x > 3;",
@@ -58,20 +58,20 @@ func TestBuildPolicyRules(t *testing.T) {
rules, warnings := BuildPolicyRules(roles, bindings)
expectedStrings := map[string]bool{
- "role(\"test-role\") <- true": false,
- "role(\"test-role\") <- user(\"alice\")": false,
- "role(\"test-role\") <- node(\"" + nodeID + "\")": false,
- "granted_service_all_types(true) <- role(\"test-role\")": false,
- "granted_service_all(\"mcp\") <- role(\"test-role\")": false,
- "granted_service_suffix(\"mcp\", \".suffix\") <- role(\"test-role\")": false,
- "granted_service_prefix(\"mcp\", \"prefix.\") <- role(\"test-role\")": false,
- "granted_service_set(\"mcp\", [\"exact\"]) <- role(\"test-role\")": false,
- "target_unrestricted(true) <- role(\"test-role\")": false,
- "target_restricted(true) <- role(\"test-role\")": false,
- "granted_target_set(\"node\", [\"legacy-peer\", \"peer-abc\"]) <- role(\"test-role\")": false,
- "granted_target_set(\"custom-fact\", [\"custom-val\"]) <- role(\"test-role\")": false,
- "custom_rule($x) <- role(\"test-role\"), fact($x), $x > 3": false,
- "custom_fact(\"hello\") <- role(\"test-role\")": false,
+ "role(\"test-role\") <- true": false,
+ "role(\"test-role\") <- user(\"alice\")": false,
+ "role(\"test-role\") <- node(\"" + nodeID + "\")": false,
+ "granted_service_all_types(true) <- role(\"test-role\")": false,
+ "granted_service_all(\"mcp\") <- role(\"test-role\")": false,
+ "granted_service_suffix(\"mcp\", \".suffix\") <- role(\"test-role\")": false,
+ "granted_service_prefix(\"mcp\", \"prefix.\") <- role(\"test-role\")": false,
+ "granted_service_set(\"mcp\", [\"exact\"]) <- role(\"test-role\")": false,
+ "target_unrestricted(true) <- role(\"test-role\")": false,
+ "target_restricted(true) <- role(\"test-role\")": false,
+ "granted_target_set(\"node\", [\"" + nodeID + "\", \"legacy-peer\"]) <- role(\"test-role\")": false,
+ "granted_target_set(\"custom-fact\", [\"custom-val\"]) <- role(\"test-role\")": false,
+ "custom_rule($x) <- role(\"test-role\"), fact($x), $x > 3": false,
+ "custom_fact(\"hello\") <- role(\"test-role\")": false,
}
for _, rule := range rules {
@@ -92,11 +92,11 @@ func TestBuildPolicyRules(t *testing.T) {
}
}
- if len(warnings) != 2 {
- t.Fatalf("warnings = %q, want one for the unparseable entry and one for the bad peer ID", warnings)
+ if len(warnings) != 3 {
+ t.Fatalf("warnings = %q, want one for the unparseable entry and two for the bad peer IDs", warnings)
}
if !strings.Contains(strings.Join(warnings, "\n"), `"node:not-a-peer-id"`) {
- t.Errorf("warnings = %q, want one naming the member that is not a peer ID", warnings)
+ t.Errorf("warnings = %q, want one naming the member/target that is not a peer ID", warnings)
}
// The text is the contract every member evaluates; it must yield the same
diff --git a/internal/node/egress_inspect.go b/internal/node/egress_inspect.go
index 98356b1a..ae2ed629 100644
--- a/internal/node/egress_inspect.go
+++ b/internal/node/egress_inspect.go
@@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"io"
+ "net"
"net/http"
"net/netip"
"net/url"
@@ -650,6 +651,16 @@ func (s *EgressService) resolveModelArmorEndpoint(ctx context.Context, template
if isForbiddenEgressAddr(ip, s.allowsLocalTarget()) {
return "", "", fmt.Errorf("model_armor.template URL host %q is in a forbidden IP range", u.Hostname())
}
+ } else {
+ ips, lErr := net.DefaultResolver.LookupNetIP(ctx, "ip", u.Hostname())
+ if lErr != nil {
+ return "", "", fmt.Errorf("failed to resolve model_armor.template URL host %q: %w", u.Hostname(), lErr)
+ }
+ for _, resolvedIP := range ips {
+ if isForbiddenEgressAddr(resolvedIP, s.allowsLocalTarget()) {
+ return "", "", fmt.Errorf("model_armor.template URL host %q resolves to forbidden IP %s", u.Hostname(), resolvedIP)
+ }
+ }
}
return tmpl, "", nil
}
diff --git a/internal/node/egress_inspect_test.go b/internal/node/egress_inspect_test.go
index eead8d21..9d819d42 100644
--- a/internal/node/egress_inspect_test.go
+++ b/internal/node/egress_inspect_test.go
@@ -702,3 +702,39 @@ func TestReplaceInspectableTextMultiMessageNewlineAndFailClosed(t *testing.T) {
t.Fatal("expected error when replacement cannot be mapped back into multi-message JSON")
}
}
+
+func TestResolveModelArmorEndpointSSRF(t *testing.T) {
+ ctx := context.Background()
+ remoteSvc, err := newEgressService(&api.EgressDestination{
+ Name: "api.openai.com",
+ ServedBy: []string{api.RoleNode},
+ }, t.TempDir())
+ if err != nil {
+ t.Fatalf("newEgressService: %v", err)
+ }
+
+ for _, forbiddenURL := range []string{
+ "http://127.0.0.1:8080/v1/projects/p/locations/us-central1/templates/t",
+ "http://169.254.169.254/computeMetadata/v1/",
+ "http://localhost:8080/v1/projects/p/locations/us-central1/templates/t",
+ } {
+ if _, _, err := remoteSvc.resolveModelArmorEndpoint(ctx, forbiddenURL); err == nil {
+ t.Errorf("expected resolveModelArmorEndpoint to reject %q when local target is not allowed", forbiddenURL)
+ }
+ }
+
+ localSvc, err := newEgressService(&api.EgressDestination{
+ Name: "api.openai.com",
+ TargetUrl: "http://127.0.0.1:8080",
+ ServedBy: []string{api.RoleNode},
+ }, t.TempDir())
+ if err != nil {
+ t.Fatalf("newEgressService (local): %v", err)
+ }
+ if _, _, err := localSvc.resolveModelArmorEndpoint(ctx, "http://localhost:8080/v1/projects/p/locations/us-central1/templates/t"); err != nil {
+ t.Fatalf("expected localhost URL to be allowed when target_url is local, got: %v", err)
+ }
+ if _, _, err := localSvc.resolveModelArmorEndpoint(ctx, "http://169.254.169.254/computeMetadata/v1/"); err == nil {
+ t.Fatal("expected link-local metadata IP to be rejected even when local target is allowed")
+ }
+}