Skip to content

Agent Mesh

fix: pre-GA security, conformance, release, and test-pyramid hardening

Antonio Ojeaa40e2c25f8df
diff --git a/.dockerignore b/.dockerignore
index 05a6cf7b..c8d24c3e 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -28,6 +28,8 @@ bin/
dist/
node_modules/
__pycache__/
+.pytest_cache/
+.worktrees/
.venv/
*.test
*.out
diff --git a/.github/k8s/sam-control-plane-template.yaml b/.github/k8s/sam-control-plane-template.yaml
index 3b50b44b..d3027c5b 100644
--- a/.github/k8s/sam-control-plane-template.yaml
+++ b/.github/k8s/sam-control-plane-template.yaml
@@ -207,6 +207,18 @@ spec:
- path:
type: Exact
value: /policies
+ - path:
+ type: Exact
+ value: /egress
+ - path:
+ type: Exact
+ value: /revocations
+ - path:
+ type: Exact
+ value: /token/exchange
+ - path:
+ type: Exact
+ value: /sts/token
- path:
type: Exact
value: /enroll
@@ -219,6 +231,21 @@ spec:
- path:
type: Exact
value: /nodes/catalog
+ - path:
+ type: Exact
+ value: /.well-known/openid-configuration
+ - path:
+ type: Exact
+ value: /.well-known/oauth-authorization-server
+ - path:
+ type: Exact
+ value: /jwks
+ - path:
+ type: Exact
+ value: /oauth/authorize
+ - path:
+ type: Exact
+ value: /oauth/token
backendRefs:
- name: sam-control-plane-${ENV_NAME}
port: 8080
diff --git a/.github/workflows/deploy-sam-one.yaml b/.github/workflows/deploy-sam-one.yaml
index f8d3c8a9..3a25bc7b 100644
--- a/.github/workflows/deploy-sam-one.yaml
+++ b/.github/workflows/deploy-sam-one.yaml
@@ -83,7 +83,7 @@ jobs:
IMG="${AR_LOCATION}-docker.pkg.dev/${GCP_PROJECT_ID}/${AR_REPO}/sam-one:${GITHUB_SHA}"
echo "IMG=${IMG}" >> "$GITHUB_ENV"
gcloud auth configure-docker "${AR_LOCATION}-docker.pkg.dev" --quiet
- docker build -f Dockerfile.sam-one -t "${IMG}" .
+ docker build --build-arg VERSION="${GITHUB_SHA}" -f Dockerfile.sam-one -t "${IMG}" .
docker push "${IMG}"
- name: Deploy to Cloud Run
diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml
index d7ef6187..25666177 100644
--- a/.github/workflows/deploy.yaml
+++ b/.github/workflows/deploy.yaml
@@ -564,13 +564,18 @@ jobs:
# COS drops inbound traffic on the host by default; the VPC rule alone does not open the port.
iptables -w -I INPUT -p tcp --dport 4501 -j ACCEPT
iptables -w -I INPUT -p udp --dport 4501 -j ACCEPT
+ install -d -m 0755 /etc/sam-router
+ printf "%s" "'"${BOOTSTRAP_TOKEN}"'" > /etc/sam-router/bootstrap-token
+ chown 65532:65532 /etc/sam-router/bootstrap-token
+ chmod 0600 /etc/sam-router/bootstrap-token
docker run -d \
--name sam-router \
--network host \
--restart always \
+ -v /etc/sam-router/bootstrap-token:/run/secrets/bootstrap-token:ro \
ghcr.io/google/sam-router:'"${IMAGE_TAG}"' \
--control-plane=https://'"${ENV_NAME}"'.sam-mesh.dev \
- --bootstrap-token='"${BOOTSTRAP_TOKEN}"' \
+ --bootstrap-token-path=/run/secrets/bootstrap-token \
--listen=/ip4/0.0.0.0/tcp/4501 \
--listen=/ip4/0.0.0.0/udp/4501/quic-v1 \
--external-addr=/ip4/${EXTERNAL_IP}/tcp/4501 \
diff --git a/.gitignore b/.gitignore
index 43a93813..ff00ee42 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,12 +48,15 @@ node_modules/
tests/ui/playwright-report/
tests/ui/test-results/
/sam-control-plane
+/sam-bench
/mcp-client
/nano-init
tests/e2e/logs/
tests/integration/logs/
tests/integration/scratch/
__pycache__/
+.pytest_cache/
+.worktrees/
.venv/
*.egg-info/
keys.db
diff --git a/Makefile b/Makefile
index 6a135644..7831dad5 100644
--- a/Makefile
+++ b/Makefile
@@ -26,12 +26,12 @@ ANDROID_CC_X86_64=$(ANDROID_NDK_TOOLCHAIN)/x86_64-linux-android30-clang
build:
go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-node" ./cmd/sam-node
- go build -v -o "$(OUT_DIR)/sam-control-plane" ./cmd/sam-control-plane
+ go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-control-plane" ./cmd/sam-control-plane
go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-router" ./cmd/sam-router
go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-one" ./cmd/sam-one
- go build -v -o "$(OUT_DIR)/mcp-client" ./cmd/mcp-client
- go build -v -o "$(OUT_DIR)/sam-bench" ./cmd/sam-bench
- go build -v -o "$(OUT_DIR)/sam-console" ./cmd/sam-console
+ go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/mcp-client" ./cmd/mcp-client
+ go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-bench" ./cmd/sam-bench
+ go build -v -ldflags "$(VERSION_LDFLAGS)" -o "$(OUT_DIR)/sam-console" ./cmd/sam-console
.PHONY: mobile-ffi-host mobile-ffi-android mobile-ffi-android-x86_64 mobile-ffi-ios mobile-ffi mobile-app-apk mobile-app-apk-emulator mobile-app-bundle
@@ -253,6 +253,7 @@ helm-test:
.PHONY: verify
verify:
./hack/verify-generated.sh
+ ./hack/verify-sdk-generated.sh
./hack/verify-secrets.sh
update:
@@ -267,6 +268,9 @@ docker-build-router:
docker-build-node:
docker build --load --build-arg VERSION="$(VERSION)" -t sam-node:local -f Dockerfile.sam-node .
+docker-build-one:
+ docker build --load --build-arg VERSION="$(VERSION)" -t sam-one:local -f Dockerfile.sam-one .
+
docker-build-mock-oidc:
docker build --load -t sam-mock-oidc:local -f tests/e2e/docker/Dockerfile.mock-oidc .
@@ -276,6 +280,6 @@ docker-build-e2e-runtime:
docker-build-sam-console:
docker build --load --build-arg VERSION="$(VERSION)" -t sam-console:local -f Dockerfile.sam-console .
-docker-build: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console
+docker-build: docker-build-control-plane docker-build-router docker-build-node docker-build-one docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console
-.PHONY: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console docker-build
+.PHONY: docker-build-control-plane docker-build-router docker-build-node docker-build-one docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console docker-build
diff --git a/api/egress.go b/api/egress.go
index 718b2b24..cef947f3 100644
--- a/api/egress.go
+++ b/api/egress.go
@@ -188,7 +188,7 @@ func validateEgressTargetURL(raw string) error {
if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf("target_url %q must use http or https", raw)
}
- if u.Host == "" {
+ if u.Hostname() == "" {
return fmt.Errorf("target_url %q has no host", raw)
}
if u.User != nil {
@@ -236,15 +236,21 @@ func EgressServedBy(d *EgressDestination, roles []string, labels map[string]stri
func BuildEgressServingRules(egress []*EgressDestination) []PolicyRule {
var rules []PolicyRule
for _, d := range egress {
- if d == nil || d.GetName() == "" {
+ if d == nil || ValidateEgressName(d.GetName()) != nil {
continue
}
head := biscuit.Predicate{Name: FactGrantedServiceExact, IDs: []biscuit.Term{biscuit.String(ServiceTypeStringEgress), biscuit.String(d.GetName())}}
for _, sel := range d.GetServedBy() {
var body biscuit.Predicate
if key, value, isLabel := strings.Cut(sel, "="); isLabel {
+ if ValidateLabelKey(key) != nil || ValidateLabelValue(value) != nil {
+ continue
+ }
body = biscuit.Predicate{Name: FactLabel, IDs: []biscuit.Term{biscuit.String(key), biscuit.String(value)}}
} else {
+ if ValidateRoleName(sel) != nil {
+ continue
+ }
body = biscuit.Predicate{Name: FactRole, IDs: []biscuit.Term{biscuit.String(sel)}}
}
r := biscuit.Rule{Head: head, Body: []biscuit.Predicate{body}}
diff --git a/api/egress_test.go b/api/egress_test.go
index e8d47c01..b694da1b 100644
--- a/api/egress_test.go
+++ b/api/egress_test.go
@@ -90,6 +90,7 @@ func TestValidateEgressDestination(t *testing.T) {
{"path", &EgressDestination{Name: "api.github.com/v3", ServedBy: []string{"pep"}}, "one hostname"},
{"target_url with credential", &EgressDestination{Name: "api.github.com", TargetUrl: "https://:[email protected]", ServedBy: []string{"pep"}}, "must not carry a credential"},
{"target_url scheme", &EgressDestination{Name: "api.github.com", TargetUrl: "ftp://api.github.com", ServedBy: []string{"pep"}}, "http or https"},
+ {"target_url empty hostname", &EgressDestination{Name: "api.github.com", TargetUrl: "https://:443", ServedBy: []string{"pep"}}, "no host"},
{"credential is a path", &EgressDestination{Name: "api.github.com", Credential: "/etc/passwd", ServedBy: []string{"pep"}}, "not a path"},
{"no served_by", &EgressDestination{Name: "api.github.com"}, "served_by"},
{"unknown role", &EgressDestination{Name: "api.github.com", ServedBy: []string{"nobody"}}, "neither a role"},
@@ -133,9 +134,10 @@ func TestEgressServedBy(t *testing.T) {
func TestBuildEgressServingRules(t *testing.T) {
rules := BuildEgressServingRules([]*EgressDestination{
- {Name: "api.github.com", ServedBy: []string{"pep", "site=eu"}},
+ {Name: "api.github.com", ServedBy: []string{"pep", "site=eu", `bad"role`, `site=bad"val`}},
nil,
{Name: "", ServedBy: []string{"pep"}},
+ {Name: `bad"name.example.com`, ServedBy: []string{"pep"}},
})
texts := PolicyRuleTexts(rules)
want := []string{
diff --git a/api/http_grants.go b/api/http_grants.go
index 43ef0967..1ad05686 100644
--- a/api/http_grants.go
+++ b/api/http_grants.go
@@ -107,7 +107,7 @@ func ValidateHTTPGrants(grants []*HTTPGrant, allowedServices []string) error {
func hasEncodedPathTraversal(p string) bool {
for i := 0; i < len(p); i++ {
c := p[i]
- if c < 0x20 || c == 0x7f || c == '\\' {
+ if c < 0x20 || c == 0x7f || c == '\\' || c == '"' {
return true
}
if c == '%' && i+2 < len(p) {
diff --git a/api/labels.go b/api/labels.go
index 9528566a..06545e22 100644
--- a/api/labels.go
+++ b/api/labels.go
@@ -65,6 +65,9 @@ func ValidateLabelValue(value string) error {
if strings.ContainsAny(value, ",=\n\r\t") {
return fmt.Errorf("label value %q must not contain ',', '=', or control characters", value)
}
+ if err := validateBindingMemberCharset(value); err != nil {
+ return fmt.Errorf("label value %q %w", value, err)
+ }
return nil
}
diff --git a/api/labels_test.go b/api/labels_test.go
index 4ad2d80a..58ed433e 100644
--- a/api/labels_test.go
+++ b/api/labels_test.go
@@ -43,7 +43,7 @@ func TestValidateLabelValue(t *testing.T) {
}
}
- invalid := []string{"", "has,comma", "has=equals", "has\ttab", strings.Repeat("a", maxLabelValueLen+1)}
+ invalid := []string{"", "has,comma", "has=equals", "has\ttab", `has"quote`, `has\slash`, "has\x00null", strings.Repeat("a", maxLabelValueLen+1)}
for _, v := range invalid {
if err := ValidateLabelValue(v); err == nil {
t.Errorf("ValidateLabelValue(%q): expected error, got nil", v)
diff --git a/api/network.go b/api/network.go
index 62505bb8..808a2be1 100644
--- a/api/network.go
+++ b/api/network.go
@@ -395,7 +395,7 @@ func ParseServiceTarget(target string) (svcType, svcName string) {
return "*", "*"
}
- if strings.Contains(target, "://") {
+ if idx := strings.Index(target, "://"); idx >= 0 && !strings.Contains(target[:idx], ":") {
matches := rfc3986URIRegex.FindStringSubmatch(target)
if len(matches) < 6 {
return "", target
diff --git a/api/network_test.go b/api/network_test.go
index b33b40f9..0693c4c1 100644
--- a/api/network_test.go
+++ b/api/network_test.go
@@ -85,6 +85,12 @@ func TestParseServiceTarget(t *testing.T) {
wantType: "mcp",
wantName: "my_service_name",
},
+ {
+ name: "Target fact with URI value",
+ target: "user:spiffe://cluster.local/ns/default/sa/web",
+ wantType: "user",
+ wantName: "spiffe://cluster.local/ns/default/sa/web",
+ },
}
for _, tt := range tests {
diff --git a/api/policy_rules.go b/api/policy_rules.go
index 1d903be5..b7b6b1a7 100644
--- a/api/policy_rules.go
+++ b/api/policy_rules.go
@@ -172,11 +172,11 @@ func BuildPolicyRules(roles []*PolicyRole, bindings []*PolicyBinding) (rules []P
add(roleHead, biscuit.Predicate{Name: prefix, IDs: []biscuit.Term{biscuit.String(value)}})
continue
}
+ if err := validateBindingMemberCharset(value); err != nil {
+ warnings = append(warnings, fmt.Sprintf("Binding member %q for role %s %v and grants role to nobody", m, b.Role, err))
+ continue
+ }
if strings.Contains(value, "*") {
- if err := validateBindingMemberCharset(value); err != nil {
- warnings = append(warnings, fmt.Sprintf("Binding member %q for role %s %v and grants role to nobody", m, b.Role, err))
- continue
- }
var text string
roleLiteral := fmt.Sprintf("%s(%s)", FactRole, strconv.Quote(b.Role))
switch {
@@ -231,9 +231,17 @@ func BuildPolicyRules(roles []*PolicyRole, bindings []*PolicyBinding) (rules []P
for _, t := range role.AllowedTargets {
if t == "*" {
hasUnrestricted = true
- } else {
- nonWildcardTargets = append(nonWildcardTargets, t)
+ continue
+ }
+ if t == "" || validateBindingMemberCharset(t) != nil {
+ 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) {
+ warnings = append(warnings, fmt.Sprintf("Role %s target %q has invalid format and is skipped", roleName, t))
+ continue
}
+ nonWildcardTargets = append(nonWildcardTargets, t)
}
if hasUnrestricted {
add(MarkerFact(FactTargetUnrestricted).Predicate, fromRole)
diff --git a/api/policy_rules_test.go b/api/policy_rules_test.go
index 838bafe0..900708cb 100644
--- a/api/policy_rules_test.go
+++ b/api/policy_rules_test.go
@@ -209,6 +209,7 @@ func TestBuildPolicyRulesWildcardsAndInjectionDefense(t *testing.T) {
{
Name: "payments",
AllowedServices: []string{"mcp:Echo"},
+ AllowedTargets: []string{"*", `node:bad"quote`, "bad fact:val"},
},
}
bindings := []*PolicyBinding{
@@ -223,13 +224,15 @@ func TestBuildPolicyRulesWildcardsAndInjectionDefense(t *testing.T) {
"user:*",
"user:system:*:Invalid",
`user:foo")*`,
+ `user:exact"quote`,
+ `user:exact\slash`,
},
},
}
rules, warnings := BuildPolicyRules(roles, bindings)
- if len(warnings) != 3 {
- t.Fatalf("warnings = %v, want 3 warnings for rejected wildcard entries", warnings)
+ if len(warnings) != 7 {
+ t.Fatalf("warnings = %v (len %d), want 7 warnings for rejected entries", warnings, len(warnings))
}
texts := PolicyRuleTexts(rules)
diff --git a/api/tar.go b/api/tar.go
index fe8b5040..e37a8c02 100644
--- a/api/tar.go
+++ b/api/tar.go
@@ -287,7 +287,7 @@ func DecodeTARBlockPayload(b64 string) (*TaskAuthorizationRule, error) {
if err := proto.Unmarshal(raw, &rule); err != nil {
return nil, fmt.Errorf("unmarshal tar_block protobuf: %w", err)
}
- if len(rule.ProtoReflect().GetUnknown()) > 0 {
+ if hasUnknownTARFields(&rule) {
return nil, fmt.Errorf("tar_block protobuf contains unknown fields")
}
if err := ValidateTaskAuthorizationRule(&rule); err != nil {
@@ -296,6 +296,27 @@ func DecodeTARBlockPayload(b64 string) (*TaskAuthorizationRule, error) {
return &rule, nil
}
+func hasUnknownTARFields(rule *TaskAuthorizationRule) bool {
+ if len(rule.ProtoReflect().GetUnknown()) > 0 {
+ return true
+ }
+ if exp := rule.GetExpireTime(); exp != nil && len(exp.ProtoReflect().GetUnknown()) > 0 {
+ return true
+ }
+ for _, r := range rule.GetRules() {
+ if r == nil {
+ continue
+ }
+ if len(r.ProtoReflect().GetUnknown()) > 0 {
+ return true
+ }
+ if op := r.GetOperation(); op != nil && len(op.ProtoReflect().GetUnknown()) > 0 {
+ return true
+ }
+ }
+ return false
+}
+
// ParseTARBlockSource validates that a Biscuit block's Datalog source text
// contains solely a single tar_block("<base64url>") fact (with optional
// trailing semicolon and whitespace) and decodes its TaskAuthorizationRule.
@@ -473,7 +494,7 @@ func EvaluateTaskRules(blocks []*TaskAuthorizationRule, req TaskRequestContext,
return fmt.Errorf("tar_block[%d] is nil", i+1)
}
if exp := block.GetExpireTime(); exp != nil {
- if !exp.IsValid() {
+ if !exp.IsValid() || exp.AsTime().Unix() <= 0 {
return fmt.Errorf("tar_block[%d] has invalid expire_time", i+1)
}
if now.After(exp.AsTime()) {
@@ -506,7 +527,7 @@ func EvaluateTaskRulesForDestination(blocks []*TaskAuthorizationRule, serviceTyp
return fmt.Errorf("tar_block[%d] is nil", i+1)
}
if exp := block.GetExpireTime(); exp != nil {
- if !exp.IsValid() {
+ if !exp.IsValid() || exp.AsTime().Unix() <= 0 {
return fmt.Errorf("tar_block[%d] has invalid expire_time", i+1)
}
if now.After(exp.AsTime()) {
@@ -544,10 +565,11 @@ func EvaluateTaskRulesForDestination(blocks []*TaskAuthorizationRule, serviceTyp
func EffectiveTARExpiration(authorityExp time.Time, blocks []*TaskAuthorizationRule) time.Time {
effective := authorityExp
for _, block := range blocks {
- if block == nil || block.GetExpireTime() == nil || !block.GetExpireTime().IsValid() {
+ exp := block.GetExpireTime()
+ if block == nil || exp == nil || !exp.IsValid() || exp.AsTime().Unix() <= 0 {
continue
}
- t := block.GetExpireTime().AsTime()
+ t := exp.AsTime()
if effective.IsZero() || t.Before(effective) {
effective = t
}
diff --git a/api/tar_test.go b/api/tar_test.go
index 81843886..2f734d0c 100644
--- a/api/tar_test.go
+++ b/api/tar_test.go
@@ -418,4 +418,34 @@ func TestBuildTARFromOAuthParamsHardening(t *testing.T) {
if MatchHTTPPath("/repos/acme/*", "/repos/acme/%2e%2e/secret") {
t.Fatal("expected MatchHTTPPath to reject URL-encoded traversal")
}
+
+ // Nested unknown protobuf fields on TaskRule must be rejected by DecodeTARBlockPayload.
+ validRule := &TaskAuthorizationRule{
+ Name: "nested-unk",
+ Rules: []*TaskRule{{AllowedServices: []string{"mcp://weather"}}},
+ }
+ validRule.Rules[0].ProtoReflect().SetUnknown([]byte{0x98, 0x06, 0x01}) // field 99 = 1
+ rawWithNestedUnknown, err := proto.Marshal(validRule)
+ if err != nil {
+ t.Fatalf("proto.Marshal: %v", err)
+ }
+ if _, err := DecodeTARBlockPayload(base64.RawURLEncoding.EncodeToString(rawWithNestedUnknown)); err == nil {
+ t.Fatal("expected DecodeTARBlockPayload to reject unknown fields on nested TaskRule")
+ }
+
+ // Epoch / pre-epoch expire_time must be rejected by EvaluateTaskRules and EvaluateTaskRulesForDestination.
+ epochTAR := []*TaskAuthorizationRule{{
+ Name: "epoch",
+ ExpireTime: &timestamppb.Timestamp{Seconds: 0},
+ Rules: []*TaskRule{{AllowedServices: []string{"mcp://weather"}}},
+ }}
+ if err := EvaluateTaskRules(epochTAR, TaskRequestContext{ServiceType: "mcp", ServiceName: "weather"}, now); err == nil {
+ t.Fatal("expected EvaluateTaskRules to reject epoch expire_time")
+ }
+ if err := EvaluateTaskRulesForDestination(epochTAR, "mcp", "weather", now); err == nil {
+ t.Fatal("expected EvaluateTaskRulesForDestination to reject epoch expire_time")
+ }
+ if got := EffectiveTARExpiration(now, epochTAR); !got.Equal(now) {
+ t.Fatalf("EffectiveTARExpiration with epoch expire_time = %v, want %v", got, now)
+ }
}
diff --git a/api/trust.go b/api/trust.go
index 53306e09..31fe08a1 100644
--- a/api/trust.go
+++ b/api/trust.go
@@ -41,6 +41,9 @@ func ValidateControlPlaneTransport(rawURL string, allowInsecure bool) error {
if err != nil {
return fmt.Errorf("invalid control plane URL %q: %w", rawURL, err)
}
+ if u.Hostname() == "" {
+ return fmt.Errorf("control plane URL %q has no host", rawURL)
+ }
switch u.Scheme {
case "https":
return nil
diff --git a/api/trust_test.go b/api/trust_test.go
index aecfb197..41e64c46 100644
--- a/api/trust_test.go
+++ b/api/trust_test.go
@@ -46,6 +46,9 @@ func TestValidateControlPlaneTransport(t *testing.T) {
{"http://10.0.0.5:8080", false, true, ErrInsecureControlPlaneURL},
{"http://127.0.0.1.evil.example:8080", false, true, ErrInsecureControlPlaneURL},
{"http://sam-control-plane:8080", true, false, nil},
+ {"https://", false, true, nil},
+ {"https://:8443", false, true, nil},
+ {"http://", true, true, nil},
{"ftp://hub.sam-mesh.dev", false, true, nil},
{"hub.sam-mesh.dev", false, true, nil},
}
diff --git a/api/validation.go b/api/validation.go
index fd6b3d99..dfe67daa 100644
--- a/api/validation.go
+++ b/api/validation.go
@@ -132,6 +132,9 @@ func ValidateTargetFormat(target string) error {
if val == "" {
return fmt.Errorf("invalid target format %q: value cannot be empty", target)
}
+ if err := validateBindingMemberCharset(val); err != nil {
+ return fmt.Errorf("invalid target format %q: %w", target, err)
+ }
// "*" as the fact matches every target_fact (granted_target_all_facts).
if fact != "*" && !slices.Contains(TargetFactNames(), fact) {
return fmt.Errorf("invalid target %q: %q is not a target fact, so nothing would ever match it (want %s or \"*\")", target, fact, strings.Join(TargetFactNames(), ", "))
diff --git a/charts/sam-mesh/Chart.yaml b/charts/sam-mesh/Chart.yaml
index c7b90481..59fbb336 100644
--- a/charts/sam-mesh/Chart.yaml
+++ b/charts/sam-mesh/Chart.yaml
@@ -3,4 +3,4 @@ name: sam-mesh
description: A Helm chart for deploying the SAM control plane, router and console
type: application
version: 0.1.0
-appVersion: "0.1.0"
+appVersion: "1.0.0"
diff --git a/charts/sam-mesh/README.md b/charts/sam-mesh/README.md
index 6f78f479..9ef03708 100644
--- a/charts/sam-mesh/README.md
+++ b/charts/sam-mesh/README.md
@@ -86,9 +86,10 @@ with no default, because the right GatewayClass is provider-specific
(`cloud-provider-kind` in kind, `gke-l7-global-external-managed` on GKE,
`istio`, `envoy-gateway`, …).
-The route exposes only the control plane's enrollment surface (`/register`,
-`/info`, `/keys`, `/routers/lease`, `/policies`, `/enroll`, `/enroll/status`,
-`/refresh`, `/nodes/catalog`) and the console under `gateway.consolePath`; everything else,
+The route exposes only the control plane's enrollment and runtime surface (`/register`,
+`/info`, `/keys`, `/routers/lease`, `/policies`, `/egress`, `/revocations`,
+`/token/exchange`, `/sts/token`, `/enroll`, `/enroll/status`, `/refresh`,
+`/nodes/catalog`, `/.well-known/*`, `/jwks`, `/oauth/authorize`, `/oauth/token`) and the console under `gateway.consolePath`; everything else,
including `/admin` and `/user`, is unrouted. `gateway.adminRoute: true`
additionally routes `/admin` — a dev convenience, leave it off in production.
diff --git a/charts/sam-mesh/templates/control-plane-deployment.yaml b/charts/sam-mesh/templates/control-plane-deployment.yaml
index 3f449c0b..5046b8b3 100644
--- a/charts/sam-mesh/templates/control-plane-deployment.yaml
+++ b/charts/sam-mesh/templates/control-plane-deployment.yaml
@@ -55,6 +55,14 @@ spec:
secretKeyRef:
name: {{ include "sam-mesh.fullname" . }}-secrets
key: db-password
+ {{- if eq .Values.database.type "postgres" }}
+ {{- $dbHost := printf "%s-db" (include "sam-mesh.fullname" .) }}
+ {{- if not .Values.database.postgres.deployInternal }}
+ {{- $dbHost = .Values.database.postgres.host }}
+ {{- end }}
+ - name: SAM_DB_DSN
+ value: "postgres://{{ .Values.database.postgres.user }}:$(DB_PASSWORD)@{{ $dbHost }}:{{ .Values.database.postgres.port | default 5432 }}/{{ .Values.database.postgres.database }}?sslmode={{ .Values.database.postgres.sslmode | default "disable" }}"
+ {{- end }}
- name: SAM_ADMIN_TOKEN
valueFrom:
secretKeyRef:
@@ -83,11 +91,6 @@ spec:
- "--bind-address=0.0.0.0:8080"
{{- if eq .Values.database.type "postgres" }}
- "--db-driver=postgres"
- {{- $dbHost := printf "%s-db" (include "sam-mesh.fullname" .) }}
- {{- if not .Values.database.postgres.deployInternal }}
- {{- $dbHost = .Values.database.postgres.host }}
- {{- end }}
- - "--db-dsn=postgres://{{ .Values.database.postgres.user }}:$(DB_PASSWORD)@{{ $dbHost }}:{{ .Values.database.postgres.port | default 5432 }}/{{ .Values.database.postgres.database }}?sslmode={{ .Values.database.postgres.sslmode | default "disable" }}"
{{- else }}
- "--db-driver=sqlite"
- "--db-dsn=/data/control-plane.db"
diff --git a/charts/sam-mesh/templates/gateway.yaml b/charts/sam-mesh/templates/gateway.yaml
index 83f4b2e0..2d07216c 100644
--- a/charts/sam-mesh/templates/gateway.yaml
+++ b/charts/sam-mesh/templates/gateway.yaml
@@ -33,7 +33,7 @@ spec:
{{- toYaml . | nindent 2 }}
{{- end }}
rules:
- # The enrollment surface, matching the cloud's allow-list exactly.
+ # The enrollment and mesh runtime surface, matching the cloud's allow-list exactly.
- matches:
- path:
type: Exact
@@ -50,6 +50,18 @@ spec:
- path:
type: Exact
value: /policies
+ - path:
+ type: Exact
+ value: /egress
+ - path:
+ type: Exact
+ value: /revocations
+ - path:
+ type: Exact
+ value: /token/exchange
+ - path:
+ type: Exact
+ value: /sts/token
- path:
type: Exact
value: /enroll
@@ -62,6 +74,21 @@ spec:
- path:
type: Exact
value: /nodes/catalog
+ - path:
+ type: Exact
+ value: /.well-known/openid-configuration
+ - path:
+ type: Exact
+ value: /.well-known/oauth-authorization-server
+ - path:
+ type: Exact
+ value: /jwks
+ - path:
+ type: Exact
+ value: /oauth/authorize
+ - path:
+ type: Exact
+ value: /oauth/token
backendRefs:
- name: {{ $fullName }}-control-plane
port: {{ .Values.controlPlane.service.port }}
diff --git a/charts/sam-mesh/tests/control-plane-deployment_test.yaml b/charts/sam-mesh/tests/control-plane-deployment_test.yaml
index 69385e10..87db82ea 100644
--- a/charts/sam-mesh/tests/control-plane-deployment_test.yaml
+++ b/charts/sam-mesh/tests/control-plane-deployment_test.yaml
@@ -57,8 +57,11 @@ tests:
database.postgres.deployInternal: false
database.postgres.host: ext-db
asserts:
+ - equal:
+ path: spec.template.spec.containers[0].env[1].name
+ value: SAM_DB_DSN
- matchRegex:
- path: spec.template.spec.containers[0].args[2]
+ path: spec.template.spec.containers[0].env[1].value
pattern: "@ext-db:5432"
- it: scheduling knobs pass through to the pod spec
diff --git a/charts/sam-mesh/tests/gateway_test.yaml b/charts/sam-mesh/tests/gateway_test.yaml
index d494932e..e98c8329 100644
--- a/charts/sam-mesh/tests/gateway_test.yaml
+++ b/charts/sam-mesh/tests/gateway_test.yaml
@@ -42,6 +42,30 @@ tests:
path:
type: Exact
value: /nodes/catalog
+ - contains:
+ path: spec.rules[0].matches
+ content:
+ path:
+ type: Exact
+ value: /egress
+ - contains:
+ path: spec.rules[0].matches
+ content:
+ path:
+ type: Exact
+ value: /revocations
+ - contains:
+ path: spec.rules[0].matches
+ content:
+ path:
+ type: Exact
+ value: /token/exchange
+ - contains:
+ path: spec.rules[0].matches
+ content:
+ path:
+ type: Exact
+ value: /sts/token
- it: empty consolePath leaves the console unrouted
set:
diff --git a/charts/sam-node/templates/deployment.yaml b/charts/sam-node/templates/deployment.yaml
index 1868e9dc..30c0db09 100644
--- a/charts/sam-node/templates/deployment.yaml
+++ b/charts/sam-node/templates/deployment.yaml
@@ -14,9 +14,10 @@ spec:
labels:
{{- include "sam-node.selectorLabels" . | nindent 8 }}
annotations:
- # A config-only change must roll the pods: the node reads its config
- # file once at startup and never reloads it.
+ # A config-only or secret change must roll the pods: the node reads its
+ # config and API token once at startup and never reloads them.
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
+ checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
spec:
serviceAccountName: {{ default (include "sam-node.fullname" .) .Values.serviceAccount.name }}
# The node reads the projected token below; the default SA token would
diff --git a/charts/sam-node/tests/deployment_test.yaml b/charts/sam-node/tests/deployment_test.yaml
index cd8a4ceb..ef90e370 100644
--- a/charts/sam-node/tests/deployment_test.yaml
+++ b/charts/sam-node/tests/deployment_test.yaml
@@ -1,8 +1,9 @@
suite: sam-node deployment
templates:
- # configmap.yaml must be loaded for the checksum/config include to resolve
+ # configmap.yaml and secret.yaml must be loaded for the checksum includes to resolve
- templates/deployment.yaml
- templates/configmap.yaml
+ - templates/secret.yaml
tests:
- it: fails without controlPlaneUrl
template: templates/deployment.yaml
diff --git a/cmd/mcp-client/main.go b/cmd/mcp-client/main.go
index 182d5813..adb78ec8 100644
--- a/cmd/mcp-client/main.go
+++ b/cmd/mcp-client/main.go
@@ -42,7 +42,8 @@ func main() {
timeoutArgs := flag.Int("timeout", 10, "Timeout in seconds")
listTools := flag.Bool("list", false, "List available tools and exit")
streamOpt := flag.Bool("stream", false, "Enable streaming mode for service discovery HTTP API")
- tokenOpt := flag.String("token", "", "Authorization Bearer token for protected sidecar endpoints")
+ tokenOpt := flag.String("token", "", "Authorization Bearer token for protected sidecar endpoints (deprecated: prefer -token-path or SAM_API_TOKEN)")
+ tokenPathOpt := flag.String("token-path", "", "Path to file containing the Authorization Bearer token (or env SAM_API_TOKEN)")
showVersion := flag.Bool("version", false, "Print the version and exit")
flag.Parse()
if *showVersion {
@@ -50,6 +51,17 @@ func main() {
return
}
+ resolvedToken := strings.TrimSpace(*tokenOpt)
+ if *tokenPathOpt != "" {
+ data, err := os.ReadFile(*tokenPathOpt)
+ if err != nil {
+ log.Fatalf("Failed to read -token-path %s: %v", *tokenPathOpt, err)
+ }
+ resolvedToken = strings.TrimSpace(string(data))
+ } else if resolvedToken == "" {
+ resolvedToken = strings.TrimSpace(os.Getenv("SAM_API_TOKEN"))
+ }
+
if *serverURL == "" {
log.Fatal("Must specify -url")
}
@@ -99,8 +111,8 @@ func main() {
log.Fatalf("Failed to create request: %v", err)
}
- if *tokenOpt != "" {
- req.Header.Set(api.HeaderSamAuthentication, "Bearer "+*tokenOpt)
+ if resolvedToken != "" {
+ req.Header.Set(api.HeaderSamAuthentication, "Bearer "+resolvedToken)
}
resp, err := http.DefaultClient.Do(req)
@@ -154,12 +166,12 @@ func main() {
// Connect to server using the URL
var mcpTransport mcp.Transport = &mcp.StreamableClientTransport{Endpoint: *serverURL}
- if *tokenOpt != "" {
+ if resolvedToken != "" {
mcpTransport = &mcp.StreamableClientTransport{
Endpoint: *serverURL,
HTTPClient: &http.Client{
Transport: &authTransport{
- token: *tokenOpt,
+ token: resolvedToken,
underlying: http.DefaultTransport,
},
},
diff --git a/cmd/sam-bench/main.go b/cmd/sam-bench/main.go
index feca1920..a04d4bf3 100644
--- a/cmd/sam-bench/main.go
+++ b/cmd/sam-bench/main.go
@@ -40,6 +40,7 @@ import (
"github.com/spf13/cobra"
"github.com/google/sam/internal/bench"
+ "github.com/google/sam/internal/version"
)
// observation is one complete, self-describing measurement.
@@ -77,8 +78,9 @@ func main() {
)
rootCmd := &cobra.Command{
- Use: "sam-bench",
- Short: "Measure the mesh from where an agent stands",
+ Use: "sam-bench",
+ Version: version.String(),
+ Short: "Measure the mesh from where an agent stands",
}
runCmd := &cobra.Command{
diff --git a/cmd/sam-bench/report.go b/cmd/sam-bench/report.go
index 6940cc65..fba69886 100644
--- a/cmd/sam-bench/report.go
+++ b/cmd/sam-bench/report.go
@@ -131,10 +131,10 @@ func render(observations []observation, by string, metrics, gauges []string) str
}
var b strings.Builder
- b.WriteString("| " + strings.Join(header, " | ") + " |\n")
- b.WriteString("|" + strings.Repeat(" --- |", len(header)) + "\n")
+ fmt.Fprintf(&b, "| %s |\n", strings.Join(header, " | "))
+ fmt.Fprintf(&b, "|%s\n", strings.Repeat(" --- |", len(header)))
for _, row := range rows {
- b.WriteString("| " + strings.Join(row, " | ") + " |\n")
+ fmt.Fprintf(&b, "| %s |\n", strings.Join(row, " | "))
}
return b.String()
}
diff --git a/cmd/sam-node/daemonize.go b/cmd/sam-node/daemonize.go
index 15861418..53242e40 100644
--- a/cmd/sam-node/daemonize.go
+++ b/cmd/sam-node/daemonize.go
@@ -76,6 +76,11 @@ func daemonizeRun(socketPath string) error {
if err != nil {
return err
}
+ secretArgs, err := migrateSecretFlagsToFiles(dataDir)
+ if err != nil {
+ return err
+ }
+ tokenArgs = append(tokenArgs, secretArgs...)
logPath := filepath.Join(dataDir, daemonLogFile)
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
@@ -334,13 +339,45 @@ func ensureNodeStopped(dataDir string) error {
return store.Close()
}
-// withoutDaemonizeFlag drops --daemonize so the child runs in the foreground.
+// migrateSecretFlagsToFiles persists any deprecated inline --bootstrap-token
+// or --jwt flag value to a 0600 file inside dataDir and returns the
+// corresponding --*-path flags so secrets never sit in the daemon's cmdline.
+func migrateSecretFlagsToFiles(dataDir string) ([]string, error) {
+ var extra []string
+ if bootstrapTokenFlag != "" && bootstrapTokenPathFlag == "" {
+ p := filepath.Join(dataDir, "bootstrap-token")
+ if err := os.WriteFile(p, []byte(bootstrapTokenFlag), 0600); err != nil {
+ return nil, fmt.Errorf("writing %s: %w", p, err)
+ }
+ extra = append(extra, "--bootstrap-token-path", p)
+ }
+ if jwtFlag != "" && jwtPathFlag == "" {
+ p := filepath.Join(dataDir, "enrollment-jwt")
+ if err := os.WriteFile(p, []byte(jwtFlag), 0600); err != nil {
+ return nil, fmt.Errorf("writing %s: %w", p, err)
+ }
+ extra = append(extra, "--jwt-path", p)
+ }
+ return extra, nil
+}
+
+// withoutDaemonizeFlag drops --daemonize and any inline secret flags
+// (--bootstrap-token, --jwt) so the child runs in the foreground without
+// exposing credentials in ps output.
func withoutDaemonizeFlag(args []string) []string {
out := make([]string, 0, len(args))
- for _, arg := range args {
+ for i := 0; i < len(args); i++ {
+ arg := args[i]
if arg == "--daemonize" || strings.HasPrefix(arg, "--daemonize=") {
continue
}
+ if arg == "--bootstrap-token" || arg == "--jwt" {
+ i++ // skip value
+ continue
+ }
+ if strings.HasPrefix(arg, "--bootstrap-token=") || strings.HasPrefix(arg, "--jwt=") {
+ continue
+ }
out = append(out, arg)
}
return out
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.go
index 34b8e2e3..c8203b42 100644
--- a/cmd/sam-node/main.go
+++ b/cmd/sam-node/main.go
@@ -610,7 +610,7 @@ func main() {
// Register static services from config
if nodeConfig != nil && len(nodeConfig.Services) > 0 {
- if err := meshNode.RegisterStaticServices(context.Background(), nodeConfig.Services); err != nil {
+ if err := meshNode.RegisterStaticServices(ctx, nodeConfig.Services); err != nil {
logger.Fatalf("Failed to register static services: %v", err)
}
}
@@ -629,7 +629,9 @@ func main() {
logger.Fatalf("Failed to start sidecar server: %v", err)
}
defer func() {
- _ = sidecarSrv.Close()
+ shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer shutdownCancel()
+ _ = sidecarSrv.Shutdown(shutdownCtx)
if err := meshNode.Teardown(); err != nil {
logger.Warnf("Error during mesh node teardown: %v", err)
}
@@ -640,7 +642,11 @@ func main() {
if err != nil {
logger.Fatalf("Failed to start metrics server: %v", err)
}
- defer func() { _ = metricsSrv.Close() }()
+ defer func() {
+ shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer shutdownCancel()
+ _ = metricsSrv.Shutdown(shutdownCtx)
+ }()
}
fmt.Printf("SAM Node Online.\nPeerID: %s\nListening on: %v\n", meshNode.Host.ID(), meshNode.Host.Addrs())
@@ -752,6 +758,11 @@ func main() {
if err := meshNode.Start(ctx); err != nil {
logger.Fatalf("Failed to start node for enrollment: %v", err)
}
+ defer func() {
+ if err := meshNode.Teardown(); err != nil {
+ logger.Warnf("Error during mesh node teardown: %v", err)
+ }
+ }()
if bootstrapTokenFlag != "" {
err = meshNode.EnrollBootstrap(ctx, targetControlPlane, bootstrapTokenFlag)
@@ -909,6 +920,8 @@ func resolveDataDir() string {
var dir string
if dataDirFlag != "" {
dir = dataDirFlag
+ } else if envDir := strings.TrimSpace(os.Getenv("SAM_DATA_DIR")); envDir != "" {
+ dir = envDir
} else {
d, err := node.GetDefaultDataDir()
if err != nil {
diff --git a/cmd/sam-one/admin.go b/cmd/sam-one/admin.go
index d859d22b..26124f20 100644
--- a/cmd/sam-one/admin.go
+++ b/cmd/sam-one/admin.go
@@ -28,7 +28,6 @@ import (
"github.com/google/sam/internal/standalone"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/proto"
)
// adminClient talks to a running sam-one's admin API; subcommands never open
@@ -85,7 +84,7 @@ func (c *adminClient) do(method, path, contentType string, body []byte) ([]byte,
// a device that spends days offline needs and what a stolen device should
// not get.
func (c *adminClient) createToken(role string, ttlHours, maxUsages int, description string, autonomousRecovery bool) (*api.BootstrapTokenCreateResponse, error) {
- payload, err := protojson.Marshal(&api.BootstrapTokenCreateRequest{
+ payload, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(&api.BootstrapTokenCreateRequest{
Role: role,
TtlHours: int32(ttlHours),
MaxUsages: int32(maxUsages),
@@ -119,11 +118,11 @@ func (c *adminClient) listTokens() ([]*api.BootstrapToken, error) {
}
func (c *adminClient) banPeer(peerID string) error {
- payload, err := proto.Marshal(&api.TokenRevokeRequest{PeerId: peerID})
+ payload, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(&api.TokenRevokeRequest{PeerId: peerID})
if err != nil {
return err
}
- _, err = c.do(http.MethodPost, "/admin/revoke", "application/x-protobuf", payload)
+ _, err = c.do(http.MethodPost, "/admin/revoke", "application/json", payload)
return err
}
diff --git a/cmd/sam-one/admin_test.go b/cmd/sam-one/admin_test.go
index d4a3b9d1..52600033 100644
--- a/cmd/sam-one/admin_test.go
+++ b/cmd/sam-one/admin_test.go
@@ -27,7 +27,6 @@ import (
"github.com/google/sam/api"
"google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
@@ -44,6 +43,10 @@ func newFakeAdminAPI(t *testing.T) *httptest.Server {
// Decode into the shared wire type, as the control plane does, so a
// client that drifts from it fails here.
body, _ := io.ReadAll(r.Body)
+ if !strings.Contains(string(body), `"ttl_hours"`) || !strings.Contains(string(body), `"max_usages"`) {
+ http.Error(w, "expected proto field names (snake_case)", http.StatusBadRequest)
+ return
+ }
req := &api.BootstrapTokenCreateRequest{}
if err := protojson.Unmarshal(body, req); err != nil {
http.Error(w, "bad body", http.StatusBadRequest)
@@ -93,11 +96,11 @@ func newFakeAdminAPI(t *testing.T) *httptest.Server {
}
body, _ := io.ReadAll(r.Body)
var req api.TokenRevokeRequest
- if err := proto.Unmarshal(body, &req); err != nil || req.PeerId != "12D3KooTestPeer" {
+ if err := protojson.Unmarshal(body, &req); err != nil || req.PeerId != "12D3KooTestPeer" || !strings.Contains(string(body), `"peer_id"`) {
http.Error(w, "wrong peer", http.StatusBadRequest)
return
}
- _, _ = w.Write([]byte("ok"))
+ _, _ = w.Write([]byte(`{"success":true}`))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
diff --git a/cmd/sam-one/main.go b/cmd/sam-one/main.go
index 1c9e8775..023192eb 100644
--- a/cmd/sam-one/main.go
+++ b/cmd/sam-one/main.go
@@ -177,9 +177,15 @@ func main() {
Router: routerTunables,
})
if err != nil {
+ if tun != nil {
+ _ = tun.Close()
+ }
logger.Fatalf("Invalid configuration: %v", err)
}
if err := srv.Start(cmd.Context()); err != nil {
+ if tun != nil {
+ _ = tun.Close()
+ }
logger.Fatalf("Failed to start: %v", err)
}
defer func() {
diff --git a/cmd/sam-router/main.go b/cmd/sam-router/main.go
index 25863e92..12a4542e 100644
--- a/cmd/sam-router/main.go
+++ b/cmd/sam-router/main.go
@@ -105,6 +105,7 @@ func main() {
}()
if err := r.Start(); err != nil {
+ _ = r.Close()
logger.Fatalf("Failed to start router: %v", err)
}
diff --git a/hack/gen-proto.sh b/hack/gen-proto.sh
index 75034201..b13fc261 100755
--- a/hack/gen-proto.sh
+++ b/hack/gen-proto.sh
@@ -19,8 +19,26 @@ echo "Installing protobuf Go plugins..."
# Keep in sync with the google.golang.org/protobuf version in go.mod.
go install google.golang.org/protobuf/cmd/[email protected]
+resolve_protoc() {
+ if [[ -n "${PROTOC:-}" ]]; then
+ echo "${PROTOC}"
+ return
+ fi
+ local p base
+ while IFS= read -r p; do
+ base=$(dirname "$(dirname "${p}")")
+ if [[ -f "${base}/include/google/protobuf/timestamp.proto" ]]; then
+ echo "${p}"
+ return
+ fi
+ done < <(type -ap protoc 2>/dev/null || true)
+ command -v protoc
+}
+
+PROTOC_BIN=$(resolve_protoc)
+
echo "Generating Go protobuf code..."
mkdir -p api
-protoc --go_out=paths=source_relative:. api/sam.proto
+"${PROTOC_BIN}" --go_out=paths=source_relative:. api/sam.proto
echo "Protobuf generation complete."
diff --git a/hack/gen-sdk-proto.sh b/hack/gen-sdk-proto.sh
index 97a618a5..112be93f 100755
--- a/hack/gen-sdk-proto.sh
+++ b/hack/gen-sdk-proto.sh
@@ -35,9 +35,27 @@ if [[ ! -x "${PROTOC_GEN_ES}" ]]; then
exit 1
fi
+resolve_protoc() {
+ if [[ -n "${PROTOC:-}" ]]; then
+ echo "${PROTOC}"
+ return
+ fi
+ local p base
+ while IFS= read -r p; do
+ base=$(dirname "$(dirname "${p}")")
+ if [[ -f "${base}/include/google/protobuf/timestamp.proto" ]]; then
+ echo "${p}"
+ return
+ fi
+ done < <(type -ap protoc 2>/dev/null || true)
+ command -v protoc
+}
+
+PROTOC_BIN=$(resolve_protoc)
+
echo "Generating JavaScript protobuf code..."
mkdir -p "${JS_GEN_DIR}"
-protoc -I api \
+"${PROTOC_BIN}" -I api \
--plugin="protoc-gen-es=${PROTOC_GEN_ES}" \
--es_out="${JS_GEN_DIR}" \
--es_opt=target=ts \
@@ -45,13 +63,13 @@ protoc -I api \
echo "Generating Python protobuf code..."
mkdir -p "${PY_GEN_DIR}"
-protoc -I api \
+"${PROTOC_BIN}" -I api \
--python_out="${PY_GEN_DIR}" \
--pyi_out="${PY_GEN_DIR}" \
api/sam.proto
# The relay v2 client speaks go-libp2p's circuit.proto directly; see the
# proto's header for why the SDK carries a copy.
-protoc -I sdk/python/proto \
+"${PROTOC_BIN}" -I sdk/python/proto \
--python_out="${PY_GEN_DIR}" \
--pyi_out="${PY_GEN_DIR}" \
sdk/python/proto/circuit.proto
diff --git a/hack/verify-generated.sh b/hack/verify-generated.sh
index 07853037..2ccc8aa4 100755
--- a/hack/verify-generated.sh
+++ b/hack/verify-generated.sh
@@ -31,14 +31,16 @@ cleanup() {
}
trap cleanup EXIT
-# Move to worktree root
+# Move to worktree root and sync current proto + generator script
cd "${TMP_DIR}"
+cp "${REPO_ROOT}/api/sam.proto" "${TMP_DIR}/api/sam.proto"
+cp "${REPO_ROOT}/hack/gen-proto.sh" "${TMP_DIR}/hack/gen-proto.sh"
echo "Running code generation..."
./hack/gen-proto.sh
echo "Checking for differences..."
-if ! git diff --exit-code; then
+if ! diff -u -I $'^// \tprotoc v' "${REPO_ROOT}/api/sam.pb.go" "${TMP_DIR}/api/sam.pb.go"; then
echo "ERROR: Generated code is not up to date."
echo "Please run ./hack/gen-proto.sh locally and commit the changes."
exit 1
diff --git a/install.sh b/install.sh
index 9504c395..ea57fff3 100755
--- a/install.sh
+++ b/install.sh
@@ -52,25 +52,29 @@ if ! curl -sfL -o "${TAR_NAME}" "${DOWNLOAD_URL}"; then
exit 1
fi
-if curl -sfL -o checksums.txt "${CHECKSUMS_URL}"; then
- echo "Verifying SHA-256 checksum..."
- EXPECTED_SUM=$(awk -v f="${TAR_NAME}" '$2 == f {print $1}' checksums.txt)
- if [ -z "${EXPECTED_SUM}" ]; then
- echo "Error: ${TAR_NAME} not found in checksums.txt"
- exit 1
- fi
- if command -v sha256sum >/dev/null 2>&1; then
- ACTUAL_SUM=$(sha256sum "${TAR_NAME}" | awk '{print $1}')
- elif command -v shasum >/dev/null 2>&1; then
- ACTUAL_SUM=$(shasum -a 256 "${TAR_NAME}" | awk '{print $1}')
- else
- echo "Error: Neither sha256sum nor shasum is available to verify archive integrity."
- exit 1
- fi
- if [ "${EXPECTED_SUM}" != "${ACTUAL_SUM}" ]; then
- echo "Error: SHA-256 checksum mismatch for ${TAR_NAME} (expected ${EXPECTED_SUM}, got ${ACTUAL_SUM})"
- exit 1
- fi
+echo "Downloading ${CHECKSUMS_URL}..."
+if ! curl -sfL -o checksums.txt "${CHECKSUMS_URL}"; then
+ echo "Error: Failed to download ${CHECKSUMS_URL}"
+ exit 1
+fi
+
+echo "Verifying SHA-256 checksum..."
+EXPECTED_SUM=$(awk -v f="${TAR_NAME}" '$2 == f {print $1}' checksums.txt)
+if [ -z "${EXPECTED_SUM}" ]; then
+ echo "Error: ${TAR_NAME} not found in checksums.txt"
+ exit 1
+fi
+if command -v sha256sum >/dev/null 2>&1; then
+ ACTUAL_SUM=$(sha256sum "${TAR_NAME}" | awk '{print $1}')
+elif command -v shasum >/dev/null 2>&1; then
+ ACTUAL_SUM=$(shasum -a 256 "${TAR_NAME}" | awk '{print $1}')
+else
+ echo "Error: Neither sha256sum nor shasum is available to verify archive integrity."
+ exit 1
+fi
+if [ "${EXPECTED_SUM}" != "${ACTUAL_SUM}" ]; then
+ echo "Error: SHA-256 checksum mismatch for ${TAR_NAME} (expected ${EXPECTED_SUM}, got ${ACTUAL_SUM})"
+ exit 1
fi
echo "Extracting..."
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index b0843c00..528da42a 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -2796,9 +2796,18 @@ func (s *Server) HandleAdminRevoke(w http.ResponseWriter, r *http.Request) {
}
var req api.TokenRevokeRequest
- if err := proto.Unmarshal(body, &req); err != nil {
- http.Error(w, "Invalid request format", http.StatusBadRequest)
- return
+ isJSON := strings.HasPrefix(strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type"))), "application/json")
+ if isJSON {
+ if err := (protojson.UnmarshalOptions{DiscardUnknown: false}).Unmarshal(body, &req); err != nil {
+ http.Error(w, "Invalid request format", http.StatusBadRequest)
+ return
+ }
+ } else if err := proto.Unmarshal(body, &req); err != nil {
+ if jsonErr := (protojson.UnmarshalOptions{DiscardUnknown: false}).Unmarshal(body, &req); jsonErr != nil {
+ http.Error(w, "Invalid request format", http.StatusBadRequest)
+ return
+ }
+ isJSON = true
}
if req.PeerId == "" {
@@ -2837,6 +2846,10 @@ func (s *Server) HandleAdminRevoke(w http.ResponseWriter, r *http.Request) {
resp := &api.TokenRevokeResponse{
Success: true,
}
+ if isJSON {
+ writeProtoJSON(w, resp)
+ return
+ }
respData, err := proto.Marshal(resp)
if err != nil {
http.Error(w, "Failed to serialize response", http.StatusInternalServerError)
diff --git a/internal/controlplane/sts.go b/internal/controlplane/sts.go
index e27e72ee..7676360e 100644
--- a/internal/controlplane/sts.go
+++ b/internal/controlplane/sts.go
@@ -748,6 +748,10 @@ func (s *Server) HandleTokenExchange(w http.ResponseWriter, r *http.Request) {
writeChallengeError(w, "Unauthorized: node credential required")
return
}
+ if nodeRecord.Role == api.RoleRouter {
+ http.Error(w, "Forbidden: router credentials cannot exchange tokens", http.StatusForbidden)
+ return
+ }
if !s.allowNodeSTSRequest(nodeRecord.PeerID) {
http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)
return
@@ -945,6 +949,10 @@ func (s *Server) HandleSTSToken(w http.ResponseWriter, r *http.Request) {
writeChallengeError(w, "Unauthorized: node credential required")
return
}
+ if nodeRecord.Role == api.RoleRouter {
+ http.Error(w, "Forbidden: router credentials cannot mint STS tokens", http.StatusForbidden)
+ return
+ }
if !s.allowNodeSTSRequest(nodeRecord.PeerID) {
http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)
return
@@ -1308,12 +1316,24 @@ func isValidOAuthRedirectURI(raw string) bool {
return false
}
+func clientRateLimitKey(r *http.Request) string {
+ host, _, err := net.SplitHostPort(r.RemoteAddr)
+ if err != nil || host == "" {
+ return "oauth:" + r.RemoteAddr
+ }
+ return "oauth:" + host
+}
+
// HandleOAuthAuthorize serves GET and POST `/oauth/authorize` (OAuth 2.1 Authorization Code + PKCE S256).
func (s *Server) HandleOAuthAuthorize(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
+ if !s.allowNodeSTSRequest(clientRateLimitKey(r)) {
+ http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)
+ return
+ }
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form parameters", http.StatusBadRequest)
@@ -1503,6 +1523,10 @@ func (s *Server) HandleOAuthToken(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
+ if !s.allowNodeSTSRequest(clientRateLimitKey(r)) {
+ writeOAuthError(w, http.StatusTooManyRequests, "temporarily_unavailable", "rate limit exceeded")
+ return
+ }
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)
if err := r.ParseForm(); err != nil {
writeOAuthError(w, http.StatusBadRequest, "invalid_request", "Failed to parse form body")
diff --git a/internal/controlplane/sts_test.go b/internal/controlplane/sts_test.go
index 4f44ef5a..49ab487e 100644
--- a/internal/controlplane/sts_test.go
+++ b/internal/controlplane/sts_test.go
@@ -39,6 +39,7 @@ import (
"github.com/google/sam/api"
"github.com/google/sam/internal/identity"
"github.com/google/sam/internal/node"
+ "github.com/google/sam/internal/storage"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/proto"
@@ -845,6 +846,60 @@ func TestSTSSecurityHardening(t *testing.T) {
if okResp.StatusCode != http.StatusOK {
t.Fatalf("expected 200 from authorized ServedBy node, got %d", okResp.StatusCode)
}
+
+ // 3. Router credentials (sam:role:router) must be forbidden on /token/exchange and /sts/token
+ routerPriv, routerPub, err := crypto.GenerateEd25519Key(rand.Reader)
+ if err != nil {
+ t.Fatalf("GenerateEd25519Key: %v", err)
+ }
+ routerPeerID, err := peer.IDFromPrivateKey(routerPriv)
+ if err != nil {
+ t.Fatalf("IDFromPrivateKey: %v", err)
+ }
+ routerPubBytes, _ := crypto.MarshalPublicKey(routerPub)
+ cpPriv, _, err := store.GetCurrentKey(ctx)
+ if err != nil {
+ t.Fatalf("GetCurrentKey: %v", err)
+ }
+ routerBiscuit, err := identity.MintBootstrapBiscuitToken(cpPriv, routerPeerID, api.RoleRouter, time.Now().Add(time.Hour), roles, nil)
+ if err != nil {
+ t.Fatalf("MintBootstrapBiscuitToken: %v", err)
+ }
+ if err := store.EnrollNode(ctx, &storage.EnrolledNode{
+ PeerID: routerPeerID.String(),
+ PublicKey: routerPubBytes,
+ Biscuit: routerBiscuit,
+ Role: api.RoleRouter,
+ EnrollmentType: "BOOTSTRAP",
+ EnrolledAt: time.Now(),
+ }); err != nil {
+ t.Fatalf("EnrollNode router: %v", err)
+ }
+
+ routerSTSResp := callSTS(routerPriv, routerPeerID, routerBiscuit, "sts.amazonaws.com")
+ _ = routerSTSResp.Body.Close()
+ if routerSTSResp.StatusCode != http.StatusForbidden {
+ t.Fatalf("expected 403 when router calls /sts/token, got %d", routerSTSResp.StatusCode)
+ }
+
+ exTS := time.Now().UnixMilli()
+ exSig, _ := routerPriv.Sign(api.TokenExchangeChallenge(routerPeerID.String(), exTS))
+ exReqBytes, _ := proto.Marshal(&api.TokenExchangeRequest{
+ SubjectToken: mintOIDC(map[string]interface{}{"sub": "alice-sub"}),
+ ChallengeUnixMs: exTS,
+ ChallengeSignature: exSig,
+ })
+ exReq, _ := http.NewRequest(http.MethodPost, baseURL+"/token/exchange", bytes.NewReader(exReqBytes))
+ exReq.Header.Set("Content-Type", "application/x-protobuf")
+ exReq.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString(routerBiscuit))
+ routerExResp, err := http.DefaultClient.Do(exReq)
+ if err != nil {
+ t.Fatalf("POST /token/exchange as router: %v", err)
+ }
+ _ = routerExResp.Body.Close()
+ if routerExResp.StatusCode != http.StatusForbidden {
+ t.Fatalf("expected 403 when router calls /token/exchange, got %d", routerExResp.StatusCode)
+ }
}
func TestOAuthAndControlPlaneHardening(t *testing.T) {
diff --git a/internal/envoy/server.go b/internal/envoy/server.go
index ebbddf3d..3405e649 100644
--- a/internal/envoy/server.go
+++ b/internal/envoy/server.go
@@ -312,18 +312,23 @@ func (s *GatewayServer) Process(stream extprocv3.ExternalProcessor_ProcessServer
strings.EqualFold(capturedMethod, http.MethodPost) &&
capturedHeaders[strings.ToLower(HeaderSamMCPTool)] == "" &&
isMCPRoute {
- pendingCheck = true
- resp = &extprocv3.ProcessingResponse{
- Response: &extprocv3.ProcessingResponse_RequestHeaders{
- RequestHeaders: &extprocv3.HeadersResponse{
- Response: &extprocv3.CommonResponse{
- Status: extprocv3.CommonResponse_CONTINUE,
+ preResp := s.evaluateExtProcDecision(stream.Context(), capturedMethod, capturedPath, capturedHost, capturedHeaders, false, true)
+ if preResp.GetImmediateResponse() != nil {
+ resp = preResp
+ } else {
+ pendingCheck = true
+ resp = &extprocv3.ProcessingResponse{
+ Response: &extprocv3.ProcessingResponse_RequestHeaders{
+ RequestHeaders: &extprocv3.HeadersResponse{
+ Response: &extprocv3.CommonResponse{
+ Status: extprocv3.CommonResponse_CONTINUE,
+ },
},
},
- },
- ModeOverride: &extprocv3http.ProcessingMode{
- RequestBodyMode: extprocv3http.ProcessingMode_BUFFERED,
- },
+ ModeOverride: &extprocv3http.ProcessingMode{
+ RequestBodyMode: extprocv3http.ProcessingMode_BUFFERED,
+ },
+ }
}
} else {
resp = s.evaluateExtProcDecision(stream.Context(), capturedMethod, capturedPath, capturedHost, capturedHeaders, false, false)
@@ -354,6 +359,13 @@ func (s *GatewayServer) Process(stream extprocv3.ExternalProcessor_ProcessServer
}
case *extprocv3.ProcessingRequest_ResponseHeaders:
+ if pendingCheck {
+ pendingCheck = false
+ if denyResp := s.evaluateExtProcDecision(stream.Context(), capturedMethod, capturedPath, capturedHost, capturedHeaders, false, false); denyResp.GetImmediateResponse() != nil {
+ resp = denyResp
+ break
+ }
+ }
resp = &extprocv3.ProcessingResponse{
Response: &extprocv3.ProcessingResponse_ResponseHeaders{
ResponseHeaders: &extprocv3.HeadersResponse{
@@ -376,6 +388,13 @@ func (s *GatewayServer) Process(stream extprocv3.ExternalProcessor_ProcessServer
}
case *extprocv3.ProcessingRequest_RequestTrailers:
+ if pendingCheck {
+ pendingCheck = false
+ if denyResp := s.evaluateExtProcDecision(stream.Context(), capturedMethod, capturedPath, capturedHost, capturedHeaders, false, false); denyResp.GetImmediateResponse() != nil {
+ resp = denyResp
+ break
+ }
+ }
resp = &extprocv3.ProcessingResponse{
Response: &extprocv3.ProcessingResponse_RequestTrailers{
RequestTrailers: &extprocv3.TrailersResponse{},
diff --git a/internal/node/egress.go b/internal/node/egress.go
index 3257d4d4..37c7a7ad 100644
--- a/internal/node/egress.go
+++ b/internal/node/egress.go
@@ -477,6 +477,13 @@ func handleLocalEgress(node *SamNode, w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
+ requiredLabels, err := parseRequiredLabels(r.Header.Get(api.HeaderSamRequiredLabels))
+ if err != nil {
+ refuse(w, http.StatusBadRequest, fmt.Sprintf("Invalid %s header: %v", api.HeaderSamRequiredLabels, err), proxyStatusDenied)
+ return
+ }
+ r.Header.Del(api.HeaderSamRequiredLabels)
+
host, upstreamPath, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/egress/"), "/")
host = api.NormalizeMeshHost(host)
if host == "" {
@@ -489,6 +496,16 @@ func handleLocalEgress(node *SamNode, w http.ResponseWriter, r *http.Request) {
refuse(w, http.StatusNotFound, fmt.Sprintf("no egress destination %q is assigned to this node", host), proxyStatusDestinationNotFound)
return
}
+ if outsideLabels(requiredLabels, node.labels(), true) {
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Required labels not attested by provider", proxyStatusDenied)
+ return
+ }
+ if outsideLabels(node.egressFloor(), node.labels(), true) {
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Forbidden: provider does not attest the egress floor", proxyStatusDenied)
+ return
+ }
identity := node.GetRequestIdentity(r.Context())
if len(identity) == 0 {
http.Error(w, "node has no credential yet", http.StatusServiceUnavailable)
diff --git a/internal/node/egress_inspect.go b/internal/node/egress_inspect.go
index 6f5698a1..98356b1a 100644
--- a/internal/node/egress_inspect.go
+++ b/internal/node/egress_inspect.go
@@ -23,6 +23,8 @@ import (
"fmt"
"io"
"net/http"
+ "net/netip"
+ "net/url"
"strconv"
"strings"
"time"
@@ -342,6 +344,8 @@ func (s *EgressService) serveInspectedEgress(w http.ResponseWriter, r *http.Requ
r.ContentLength = int64(len(reqBody))
if len(reqBody) > 0 {
r.Header.Set("Content-Length", strconv.Itoa(len(reqBody)))
+ } else {
+ r.Header.Del("Content-Length")
}
reqCtx := context.WithValue(r.Context(), egressAuthKey{}, auth)
@@ -638,6 +642,15 @@ func (s *EgressService) resolveModelArmorEndpoint(ctx context.Context, template
return strings.TrimRight(s.modelArmorBaseURL, "/") + "/v1/" + strings.TrimLeft(tmpl, "/"), "", nil
}
if strings.HasPrefix(tmpl, "http://") || strings.HasPrefix(tmpl, "https://") {
+ u, err := url.Parse(tmpl)
+ if err != nil || u.Hostname() == "" {
+ return "", "", fmt.Errorf("invalid model_armor.template URL %q", tmpl)
+ }
+ if ip, pErr := netip.ParseAddr(u.Hostname()); pErr == nil {
+ if isForbiddenEgressAddr(ip, s.allowsLocalTarget()) {
+ return "", "", fmt.Errorf("model_armor.template URL host %q is in a forbidden IP range", u.Hostname())
+ }
+ }
return tmpl, "", nil
}
// Parse location from projects/P/locations/L/templates/T
@@ -731,19 +744,28 @@ func extractInspectableText(body []byte) string {
}
func replaceInspectableText(body []byte, original, replacement string) ([]byte, error) {
+ if original == replacement {
+ return body, nil
+ }
var doc map[string]any
if err := json.Unmarshal(body, &doc); err != nil {
return bytes.ReplaceAll(body, []byte(original), []byte(replacement)), nil
}
// If messages[].content were joined by '\n' in extractInspectableText, split back
- // when the replacement has the same number of lines; otherwise replace in-place.
+ // using each message's original line count; if the line count matches, map each
+ // message back in-place.
if msgs, ok := doc["messages"].([]any); ok {
var contentMaps []map[string]any
+ var lineCounts []int
+ totalLines := 0
for _, m := range msgs {
if mm, ok := m.(map[string]any); ok {
if c, ok := mm["content"].(string); ok && c != "" {
contentMaps = append(contentMaps, mm)
+ lc := strings.Count(c, "\n") + 1
+ lineCounts = append(lineCounts, lc)
+ totalLines += lc
}
}
}
@@ -753,9 +775,12 @@ func replaceInspectableText(body []byte, original, replacement string) ([]byte,
}
if len(contentMaps) > 1 {
parts := strings.Split(replacement, "\n")
- if len(parts) == len(contentMaps) {
+ if len(parts) == totalLines {
+ offset := 0
for i, mm := range contentMaps {
- mm["content"] = parts[i]
+ lc := lineCounts[i]
+ mm["content"] = strings.Join(parts[offset:offset+lc], "\n")
+ offset += lc
}
return json.Marshal(doc)
}
@@ -766,7 +791,10 @@ func replaceInspectableText(body []byte, original, replacement string) ([]byte,
if modified {
return json.Marshal(updated)
}
- return bytes.ReplaceAll(body, []byte(original), []byte(replacement)), nil
+ if bytes.Contains(body, []byte(original)) {
+ return bytes.ReplaceAll(body, []byte(original), []byte(replacement)), nil
+ }
+ return nil, errors.New("failed to map inspection replacement back into structured JSON payload")
}
func replaceStringInJSONValue(v any, original, replacement string) (any, bool) {
diff --git a/internal/node/egress_inspect_test.go b/internal/node/egress_inspect_test.go
index dce06a3e..eead8d21 100644
--- a/internal/node/egress_inspect_test.go
+++ b/internal/node/egress_inspect_test.go
@@ -682,3 +682,23 @@ func TestBoundedResponseRecorderOverflow(t *testing.T) {
t.Fatalf("expected bounded buffer to cap at maxBytes+1 (17), got %d", rec.body.Len())
}
}
+
+func TestReplaceInspectableTextMultiMessageNewlineAndFailClosed(t *testing.T) {
+ body := []byte(`{"messages":[{"role":"system","content":"line1\nline2"},{"role":"user","content":"my ssn is 123-45-6789"}]}`)
+ extracted := extractInspectableText(body)
+ if extracted != "line1\nline2\nmy ssn is 123-45-6789" {
+ t.Fatalf("unexpected extracted text: %q", extracted)
+ }
+ replaced, err := replaceInspectableText(body, extracted, "line1\nline2\nmy ssn is [REDACTED]")
+ if err != nil {
+ t.Fatalf("replaceInspectableText multi-message with newline failed: %v", err)
+ }
+ if strings.Contains(string(replaced), "123-45-6789") || !strings.Contains(string(replaced), "[REDACTED]") {
+ t.Fatalf("expected redacted payload, got %s", string(replaced))
+ }
+
+ // If replacement changes line count so it cannot be mapped back to multi-message JSON, must fail-closed.
+ if _, err := replaceInspectableText(body, extracted, "single-line-summary [REDACTED]"); err == nil {
+ t.Fatal("expected error when replacement cannot be mapped back into multi-message JSON")
+ }
+}
diff --git a/internal/node/egress_tunnel.go b/internal/node/egress_tunnel.go
index aba736a2..68fb4c22 100644
--- a/internal/node/egress_tunnel.go
+++ b/internal/node/egress_tunnel.go
@@ -89,15 +89,54 @@ func isForbiddenEgressAddr(addr netip.Addr, allowLocal bool) bool {
return true
}
if !allowLocal {
- if ip.IsLoopback() || ip.IsPrivate() {
+ if ip.IsLoopback() || ip.IsPrivate() || !ip.IsGlobalUnicast() {
return true
}
if ip.Is4() {
b := ip.As4()
+ // RFC 1122 "This" network (0.0.0.0/8)
+ if b[0] == 0 {
+ return true
+ }
// RFC 6598 Carrier-Grade NAT (100.64.0.0/10)
if b[0] == 100 && b[1] >= 64 && b[1] <= 127 {
return true
}
+ // RFC 6890 IETF Protocol Assignments (192.0.0.0/24) & TEST-NET-1 (192.0.2.0/24)
+ if b[0] == 192 && b[1] == 0 && (b[2] == 0 || b[2] == 2) {
+ return true
+ }
+ // RFC 2544 Benchmarking (198.18.0.0/15)
+ if b[0] == 198 && (b[1] == 18 || b[1] == 19) {
+ return true
+ }
+ // RFC 5737 TEST-NET-2 (198.51.100.0/24) & TEST-NET-3 (203.0.113.0/24)
+ if (b[0] == 198 && b[1] == 51 && b[2] == 100) || (b[0] == 203 && b[1] == 0 && b[2] == 113) {
+ return true
+ }
+ // RFC 1112 Reserved for Future Use / Class E (240.0.0.0/4)
+ if b[0] >= 240 {
+ return true
+ }
+ } else if ip.Is6() {
+ b := ip.As16()
+ // RFC 6052 Well-Known Prefix NAT64 (64:ff9b::/96)
+ if b[0] == 0x00 && b[1] == 0x64 && b[2] == 0xff && b[3] == 0x9b &&
+ b[4] == 0 && b[5] == 0 && b[6] == 0 && b[7] == 0 &&
+ b[8] == 0 && b[9] == 0 && b[10] == 0 && b[11] == 0 {
+ return true
+ }
+ // RFC 3849 Documentation (2001:db8::/32)
+ if b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x0d && b[3] == 0xb8 {
+ return true
+ }
+ // RFC 3056 6to4 (2002::/16) — check embedded IPv4
+ if b[0] == 0x20 && b[1] == 0x02 {
+ v4 := netip.AddrFrom4([4]byte{b[2], b[3], b[4], b[5]})
+ if isForbiddenEgressAddr(v4, false) {
+ return true
+ }
+ }
}
}
return false
@@ -306,6 +345,13 @@ func handleConnectTunnel(node *SamNode, w http.ResponseWriter, r *http.Request)
refuse(w, http.StatusBadRequest, err.Error(), proxyStatusDenied)
return
}
+ requiredLabels, err := parseRequiredLabels(r.Header.Get(api.HeaderSamRequiredLabels))
+ if err != nil {
+ refuse(w, http.StatusBadRequest, fmt.Sprintf("Invalid %s header: %v", api.HeaderSamRequiredLabels, err), proxyStatusDenied)
+ return
+ }
+ r.Header.Del(api.HeaderSamRequiredLabels)
+
if node == nil {
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
return
@@ -316,7 +362,9 @@ func handleConnectTunnel(node *SamNode, w http.ResponseWriter, r *http.Request)
return
}
- // 1. If this node serves egress://<host> locally, authorize and serve directly.
+ // 1. If this node serves egress://<host> locally and satisfies any caller
+ // label requirement and the operator's egress floor, authorize and serve directly.
+ var localLabelFailed bool
if node.services != nil {
if svc, ok := node.services.GetTyped(api.ServiceType_SERVICE_TYPE_EGRESS, host); ok {
es, ok := svc.(*EgressService)
@@ -324,45 +372,67 @@ func handleConnectTunnel(node *SamNode, w http.ResponseWriter, r *http.Request)
refuse(w, http.StatusNotFound, "invalid egress service", proxyStatusDestinationNotFound)
return
}
- var localPID peer.ID
- if pid, pErr := node.localPeerID(); pErr == nil {
- localPID = pid
- }
- reqCtx := RequestContext{
- PeerID: localPID,
- Protocol: "local-api",
- Target: api.EgressServicePrefix + host,
- HTTP: &HTTPRequestFacts{Method: http.MethodConnect, Path: ""},
- Egress: &EgressFacts{Host: host, Port: port},
- Local: true,
- }
- if err := node.VerifyBiscuitToken(identity, reqCtx); err != nil {
- recordEgressDecision(host, egressOutcomeDeny)
- refuse(w, http.StatusForbidden, "Authorization failed", proxyStatusDenied)
+ if outsideLabels(requiredLabels, node.labels(), true) || outsideLabels(node.egressFloor(), node.labels(), true) {
+ localLabelFailed = true
+ } else {
+ var localPID peer.ID
+ if pid, pErr := node.localPeerID(); pErr == nil {
+ localPID = pid
+ }
+ reqCtx := RequestContext{
+ PeerID: localPID,
+ Protocol: "local-api",
+ Target: api.EgressServicePrefix + host,
+ HTTP: &HTTPRequestFacts{Method: http.MethodConnect, Path: ""},
+ Egress: &EgressFacts{Host: host, Port: port},
+ Local: true,
+ }
+ if err := node.VerifyBiscuitToken(identity, reqCtx); err != nil {
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Authorization failed", proxyStatusDenied)
+ return
+ }
+ recordEgressDecision(host, egressOutcomeAllow)
+ es.ServeTunnel(WithCallerBiscuit(r.Context(), identity), w, r, port)
return
}
- recordEgressDecision(host, egressOutcomeAllow)
- es.ServeTunnel(WithCallerBiscuit(r.Context(), identity), w, r, port)
- return
}
}
- // 2. Otherwise discover a remote provider in the mesh and forward the tunnel over /libp2p-http.
+ // 2. Otherwise discover a remote provider in the mesh, verify its labels, and forward the tunnel over /libp2p-http.
if node.Host == nil {
+ if localLabelFailed {
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Required labels not attested by provider", proxyStatusDenied)
+ return
+ }
refuse(w, http.StatusNotFound, fmt.Sprintf("no egress destination %q is assigned to this node", host), proxyStatusDestinationNotFound)
return
}
providers, err := node.DiscoverRemoteServices(r.Context(), api.ServiceType_SERVICE_TYPE_EGRESS, host)
if err != nil || len(providers) == 0 {
+ if localLabelFailed {
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Required labels not attested by provider", proxyStatusDenied)
+ return
+ }
refuse(w, http.StatusNotFound, fmt.Sprintf("no provider found for egress://%s", host), proxyStatusDestinationNotFound)
return
}
- targetPeer, err := peer.Decode(providers[0].GetPeerId())
- if err != nil {
- refuse(w, http.StatusBadGateway, "invalid provider peer ID", proxyStatusConfigurationError)
+ for _, p := range providers {
+ targetPeer, decErr := peer.Decode(p.GetPeerId())
+ if decErr != nil {
+ continue
+ }
+ if err := node.VerifyPeerLabels(r.Context(), targetPeer, requiredLabels); err != nil {
+ logger.Warnf("[Egress] CONNECT label gate refused egress to %s: %v", targetPeer, err)
+ continue
+ }
+ forwardConnectTunnelToPeer(node, w, r, targetPeer, host, port, identity)
return
}
- forwardConnectTunnelToPeer(node, w, r, targetPeer, host, port, identity)
+ recordEgressDecision(host, egressOutcomeDeny)
+ refuse(w, http.StatusForbidden, "Required labels not attested by provider", proxyStatusDenied)
}
func forwardConnectTunnelToPeer(node *SamNode, w http.ResponseWriter, r *http.Request, targetPeer peer.ID, host string, port int, biscuitBytes []byte) {
diff --git a/internal/node/egress_tunnel_test.go b/internal/node/egress_tunnel_test.go
index adf004f6..c410135c 100644
--- a/internal/node/egress_tunnel_test.go
+++ b/internal/node/egress_tunnel_test.go
@@ -28,6 +28,7 @@ import (
"net"
"net/http"
"net/http/httptest"
+ "net/netip"
"net/url"
"strings"
"sync/atomic"
@@ -223,3 +224,52 @@ func TestEgressTCPTunnelEndToEnd(t *testing.T) {
}
})
}
+
+func TestIsForbiddenEgressAddrRanges(t *testing.T) {
+ forbidden := []string{
+ "127.0.0.1",
+ "10.0.0.1",
+ "169.254.169.254",
+ "100.64.0.1",
+ "0.1.2.3",
+ "192.0.0.1",
+ "192.0.2.1",
+ "198.18.0.1",
+ "198.51.100.1",
+ "203.0.113.1",
+ "240.0.0.1",
+ "::1",
+ "fe80::1",
+ "64:ff9b::a9fe:a9fe",
+ "2001:db8::1",
+ "2002:7f00:0001::1",
+ }
+ for _, s := range forbidden {
+ ip := netip.MustParseAddr(s)
+ if !isForbiddenEgressAddr(ip, false) {
+ t.Errorf("expected %s to be forbidden when allowLocal=false", s)
+ }
+ }
+ allowed := []string{
+ "8.8.8.8",
+ "1.1.1.1",
+ "2606:4700:4700::1111",
+ }
+ for _, s := range allowed {
+ ip := netip.MustParseAddr(s)
+ if isForbiddenEgressAddr(ip, false) {
+ t.Errorf("expected %s to be allowed when allowLocal=false", s)
+ }
+ }
+}
+
+func TestHandleConnectTunnelRequiredLabels(t *testing.T) {
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodConnect, "http://pg.internal.example:5432", nil)
+ req.Host = "pg.internal.example:5432"
+ req.Header.Set(api.HeaderSamRequiredLabels, ",,")
+ handleConnectTunnel(nil, rec, req)
+ if rec.Code != http.StatusBadRequest {
+ t.Fatalf("malformed X-Sam-Required-Labels on CONNECT: got %d, want 400", rec.Code)
+ }
+}
diff --git a/internal/node/inference_service.go b/internal/node/inference_service.go
index bc862b91..6d20be23 100644
--- a/internal/node/inference_service.go
+++ b/internal/node/inference_service.go
@@ -240,7 +240,7 @@ func parseResponseStream(r io.Reader, peerID string, isSSE bool) {
func parseJSONResponse(r io.Reader, peerID string) {
var resp openai.ChatCompletion
- if err := json.NewDecoder(r).Decode(&resp); err != nil {
+ if err := json.NewDecoder(io.LimitReader(r, maxInferenceBodyBytes)).Decode(&resp); err != nil {
return
}
if resp.JSON.Usage.Valid() && (resp.Usage.PromptTokens > 0 || resp.Usage.CompletionTokens > 0) {
@@ -249,7 +249,7 @@ func parseJSONResponse(r io.Reader, peerID string) {
}
func parseSSEResponse(r io.Reader, peerID string) {
- reader := bufio.NewReader(r)
+ reader := bufio.NewReader(io.LimitReader(r, maxInferenceBodyBytes*4))
var model string
for {
line, err := reader.ReadString('\n')
diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.go
index 7fab2568..a44f99c9 100644
--- a/internal/node/mcp_handlers_test.go
+++ b/internal/node/mcp_handlers_test.go
@@ -381,9 +381,6 @@ func TestHandleFindRemoteTools_MeshWide(t *testing.T) {
}
func TestHandleFindRemoteTools_PartialFailure(t *testing.T) {
- if testing.Short() {
- t.Skip("network/dial: skipped in -short")
- }
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
diff --git a/internal/node/mcp_service_command_test.go b/internal/node/mcp_service_command_test.go
index f306b4eb..60e7d7a9 100644
--- a/internal/node/mcp_service_command_test.go
+++ b/internal/node/mcp_service_command_test.go
@@ -63,6 +63,9 @@ func TestEchoHelperProcess(t *testing.T) {
_ = w.WriteByte('\n')
_ = w.Flush()
}
+ if err := scanner.Err(); err != nil {
+ os.Exit(1)
+ }
os.Exit(0)
}
diff --git a/internal/node/node.go b/internal/node/node.go
index ea4fb9e1..78bb9ed8 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -397,7 +397,7 @@ func NewSamNode(cfg Options) (*SamNode, error) {
}
var err error
- node.rateLimiter, err = ratelimit.NewPeerRateLimiter(RateLimiterSize)
+ node.rateLimiter, err = ratelimit.NewPeerRateLimiterWithRate(RateLimiterSize, 20, 30)
if err != nil {
return nil, fmt.Errorf("failed to create rate limiter: %w", err)
}
@@ -795,8 +795,14 @@ func (n *SamNode) triggerReprovide() {
func (n *SamNode) startReprovideLoop(ctx context.Context, interval time.Duration) {
go func() {
+ initialDelay := 5 * time.Second
+ if n.config.DiscoveryInterval != "" {
+ if d, err := time.ParseDuration(n.config.DiscoveryInterval); err == nil && d > 0 && d < initialDelay {
+ initialDelay = d
+ }
+ }
// Initial delay lets DHT bootstrap stabilize.
- timer := time.NewTimer(5 * time.Second)
+ timer := time.NewTimer(initialDelay)
defer timer.Stop()
retry := reprovideRetryInterval
@@ -2264,6 +2270,11 @@ func (n *SamNode) StartIngressServer(ctx context.Context) error {
http.Error(w, "Invalid remote peer", http.StatusBadRequest)
return
}
+ if n.rateLimiter != nil && !n.rateLimiter.Allow(remotePeer.String()) {
+ logger.Warnf("[Ingress] Rate limit exceeded for %s", remotePeer)
+ http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
+ return
+ }
target = strings.ToLower(serviceTypeStr) + "://" + serviceName
reqCtx := RequestContext{
@@ -2286,6 +2297,13 @@ func (n *SamNode) StartIngressServer(ctx context.Context) error {
}
}
if serviceType == api.ServiceType_SERVICE_TYPE_MCP {
+ preCtx := reqCtx
+ preCtx.AllowMCPStreamInit = true
+ if err := n.VerifyBiscuitToken(biscuitBytes, preCtx); err != nil {
+ logger.Warnf("[Ingress] AuthZ Denied for %s: %v", remotePeer, err)
+ refuse(w, http.StatusForbidden, "Authorization failed", proxyStatusDenied)
+ return
+ }
mcpTool, allowInit, err := inspectMCPHTTPRequestBody(r)
if err != nil {
refuse(w, http.StatusRequestEntityTooLarge, err.Error(), proxyStatusDenied)
diff --git a/internal/node/oidc.go b/internal/node/oidc.go
index 56707b68..d9da6bb7 100644
--- a/internal/node/oidc.go
+++ b/internal/node/oidc.go
@@ -441,7 +441,7 @@ func (n *SamNode) DeviceLogin(ctx context.Context, deviceAuthURL, tokenURL, clie
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
- _ = json.NewDecoder(resp.Body).Decode(&errResp)
+ _ = json.NewDecoder(io.LimitReader(resp.Body, maxControlPlaneBodyBytes)).Decode(&errResp)
return "", fmt.Errorf("device authorization failed: %s - %s", errResp.Error, errResp.ErrorDescription)
}
@@ -454,7 +454,7 @@ func (n *SamNode) DeviceLogin(ctx context.Context, deviceAuthURL, tokenURL, clie
Interval int `json:"interval"`
Message string `json:"message"`
}
- if err := json.NewDecoder(resp.Body).Decode(&deviceResp); err != nil {
+ if err := json.NewDecoder(io.LimitReader(resp.Body, maxControlPlaneBodyBytes)).Decode(&deviceResp); err != nil {
return "", fmt.Errorf("failed to decode device authorization response: %w", err)
}
if deviceResp.DeviceCode == "" {
@@ -768,7 +768,7 @@ func (n *SamNode) RefreshJWT(ctx context.Context, tokenURL, clientID, clientSecr
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
- if err := json.NewDecoder(resp.Body).Decode(&errResp); err == nil && errResp.Error != "" {
+ if err := json.NewDecoder(io.LimitReader(resp.Body, maxControlPlaneBodyBytes)).Decode(&errResp); err == nil && errResp.Error != "" {
return "", "", fmt.Errorf("refresh token request failed (status %s): %s - %s", resp.Status, errResp.Error, errResp.ErrorDescription)
}
return "", "", fmt.Errorf("refresh token request failed with status: %s", resp.Status)
@@ -779,7 +779,7 @@ func (n *SamNode) RefreshJWT(ctx context.Context, tokenURL, clientID, clientSecr
IdToken string `json:"id_token"`
RefreshToken string `json:"refresh_token"`
}
- if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
+ if err := json.NewDecoder(io.LimitReader(resp.Body, maxControlPlaneBodyBytes)).Decode(&tokenResp); err != nil {
return "", "", err
}
diff --git a/internal/node/sidecar.go b/internal/node/sidecar.go
index cf6fc674..c1e1a4e9 100644
--- a/internal/node/sidecar.go
+++ b/internal/node/sidecar.go
@@ -772,12 +772,21 @@ func createEgressProxy(node *SamNode) http.Handler {
return
}
+ var requiredLabels map[string]string
+ if labelsHeader := r.Header.Get(api.HeaderSamRequiredLabels); labelsHeader != "" {
+ r.Header.Del(api.HeaderSamRequiredLabels)
+ var err error
+ requiredLabels, err = parseRequiredLabels(labelsHeader)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("Invalid %s header: %v", api.HeaderSamRequiredLabels, err), http.StatusBadRequest)
+ return
+ }
+ }
+
// Every egress destination must prove it is an enrolled peer: the
// label gate verifies the peer's control-plane-signed biscuit (cached
- // per peer) and, when the operator set an egress floor
- // (egress.require_labels), holds it to that too. required is nil: any
- // caller requirement was already enforced by the surface that parsed
- // it; the floor is what a silent caller cannot waive.
+ // per peer), any caller label requirement, and, when the operator set
+ // an egress floor (egress.require_labels), holds it to that too.
route, ok := parseEgressRoute(r.URL.Path)
if !ok {
http.Error(w, "Bad Request: request names no peer", http.StatusBadRequest)
@@ -798,9 +807,11 @@ func createEgressProxy(node *SamNode) http.Handler {
r.URL.Path = strings.Join(parts, "/")
}
}
- if err := node.VerifyPeerLabels(r.Context(), pid, nil); err != nil {
+ if err := node.VerifyPeerLabels(r.Context(), pid, requiredLabels); err != nil {
logger.Warnf("[Egress] refused egress to %s: %v", pid, err)
- if len(node.egressFloor()) > 0 {
+ if len(requiredLabels) > 0 {
+ http.Error(w, "Required labels not attested by provider", http.StatusForbidden)
+ } else if len(node.egressFloor()) > 0 {
http.Error(w, "Forbidden: provider does not attest the egress floor", http.StatusForbidden)
} else {
http.Error(w, "Forbidden: destination is not an enrolled peer", http.StatusForbidden)
diff --git a/internal/node/store.go b/internal/node/store.go
index dca4ebf9..34354a12 100644
--- a/internal/node/store.go
+++ b/internal/node/store.go
@@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"strconv"
+ "strings"
"time"
"github.com/google/sam/api"
@@ -73,6 +74,12 @@ type Store struct {
var ErrStoreLocked = errors.New("another sam-node instance is using this data directory")
func GetDefaultDataDir() (string, error) {
+ if envDir := strings.TrimSpace(os.Getenv("SAM_DATA_DIR")); envDir != "" {
+ if err := os.MkdirAll(envDir, 0700); err != nil {
+ return "", err
+ }
+ return envDir, nil
+ }
base, err := os.UserConfigDir()
if err != nil {
return "", err
diff --git a/internal/node/store_test.go b/internal/node/store_test.go
index e83ff4d8..8cc6e54d 100644
--- a/internal/node/store_test.go
+++ b/internal/node/store_test.go
@@ -567,4 +567,14 @@ func TestGetDefaultDataDir(t *testing.T) {
if dir == "" {
t.Error("Expected non-empty directory path")
}
+
+ custom := filepath.Join(t.TempDir(), "custom-sam-dir")
+ t.Setenv("SAM_DATA_DIR", custom)
+ got, err := GetDefaultDataDir()
+ if err != nil {
+ t.Fatalf("GetDefaultDataDir with SAM_DATA_DIR failed: %v", err)
+ }
+ if got != custom {
+ t.Fatalf("GetDefaultDataDir = %q, want %q", got, custom)
+ }
}
diff --git a/internal/router/relay_acl_test.go b/internal/router/relay_acl_test.go
index e6b8b4a3..403ae5e8 100644
--- a/internal/router/relay_acl_test.go
+++ b/internal/router/relay_acl_test.go
@@ -75,6 +75,55 @@ func TestRelayACLAllowConnectRequiresAuthenticatedSource(t *testing.T) {
}
}
+func TestRelayACLExpiresAuthenticatedPeers(t *testing.T) {
+ r := &Router{}
+ acl := &relayACL{r: r}
+ src, dest := newTestPeerID(t), newTestPeerID(t)
+ addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
+
+ r.authenticatedPeers.Store(src, time.Now().Add(time.Hour))
+ r.authenticatedPeers.Store(dest, time.Now().Add(time.Hour))
+ if !acl.AllowReserve(src, addr) {
+ t.Fatal("expected peer with future expiration to be allowed to reserve")
+ }
+ if !acl.AllowConnect(src, addr, dest) {
+ t.Fatal("expected peers with future expiration to be allowed to connect")
+ }
+
+ // Expire src's biscuit.
+ r.authenticatedPeers.Store(src, time.Now().Add(-time.Second))
+ if acl.AllowReserve(src, addr) {
+ t.Error("expected peer with expired biscuit to be denied reservation")
+ }
+ if _, stillPresent := r.authenticatedPeers.Load(src); stillPresent {
+ t.Error("expected expired peer to be evicted from authenticatedPeers")
+ }
+ r.authenticatedPeers.Store(src, time.Now().Add(-time.Second))
+ if acl.AllowConnect(src, addr, dest) {
+ t.Error("expected expired source peer to be denied relay connect")
+ }
+}
+
+func TestRouterConnGateRejectsBannedPeers(t *testing.T) {
+ r := &Router{}
+ gate := &routerConnGate{r: r}
+ allowedPeer, bannedPeer := newTestPeerID(t), newTestPeerID(t)
+ r.bannedPeers.Store(bannedPeer, time.Now())
+
+ if !gate.InterceptPeerDial(allowedPeer) {
+ t.Error("expected allowed peer dial to succeed")
+ }
+ if gate.InterceptPeerDial(bannedPeer) {
+ t.Error("expected banned peer dial to be rejected")
+ }
+ if !gate.InterceptSecured(0, allowedPeer, nil) {
+ t.Error("expected allowed peer secured connection to succeed")
+ }
+ if gate.InterceptSecured(0, bannedPeer, nil) {
+ t.Error("expected banned peer secured connection to be rejected")
+ }
+}
+
// The router's GossipSub validator drops unsigned or forged events at the
// first hop instead of fanning them out to every attached node.
func TestRouterValidateMeshEvent(t *testing.T) {
diff --git a/internal/router/router.go b/internal/router/router.go
index c87840e8..7b2255dc 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -21,7 +21,6 @@ import (
"encoding/base64"
"errors"
"fmt"
- "io"
"net"
"net/http"
"net/netip"
@@ -43,6 +42,8 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht"
records "github.com/libp2p/go-libp2p-kad-dht/records"
pubsub "github.com/libp2p/go-libp2p-pubsub"
+ coreconnmgr "github.com/libp2p/go-libp2p/core/connmgr"
+ "github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
@@ -73,6 +74,38 @@ const (
ConnGracePeriod = 1 * time.Minute
)
+var _ coreconnmgr.ConnectionGater = (*routerConnGate)(nil)
+
+type routerConnGate struct {
+ r *Router
+}
+
+func (g *routerConnGate) InterceptPeerDial(p peer.ID) bool {
+ if _, banned := g.r.bannedPeers.Load(p); banned {
+ return false
+ }
+ return true
+}
+
+func (g *routerConnGate) InterceptAddrDial(peer.ID, multiaddr.Multiaddr) bool {
+ return true
+}
+
+func (g *routerConnGate) InterceptAccept(network.ConnMultiaddrs) bool {
+ return true
+}
+
+func (g *routerConnGate) InterceptSecured(_ network.Direction, p peer.ID, _ network.ConnMultiaddrs) bool {
+ if _, banned := g.r.bannedPeers.Load(p); banned {
+ return false
+ }
+ return true
+}
+
+func (g *routerConnGate) InterceptUpgraded(network.Conn) (bool, control.DisconnectReason) {
+ return true, 0
+}
+
type relayACL struct {
r *Router
}
@@ -82,7 +115,7 @@ func (a *relayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
logger.Debugf("[Relay] Rejecting reservation for %s: peer is banned", p)
return false
}
- _, ok := a.r.authenticatedPeers.Load(p)
+ ok := a.r.isPeerAuthenticated(p)
if !ok {
logger.Debugf("[Relay] Rejecting reservation for %s: not authenticated", p)
}
@@ -100,17 +133,38 @@ func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest p
}
// Both ends must have authenticated: every node authenticates to the
// router on connect, so a source that has not is not a mesh member.
- if _, ok := a.r.authenticatedPeers.Load(src); !ok {
+ if !a.r.isPeerAuthenticated(src) {
logger.Debugf("[Relay] Rejecting connect from %s to %s: src not authenticated", src, dest)
return false
}
- _, ok := a.r.authenticatedPeers.Load(dest)
+ ok := a.r.isPeerAuthenticated(dest)
if !ok {
logger.Debugf("[Relay] Rejecting connect from %s to %s: dest not authenticated", src, dest)
}
return ok
}
+// isPeerAuthenticated reports whether p completed the auth handshake and its
+// verified biscuit has not lapsed since.
+func (r *Router) isPeerAuthenticated(p peer.ID) bool {
+ v, ok := r.authenticatedPeers.Load(p)
+ if !ok {
+ return false
+ }
+ switch exp := v.(type) {
+ case time.Time:
+ if !time.Now().Before(exp) {
+ r.authenticatedPeers.Delete(p)
+ return false
+ }
+ return true
+ case bool:
+ return exp
+ default:
+ return false
+ }
+}
+
// credential is the router's own mesh identity as the control plane issued
// it. issuedUnder is the trusted key set at issuance: a trusted key outside
// it is a rotation the biscuit predates, so the biscuit must be refreshed
@@ -197,7 +251,7 @@ type Router struct {
cancel context.CancelFunc
wg sync.WaitGroup
isReady atomic.Bool
- shutdown bool
+ shutdown atomic.Bool
metricsServer *http.Server
metricsAddr net.Addr
@@ -261,16 +315,17 @@ func perIPConnResourceManager(limit int) (network.ResourceManager, error) {
libp2p.SetDefaultServiceLimits(&limits)
scaled := limits.AutoScale()
+ sysBase := max(limit, DefaultHighWaterMark)
overrides := rcmgr.PartialLimitConfig{
System: rcmgr.ResourceLimits{
- Conns: rcmgr.LimitVal(2 * limit),
- ConnsInbound: rcmgr.LimitVal(limit),
- FD: rcmgr.LimitVal(2 * limit),
+ Conns: rcmgr.LimitVal(2 * sysBase),
+ ConnsInbound: rcmgr.LimitVal(sysBase),
+ FD: rcmgr.LimitVal(2 * sysBase),
},
Transient: rcmgr.ResourceLimits{
- Conns: rcmgr.LimitVal(limit),
- ConnsInbound: rcmgr.LimitVal(limit),
- FD: rcmgr.LimitVal(limit),
+ Conns: rcmgr.LimitVal(sysBase),
+ ConnsInbound: rcmgr.LimitVal(sysBase),
+ FD: rcmgr.LimitVal(sysBase),
},
}
@@ -313,7 +368,13 @@ func defaultResourceManager() (network.ResourceManager, error) {
}
// Start performs enrollment, syncs keys, launches libp2p host, and starts tasks.
-func (r *Router) Start() error {
+func (r *Router) Start() (retErr error) {
+ defer func() {
+ if retErr != nil {
+ _ = r.Close()
+ }
+ }()
+
// The operator listener comes up first so /healthz answers while
// enrollment is still in flight; /readyz turns 200 at the end.
if r.config.MetricsAddr != "" {
@@ -370,6 +431,7 @@ func (r *Router) Start() error {
libp2p.Security(libp2ptls.ID, libp2ptls.New),
libp2p.Security(noise.ID, noise.New),
libp2p.ConnectionManager(cm),
+ libp2p.ConnectionGater(&routerConnGate{r: r}),
libp2p.EnableAutoNATv2(),
libp2p.EnableNATService(),
libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
@@ -425,13 +487,11 @@ func (r *Router) Start() error {
}
kadDHT, err := dht.New(hostNode, dhtOpts...)
if err != nil {
- _ = hostNode.Close()
return err
}
r.DHT = kadDHT
if err = kadDHT.Bootstrap(r.ctx); err != nil {
- _ = hostNode.Close()
return err
}
@@ -439,7 +499,6 @@ func (r *Router) Start() error {
_, err = relay.New(hostNode, relay.WithACL(&relayACL{r: r}),
relay.WithLimit(relayLimit(r.config.RelayLimitDuration, r.config.RelayLimitData)))
if err != nil {
- _ = hostNode.Close()
return err
}
@@ -447,7 +506,6 @@ func (r *Router) Start() error {
// validator trusts msg.GetFrom().
ps, err := pubsub.NewGossipSub(r.ctx, hostNode, pubsub.WithMessageSignaturePolicy(pubsub.StrictSign))
if err != nil {
- _ = hostNode.Close()
return err
}
r.PubSub = ps
@@ -455,13 +513,11 @@ func (r *Router) Start() error {
// stops a junk event at the first hop instead of fanning it out to every
// attached node, each of which would spend a verify on it.
if err := ps.RegisterTopicValidator(api.GossipEvents, r.validateMeshEvent); err != nil {
- _ = hostNode.Close()
return fmt.Errorf("register mesh event validator: %w", err)
}
topic, err := ps.Join(api.GossipEvents)
if err != nil {
- _ = hostNode.Close()
return err
}
r.EventTopic = topic
@@ -530,7 +586,7 @@ func (r *Router) enroll(peerID peer.ID) error {
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
+ body, _ := cpclient.ReadBody(resp.Body)
return fmt.Errorf("enrollment response status %s: %s", resp.Status, string(body))
}
@@ -594,7 +650,7 @@ func (r *Router) enrollBootstrap(peerID peer.ID) error {
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
+ body, _ := cpclient.ReadBody(resp.Body)
return fmt.Errorf("bootstrap enrollment request response status %s: %s", resp.Status, string(body))
}
@@ -651,7 +707,7 @@ func (r *Router) enrollBootstrap(peerID peer.ID) error {
}
if statusResp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(statusResp.Body)
+ body, _ := cpclient.ReadBody(statusResp.Body)
_ = statusResp.Body.Close()
logger.Warnf("status poll returned code %s: %s", statusResp.Status, string(body))
continue
@@ -1294,8 +1350,8 @@ func (r *Router) HandleAuthHandshake(s network.Stream) {
return
}
- // Verify Biscuit
- _, err = identity.VerifyBiscuit(exchange.Biscuit, remotePeer, r.getTrustedPublicKeys(), r.config.BiscuitTimeout)
+ // Verify Biscuit and record its expiration so relayACL drops the session on time.
+ expiry, err := identity.VerifyBiscuitAndGetExpiry(exchange.Biscuit, remotePeer, r.getTrustedPublicKeys(), r.config.BiscuitTimeout)
if err != nil {
// A peer stuck on a bad credential retries every few seconds for
// days; one line a minute per peer keeps the log readable and the
@@ -1308,7 +1364,7 @@ func (r *Router) HandleAuthHandshake(s network.Stream) {
return
}
- r.authenticatedPeers.Store(remotePeer, true)
+ r.authenticatedPeers.Store(remotePeer, expiry)
authHandshakesTotal.WithLabelValues(handshakeOK).Inc()
logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)
@@ -1380,20 +1436,29 @@ func (r *Router) performMutualAuth(s network.Stream) error {
return fmt.Errorf("remote peer lacks router authorization role: %w", err)
}
- r.authenticatedPeers.Store(remotePeer, true)
+ expiry, err := identity.VerifyBiscuitAndGetExpiry(resp.Biscuit, remotePeer, []ed25519.PublicKey{verifyingKey}, r.config.BiscuitTimeout)
+ if err != nil {
+ return fmt.Errorf("failed to extract peer router biscuit expiry: %w", err)
+ }
+
+ r.authenticatedPeers.Store(remotePeer, expiry)
return nil
}
// Close closes the underlying p2p host and keyring db.
func (r *Router) Close() error {
- r.shutdown = true
+ r.isReady.Store(false)
+ r.shutdown.Store(true)
r.cancel()
var errs []error
if r.metricsServer != nil {
- if err := r.metricsServer.Close(); err != nil {
+ shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
+ if err := r.metricsServer.Shutdown(shutdownCtx); err != nil {
+ _ = r.metricsServer.Close()
errs = append(errs, err)
}
+ shutdownCancel()
}
if r.DHT != nil {
if err := r.DHT.Close(); err != nil {
@@ -1533,7 +1598,7 @@ func (r *Router) RefreshEnrollment(ctx context.Context) error {
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusUnauthorized {
- body, _ := io.ReadAll(resp.Body)
+ body, _ := cpclient.ReadBody(resp.Body)
logger.Warnf("Proactive biscuit refresh rejected (401 Unauthorized: %s), attempting re-enrollment...", string(body))
return r.reEnroll()
}
@@ -1542,12 +1607,12 @@ func (r *Router) RefreshEnrollment(ctx context.Context) error {
// A 403 is a claim by whoever answered; only a verified
// MeshEvent_BANNED is the control plane's word. Keep serving on the
// current biscuit until it expires.
- body, _ := io.ReadAll(resp.Body)
+ body, _ := cpclient.ReadBody(resp.Body)
return fmt.Errorf("refresh refused (403 Forbidden): %s", string(body))
}
if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
+ body, _ := cpclient.ReadBody(resp.Body)
return fmt.Errorf("refresh failed with status %s: %s", resp.Status, string(body))
}
diff --git a/internal/standalone/standalone.go b/internal/standalone/standalone.go
index 3f0d7d1f..1c11cb82 100644
--- a/internal/standalone/standalone.go
+++ b/internal/standalone/standalone.go
@@ -223,6 +223,7 @@ type Server struct {
store storage.Store
cp *controlplane.Server
router *router.Router
+ mesh *controlplane.P2PMeshAdapter
adminToken string
joinToken string
publicAddr string
@@ -240,7 +241,13 @@ func New(opts Options) (*Server, error) {
// Start boots the store, the control plane on a loopback-only listener, and
// the router owning the single public port. It returns once the mesh accepts
// enrollments.
-func (s *Server) Start(ctx context.Context) error {
+func (s *Server) Start(ctx context.Context) (retErr error) {
+ defer func() {
+ if retErr != nil {
+ _ = s.Close()
+ }
+ }()
+
if err := os.MkdirAll(s.opts.DataDir, 0o755); err != nil {
return fmt.Errorf("failed to create data dir: %w", err)
}
@@ -376,6 +383,7 @@ func (s *Server) Start(ctx context.Context) error {
return fmt.Errorf("failed to create router: %w", err)
}
if err := rtr.Start(); err != nil {
+ _ = rtr.Close()
return fmt.Errorf("failed to start router: %w", err)
}
s.router = rtr
@@ -383,12 +391,11 @@ func (s *Server) Start(ctx context.Context) error {
// the embedded router's own topic instead of dialing itself.
mesh, err := controlplane.NewP2PMeshAdapter(rtr.Host, rtr.EventTopic, store)
if err != nil {
- _ = rtr.Close()
return fmt.Errorf("failed to attach control plane to the mesh: %w", err)
}
+ s.mesh = mesh
cp.SetMeshAdapter(mesh)
if s.publicAddr, err = s.resolvePublicAddr(); err != nil {
- _ = rtr.Close()
return err
}
return nil
@@ -397,20 +404,29 @@ func (s *Server) Start(ctx context.Context) error {
// Close shuts down the router, control plane and store.
func (s *Server) Close() error {
var errs []string
+ if s.mesh != nil {
+ if err := s.mesh.Close(); err != nil {
+ errs = append(errs, err.Error())
+ }
+ s.mesh = nil
+ }
if s.router != nil {
if err := s.router.Close(); err != nil {
errs = append(errs, err.Error())
}
+ s.router = nil
}
if s.cp != nil {
if err := s.cp.Close(); err != nil {
errs = append(errs, err.Error())
}
+ s.cp = nil
}
if s.store != nil {
if err := s.store.Close(); err != nil {
errs = append(errs, err.Error())
}
+ s.store = nil
}
if len(errs) > 0 {
return fmt.Errorf("standalone shutdown: %s", strings.Join(errs, "; "))
diff --git a/internal/tunnel/cloudflare.go b/internal/tunnel/cloudflare.go
index 37702340..f266e575 100644
--- a/internal/tunnel/cloudflare.go
+++ b/internal/tunnel/cloudflare.go
@@ -341,5 +341,6 @@ func (p *process) scan(r io.Reader, urlCh chan<- string) {
}
if err := sc.Err(); err != nil && !errors.Is(err, io.ErrClosedPipe) {
logger.Debugf("cloudflared output closed: %v", err)
+ _, _ = io.Copy(io.Discard, r)
}
}
diff --git a/internal/tunnel/cloudflare_release.go b/internal/tunnel/cloudflare_release.go
index 4dd9869e..683d907c 100644
--- a/internal/tunnel/cloudflare_release.go
+++ b/internal/tunnel/cloudflare_release.go
@@ -161,15 +161,19 @@ func downloadCloudflared(ctx context.Context, client *http.Client, baseURL, dir
binPath := bin.Name()
defer func() { _ = os.Remove(binPath) }()
var src io.Reader = tmp
+ var gzCloser io.Closer
if asset.Tarball {
- if src, err = tarballEntry(tmp, "cloudflared"); err != nil {
+ src, gzCloser, err = tarballEntry(tmp, "cloudflared")
+ if err != nil {
_ = tmp.Close()
_ = bin.Close()
return "", err
}
+ defer func() { _ = gzCloser.Close() }()
}
h := sha256.New()
- if _, err := io.Copy(io.MultiWriter(bin, h), src); err != nil {
+ n, err := io.Copy(io.MultiWriter(bin, h), io.LimitReader(src, maxCloudflaredDownload+1))
+ if err != nil {
_ = tmp.Close()
_ = bin.Close()
return "", fmt.Errorf("failed to write cloudflared: %w", err)
@@ -178,6 +182,9 @@ func downloadCloudflared(ctx context.Context, client *http.Client, baseURL, dir
if err := bin.Close(); err != nil {
return "", err
}
+ if n > maxCloudflaredDownload {
+ return "", fmt.Errorf("extracted cloudflared exceeds %d bytes", maxCloudflaredDownload)
+ }
if got := hex.EncodeToString(h.Sum(nil)); got != asset.BinarySHA256 {
return "", fmt.Errorf("cloudflared binary digest mismatch: got %s, want %s", got, asset.BinarySHA256)
}
@@ -222,22 +229,24 @@ func fetchVerified(ctx context.Context, client *http.Client, url string, w io.Wr
// tarballEntry positions a reader on the named regular file inside a
// gzip-compressed tarball.
-func tarballEntry(r io.Reader, name string) (io.Reader, error) {
+func tarballEntry(r io.Reader, name string) (io.Reader, io.Closer, error) {
gz, err := gzip.NewReader(r)
if err != nil {
- return nil, fmt.Errorf("invalid cloudflared tarball: %w", err)
+ return nil, nil, fmt.Errorf("invalid cloudflared tarball: %w", err)
}
tr := tar.NewReader(gz)
for {
hdr, err := tr.Next()
if err == io.EOF {
- return nil, fmt.Errorf("cloudflared tarball has no %q entry", name)
+ _ = gz.Close()
+ return nil, nil, fmt.Errorf("cloudflared tarball has no %q entry", name)
}
if err != nil {
- return nil, fmt.Errorf("invalid cloudflared tarball: %w", err)
+ _ = gz.Close()
+ return nil, nil, fmt.Errorf("invalid cloudflared tarball: %w", err)
}
if hdr.Typeflag == tar.TypeReg && filepath.Base(hdr.Name) == name {
- return tr, nil
+ return tr, gz, nil
}
}
}
diff --git a/internal/tunnel/codespaces.go b/internal/tunnel/codespaces.go
index feb97042..1bce51ee 100644
--- a/internal/tunnel/codespaces.go
+++ b/internal/tunnel/codespaces.go
@@ -20,6 +20,7 @@ import (
"net/http"
"net/url"
"os"
+ "strings"
"sync"
"time"
)
@@ -61,7 +62,11 @@ func (c *Codespaces) Open(ctx context.Context, target string) (Tunnel, error) {
if name == "" || domain == "" {
return nil, fmt.Errorf("not running in GitHub Codespaces (%s and %s are unset); behind another proxy pass --external-url", codespaceNameEnv, codespaceDomainEnv)
}
- t, err := url.Parse(target)
+ rawTarget := target
+ if !strings.Contains(rawTarget, "://") {
+ rawTarget = "http://" + rawTarget
+ }
+ t, err := url.Parse(rawTarget)
if err != nil || t.Port() == "" {
return nil, fmt.Errorf("tunnel target %q has no port", target)
}
diff --git a/mobile/sam-node-app/lib/mcp_server.dart b/mobile/sam-node-app/lib/mcp_server.dart
index 72a1df77..dacaecf1 100644
--- a/mobile/sam-node-app/lib/mcp_server.dart
+++ b/mobile/sam-node-app/lib/mcp_server.dart
@@ -89,6 +89,8 @@ class SamDartMcpServer {
debugPrint('SAM Dart MCP Server stopped');
}
+ static const int _maxRequestBodyBytes = 1024 * 1024;
+
/// Handles SSE (Server-Sent Events) for MCP stream
void _handleSse(HttpRequest request) async {
request.response.headers.contentType = ContentType('text', 'event-stream');
@@ -100,14 +102,33 @@ class SamDartMcpServer {
_sseClients.remove(request.response);
});
- // Initial connection event
- request.response.write('event: connected\ndata: {}\n\n');
- await request.response.flush();
+ try {
+ // Initial connection event
+ request.response.write('event: connected\ndata: {}\n\n');
+ await request.response.flush();
+ } catch (_) {
+ _sseClients.remove(request.response);
+ }
}
/// Handles MCP JSON-RPC Requests
void _handlePost(HttpRequest request) async {
- final body = await utf8.decoder.bind(request).join();
+ if (request.contentLength > _maxRequestBodyBytes) {
+ request.response.statusCode = HttpStatus.requestEntityTooLarge;
+ await request.response.close();
+ return;
+ }
+
+ final chunks = <int>[];
+ await for (final chunk in request) {
+ if (chunks.length + chunk.length > _maxRequestBodyBytes) {
+ request.response.statusCode = HttpStatus.requestEntityTooLarge;
+ await request.response.close();
+ return;
+ }
+ chunks.addAll(chunk);
+ }
+ final body = utf8.decode(chunks, allowMalformed: true);
try {
final jsonRpc = jsonDecode(body);
diff --git a/mobile/sam-node-ffi/ffi/ffi.go b/mobile/sam-node-ffi/ffi/ffi.go
index 47b3d050..ff8829a8 100644
--- a/mobile/sam-node-ffi/ffi/ffi.go
+++ b/mobile/sam-node-ffi/ffi/ffi.go
@@ -384,6 +384,13 @@ func EnrollNodeBootstrap(dataDir string, controlPlaneURL string, bootstrapToken
// stored (or freshly generated) key, then persists the control plane URL and
// syncs the mesh config so the next StartNode finds everything in place.
func enrollWith(dataDir string, controlPlaneURL string, allowLoopback bool, labels string, enroll func(context.Context, *node.SamNode, *node.Store) error) error {
+ mu.Lock()
+ isRunning := activeNode != nil || unauthSrv != nil
+ mu.Unlock()
+ if isRunning {
+ return errors.New("stop the node before enrolling")
+ }
+
parsedLabels, err := decodeLabels(labels)
if err != nil {
return err
@@ -414,7 +421,7 @@ func enrollWith(dataDir string, controlPlaneURL string, allowLoopback bool, labe
}
}
- enrollCtx, enrollCancel := context.WithCancel(context.Background())
+ enrollCtx, enrollCancel := context.WithTimeout(context.Background(), 60*time.Second)
defer enrollCancel()
var listenAddrs []string
@@ -437,6 +444,7 @@ func enrollWith(dataDir string, controlPlaneURL string, allowLoopback bool, labe
}
if err := meshNode.Start(enrollCtx); err != nil {
+ _ = meshNode.Teardown()
return fmt.Errorf("failed to start node for enrollment: %w", err)
}
defer func() {
@@ -454,16 +462,26 @@ func enrollWith(dataDir string, controlPlaneURL string, allowLoopback bool, labe
return nil
}
+func jsonError(msg string) string {
+ b, err := json.Marshal(map[string]string{"error": msg})
+ if err != nil {
+ return fmt.Sprintf(`{"error": %q}`, msg)
+ }
+ return string(b)
+}
+
// FetchControlPlaneInfoJSON fetches control plane info and returns it as a JSON string.
// If an error occurs, it returns a JSON object with an "error" field.
func FetchControlPlaneInfoJSON(controlPlaneURL string) string {
- info, err := node.FetchControlPlaneInfo(context.Background(), controlPlaneURL)
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+ info, err := node.FetchControlPlaneInfo(ctx, controlPlaneURL)
if err != nil {
- return fmt.Sprintf(`{"error": %q}`, err.Error())
+ return jsonError(err.Error())
}
jsonBytes, err := protojson.Marshal(info)
if err != nil {
- return fmt.Sprintf(`{"error": %q}`, err.Error())
+ return jsonError(err.Error())
}
return string(jsonBytes)
}
@@ -494,10 +512,10 @@ func GetMeshInfo() string {
defer mu.Unlock()
if activeNode == nil {
- return `{"error": "node not running"}`
+ return jsonError("node not running")
}
if activeNode.Host == nil {
- return `{"error": "host not initialized"}`
+ return jsonError("host not initialized")
}
peers := activeNode.Host.Network().Peers()
@@ -514,7 +532,7 @@ func GetMeshInfo() string {
jsonBytes, err := json.Marshal(resData)
if err != nil {
- return fmt.Sprintf(`{"error": %q}`, err.Error())
+ return jsonError(err.Error())
}
return string(jsonBytes)
}
@@ -526,32 +544,34 @@ func CallRemoteTool(peerIDStr string, toolName string, argsJSON string) string {
mu.Unlock()
if n == nil {
- return `{"error": "node not running"}`
+ return jsonError("node not running")
}
if n.Host == nil {
- return `{"error": "host not initialized"}`
+ return jsonError("host not initialized")
}
targetPeer, err := peer.Decode(peerIDStr)
if err != nil {
- return fmt.Sprintf(`{"error": "invalid peer ID: %s"}`, err.Error())
+ return jsonError("invalid peer ID: " + err.Error())
}
var params any
if argsJSON != "" && argsJSON != "{}" {
if err := json.Unmarshal([]byte(argsJSON), &params); err != nil {
- return fmt.Sprintf(`{"error": "invalid arguments JSON: %s"}`, err.Error())
+ return jsonError("invalid arguments JSON: " + err.Error())
}
}
- res, err := n.CallMCPTool(context.Background(), targetPeer, toolName, params, nil)
+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
+ defer cancel()
+ res, err := n.CallMCPTool(ctx, targetPeer, toolName, params, nil)
if err != nil {
- return fmt.Sprintf(`{"error": "failed to call tool: %s"}`, err.Error())
+ return jsonError("failed to call tool: " + err.Error())
}
jsonBytes, err := json.Marshal(res)
if err != nil {
- return fmt.Sprintf(`{"error": "failed to marshal result: %s"}`, err.Error())
+ return jsonError("failed to marshal result: " + err.Error())
}
return string(jsonBytes)
@@ -604,7 +624,11 @@ func ReEnrollNode(dataDir string, labels string) error {
if err != nil {
return fmt.Errorf("failed to create node for re-enrollment: %w", err)
}
- if err := meshNode.ReEnrollWithRefreshToken(context.Background()); err != nil {
+ defer func() { _ = meshNode.Teardown() }()
+
+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
+ defer cancel()
+ if err := meshNode.ReEnrollWithRefreshToken(ctx); err != nil {
return fmt.Errorf("re-enrollment failed: %w", err)
}
return nil
diff --git a/sdk/js/src/authorizer.ts b/sdk/js/src/authorizer.ts
index 5c3b97a7..c18cbdc9 100644
--- a/sdk/js/src/authorizer.ts
+++ b/sdk/js/src/authorizer.ts
@@ -20,7 +20,7 @@
import { AUTHORIZER_LIMITS, BiscuitVerificationError, loadBiscuit, verifyPeerBiscuit, withinLimits, type VerifiedBiscuit } from "./biscuit.ts";
import { parseServiceTarget } from "./discovery.ts";
import { BASELINE_DATALOG } from "./gen/datalog.ts";
-import { evaluateTaskRules } from "./tar.ts";
+import { evaluateTaskRules, isSafeRequestHTTPPath } from "./tar.ts";
/** What a caller asks for, as sam-node's RequestContext. */
export interface AuthorizeRequest {
@@ -133,6 +133,9 @@ export async function authorizeCaller(req: AuthorizeRequest, options: ProviderAu
// The request as the wire carried it, never as the caller describes it.
if (req.method !== undefined) {
+ if (req.method !== "CONNECT" && req.path !== undefined && req.path !== "" && !isSafeRequestHTTPPath(req.path)) {
+ throw new AuthorizationError(req.peerId, `invalid HTTP path ${JSON.stringify(req.path)}`);
+ }
fact(`${BASELINE_DATALOG.fact_method}({m})`, { m: req.method });
fact(`${BASELINE_DATALOG.fact_path}({p})`, { p: req.path ?? "" });
}
diff --git a/sdk/js/src/conformance-join.ts b/sdk/js/src/conformance-join.ts
index aa3fb217..3b3d8e3a 100644
--- a/sdk/js/src/conformance-join.ts
+++ b/sdk/js/src/conformance-join.ts
@@ -64,6 +64,7 @@ interface Command {
body?: string;
headers?: Record<string, string>;
required_labels?: Record<string, string>;
+ timeout_ms?: string | number;
}
function requireEnv(name: string): string {
@@ -109,7 +110,8 @@ async function handle(session: MeshSession, command: Command): Promise<unknown>
try {
switch (command.cmd) {
case "auth": {
- const verified = await session.authenticate(command.addr ?? "", AbortSignal.timeout(15_000));
+ const timeoutMs = command.timeout_ms !== undefined ? Number(command.timeout_ms) : 15_000;
+ const verified = await session.authenticate(command.addr ?? "", AbortSignal.timeout(timeoutMs));
return {
cmd: "auth",
ok: true,
diff --git a/sdk/js/src/host.ts b/sdk/js/src/host.ts
index 32693947..28f241d2 100644
--- a/sdk/js/src/host.ts
+++ b/sdk/js/src/host.ts
@@ -62,6 +62,9 @@ export async function createMeshHost(identity: Identity, options: MeshHostOption
transports: [...transports(), circuitRelayTransport()],
connectionEncrypters: connectionEncrypters(),
streamMuxers: [yamux()],
+ connectionManager: {
+ inboundConnectionThreshold: Infinity,
+ },
connectionGater: {
denyDialPeer: denyBanned,
denyInboundEncryptedConnection: denyBanned,
@@ -80,7 +83,7 @@ export async function createMeshHost(identity: Identity, options: MeshHostOption
// A client of the mesh DHT: it looks providers up and does not hold
// records. Peers keep their private addresses; a mesh member often
// is one, and the router relays to it.
- dht: kadDHT({ protocol: DHT_PROTOCOL, clientMode: true, peerInfoMapper: passthroughMapper }),
+ dht: kadDHT({ protocol: DHT_PROTOCOL, clientMode: true, peerInfoMapper: passthroughMapper, initialQuerySelfInterval: 20 }),
// The control plane's events reach members through the routers.
// StrictSign, as every Go component pins it: the pubsub envelope is
// signed by the author, and the event inside by the control plane.
diff --git a/sdk/js/src/libp2p-http.test.ts b/sdk/js/src/libp2p-http.test.ts
index b05fd7c2..5208e192 100644
--- a/sdk/js/src/libp2p-http.test.ts
+++ b/sdk/js/src/libp2p-http.test.ts
@@ -412,7 +412,19 @@ test("a dot segment is refused however it is spelled", async () => {
const endpoint = a2aEndpoint({ handler: () => new Response() });
// Nothing in options is consulted before the path check.
const options = providerOptions(new Uint8Array());
- for (const target of ["/a2a/agent/../x", "/a2a/agent/./x", "/a2a/agent/%2e%2e/x", "/a2a/agent/%2E%2E/x", "/a2a/agent/.%2e/x", "/a2a/agent/%2e/x", "/a2a/%2e%2e/other/x?q=1"]) {
+ for (const target of [
+ "/a2a/agent/../x",
+ "/a2a/agent/./x",
+ "/a2a/agent/%2e%2e/x",
+ "/a2a/agent/%2E%2E/x",
+ "/a2a/agent/.%2e/x",
+ "/a2a/agent/%2e/x",
+ "/a2a/%2e%2e/other/x?q=1",
+ "/a2a/agent/%2f..%2fother",
+ "/a2a/agent/%5c..%5cother",
+ "/a2a/agent\\..\\other",
+ "/a2a/agent/%00/x",
+ ]) {
assert.deepEqual(await admitIngress({ method: "GET", target, headers: new Headers(), remotePeer: "peer" }, endpoint, options), { status: 400, text: "Invalid path" }, target);
}
// Not dot segments: the request reaches the next check, the missing biscuit.
diff --git a/sdk/js/src/libp2p-http.ts b/sdk/js/src/libp2p-http.ts
index 3e7bc3f8..15088a75 100644
--- a/sdk/js/src/libp2p-http.ts
+++ b/sdk/js/src/libp2p-http.ts
@@ -125,9 +125,24 @@ export interface ProviderOptions extends ProviderAuthorizerOptions {
onAuthorized?(peerId: string, verified: VerifiedBiscuit, targetService: string): void;
}
-// A URL parser reads %2e as a dot too (WHATWG URL, path state), so the check
-// sees what the parser and the backend will see.
+// A URL parser reads %2e as a dot too (WHATWG URL, path state), and %2f/%5c/\
+// as path separators on some backends, so the check rejects those and dot segments.
function hasDotSegment(path: string): boolean {
+ if (!path.startsWith("/") || path.includes("#")) {
+ return true;
+ }
+ for (let i = 0; i < path.length; i++) {
+ const code = path.charCodeAt(i);
+ if (code < 0x20 || code === 0x7f || path[i] === "\\" || path[i] === '"') {
+ return true;
+ }
+ if (path[i] === "%" && i + 2 < path.length) {
+ const hex = path.slice(i + 1, i + 3).toLowerCase();
+ if (hex === "2f" || hex === "5c" || hex === "00") {
+ return true;
+ }
+ }
+ }
return path.split("/").some((seg) => {
const s = seg.replace(/%2e/gi, ".");
return s === "." || s === "..";
diff --git a/sdk/js/src/session.ts b/sdk/js/src/session.ts
index d59db2a3..31fb16ae 100644
--- a/sdk/js/src/session.ts
+++ b/sdk/js/src/session.ts
@@ -497,7 +497,9 @@ export class MeshSession {
throw new Error(`service type must be mcp, inference, a2a or egress, got ${JSON.stringify(target.type)}`);
}
const cid = await serviceCID(target.type, target.name);
- const signal = AbortSignal.timeout(options.timeoutMs ?? DISCOVERY_TIMEOUT_MS);
+ const ac = new AbortController();
+ const signal = AbortSignal.any([ac.signal, AbortSignal.timeout(options.timeoutMs ?? DISCOVERY_TIMEOUT_MS)]);
+ let settleTimer: ReturnType<typeof setTimeout> | undefined;
const found = new Map<string, DiscoveredProvider>();
try {
for await (const provider of this.node.contentRouting.findProviders(cid, { signal })) {
@@ -516,12 +518,18 @@ export class MeshSession {
if (found.size >= (options.limit ?? 20)) {
break;
}
+ if (settleTimer === undefined) {
+ settleTimer = setTimeout(() => ac.abort(), 200);
+ settleTimer.unref?.();
+ }
}
} catch (err) {
// The lookup ended on its deadline; what was found so far is the answer.
if (!(err instanceof Error && err.name === "TimeoutError") && !signal.aborted) {
throw err;
}
+ } finally {
+ clearTimeout(settleTimer);
}
return [...found.values()];
}
diff --git a/sdk/js/src/tar.ts b/sdk/js/src/tar.ts
index 64848e9a..a4ff338a 100644
--- a/sdk/js/src/tar.ts
+++ b/sdk/js/src/tar.ts
@@ -51,7 +51,7 @@ function hasASCIIControl(s: string): boolean {
function hasEncodedPathTraversal(p: string): boolean {
for (let i = 0; i < p.length; i++) {
const code = p.charCodeAt(i);
- if (code < 0x20 || code === 0x7f || p[i] === "\\") {
+ if (code < 0x20 || code === 0x7f || p[i] === "\\" || p[i] === '"') {
return true;
}
if (p[i] === "%" && i + 2 < p.length) {
@@ -128,7 +128,7 @@ export function validateHTTPGrantPath(p: string): void {
if (stars > 1 || (stars === 1 && !p.endsWith("*"))) {
throw new Error(`path ${JSON.stringify(p)}: '*' is only valid once, at the end`);
}
- for (const seg of p.slice(1).split("/")) {
+ for (const seg of p.replace(/\*$/, "").split("/")) {
if (seg === "." || seg === "..") {
throw new Error(`path ${JSON.stringify(p)} must not contain '.' or '..' segments`);
}
diff --git a/sdk/python/src/agent_mesh/authorizer.py b/sdk/python/src/agent_mesh/authorizer.py
index 23b86768..3acbe623 100644
--- a/sdk/python/src/agent_mesh/authorizer.py
+++ b/sdk/python/src/agent_mesh/authorizer.py
@@ -29,7 +29,7 @@ import biscuit_auth as ba
from .biscuit import BiscuitVerificationError, VerifiedBiscuit, _extract_tar_chain, _limits, verify_peer_biscuit
from .discovery import parse_service_target
-from .tar import TaskRequestContext, evaluate_task_rules
+from .tar import TaskRequestContext, evaluate_task_rules, is_safe_request_http_path
BASELINE_DATALOG: dict = json.loads(resources.files("agent_mesh._gen").joinpath("datalog.json").read_text())
@@ -114,6 +114,8 @@ def authorize_caller(req: AuthorizeRequest, options: ProviderAuthorizerOptions)
# The request as the wire carried it, never as the caller describes it.
if req.method is not None:
+ if req.method != "CONNECT" and req.path and not is_safe_request_http_path(req.path):
+ raise AuthorizationError(req.peer_id, f"invalid HTTP path {req.path!r}")
b.add_fact(ba.Fact(BASELINE_DATALOG["fact_method"] + "({m})", {"m": req.method}))
b.add_fact(ba.Fact(BASELINE_DATALOG["fact_path"] + "({p})", {"p": req.path}))
diff --git a/sdk/python/src/agent_mesh/conformance_join.py b/sdk/python/src/agent_mesh/conformance_join.py
index aa7831e2..2473f878 100644
--- a/sdk/python/src/agent_mesh/conformance_join.py
+++ b/sdk/python/src/agent_mesh/conformance_join.py
@@ -94,7 +94,9 @@ async def _handle(session: MeshSession, command: dict) -> dict:
cmd = command.get("cmd")
try:
if cmd == "auth":
- verified = await session.authenticate(command["addr"])
+ timeout_s = float(command["timeout_ms"]) / 1000.0 if "timeout_ms" in command else 15.0
+ with trio.fail_after(timeout_s):
+ verified = await session.authenticate(command["addr"])
return {
"cmd": cmd,
"ok": True,
diff --git a/sdk/python/src/agent_mesh/libp2p_http.py b/sdk/python/src/agent_mesh/libp2p_http.py
index 27764370..baf5ebfd 100644
--- a/sdk/python/src/agent_mesh/libp2p_http.py
+++ b/sdk/python/src/agent_mesh/libp2p_http.py
@@ -131,8 +131,16 @@ _DOT_ENCODED = re.compile("%2e", re.IGNORECASE)
def _has_dot_segment(path: str) -> bool:
- # A URL parser reads %2e as a dot too (WHATWG URL, path state), so the
- # check sees what the backend will see.
+ # A URL parser reads %2e as a dot too (WHATWG URL, path state), and %2f/%5c/\
+ # as path separators on some backends, so the check rejects those and dot segments.
+ if not path.startswith("/") or "#" in path:
+ return True
+ for i, c in enumerate(path):
+ if ord(c) < 0x20 or ord(c) == 0x7F or c in ("\\", '"'):
+ return True
+ if c == "%" and i + 2 < len(path):
+ if path[i + 1 : i + 3].lower() in ("2f", "5c", "00"):
+ return True
return any(_DOT_ENCODED.sub(".", seg) in (".", "..") for seg in path.split("/"))
@@ -219,6 +227,7 @@ async def _handle_ingress(
# follows could resolve to a sibling path on the backend.
if _has_dot_segment(raw_path):
return plain(400, "Invalid path")
+ raw_path = _DOT_ENCODED.sub(".", raw_path)
parts = raw_path.lstrip("/").split("/")
if len(parts) < 2 or not parts[0] or not parts[1]:
return plain(400, "Invalid path")
diff --git a/sdk/python/src/agent_mesh/tar.py b/sdk/python/src/agent_mesh/tar.py
index eff46438..8df60ea8 100644
--- a/sdk/python/src/agent_mesh/tar.py
+++ b/sdk/python/src/agent_mesh/tar.py
@@ -47,7 +47,7 @@ def _has_ascii_control(s: str) -> bool:
def _has_encoded_path_traversal(p: str) -> bool:
for i, c in enumerate(p):
- if ord(c) < 0x20 or ord(c) == 0x7F or c == "\\":
+ if ord(c) < 0x20 or ord(c) == 0x7F or c == "\\" or c == '"':
return True
if c == "%" and i + 2 < len(p):
hx = p[i + 1 : i + 3].lower()
@@ -109,7 +109,7 @@ def validate_http_grant_path(p: str) -> None:
stars = p.count("*")
if stars > 1 or (stars == 1 and not p.endswith("*")):
raise ValueError(f"path {p!r}: '*' is only valid once, at the end")
- for seg in p[1:].split("/"):
+ for seg in p.removesuffix("*").split("/"):
if seg in (".", ".."):
raise ValueError(f"path {p!r} must not contain '.' or '..' segments")
diff --git a/sdk/python/tests/test_libp2p_http.py b/sdk/python/tests/test_libp2p_http.py
index 651d3d6f..eea2b681 100644
--- a/sdk/python/tests/test_libp2p_http.py
+++ b/sdk/python/tests/test_libp2p_http.py
@@ -252,6 +252,8 @@ def test_the_agent_is_reachable_by_authorized_callers(backend_url):
# A URL parser reads %2e as a dot too; the spelling does not get past the check.
assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/%2e%2e/other/x")).status == 400
assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/.%2E/other/x")).status == 400
+ assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/%2f..%2fother/x")).status == 400
+ assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/%5c..%5cother/x")).status == 400
nursery.cancel_scope.cancel()
async def with_timeout():
diff --git a/site/content/docs/guides/kubernetes.md b/site/content/docs/guides/kubernetes.md
index 1f8b0a15..1d90806b 100644
--- a/site/content/docs/guides/kubernetes.md
+++ b/site/content/docs/guides/kubernetes.md
@@ -105,8 +105,10 @@ gateway:
```
The route exposes only the paths that nodes need (`/info`, `/register`,
-`/keys`, `/routers/lease`, `/policies`, `/enroll`, `/enroll/status`,
-`/refresh`, `/nodes/catalog`) and the console under `gateway.consolePath`.
+`/keys`, `/routers/lease`, `/policies`, `/egress`, `/revocations`,
+`/token/exchange`, `/sts/token`, `/enroll`, `/enroll/status`,
+`/refresh`, `/nodes/catalog`, `/.well-known/*`, `/jwks`, `/oauth/authorize`,
+`/oauth/token`) and the console under `gateway.consolePath`.
`/admin` and `/user` are not routed unless you set `gateway.adminRoute`,
which is meant for development clusters.
diff --git a/site/static/install.sh b/site/static/install.sh
index 82aa3b96..ea57fff3 100755
--- a/site/static/install.sh
+++ b/site/static/install.sh
@@ -52,25 +52,29 @@ if ! curl -sfL -o "${TAR_NAME}" "${DOWNLOAD_URL}"; then
exit 1
fi
-if curl -sfL -o checksums.txt "${CHECKSUMS_URL}"; then
- echo "Verifying SHA-256 checksum..."
- EXPECTED_SUM=$(awk -v f="${TAR_NAME}" '$2 == f {print $1}' checksums.txt)
- if [ -z "${EXPECTED_SUM}" ]; then
- echo "Error: ${TAR_NAME} not found in checksums.txt"
- exit 1
- fi
- if command -v sha256sum >/dev/null 2>&1; then
- ACTUAL_SUM=$(sha256sum "${TAR_NAME}" | awk '{print $1}')
- elif command -v shasum >/dev/null 2>&1; then
- ACTUAL_SUM=$(shasum -a 256 "${TAR_NAME}" | awk '{print $1}')
- else
- echo "Error: Neither sha256sum nor shasum is available to verify archive integrity."
- exit 1
- fi
- if [ "${EXPECTED_SUM}" != "${ACTUAL_SUM}" ]; then
- echo "Error: SHA-256 checksum mismatch for ${TAR_NAME} (expected ${EXPECTED_SUM}, got ${ACTUAL_SUM})"
- exit 1
- fi
+echo "Downloading ${CHECKSUMS_URL}..."
+if ! curl -sfL -o checksums.txt "${CHECKSUMS_URL}"; then
+ echo "Error: Failed to download ${CHECKSUMS_URL}"
+ exit 1
+fi
+
+echo "Verifying SHA-256 checksum..."
+EXPECTED_SUM=$(awk -v f="${TAR_NAME}" '$2 == f {print $1}' checksums.txt)
+if [ -z "${EXPECTED_SUM}" ]; then
+ echo "Error: ${TAR_NAME} not found in checksums.txt"
+ exit 1
+fi
+if command -v sha256sum >/dev/null 2>&1; then
+ ACTUAL_SUM=$(sha256sum "${TAR_NAME}" | awk '{print $1}')
+elif command -v shasum >/dev/null 2>&1; then
+ ACTUAL_SUM=$(shasum -a 256 "${TAR_NAME}" | awk '{print $1}')
+else
+ echo "Error: Neither sha256sum nor shasum is available to verify archive integrity."
+ exit 1
+fi
+if [ "${EXPECTED_SUM}" != "${ACTUAL_SUM}" ]; then
+ echo "Error: SHA-256 checksum mismatch for ${TAR_NAME} (expected ${EXPECTED_SUM}, got ${ACTUAL_SUM})"
+ exit 1
fi
echo "Extracting..."
@@ -78,7 +82,7 @@ tar -xzf "${TAR_NAME}"
echo "Installing to ${INSTALL_DIR} (may require sudo)..."
INSTALLED_BINS=()
-for b in sam-one sam-node sam-control-plane sam-router mcp-client sam-box sam-console nano-init; do
+for b in sam-one sam-node sam-control-plane sam-router mcp-client sam-console; do
if [ -f "$b" ]; then
INSTALLED_BINS+=("$b")
fi
diff --git a/tests/integration/failover_test.go b/tests/integration/failover_test.go
index a91c4460..69bccd5a 100644
--- a/tests/integration/failover_test.go
+++ b/tests/integration/failover_test.go
@@ -149,7 +149,7 @@ roles: []
"--keys-path", filepath.Join(tmpDir, "router_keysA.db"),
"--allow-loopback",
"--oidc-token", routerJWT,
- "--lease-renew-interval", "1s",
+ "--lease-renew-interval", "250ms",
)
if err := cmdRouterA.Start(); err != nil {
t.Fatalf("failed to start Router A: %v", err)
@@ -182,7 +182,7 @@ roles: []
"--keys-path", filepath.Join(tmpDir, "router_keysB.db"),
"--allow-loopback",
"--oidc-token", routerJWT,
- "--lease-renew-interval", "1s",
+ "--lease-renew-interval", "250ms",
)
if err := cmdRouterB.Start(); err != nil {
t.Fatalf("failed to start Router B: %v", err)
@@ -207,10 +207,10 @@ roles: []
"--jwt-path", jwtPath,
"--api-token-path", tokenPath(t, "dummy-token"),
"--allow-loopback",
- "--monitor-bootstrap", "1s",
- "--monitor-interval", "1s",
- "--autorelay-min-interval", "1s",
- "--autorelay-backoff", "1s",
+ "--monitor-bootstrap", "200ms",
+ "--monitor-interval", "200ms",
+ "--autorelay-min-interval", "200ms",
+ "--autorelay-backoff", "200ms",
"--autorelay-boot-delay", "0s",
)
samNode.waitForAPI(t)
@@ -255,7 +255,7 @@ roles: []
// handshaken with Router B before it dials the circuit. A fresh host per
// attempt sidesteps the swarm's dial backoff.
var connectErr error
- for i := 0; i < 15; i++ {
+ for i := 0; i < 25; i++ {
clientHost, err := libp2p.New(libp2p.NoListenAddrs, libp2p.EnableRelay())
if err != nil {
t.Fatal(err)
@@ -271,7 +271,7 @@ roles: []
break
}
t.Logf("attempt %d: %v", i+1, connectErr)
- time.Sleep(1 * time.Second)
+ time.Sleep(200 * time.Millisecond)
}
if connectErr != nil {
diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.go
index 2c994d9f..5c10864f 100644
--- a/tests/integration/minimal_helpers_test.go
+++ b/tests/integration/minimal_helpers_test.go
@@ -467,7 +467,7 @@ func startRouter(t *testing.T, tmpDir string, cpPort int, mintToken func(map[str
"--oidc-token", routerJWT,
// Each renewal carries the router's connected peers, which is how
// tests see a node reach the mesh through the control plane.
- "--lease-renew-interval", "1s",
+ "--lease-renew-interval", "250ms",
)
routerCmd.Stdout = os.Stdout
routerCmd.Stderr = os.Stderr
@@ -494,7 +494,7 @@ func startRouter(t *testing.T, tmpDir string, cpPort int, mintToken func(map[str
if time.Now().After(deadline) {
t.Fatalf("router %s never leased with control plane :%d", name, cpPort)
}
- time.Sleep(100 * time.Millisecond)
+ time.Sleep(50 * time.Millisecond)
}
}
@@ -610,7 +610,7 @@ func waitForPeerOnRouter(t *testing.T, cpPort int, adminToken string, peerID str
if time.Now().After(deadline) {
t.Fatalf("no router on control plane :%d reported %s connected within %v", cpPort, peerID, timeout)
}
- time.Sleep(200 * time.Millisecond)
+ time.Sleep(50 * time.Millisecond)
}
}
diff --git a/tests/integration/policy_permutations_test.go b/tests/integration/policy_permutations_test.go
index b7caeeeb..fb502f10 100644
--- a/tests/integration/policy_permutations_test.go
+++ b/tests/integration/policy_permutations_test.go
@@ -240,60 +240,66 @@ services:
},
}
- for i, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- homeA := filepath.Join(tmpDir, fmt.Sprintf("nodeA_%d", i))
- apiTokenA := "tokenA"
+ t.Run("cases", func(t *testing.T) {
+ for i, tt := range tests {
+ i, tt := i, tt
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ homeA := filepath.Join(tmpDir, fmt.Sprintf("nodeA_%d", i))
+ apiTokenA := "tokenA"
- // The seat comes from the sam:system:authenticated binding above,
- // never from a roles claim naming sam:role:node.
- jwtA := mintToken(tt.jwtClaims)
+ // The seat comes from the sam:system:authenticated binding above,
+ // never from a roles claim naming sam:role:node.
+ jwtA := mintToken(tt.jwtClaims)
- nodeA := launchNode(t, nodeBin, os.Environ(), homeA, "run",
- "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", httpPortCP),
- "--data-dir", homeA,
- "--api-token-path", tokenPath(t, apiTokenA),
- "--jwt", jwtA,
- "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
- "--allow-loopback",
- )
- actualApiAddrA := nodeA.waitForAPI(t)
+ nodeA := launchNode(t, nodeBin, os.Environ(), homeA, "run",
+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", httpPortCP),
+ "--data-dir", homeA,
+ "--api-token-path", tokenPath(t, apiTokenA),
+ "--jwt", jwtA,
+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
+ "--allow-loopback",
+ )
+ defer nodeA.kill()
+ actualApiAddrA := nodeA.waitForAPI(t)
- parts := strings.Split(addrB, "/p2p/")
- if len(parts) != 2 {
- t.Fatalf("unexpected addrB format: %s", addrB)
- }
- peerIDB := parts[1]
+ parts := strings.Split(addrB, "/p2p/")
+ if len(parts) != 2 {
+ t.Fatalf("unexpected addrB format: %s", addrB)
+ }
+ peerIDB := parts[1]
+ connectPeerWithToken(t, actualApiAddrA, apiTokenA, addrB)
- // Use the local callMCPAllowError targeting Node A to hit Node B
- resp, callErr := callMCPAllowError(t, actualApiAddrA, apiTokenA, "call_remote_tool", map[string]any{
- "peer_id": peerIDB,
- "tool_name": tt.targetSvc + "/test_tool",
- "arguments": map[string]any{},
- })
+ // Use the local callMCPAllowError targeting Node A to hit Node B
+ resp, callErr := callMCPAllowError(t, actualApiAddrA, apiTokenA, "call_remote_tool", map[string]any{
+ "peer_id": peerIDB,
+ "tool_name": tt.targetSvc + "/test_tool",
+ "arguments": map[string]any{},
+ })
- // call_remote_tool might return a JSON error inside resp, or callErr might be non-nil.
- failed := false
- if callErr != nil {
- if strings.Contains(callErr.Error(), "EOF") {
- // EOF means the 'echo' backend started and exited, which means AuthZ succeeded!
- failed = false
- } else {
+ // call_remote_tool might return a JSON error inside resp, or callErr might be non-nil.
+ failed := false
+ if callErr != nil {
+ if strings.Contains(callErr.Error(), "EOF") {
+ // EOF means the 'echo' backend started and exited, which means AuthZ succeeded!
+ failed = false
+ } else {
+ failed = true
+ }
+ } else if strings.Contains(resp, "Authorization failed") || strings.Contains(resp, "failed to connect") || strings.Contains(resp, "token lacks") || strings.Contains(resp, "denied") {
failed = true
}
- } else if strings.Contains(resp, "Authorization failed") || strings.Contains(resp, "failed to connect") || strings.Contains(resp, "token lacks") || strings.Contains(resp, "denied") {
- failed = true
- }
- if tt.expectAllow {
- if failed {
- t.Errorf("expected success, got error: %v / %s", callErr, resp)
- }
- } else {
- if !failed {
- t.Errorf("expected failure, got success: %s", resp)
+ if tt.expectAllow {
+ if failed {
+ t.Errorf("expected success, got error: %v / %s", callErr, resp)
+ }
+ } else {
+ if !failed {
+ t.Errorf("expected failure, got success: %s", resp)
+ }
}
- }
- })
- }
+ })
+ }
+ })
}
diff --git a/tests/integration/rotation_offline_test.go b/tests/integration/rotation_offline_test.go
index 24216e56..d4da1091 100644
--- a/tests/integration/rotation_offline_test.go
+++ b/tests/integration/rotation_offline_test.go
@@ -21,8 +21,10 @@ import (
"fmt"
"net/http"
"os"
+ "os/exec"
"path/filepath"
"sort"
+ "sync"
"testing"
"time"
@@ -57,10 +59,22 @@ func TestKeyRotationWhileMembersOffline(t *testing.T) {
node := members.startNode(t, false)
members.assertNodeBackUnderKey(t, node, mesh, newPub)
- // The SDK members start again from their state directories. The token
- // path names no file: the member resumes or fails, it cannot enroll.
- for _, s := range members.sdk {
- m := s.start(t, mesh, " resumed", "SAM_BOOTSTRAP_TOKEN_PATH="+filepath.Join(t.TempDir(), "no-token"))
+ // The SDK members start again from their state directories concurrently.
+ // The token path names no file: the member resumes or fails, it cannot enroll.
+ resumed := make([]*sdkMember, len(members.sdk))
+ noToken := "SAM_BOOTSTRAP_TOKEN_PATH=" + filepath.Join(t.TempDir(), "no-token")
+ var wg sync.WaitGroup
+ for i, s := range members.sdk {
+ i, s := i, s
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ resumed[i] = s.start(t, mesh, " resumed", noToken)
+ }()
+ }
+ wg.Wait()
+ for i, s := range members.sdk {
+ m := resumed[i]
s.assertBackUnderKey(t, m, mesh, newPub, node)
if res := m.sync(t); !res.OK {
t.Fatalf("%s: pull after the resume failed: %s", m.name, res.Error)
@@ -85,7 +99,7 @@ func TestKeyRotationPastGraceWhileMembersOffline(t *testing.T) {
ctx := context.Background()
members := enrollMembersThenStop(t, mesh)
- newPub := rotateSigningKey(t, ctx, mesh, time.Second)
+ newPub := rotateSigningKey(t, ctx, mesh, 250*time.Millisecond)
waitForKeysRetired(t, mesh.cpPort, [][]byte{mesh.cpPub})
// The node comes back with a fresh platform token on disk.
@@ -97,8 +111,19 @@ func TestKeyRotationPastGraceWhileMembersOffline(t *testing.T) {
// Each SDK member comes back with a fresh token available and enrolls
// again as the identity it persisted.
- for _, s := range members.sdk {
- m := s.start(t, mesh, " past grace")
+ resumed := make([]*sdkMember, len(members.sdk))
+ var wg sync.WaitGroup
+ for i, s := range members.sdk {
+ i, s := i, s
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ resumed[i] = s.start(t, mesh, " past grace")
+ }()
+ }
+ wg.Wait()
+ for i, s := range members.sdk {
+ m := resumed[i]
s.assertBackUnderKey(t, m, mesh, newPub, node)
m.quit(t)
}
@@ -162,16 +187,34 @@ func enrollMembersThenStop(t *testing.T, mesh *sdkMesh) *offlineMembers {
// A token is read only to enroll, so the file can go once the member
// holds a credential.
+ type slot struct {
+ launcher sdkRunner
+ cmd *exec.Cmd
+ stateDir string
+ member *sdkMember
+ }
+ var slots []slot
for _, launcher := range sdkMemberLaunchers {
cmd, skip := launcher.cmd(mesh.root)
if skip != "" {
t.Logf("%s SDK skipped: %s", launcher.name, skip)
continue
}
- stateDir := filepath.Join(t.TempDir(), "state")
- m := launchSDKMember(t, launcher.name, cmd, mesh.root, mesh.baseURL, mesh.adminToken, "SAM_SDK_STATE_DIR="+stateDir)
- members.sdk = append(members.sdk, offlineSDKMember{launcher: launcher, stateDir: stateDir, peerID: m.report.PeerID, biscuit: m.report.Biscuit})
- m.quit(t)
+ slots = append(slots, slot{launcher: launcher, cmd: cmd, stateDir: filepath.Join(t.TempDir(), "state")})
+ }
+ var wg sync.WaitGroup
+ for i := range slots {
+ i := i
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ slots[i].member = launchSDKMember(t, slots[i].launcher.name, slots[i].cmd, mesh.root, mesh.baseURL, mesh.adminToken, "SAM_SDK_STATE_DIR="+slots[i].stateDir)
+ }()
+ }
+ wg.Wait()
+ for _, s := range slots {
+ members.sdk = append(members.sdk, offlineSDKMember{launcher: s.launcher, stateDir: s.stateDir, peerID: s.member.report.PeerID, biscuit: s.member.report.Biscuit})
+ s.member.quit(t)
}
// The Go node: enrolled through a platform token on disk, its identity
diff --git a/tests/integration/sdk_canary_test.go b/tests/integration/sdk_canary_test.go
index a6df85de..81c106ad 100644
--- a/tests/integration/sdk_canary_test.go
+++ b/tests/integration/sdk_canary_test.go
@@ -133,7 +133,7 @@ func TestSDKCanaryScript(t *testing.T) {
callCmd, _ := caller.cmd(context.Background(), mesh.root, "a2a-call")
verdicts := runCanaryScript(t, script, dir, caller.name+"-calls-"+agent.name+"-agent", strings.Join(callCmd.Args, " "),
- append(env, "SAM_STATE_DIR="+filepath.Join(dir, "caller"), "INTERVAL=1"))
+ append(env, "SAM_STATE_DIR="+filepath.Join(dir, "caller"), "INTERVAL=0"))
for i := 0; i < 2; i++ {
v := <-verdicts
if !v.OK || v.Error != "" || v.AgentPeer == "" {
@@ -208,6 +208,7 @@ func runCanaryScript(t *testing.T, script, dir, canary, call string, env []strin
}
verdicts <- v
}
+ _ = scanner.Err()
}()
out := make(chan canaryVerdict)
go func() {
diff --git a/tests/integration/sdk_examples_test.go b/tests/integration/sdk_examples_test.go
index adc6fd42..12a69b65 100644
--- a/tests/integration/sdk_examples_test.go
+++ b/tests/integration/sdk_examples_test.go
@@ -57,15 +57,9 @@ var sdkExampleLaunchers = []sdkExampleLauncher{
{
name: "python",
cmd: func(ctx context.Context, root, example string, args ...string) (*exec.Cmd, string) {
- python := filepath.Join(root, "sdk", "python", ".venv", "bin", "python")
- if _, err := os.Stat(python); err != nil {
- var lookErr error
- if python, lookErr = exec.LookPath("python3"); lookErr != nil {
- return nil, "python3 is not installed"
- }
- }
- if err := exec.Command(python, "-c", "import agent_mesh.session").Run(); err != nil {
- return nil, "agent_mesh is not importable with libp2p (pip install -e sdk/python)"
+ python, skip := resolvePythonSession(root)
+ if skip != "" {
+ return nil, skip
}
// Python names its files with underscores: a2a-agent is a2a_agent.py.
entry := filepath.Join(root, "sdk", "python", "examples", strings.ReplaceAll(example, "-", "_")+".py")
@@ -315,6 +309,7 @@ func startExampleAgent(t *testing.T, name string, cmd *exec.Cmd) (*sdkExampleAge
return
}
}
+ _ = scanner.Err()
close(lines)
}()
select {
diff --git a/tests/integration/sdk_mesh_test.go b/tests/integration/sdk_mesh_test.go
index c5297d92..13b5df46 100644
--- a/tests/integration/sdk_mesh_test.go
+++ b/tests/integration/sdk_mesh_test.go
@@ -198,8 +198,27 @@ egress:
// Two routers, as the testnets run several: a member authenticates with
// every one and reserves a relay slot on the first the control plane
// lists for it, a different one from member to member.
- routerA, _ := startRouter(t, t.TempDir(), cpPort, mintToken, "router-a")
- routerB, _ := startRouter(t, t.TempDir(), cpPort, mintToken, "router-b")
+ dirA, dirB := t.TempDir(), t.TempDir()
+ var routerA, routerB, nodeBin string
+ var rwg sync.WaitGroup
+ rwg.Add(4)
+ go func() {
+ defer rwg.Done()
+ routerA, _ = startRouter(t, dirA, cpPort, mintToken, "router-a")
+ }()
+ go func() {
+ defer rwg.Done()
+ routerB, _ = startRouter(t, dirB, cpPort, mintToken, "router-b")
+ }()
+ go func() {
+ defer rwg.Done()
+ nodeBin = buildBinary(t, "./cmd/sam-node")
+ }()
+ go func() {
+ defer rwg.Done()
+ _, _ = resolvePythonSession(root)
+ }()
+ rwg.Wait()
routerAddrs := []string{routerA, routerB}
routerAddr := routerA
@@ -222,7 +241,6 @@ egress:
_, _ = io.WriteString(w, sdkMeshStockAgentCard)
}))
t.Cleanup(agentCard.Close)
- nodeBin := buildBinary(t, "./cmd/sam-node")
nodeHome := filepath.Join(t.TempDir(), "node")
if err := os.MkdirAll(nodeHome, 0o755); err != nil {
t.Fatal(err)
@@ -317,6 +335,31 @@ type sdkAuthResult struct {
Expiration int64 `json:"expiration"`
}
+var (
+ pythonSessionOnce sync.Once
+ pythonSessionBin string
+ pythonSessionSkip string
+)
+
+func resolvePythonSession(root string) (string, string) {
+ pythonSessionOnce.Do(func() {
+ python := filepath.Join(root, "sdk", "python", ".venv", "bin", "python")
+ if _, err := os.Stat(python); err != nil {
+ var lookErr error
+ if python, lookErr = exec.LookPath("python3"); lookErr != nil {
+ pythonSessionSkip = "python3 is not installed"
+ return
+ }
+ }
+ if err := exec.Command(python, "-c", "import agent_mesh.session").Run(); err != nil {
+ pythonSessionSkip = "agent_mesh is not importable with libp2p (pip install -e sdk/python)"
+ return
+ }
+ pythonSessionBin = python
+ })
+ return pythonSessionBin, pythonSessionSkip
+}
+
var sdkMemberLaunchers = []sdkRunner{
{
name: "js",
@@ -334,15 +377,9 @@ var sdkMemberLaunchers = []sdkRunner{
{
name: "python",
cmd: func(root string) (*exec.Cmd, string) {
- python := filepath.Join(root, "sdk", "python", ".venv", "bin", "python")
- if _, err := os.Stat(python); err != nil {
- var lookErr error
- if python, lookErr = exec.LookPath("python3"); lookErr != nil {
- return nil, "python3 is not installed"
- }
- }
- if err := exec.Command(python, "-c", "import agent_mesh.session").Run(); err != nil {
- return nil, "agent_mesh is not importable with libp2p (pip install -e sdk/python)"
+ python, skip := resolvePythonSession(root)
+ if skip != "" {
+ return nil, skip
}
return exec.Command(python, "-m", "agent_mesh.conformance_join"), ""
},
@@ -382,18 +419,33 @@ func TestNativeSDKsMesh(t *testing.T) {
// One member per SDK, each listening directly on loopback too so both
// the direct and the relayed path can be walked.
- var members []*sdkMember
+ type memberSlot struct {
+ launcher sdkRunner
+ cmd *exec.Cmd
+ }
+ var slots []memberSlot
for _, launcher := range sdkMemberLaunchers {
cmd, skip := launcher.cmd(root)
if skip != "" {
t.Logf("%s SDK skipped: %s", launcher.name, skip)
continue
}
- members = append(members, startSDKMember(t, launcher.name, cmd, root, baseURL, adminToken, mesh.routerAddrs))
+ slots = append(slots, memberSlot{launcher: launcher, cmd: cmd})
}
- if len(members) == 0 {
+ if len(slots) == 0 {
t.Skip("no SDK toolchain available; see sdk/README.md")
}
+ members := make([]*sdkMember, len(slots))
+ var mwg sync.WaitGroup
+ for i := range slots {
+ i := i
+ mwg.Add(1)
+ go func() {
+ defer mwg.Done()
+ members[i] = startSDKMember(t, slots[i].launcher.name, slots[i].cmd, root, baseURL, adminToken, mesh.routerAddrs)
+ }()
+ }
+ mwg.Wait()
// The router reports every member connected to the control plane.
waitForPeerOnRouter(t, cpPort, adminToken, samNode.peerID.String(), 5*time.Second)
@@ -555,30 +607,34 @@ func TestNativeSDKsMesh(t *testing.T) {
// satisfy (launchSDKMember). MCP to an SDK member fails before the floor: no /sam/mcp.
t.Run("egress-floor", func(t *testing.T) {
for _, launcher := range sdkMemberLaunchers {
+ launcher := launcher
cmd, skip := launcher.cmd(root)
if skip != "" {
continue
}
- floored := launchSDKMember(t, launcher.name+"-floored", cmd, root, baseURL, adminToken, "SAM_SDK_EGRESS_REQUIRE_LABELS=team=nobody")
- byFloor := func(what, target, err string) {
- if !strings.Contains(err, "LabelsNotSatisfied") {
- t.Errorf("%s %s to %s was not refused by the floor: %q", floored.name, what, target, err)
- }
- }
- nodeRelayAddr := samNode.peerID.String()
- byFloor("tools", "the node", floored.toolsRequiring(t, nodeRelayAddr, "mcp://"+serviceName, nil).Error)
- byFloor("call", "the node", floored.callRaw(t, nodeRelayAddr, "mcp://"+serviceName, "add", nil).Error)
- byFloor("http", "the node", floored.httpRaw(t, nodeRelayAddr, "egress://"+sdkMeshEgressHost, "/").Error)
- for _, m := range members {
- byFloor("http", m.name, floored.httpRaw(t, m.report.PeerID, "a2a://agent", "/card").Error)
- if res := floored.toolsRequiring(t, m.report.PeerID, "mcp://"+serviceName, nil); res.OK {
- t.Errorf("%s listed tools of %s, an SDK member: %v", floored.name, m.name, res.Tools)
+ t.Run(launcher.name, func(t *testing.T) {
+ t.Parallel()
+ floored := launchSDKMember(t, launcher.name+"-floored", cmd, root, baseURL, adminToken, "SAM_SDK_EGRESS_REQUIRE_LABELS=team=nobody")
+ byFloor := func(what, target, err string) {
+ if !strings.Contains(err, "LabelsNotSatisfied") {
+ t.Errorf("%s %s to %s was not refused by the floor: %q", floored.name, what, target, err)
+ }
}
- if res := floored.callRaw(t, m.report.PeerID, "mcp://"+serviceName, "add", nil); res.OK {
- t.Errorf("%s called a tool of %s, an SDK member: %+v", floored.name, m.name, res)
+ nodeRelayAddr := samNode.peerID.String()
+ byFloor("tools", "the node", floored.toolsRequiring(t, nodeRelayAddr, "mcp://"+serviceName, nil).Error)
+ byFloor("call", "the node", floored.callRaw(t, nodeRelayAddr, "mcp://"+serviceName, "add", nil).Error)
+ byFloor("http", "the node", floored.httpRaw(t, nodeRelayAddr, "egress://"+sdkMeshEgressHost, "/").Error)
+ for _, m := range members {
+ byFloor("http", m.name, floored.httpRaw(t, m.report.PeerID, "a2a://agent", "/card").Error)
+ if res := floored.toolsRequiring(t, m.report.PeerID, "mcp://"+serviceName, nil); res.OK {
+ t.Errorf("%s listed tools of %s, an SDK member: %v", floored.name, m.name, res.Tools)
+ }
+ if res := floored.callRaw(t, m.report.PeerID, "mcp://"+serviceName, "add", nil); res.OK {
+ t.Errorf("%s called a tool of %s, an SDK member: %+v", floored.name, m.name, res)
+ }
}
- }
- floored.quit(t)
+ floored.quit(t)
+ })
}
})
@@ -591,7 +647,7 @@ func TestNativeSDKsMesh(t *testing.T) {
relayAddr, released := startStalledRelay(t)
stranger := "12D3KooWA4Xop1JaT3MHxwYMkCepYsv4iPVopMXwCz5iHYdBfeSB"
for _, m := range members {
- m.write(t, map[string]string{"cmd": "auth", "addr": relayAddr + "/p2p-circuit/p2p/" + stranger})
+ m.write(t, map[string]string{"cmd": "auth", "addr": relayAddr + "/p2p-circuit/p2p/" + stranger, "timeout_ms": "400"})
}
for _, m := range members {
var res sdkAuthResult
@@ -850,9 +906,16 @@ func TestNativeSDKsAcrossRouters(t *testing.T) {
type placed struct {
*sdkMember
- router string
+ router string
+ routerAddr string
}
- var members []placed
+ type launchSpec struct {
+ launcher sdkRunner
+ cmd *exec.Cmd
+ router string
+ routerAddr string
+ }
+ var specs []launchSpec
for _, launcher := range sdkMemberLaunchers {
for _, routerAddr := range mesh.routerAddrs {
cmd, skip := launcher.cmd(mesh.root)
@@ -860,27 +923,42 @@ func TestNativeSDKsAcrossRouters(t *testing.T) {
t.Logf("%s SDK skipped: %s", launcher.name, skip)
break
}
- router := extractPeerID(routerAddr)
- m := launchSDKMember(t, launcher.name+"@"+router[len(router)-6:], cmd, mesh.root, mesh.baseURL, mesh.adminToken, "SAM_SDK_ROUTERS="+router)
- if got := routerIDs(m.report); !reflect.DeepEqual(got, []string{router}) {
- t.Fatalf("%s joined through %v, want %s only", m.name, got, router)
- }
- for _, a := range m.report.RelayAddresses {
- if !strings.HasPrefix(a, routerAddr) {
- t.Fatalf("%s reserved on %s, want %s", m.name, a, routerAddr)
- }
- }
- if len(m.report.DirectAddresses) != 0 {
- t.Fatalf("%s listens on %v, want the relayed path to be the only one", m.name, m.report.DirectAddresses)
- }
- m.accept(t, "agent")
- members = append(members, placed{m, router})
+ specs = append(specs, launchSpec{
+ launcher: launcher,
+ cmd: cmd,
+ router: extractPeerID(routerAddr),
+ routerAddr: routerAddr,
+ })
}
}
- if len(members) == 0 {
+ if len(specs) == 0 {
t.Skip("no SDK toolchain available; see sdk/README.md")
}
+ members := make([]placed, len(specs))
+ var wg sync.WaitGroup
+ for i, sp := range specs {
+ i, sp := i, sp
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ m := launchSDKMember(t, sp.launcher.name+"@"+sp.router[len(sp.router)-6:], sp.cmd, mesh.root, mesh.baseURL, mesh.adminToken, "SAM_SDK_ROUTERS="+sp.router)
+ m.accept(t, "agent")
+ members[i] = placed{m, sp.router, sp.routerAddr}
+ }()
+ }
+ wg.Wait()
for _, m := range members {
+ if got := routerIDs(m.report); !reflect.DeepEqual(got, []string{m.router}) {
+ t.Fatalf("%s joined through %v, want %s only", m.name, got, m.router)
+ }
+ for _, a := range m.report.RelayAddresses {
+ if !strings.HasPrefix(a, m.routerAddr) {
+ t.Fatalf("%s reserved on %s, want %s", m.name, a, m.routerAddr)
+ }
+ }
+ if len(m.report.DirectAddresses) != 0 {
+ t.Fatalf("%s listens on %v, want the relayed path to be the only one", m.name, m.report.DirectAddresses)
+ }
waitForPeerOnRouter(t, mesh.cpPort, mesh.adminToken, m.report.PeerID, 5*time.Second)
}