Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(endpoint): compare the collector port as a number, not a substring

shukieshah0cbb422863fa
diff --git a/cli/beacon/cmd/endpoint_console_user_config_test.go b/cli/beacon/cmd/endpoint_console_user_config_test.go
index 70098dd5..655d22af 100644
--- a/cli/beacon/cmd/endpoint_console_user_config_test.go
+++ b/cli/beacon/cmd/endpoint_console_user_config_test.go
@@ -117,6 +117,41 @@ func TestConsoleUserConfigCheckAcceptsLocalhostSpelling(t *testing.T) {
}
}
+// A port is a number, not a string prefix. Matching `host:port` as a substring has no right-hand
+// boundary, so an endpoint on port 431 would accept a file pointing at 4317 -- a false OK on the
+// check that is trusted when it says the user *is* captured. That direction is the dangerous one:
+// a false failure sends someone to run a repair, a false pass tells them a silent capture gap is
+// fine.
+func TestConsoleUserConfigCheckDoesNotMatchAPortPrefix(t *testing.T) {
+ settings := `{"env":{"OTEL_EXPORTER_OTLP_ENDPOINT":"http://127.0.0.1:4317"}}`
+
+ for name, tc := range map[string]struct {
+ grpc, http int
+ want bool
+ }{
+ "exact port matches": {4317, 4318, true},
+ "prefix of the configured port": {431, 432, false},
+ "configured port is longer": {43170, 43180, false},
+ "unrelated ports": {9999, 9998, false},
+ } {
+ t.Run(name, func(t *testing.T) {
+ if got := targetsCollectorPort(settings, tc.grpc, tc.http); got != tc.want {
+ t.Errorf("targetsCollectorPort(:%d/:%d) = %v, want %v", tc.grpc, tc.http, got, tc.want)
+ }
+ })
+ }
+}
+
+// The three loopback spellings a runtime config could legitimately carry, all against the same
+// collector.
+func TestConsoleUserConfigCheckAcceptsEveryLoopbackSpelling(t *testing.T) {
+ for _, addr := range []string{"127.0.0.1:4317", "localhost:4317", "[::1]:4317"} {
+ if !targetsCollectorPort(`{"endpoint":"http://`+addr+`"}`, 4317, 4318) {
+ t.Errorf("%s did not read as pointed at the collector", addr)
+ }
+ }
+}
+
// Hook-only runtimes count as configured: the installed hook command names the hook binary, which
// is what points that runtime at the endpoint.
func TestConsoleUserConfigCheckAcceptsAHookOnlyRuntime(t *testing.T) {
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index fbfc9322..dd997b4b 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "regexp"
"strconv"
"strings"
"time"
@@ -1127,7 +1128,7 @@ func consoleUserHarnessConfigured(info consoleUserInfo, cfg endpointconfig.Confi
if len(paths) == 0 {
return false, "no agent runtime configuration found in " + info.HomeDir
}
- markers := collectorEndpointMarkers(cfg)
+ grpc, http := collectorPorts(cfg)
for _, p := range paths {
data, err := os.ReadFile(p)
if err != nil {
@@ -1140,20 +1141,43 @@ func consoleUserHarnessConfigured(info consoleUserInfo, cfg endpointconfig.Confi
if strings.Contains(text, embedded.BinaryStem) {
return true, p
}
- for _, marker := range markers {
- if strings.Contains(text, marker) {
- return true, p
- }
+ if targetsCollectorPort(text, grpc, http) {
+ return true, p
}
}
return false, "found " + strconv.Itoa(len(paths)) + " runtime config file(s), none pointed at this collector"
}
-// collectorEndpointMarkers are the address spellings a configured runtime could contain.
+// loopbackEndpointPattern finds loopback addresses and captures the port as a whole number.
+//
+// All three spellings, because Beacon writes 127.0.0.1 but a user or an MDM profile may have
+// written localhost or ::1 against the same collector, and reporting that as unconfigured would be
+// wrong.
+var loopbackEndpointPattern = regexp.MustCompile(`(?:127\.0\.0\.1|\[::1\]|localhost):(\d+)`)
+
+// targetsCollectorPort reports whether the text points at this endpoint's collector.
//
-// Both loopback spellings, because Beacon writes 127.0.0.1 but a user or an MDM profile may have
-// written localhost against the same collector, and reporting that as unconfigured would be wrong.
-func collectorEndpointMarkers(cfg endpointconfig.Config) []string {
+// The port is captured and compared as a number rather than matched as a substring. A substring
+// has no right-hand boundary, so an endpoint configured on port 431 would match a file pointing at
+// 4317 -- a false OK on the one check that is being trusted when it says the user *is* captured,
+// which is the direction that matters most here: a false failure sends someone to run a repair,
+// while a false pass tells them a silent capture gap is fine.
+func targetsCollectorPort(text string, grpc, http int) bool {
+ for _, match := range loopbackEndpointPattern.FindAllStringSubmatch(text, -1) {
+ port, err := strconv.Atoi(match[1])
+ if err != nil {
+ continue
+ }
+ if port == grpc || port == http {
+ return true
+ }
+ }
+ return false
+}
+
+// collectorPorts resolves the ports this endpoint actually listens on, falling back to the
+// defaults for a configuration that predates them being recorded.
+func collectorPorts(cfg endpointconfig.Config) (int, int) {
grpc := cfg.Collector.GRPCPort
if grpc == 0 {
grpc = endpointconfig.DefaultGRPCPort
@@ -1162,14 +1186,7 @@ func collectorEndpointMarkers(cfg endpointconfig.Config) []string {
if http == 0 {
http = endpointconfig.DefaultHTTPPort
}
- var markers []string
- for _, host := range []string{"127.0.0.1", "localhost"} {
- markers = append(markers,
- host+":"+strconv.Itoa(grpc),
- host+":"+strconv.Itoa(http),
- )
- }
- return markers
+ return grpc, http
}
// consoleUserConfigPaths lists the runtime configuration files that exist in a specific user's