Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ func ValidatePeerRecordIngress(ing *ServiceIngress) error {
return nil
}

// HasDotSegment reports whether a URL path carries a "." or ".." segment.
// Policy is decided on the /<type>/<name> prefix of a path, so a dot segment
// in what follows could resolve to a sibling service or file on the backend.
// A backslash counts as a separator too: it reaches the backend
// percent-encoded, and a backend on a platform that treats "\" as "/" would
// otherwise see a traversal this check did not.
func HasDotSegment(p string) bool {
for _, seg := range strings.Split(strings.ReplaceAll(p, "\\", "/"), "/") {
if seg == "." || seg == ".." {
return true
}
}
return false
}

// SanitizeExternalIngressHeaders removes ambient cookies, spoofable X-Mesh-*
// and X-Peer-Id attribution headers, and (when stripAuthorization is true) the
// external caller's Authorization header before forwarding an external ingress
Expand Down
10 changes: 5 additions & 5 deletions api/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestValidateServiceIngress(t *testing.T) {
svcType: ServiceType_SERVICE_TYPE_MCP,
ing: &ServiceIngress{
AuthMode: IngressAuthMode_INGRESS_AUTH_MODE_EDGE,
Allow: []string{"email:alice@example.com", "email:*@google.com", "node:" + pid.String()},
Allow: []string{"email:alice@example.com", "email:*@example.com", "node:" + pid.String()},
},
},
{
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestServiceIngressFromConfig(t *testing.T) {
}

got, err = ServiceIngressFromConfig(ServiceType_SERVICE_TYPE_A2A, &IngressConfig{
Allow: []string{"email:yaroslav@google.com"},
Allow: []string{"email:carol@example.com"},
})
if err != nil {
t.Fatalf("ServiceIngressFromConfig default mode: %v", err)
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestValidateIngressURL(t *testing.T) {
func TestMatchIngressAllow(t *testing.T) {
pid := testPeerID(t)
claims := map[string][]string{
FactEmail: {"yaroslav@google.com"},
FactEmail: {"carol@example.com"},
FactGroup: {"a2a-team"},
FactNode: {pid.String()},
}
Expand All @@ -251,8 +251,8 @@ func TestMatchIngressAllow(t *testing.T) {
if !MatchIngressAllow([]string{SystemAuthenticated}, claims) {
t.Fatal("SystemAuthenticated must match non-empty identity claims")
}
if !MatchIngressAllow([]string{"email:*@google.com"}, claims) {
t.Fatal("wildcard domain should match yaroslav@google.com")
if !MatchIngressAllow([]string{"email:*@example.com"}, claims) {
t.Fatal("wildcard domain should match carol@example.com")
}
if !MatchIngressAllow([]string{"node:" + pid.String()}, claims) {
t.Fatal("exact node peer ID should match")
Expand Down
57 changes: 20 additions & 37 deletions internal/controlplane/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,39 +1206,6 @@ func (s *Server) authorizeBiscuitForEgress(ctx context.Context, rawBiscuit []byt
if err != nil {
return claims, http.StatusForbidden, err
}
authorizer.AddFact(biscuit.Fact{
Predicate: biscuit.Predicate{
Name: api.FactService,
IDs: []biscuit.Term{biscuit.String("egress"), biscuit.String(destination)},
},
})
authorizer.AddFact(biscuit.Fact{
Predicate: biscuit.Predicate{
Name: api.FactConnectionPeerID,
IDs: []biscuit.Term{biscuit.String(clientPeer.String())},
},
})
authorizer.AddCheck(api.BaselineReplayCheck)
authorizer.AddFact(api.MarkerFact(api.FactTargetUnrestricted))
authorizer.AddCheck(api.BaselineTargetCheck)
identity.EnforceExpiration(authorizer)

for _, p := range api.BaselinePolicies {
authorizer.AddPolicy(p)
}
for _, r := range api.BaselineRules {
authorizer.AddRule(r)
}
for _, r := range api.BaselineHTTPRules {
authorizer.AddRule(r)
}
// Destination-level STS minting checks whether the role grants egress://<destination>
// at all (plain or HTTP-narrowed); per-request method/path restrictions are
// enforced by the egress agentmesh-node PEP on the wire HTTP request.
for _, r := range stsDestinationRules {
authorizer.AddRule(r)
}

roles, bindings, err := s.store.GetMeshPolicy(ctx)
if err != nil && err != storage.ErrNotFound {
return claims, http.StatusInternalServerError, fmt.Errorf("failed to load mesh policy: %w", err)
Expand All @@ -1249,11 +1216,27 @@ func (s *Server) authorizeBiscuitForEgress(ctx context.Context, rawBiscuit []byt
}
policyRules, _ := api.BuildPolicyRules(roles, bindings)
policyRules = append(policyRules, api.BuildEgressServingRules(egressDests)...)
meshRules := make([]biscuit.Rule, 0, len(policyRules))
for _, pr := range policyRules {
authorizer.AddRule(pr.Rule)
}

if err := authorizer.Authorize(); err != nil {
meshRules = append(meshRules, pr.Rule)
}

// The same assembly every node PEP runs. The control plane has no
// provider credential of its own to match target grants against, so the
// request is unrestricted on targets; it is bound to the client peer the
// Biscuit names, as a stream to that node would be. Destination-level
// minting asks whether the role grants egress://<destination> at all
// (plain or HTTP-narrowed, stsDestinationRules); per-request method and
// path restrictions are enforced by the egress agentmesh-node on the
// wire HTTP request.
meshReq := identity.MeshRequest{
ServiceType: api.ServiceTypeStringEgress,
ServiceName: destination,
Unrestricted: true,
ConnectionPeer: clientPeer,
}
pol := identity.MeshPolicy{LocalRules: stsDestinationRules, MeshRules: meshRules}
if err := identity.AuthorizeMeshRequest(authorizer, meshReq, pol); err != nil {
return claims, http.StatusForbidden, fmt.Errorf("standing policy denied egress://%s: %w", destination, err)
}

Expand Down
175 changes: 175 additions & 0 deletions internal/identity/authorize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package identity

import (
"github.com/eclipse-biscuit/biscuit-go/v2"
"github.com/google/agentmesh/api"
"github.com/libp2p/go-libp2p/core/peer"
)

// MeshRequest is what a policy enforcement point knows about one request,
// whichever path it arrived on: a mesh stream from another member, the
// node's own sidecar, router external ingress, or the control plane's
// /sts/token. Every PEP turns its request into one of these and hands it to
// AuthorizeMeshRequest, so the facts and checks a credential is evaluated
// against are decided in one place and a rule added for one path reaches
// the others. The fields a path does not have stay at their zero value, and
// the zero value always means "not present", never "unrestricted".
type MeshRequest struct {
// ServiceType and ServiceName are the service() fact: the service the
// request targets, or (api.SystemNamespace, <protocol>) for a bare stream
// that names no service.
ServiceType string
ServiceName string

// HasHTTP reports whether Method and Path were carried on the wire. They
// become method() and path() only then, so a grant narrowed to methods
// and paths (PolicyRole.http) has nothing to match on a bare stream and
// fails closed.
HasHTTP bool
Method string
Path string

// HasEgress reports whether the request is an egress call to
// EgressHost:EgressPort, which become host() and port().
HasEgress bool
EgressHost string
EgressPort int64

// Unrestricted adds target_unrestricted: the caller is this node's own
// sidecar, or the verifier has no provider identity to match target
// grants against (the control plane minting a border JWT).
Unrestricted bool

// ConnectionPeer, when set, is the libp2p peer the credential arrived
// from: connection_peer_id() and BaselineReplayCheck then bind
// client_peer_id to it, so an exfiltrated token cannot be replayed from
// another peer. It stays empty for a bearer presented over HTTPS at the
// border, where there is no libp2p connection to bind to
// (security-architecture, section 5.2).
ConnectionPeer peer.ID

// MCPTool and AllowMCPStreamInit describe the MCP call for
// TaskAuthorizationRule evaluation (TaskRequest).
MCPTool string
AllowMCPStreamInit bool
}

// MeshPolicy is what the enforcement point adds to every evaluation on top
// of the credential: the provider's own target facts, its local attenuation,
// and the rules the control plane rendered from the mesh policy.
type MeshPolicy struct {
// TargetFacts are target_fact(type, value) facts from the provider's own
// credential (TargetFactsFromBiscuit); BaselineTargetCheck is always
// added, so a caller whose grant names a target the provider does not
// carry is refused, and so is every caller when the provider has none.
TargetFacts []biscuit.Fact
// LocalPolicies, LocalChecks and LocalRules are the enforcement point's
// own attenuation (agentmesh-node.yaml, or a PEP's static rules).
LocalPolicies []biscuit.Policy
LocalChecks []biscuit.Check
LocalRules []biscuit.Rule
// MeshRules are PolicyConfigGetResponse.datalog_rules as parsed.
MeshRules []biscuit.Rule
}

// AuthorizeMeshRequest loads req and pol into authorizer in the one order
// every enforcement point uses and runs it. The authorizer must already be
// built on the credential under the key that verified it; the caller
// checks revocation, bans and the authority-block binding before, and
// evaluates TaskAuthorizationRules (req.TaskRequest) after.
func AuthorizeMeshRequest(authorizer biscuit.Authorizer, req MeshRequest, pol MeshPolicy) error {
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactService,
IDs: []biscuit.Term{biscuit.String(req.ServiceType), biscuit.String(req.ServiceName)},
}})
if req.HasHTTP {
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactMethod,
IDs: []biscuit.Term{biscuit.String(req.Method)},
}})
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactPath,
IDs: []biscuit.Term{biscuit.String(req.Path)},
}})
}
if req.HasEgress {
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactHost,
IDs: []biscuit.Term{biscuit.String(req.EgressHost)},
}})
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactPort,
IDs: []biscuit.Term{biscuit.Integer(req.EgressPort)},
}})
}
if req.Unrestricted {
authorizer.AddFact(api.MarkerFact(api.FactTargetUnrestricted))
}
if req.ConnectionPeer != "" {
authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactConnectionPeerID,
IDs: []biscuit.Term{biscuit.String(req.ConnectionPeer.String())},
}})
authorizer.AddCheck(api.BaselineReplayCheck)
}

