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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/httpapi/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,12 +747,25 @@ func (s *Server) handleActivitySummary(w http.ResponseWriter, r *http.Request) {
otherCount++
}

// Count by server
// Count by server / by tool — UPSTREAM traffic only.
//
// Issue #1146 gave the management built-ins a target_server so the
// Activity Log could render a Server column and --server could filter
// on it. These two lists answer a different question ("which upstreams
// is this proxy talking to"), and they used to skip those rows only by
// the accident of an empty ServerName. Excluding them explicitly keeps
// a burst of config edits from reading as traffic to the server being
// configured, and keeps "github:upstream_servers" — a built-in no
// upstream owns — out of the top-tools list. The rows stay in
// totalCount: they are real activity, just not upstream traffic.
if storage.IsManagementBuiltin(a) {
continue
}

if a.ServerName != "" {
serverCounts[a.ServerName]++
}

// Count by tool (server:tool)
if a.ServerName != "" && a.ToolName != "" {
key := a.ServerName + ":" + a.ToolName
toolCounts[key]++
Expand Down
96 changes: 96 additions & 0 deletions internal/httpapi/activity_summary_management_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package httpapi

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
)

// Cross-model review of #1146, finding 3. Giving the management built-ins a
// target_server fixed the Activity Log's Server column — and, as a side effect,
// let their rows into /api/v1/activity/summary's per-server aggregation, which
// previously skipped them only by the accident of an empty ServerName.
//
// top_servers / top_tools answer "which upstreams is this proxy talking to".
// upstream_servers and quarantine_security are mcpproxy's own housekeeping —
// the population storage.CountsAsCall already excludes by name — so a burst of
// config edits must not read as traffic to the server being configured, and
// "github:upstream_servers" is not a tool github owns.
func TestActivitySummary_ManagementChatterIsNotUpstreamTraffic(t *testing.T) {
const (
realCalls = 3
managementRows = 10
managementTool = "upstream_servers"
quarantineTool = "quarantine_security"
quarantineCount = 4
)
now := time.Now().UTC()

var activities []*storage.ActivityRecord
for i := 0; i < realCalls; i++ {
activities = append(activities, &storage.ActivityRecord{
ID: fmt.Sprintf("call-%d", i), Type: storage.ActivityTypeToolCall,
ServerName: "github", ToolName: "search",
Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute),
})
}
for i := 0; i < managementRows; i++ {
activities = append(activities, &storage.ActivityRecord{
ID: fmt.Sprintf("mgmt-%d", i), Type: storage.ActivityTypeInternalToolCall,
ServerName: "github", ToolName: managementTool,
Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute),
})
}
for i := 0; i < quarantineCount; i++ {
activities = append(activities, &storage.ActivityRecord{
ID: fmt.Sprintf("quar-%d", i), Type: storage.ActivityTypeInternalToolCall,
ServerName: "github", ToolName: quarantineTool,
Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute),
})
}

srv := NewServer(&summaryScopeController{activities: activities}, zap.NewNop().Sugar(), nil)

req := httptest.NewRequest(http.MethodGet, "/api/v1/activity/summary?period=24h", nil)
req.Header.Set("X-API-Key", "test-key")
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)

var resp struct {
Success bool `json:"success"`
Data contracts.ActivitySummaryResponse `json:"data"`
}
require.NoError(t, json.NewDecoder(w.Body).Decode(&resp))
require.True(t, resp.Success)

// Every row is still in the log and still in the denominator.
assert.Equal(t, realCalls+managementRows+quarantineCount, resp.Data.TotalCount,
"the management rows are real activity and must stay in the total")

require.Len(t, resp.Data.TopServers, 1)
assert.Equal(t, "github", resp.Data.TopServers[0].Name)
assert.Equal(t, realCalls, resp.Data.TopServers[0].Count,
"config edits to a server are not traffic to it")

