Skip to content

Agent Mesh

node: compare the sidecar token in constant time, and gate /metrics on TCP

Antonio Ojeab884271a4bf9
diff --git a/internal/node/sidecar.go b/internal/node/sidecar.go
index ef3fee4f..f5167998 100644
--- a/internal/node/sidecar.go
+++ b/internal/node/sidecar.go
@@ -16,6 +16,8 @@ package node
import (
"context"
+ "crypto/sha256"
+ "crypto/subtle"
"crypto/tls"
"crypto/x509"
"encoding/base64"
@@ -48,7 +50,10 @@ func StartSidecarServer(node *SamNode, addr, socketPath, token, certFile, keyFil
// Public endpoints
mux.HandleFunc("/healthz", handleHealthz)
mux.HandleFunc("/readyz", handleReadyz)
- mux.Handle("/metrics", promhttp.Handler())
+ // Gated like the rest: the labels carry peer IDs and per-peer request counts,
+ // and this mux is reachable by any local process over TCP. Socket callers are
+ // unaffected, which is how every scrape in this repo reads it.
+ mux.Handle("/metrics", withAuth(token, true, promhttp.Handler()))
// Protected endpoints. allowAuthorizationFallback=true is safe here: none of
// these ever forward the inbound Authorization header to another service.
@@ -403,7 +408,7 @@ func withAuth(token string, allowAuthorizationFallback bool, next http.Handler)
return
}
- if parts[1] != token {
+ if !constantTimeEqual(parts[1], token) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
@@ -418,6 +423,16 @@ func withAuth(token string, allowAuthorizationFallback bool, next http.Handler)
})
}
+// constantTimeEqual compares two secrets without leaking their contents through
+// timing. Hashing first keeps the comparison length-independent, so a mismatched
+// length is no more distinguishable than a mismatched byte. This mirrors how the
+// control plane checks its admin token.
+func constantTimeEqual(got, want string) bool {
+ gotHash := sha256.Sum256([]byte(got))
+ wantHash := sha256.Sum256([]byte(want))
+ return subtle.ConstantTimeCompare(gotHash[:], wantHash[:]) == 1
+}
+
type ServiceRequest struct {
ServiceName string `json:"service_name"`
}
diff --git a/internal/node/sidecar_auth_test.go b/internal/node/sidecar_auth_test.go
new file mode 100644
index 00000000..fc8b311b
--- /dev/null
+++ b/internal/node/sidecar_auth_test.go
@@ -0,0 +1,99 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package node
+
+import (
+ "net/http"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// TestConstantTimeEqual is a correctness check, not a timing one: a timing
+// assertion in a unit test is flaky enough to be worse than nothing. What it
+// pins is that swapping the comparison for a constant-time one did not change
+// the answer for any of the shapes the plain == used to handle, in particular
+// that a prefix of the token is still not the token.
+func TestConstantTimeEqual(t *testing.T) {
+ const token = "s3cret-sidecar-token"
+
+ tests := []struct {
+ name string
+ got string
+ want bool
+ }{
+ {name: "identical", got: token, want: true},
+ {name: "empty against a secret", got: ""},
+ {name: "prefix", got: token[:len(token)-1]},
+ {name: "longer", got: token + "x"},
+ {name: "same length, one byte off", got: "s3cret-sidecar-tokeN"},
+ {name: "different case", got: "S3CRET-SIDECAR-TOKEN"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := constantTimeEqual(tt.got, token); got != tt.want {
+ t.Errorf("constantTimeEqual(%q, %q) = %v, want %v", tt.got, token, got, tt.want)
+ }
+ })
+ }
+
+ // Two empty strings hash alike, so this holds; withAuth never reaches here
+ // with an empty configured token, but the function should not pretend.
+ if !constantTimeEqual("", "") {
+ t.Error("constantTimeEqual(\"\", \"\") = false, want true")
+ }
+}
+
+// TestMetricsGatedOnTCPButNotOnTheSocket pins where the metrics endpoint sits in
+// the sidecar's trust model. Its labels carry peer IDs and per-peer request
+// counts, and the TCP listener is reachable by any local process, so it is
+// gated there. Reaching the socket already proves the caller owns it, which is
+// the same bar as reading the token file, and every scrape in this repo goes
+// that way.
+func TestMetricsGatedOnTCPButNotOnTheSocket(t *testing.T) {
+ node := &SamNode{
+ BiscuitTimeout: 500 * time.Millisecond,
+ services: NewServiceRegistry(&fakeDHT{}),
+ }
+ socketPath := filepath.Join(t.TempDir(), "sam.sock")
+
+ srv, err := StartSidecarServer(node, "127.0.0.1:0", socketPath, "test-token", "", "", "")
+ if err != nil {
+ t.Fatalf("failed to start sidecar server: %v", err)
+ }
+ defer func() { _ = srv.Close() }()
+
+ client := waitForSocket(t, socketPath)
+
+ resp, err := client.Get("http://localhost/metrics")
+ if err != nil {
+ t.Fatalf("socket metrics request failed: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+ if resp.StatusCode != http.StatusOK {
+ t.Errorf("untokened socket scrape: got status %d, want %d", resp.StatusCode, http.StatusOK)
+ }
+
+ tcpClient := &http.Client{Timeout: 2 * time.Second}
+ tcpResp, err := tcpClient.Get("http://" + node.BoundHTTPAddr + "/metrics")
+ if err != nil {
+ t.Fatalf("tcp metrics request failed: %v", err)
+ }
+ defer func() { _ = tcpResp.Body.Close() }()
+ if tcpResp.StatusCode != http.StatusUnauthorized {
+ t.Errorf("untokened TCP scrape: got status %d, want %d", tcpResp.StatusCode, http.StatusUnauthorized)
+ }
+}
diff --git a/internal/node/sidecar_test.go b/internal/node/sidecar_test.go
index 6118519e..62ad8dbb 100644
--- a/internal/node/sidecar_test.go
+++ b/internal/node/sidecar_test.go
@@ -404,6 +404,8 @@ func TestSidecarServerAuthEnforcement(t *testing.T) {
}{
{"healthz is public", "GET", "/healthz", http.StatusOK, false},
{"readyz is public", "GET", "/readyz", http.StatusOK, false},
+ // Liveness says nothing; metrics name peers and count their traffic.
+ {"metrics is protected", "GET", "/metrics", http.StatusUnauthorized, false},
{"register is protected", "POST", "/sam/service/register", http.StatusUnauthorized, false},
{"unregister is protected", "POST", "/sam/service/unregister", http.StatusUnauthorized, false},
{"discover is protected", "GET", "/sam/service/discover?type=mcp&name=test", http.StatusUnauthorized, false},
@@ -411,6 +413,7 @@ func TestSidecarServerAuthEnforcement(t *testing.T) {
{"mcp root is protected", "GET", "/mcp", http.StatusUnauthorized, false},
{"register with token (bad req)", "POST", "/sam/service/register", http.StatusServiceUnavailable, true},
+ {"metrics with token", "GET", "/metrics", http.StatusOK, true},
{"unregister with token (bad req)", "POST", "/sam/service/unregister", http.StatusServiceUnavailable, true},
// /sam/service/discover without mesh connection will return 503 instead of 400 since node is not connected,
// but as long as it gets past auth, that's what we want to verify.