EnforceExpiration(authorizer)

for _, f := range pol.TargetFacts {
authorizer.AddFact(f)
}
authorizer.AddCheck(api.BaselineTargetCheck)

for _, p := range pol.LocalPolicies {
authorizer.AddPolicy(p)
}
for _, c := range pol.LocalChecks {
authorizer.AddCheck(c)
}
for _, r := range pol.LocalRules {
authorizer.AddRule(r)
}

for _, p := range api.BaselinePolicies {
authorizer.AddPolicy(p)
}
for _, r := range api.BaselineRules {
authorizer.AddRule(r)
}
for _, r := range api.BaselineHTTPRules {
authorizer.AddRule(r)
}
for _, r := range pol.MeshRules {
authorizer.AddRule(r)
}

return authorizer.Authorize()
}

// TaskRequest is req as TaskAuthorizationRule evaluation sees it, so the
// TAR intersection is computed from the same request on every path.
func (req MeshRequest) TaskRequest() api.TaskRequestContext {
return api.TaskRequestContext{
ServiceType: req.ServiceType,
ServiceName: req.ServiceName,
HasHTTP: req.HasHTTP,
Method: req.Method,
Path: req.Path,
MCPTool: req.MCPTool,
AllowMCPStreamInit: req.AllowMCPStreamInit,
}
}
Loading
Loading