for _, tool := range resp.Data.TopTools {
assert.NotEqual(t, managementTool, tool.Tool,
"upstream_servers is mcpproxy's built-in, not a tool the upstream owns")
assert.NotEqual(t, quarantineTool, tool.Tool,
"quarantine_security is mcpproxy's built-in, not a tool the upstream owns")
}
require.Len(t, resp.Data.TopTools, 1)
assert.Equal(t, "github", resp.Data.TopTools[0].Server)
assert.Equal(t, "search", resp.Data.TopTools[0].Tool)
assert.Equal(t, realCalls, resp.Data.TopTools[0].Count)
}
114 changes: 108 additions & 6 deletions internal/oauth/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ var sensitiveHeaderSegments = map[string]bool{

var headerNameSegmentPattern = regexp.MustCompile(`[a-z0-9]+`)

// headerNameCamelSegmentPattern splits a CamelCase header name on its case
// transitions. Issue #1146 (review round 3): the delimiter-based pass above
// only ever sees whole `-`/`_`-separated segments, so `X-AuthToken` collapsed
// to the single segment "authtoken", matched nothing, and a credential under a
// custom header name was recorded and logged in the clear. Splitting on case
// transitions as well recovers "Auth" and "Token" without reopening the
// substring false positives the segment rule exists to prevent: "Author" and
// "Monkey" are still single segments that equal no marker.
//
// Only mixed-case words are extracted here — an all-caps run like
// `X-CUSTOM-TOKEN` has no case transition to split on and is already covered by
// the delimiter pass.
var headerNameCamelSegmentPattern = regexp.MustCompile(`[A-Z][a-z0-9]*`)

// IsSensitiveHeaderName reports whether an HTTP header NAME looks like it
// carries a credential. Exported for callers that must decide masking from a
// name they hold apart from the value (the activity/audit redactor, issue
// #1146) so the rule stays defined in exactly one place.
func IsSensitiveHeaderName(name string) bool {
return isSensitiveHeaderKey(name)
}

func isSensitiveHeaderKey(name string) bool {
if sensitiveHeaders[strings.ToLower(name)] {
return true
Expand All @@ -58,6 +80,12 @@ func isSensitiveHeaderKey(name string) bool {
}
}

for _, segment := range headerNameCamelSegmentPattern.FindAllString(name, -1) {
if sensitiveHeaderSegments[strings.ToLower(segment)] {
return true
}
}

return false
}

Expand Down Expand Up @@ -159,13 +187,28 @@ func RedactHeaders(headers http.Header) map[string]string {
// "Convert to secret" affordance work on the UI side because the user
// can confirm a recognisable suffix before approving.
func RedactStringHeaders(headers map[string]string) map[string]string {
return RedactStringHeadersWith(headers, MaskValue)
}

// RedactStringHeadersWith is RedactStringHeaders with a caller-chosen mask.
//
// Issue #1146 (review round 3): MaskValue's `••••<last2> (<N> chars)` rendering
// is a deliberate trade for the INTERACTIVE surfaces — it lets an operator
// recognise which token is configured, and the write path (UnmaskHeaders)
// depends on being able to recognise it when a client echoes it back. On the
// AUDIT surfaces that trade is wrong: those rows are persisted, streamed and
// exported, so the length and trailing bytes become a durable fingerprint of
// every credential. Those callers pass AuditMaskValue instead. Splitting the
// mask out — rather than forking the "which name is sensitive" rules — keeps
// one definition of sensitivity behind both renderings.
func RedactStringHeadersWith(headers map[string]string, mask func(string) string) map[string]string {
if headers == nil {
return nil
}
redacted := make(map[string]string, len(headers))
for key, value := range headers {
if isSensitiveHeaderKey(key) {
redacted[key] = MaskValue(value)
redacted[key] = mask(value)
} else {
redacted[key] = RedactSensitiveData(value)
}
Expand All @@ -191,6 +234,16 @@ var sensitiveEnvMarkers = []string{
"DSN", "CONNECTION_STRING", "CONN_STR",
}

// IsSensitiveKeyName reports whether a NAME looks like it holds a secret, using
// the same case-insensitive marker match RedactEnvValues applies to env-var
// keys. Exported for callers that have to decide masking from a name they hold
// separately from the value — e.g. a command-line flag naming the argv token
// that follows it (issue #1146), where there is no map to hand to
// RedactEnvValues.
func IsSensitiveKeyName(name string) bool {
return isSensitiveEnvKey(name)
}

// isSensitiveEnvKey reports whether an env var name looks like it holds a
// secret, based on a case-insensitive substring match against
// sensitiveEnvMarkers.
Expand Down Expand Up @@ -218,12 +271,18 @@ func isSensitiveEnvKey(name string) bool {
// with a RedactSensitiveData pass over the value as a defence-in-depth fallback
// for embedded secrets (it leaves ordinary values like `debug` untouched).
func RedactEnvValues(env map[string]string) map[string]string {
return RedactEnvValuesWith(env, MaskValue)
}

// RedactEnvValuesWith is RedactEnvValues with a caller-chosen mask. See
// RedactStringHeadersWith for why the audit surfaces need a different one.
func RedactEnvValuesWith(env map[string]string, mask func(string) string) map[string]string {
if env == nil {
return nil
}
redacted := make(map[string]string, len(env))
for key, value := range env {
redacted[key] = maskedEnvValue(key, value)
redacted[key] = maskedEnvValueWith(key, value, mask)
}
return redacted
}
Expand All @@ -239,11 +298,15 @@ func RedactEnvValues(env map[string]string) map[string]string {
// embedded userinfo password and any credential query params are masked while
// scheme/host/db stay readable.
func maskedEnvValue(key, value string) string {
return maskedEnvValueWith(key, value, MaskValue)
}

func maskedEnvValueWith(key, value string, mask func(string) string) string {
if isSensitiveEnvKey(key) {
return MaskValue(value)
return mask(value)
}
if strings.Contains(value, "://") {
return RedactURLQueryParams(value)
return RedactURLQueryParamsWith(value, mask)
}
return RedactSensitiveData(value)
}
Expand Down Expand Up @@ -302,6 +365,12 @@ func isSensitiveQueryParam(name string) bool {
// are labels, not secrets. A URL with no query, or no sensitive params, is
// returned unchanged. On parse failure it falls back to the regex RedactURL.
func RedactURLQueryParams(rawURL string) string {
return RedactURLQueryParamsWith(rawURL, MaskValue)
}

// RedactURLQueryParamsWith is RedactURLQueryParams with a caller-chosen mask.
// See RedactStringHeadersWith for why the audit surfaces need a different one.
func RedactURLQueryParamsWith(rawURL string, mask func(string) string) string {
if rawURL == "" {
return rawURL
}
Expand All @@ -318,7 +387,7 @@ func RedactURLQueryParams(rawURL string) string {
// below.
if u.User != nil {
if pw, hasPW := u.User.Password(); hasPW && !isConfigReference(pw) {
u.User = url.UserPassword(u.User.Username(), MaskValue(pw))
u.User = url.UserPassword(u.User.Username(), mask(pw))
changed = true
}
}
Expand Down Expand Up @@ -351,7 +420,7 @@ func RedactURLQueryParams(rawURL string) string {
if isConfigReference(decVal) {
continue
}
parts[i] = key + "=" + url.QueryEscape(MaskValue(decVal))
parts[i] = key + "=" + url.QueryEscape(mask(decVal))
queryChanged = true
}
if queryChanged {
Expand Down Expand Up @@ -382,6 +451,39 @@ func isConfigReference(v string) bool {
return configRefPattern.MatchString(v)
}

// IsConfigReference is the exported form of isConfigReference, for callers
// outside this package that must decide whether a value is a secret-store
// LABEL rather than a secret (issue #1146: the activity redactor runs a
// value-shaped detector over everything the name rules pass through, and a
// reference must not be mangled by it).
func IsConfigReference(v string) bool {
return isConfigReference(v)
}

// AuditMaskValue is the mask for surfaces that PERSIST and EXPORT what they
// redact — the activity store, its SSE payloads and `mcpproxy activity list`
// (issue #1146, review round 3).
//
// Unlike MaskValue it carries no length and no trailing bytes. On an
// interactive surface those help an operator recognise which token is
// configured and are re-read within seconds; written into an audit row they
// become a durable fingerprint of the credential — a correlation handle across
// records and a materially smaller search space for a low-entropy secret. The
// audit surfaces have no need for the affordance, so they do not pay for it.
//
// ${keyring:…} / ${env:…} references still pass through unchanged: they are
// labels pointing at the secret store, and masking them would erase exactly the
// information the audit row exists to carry.
func AuditMaskValue(v string) string {
if v == "" {
return "(empty)"
}
if isConfigReference(v) {
return v
}
return "••••"
}

// MaskValue renders a string secret as `••••<last2> (<N> chars)` for
// human display. Returns "(empty)" for empty input, a 4-bullet preview
// for values up to 4 characters (where revealing the last two would
Expand Down
Loading
Loading