From 908c583bfa9e73edcfe6eca44c6f014c7c3a5073 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:31:51 +0530 Subject: [PATCH 1/8] feat(agent-vault): ask the service's hook before attaching the credential --- AGENTS.md | 4 + packages/agentvault/cache.go | 39 +- packages/agentvault/cache_test.go | 35 ++ packages/agentvault/connect.go | 2 +- packages/agentvault/discovery.go | 9 + packages/agentvault/hook.go | 547 ++++++++++++++++++++ packages/agentvault/hook_test.go | 401 ++++++++++++++ packages/agentvault/mcp_tools.go | 94 +++- packages/agentvault/mcp_tools_test.go | 141 +++++ packages/agentvault/native.go | 60 ++- packages/agentvault/native_logger.go | 11 +- packages/agentvault/native_logger_test.go | 4 +- packages/agentvault/native_proxy_test.go | 69 +++ packages/agentvault/proxy.go | 23 +- packages/agentvault/proxy_plaintext_test.go | 21 + packages/agentvault/proxy_policy_test.go | 210 ++++++++ packages/agentvault/resolve.go | 11 + packages/agentvault/resolve_client_test.go | 41 ++ packages/agentvault/session_endpoints.go | 4 +- packages/agentvault/session_log_spool.go | 5 + packages/agentvault/session_log_test.go | 13 + packages/agentvault/socks5_e2e_test.go | 18 + packages/agentvault/upstream_auth_test.go | 24 +- packages/api/agent_vault.go | 38 +- 24 files changed, 1780 insertions(+), 44 deletions(-) create mode 100644 packages/agentvault/hook.go create mode 100644 packages/agentvault/hook_test.go diff --git a/AGENTS.md b/AGENTS.md index e9c9fd06b..0b3010087 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,6 +36,10 @@ Short commands use `Telemetry.CaptureEvent`, which flushes before exiting. Use ` Never write: narration restating the next line; section headers inside a function; change history; references to plans, tickets, PRs, or reviewers; docstrings restating the signature; commented-out code. +### Agent Vault hooks + +A service can have a hook (`packages/agentvault/hook.go`): the customer's server, which the proxy asks before it brokers. Every request to a hooked HTTP or MCP service asks it, in `forward()` after the proxy's own checks (policy, plain-HTTP refusals, tool allowlist) and before substitutions, custom headers and the credential, so the payload only ever holds what the agent sent. A database or SSH service asks once per connection, after `registerNative` and before the tunnel opens. It fails closed: anything but an `allow` or `deny` answer within 3 seconds refuses the request, and a hook Infisical sent that the proxy can't use stays a hook that always fails, never no hook. Keep any new path that attaches a credential behind the same call, and never log the hook URL beyond `scheme://host`. + ## Issue and PR guidelines - Never create a GitHub issue. diff --git a/packages/agentvault/cache.go b/packages/agentvault/cache.go index 3ed3c36c3..47da44198 100644 --- a/packages/agentvault/cache.go +++ b/packages/agentvault/cache.go @@ -63,6 +63,7 @@ type resolvedService struct { id string name string kind string + accessBundleID string accessBundleName string // As Infisical sent it, so a credential refresh can name the destination the rejected token went to. hostPattern string @@ -82,6 +83,8 @@ type resolvedService struct { native *nativeTarget // Some credential field comes from each member, so a rejection is theirs to fix rather than an admin's. memberProvided bool + // Asked before the credential is attached. Nil when the service has none. + hook *serviceHook } // lastSeen and fetchedAt are wall-clock (Round(0)) so time asleep counts toward the grace and idle windows. @@ -92,8 +95,10 @@ type sessionEntry struct { expiresAt *time.Time services []*resolvedService sessionLog *sessionLogGrant - lastSeen time.Time - fetchedAt time.Time + // What a hook is told about the session. Nil from servers that don't send it. + session *api.AgentVaultResolvedSession + lastSeen time.Time + fetchedAt time.Time // Keyed by service ID. A poll whose resolve began before a swap can return the token that swap replaced, // so the merge consults this to keep the newer one. swaps map[string]*credentialSwap @@ -245,23 +250,25 @@ func isSessionGone(err error) bool { } func (c *sessionCache) get(sessionToken string) ([]*resolvedService, error) { - services, _, err := c.lookup(sessionToken, nil) - return services, err + found, err := c.lookup(sessionToken, nil) + return found.services, err } // getAdmitted is get for a caller that rations lookups of tokens it hasn't seen: admit runs only when Infisical // would be asked, so a known session, or one already refused, never spends from the budget. func (c *sessionCache) getAdmitted(sessionToken string, admit func() bool) ([]*resolvedService, error) { - services, _, err := c.lookup(sessionToken, admit) - return services, err + found, err := c.lookup(sessionToken, admit) + return found.services, err } type cacheLookup struct { + sessionID string services []*resolvedService sessionLog *sessionLogGrant + session *api.AgentVaultResolvedSession } -func (c *sessionCache) lookup(sessionToken string, admit func() bool) ([]*resolvedService, *sessionLogGrant, error) { +func (c *sessionCache) lookup(sessionToken string, admit func() bool) (cacheLookup, error) { key := sessionKey(sessionToken) c.mu.Lock() @@ -270,22 +277,22 @@ func (c *sessionCache) lookup(sessionToken string, admit func() bool) ([]*resolv if entry.expiresAt != nil && time.Now().After(*entry.expiresAt) { c.forgetLocked(key, true) c.mu.Unlock() - return nil, nil, errSessionGone + return cacheLookup{}, errSessionGone } // Past the grace window the entry is a miss, so a stalled refresh loop cannot keep an old credential alive. if time.Since(entry.fetchedAt) > c.grace() { c.forgetLocked(key, false) } else { entry.lastSeen = time.Now().Round(0) - svcs, grant := entry.services, entry.sessionLog + found := cacheLookup{sessionID: entry.sessionID, services: entry.services, sessionLog: entry.sessionLog, session: entry.session} c.mu.Unlock() - return svcs, grant, nil + return found, nil } } if refused, ok := c.refused[key]; ok { if time.Now().Before(refused.until) { c.mu.Unlock() - return nil, nil, refused.err + return cacheLookup{}, refused.err } delete(c.refused, key) } @@ -317,18 +324,18 @@ func (c *sessionCache) lookup(sessionToken string, admit func() bool) ([]*resolv expiresAt: result.ExpiresAt, services: result.Services, sessionLog: result.SessionLog, + session: result.Session, lastSeen: time.Now().Round(0), fetchedAt: time.Now().Round(0), } c.tokens[key] = sessionToken c.applyNativeLocked(key, result.Services, result.ExpiresAt) - return cacheLookup{services: result.Services, sessionLog: result.SessionLog}, nil + return cacheLookup{sessionID: result.SessionID, services: result.Services, sessionLog: result.SessionLog, session: result.Session}, nil }) if err != nil { - return nil, nil, err + return cacheLookup{}, err } - out := resolved.(cacheLookup) - return out.services, out.sessionLog, nil + return resolved.(cacheLookup), nil } func (c *sessionCache) evictIfFullLocked() { @@ -440,6 +447,7 @@ func (c *sessionCache) refreshOne(key, token string) { entry.expiresAt = result.ExpiresAt entry.services = keepNewerSwaps(entry, result.Services, startGen) entry.sessionLog = result.SessionLog + entry.session = result.Session entry.fetchedAt = time.Now().Round(0) c.applyNativeLocked(key, entry.services, entry.expiresAt) return @@ -451,6 +459,7 @@ func (c *sessionCache) refreshOne(key, token string) { expiresAt: result.ExpiresAt, services: result.Services, sessionLog: result.SessionLog, + session: result.Session, lastSeen: time.Now().Round(0), fetchedAt: time.Now().Round(0), } diff --git a/packages/agentvault/cache_test.go b/packages/agentvault/cache_test.go index 49bdb2be7..031b3832a 100644 --- a/packages/agentvault/cache_test.go +++ b/packages/agentvault/cache_test.go @@ -399,3 +399,38 @@ func TestCacheTimesIgnoreTheSleepingClock(t *testing.T) { t.Fatal("a refresh stamps fetchedAt with a monotonic reading") } } + +func TestTheSessionAHookIsToldAboutIsKeptAndRefreshed(t *testing.T) { + label := "Nightly refunds" + first := &api.AgentVaultResolvedSession{Label: &label, Actor: api.AgentVaultSessionActor{Type: "machineIdentity", ID: "a-1", Name: "agent"}} + svc := serviceWithSecret("v") + svc.hook = &serviceHook{secret: []byte("s")} + resolver := &stubResolver{result: &resolveResult{SessionID: "s1", Services: []*resolvedService{svc}, Session: first}} + cache := newTestCache(resolver) + + found, err := cache.lookup("agv_token", nil) + if err != nil || found.session != first || found.sessionID != "s1" || found.services[0].hook != svc.hook { + t.Fatalf("lookup = %+v, %v", found, err) + } + + second := &api.AgentVaultResolvedSession{Actor: api.AgentVaultSessionActor{Type: "user", ID: "u-1", Name: "Ana"}} + resolver.mu.Lock() + resolver.result = &resolveResult{SessionID: "s1", Services: []*resolvedService{svc}, Session: second} + resolver.mu.Unlock() + cache.refresh() + if found, _ := cache.lookup("agv_token", nil); found.session != second { + t.Fatalf("the refresh kept the old session info: %+v", found.session) + } +} + +func TestACredentialSwapKeepsThePolledHook(t *testing.T) { + polledHook := &serviceHook{secret: []byte("new")} + polled := &resolvedService{id: "svc-1", hostPattern: "h", credential: oauthToken("stale"), hook: polledHook} + entry := &sessionEntry{swaps: map[string]*credentialSwap{"svc-1": { + installed: oauthToken("fresh"), hostPattern: "h", replaced: map[string]bool{credentialHash(&polled.credential): true}, gen: 2, + }}} + got := keepNewerSwaps(entry, []*resolvedService{polled}, 1) + if string(got[0].credential.value) != "fresh" || got[0].hook != polledHook { + t.Fatalf("merged service = %+v", got[0]) + } +} diff --git a/packages/agentvault/connect.go b/packages/agentvault/connect.go index d700dae26..ba2dcac42 100644 --- a/packages/agentvault/connect.go +++ b/packages/agentvault/connect.go @@ -184,7 +184,7 @@ func (c *ConnectClient) connect(ctx context.Context, service string, probe bool) } func refusalOf(resp *http.Response) error { - body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048)) + body, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<10)) message := strings.TrimSpace(string(body)) // The proxy's refusal carries its text in a JSON body; a proxy too old to send one wrote the text itself. var coded proxyErrorBody diff --git a/packages/agentvault/discovery.go b/packages/agentvault/discovery.go index 5fd2f51dc..3dd26d136 100644 --- a/packages/agentvault/discovery.go +++ b/packages/agentvault/discovery.go @@ -225,6 +225,9 @@ func writeUncheckedProxyError(w http.ResponseWriter, status int, decision, code, func writeProxyErrorAPIURL(w http.ResponseWriter, status int, decision, code, message string, namesAPIURL bool) { w.Header().Set(HeaderDecision, decision) w.Header().Set(HeaderError, code) + if code == errorCodeHookFailed { + w.Header().Set("Retry-After", "5") + } if apiURL := proxyAPIURL(); apiURL != "" && namesAPIURL { w.Header().Set(HeaderAPIURL, apiURL) } @@ -270,6 +273,8 @@ const ( errorCodeInfisicalUnreachable = "infisical_unreachable" errorCodeServiceRefused = "service_refused" errorCodeTLSRequired = "tls_required" + errorCodeHookDenied = "hook_denied" + errorCodeHookFailed = "hook_failed" ) var proxyErrorHints = map[string]string{ @@ -291,4 +296,8 @@ var proxyErrorHints = map[string]string{ "different password or key will not help. " + servicesURLPlaceholder + " lists the session's services and their status.", errorCodeTLSRequired: "This is the Agent Vault proxy: database and SSH services are reached with infisical agent-vault connect, " + "which talks to the proxy over TLS.", + errorCodeHookDenied: "This is the Agent Vault proxy, not the API: your organization's hook refused this request. Do what its " + + "message says, including whether to try again.", + errorCodeHookFailed: "This is the Agent Vault proxy, not the API. Try again in a moment; if it keeps failing, an admin has to " + + "check the hook server.", } diff --git a/packages/agentvault/hook.go b/packages/agentvault/hook.go new file mode 100644 index 000000000..ef79ec166 --- /dev/null +++ b/packages/agentvault/hook.go @@ -0,0 +1,547 @@ +package agentvault + +import ( + "bytes" + "context" + "crypto/hmac" + "crypto/sha256" + "crypto/tls" + "crypto/x509" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" + "unicode" + "unicode/utf8" + + "github.com/Infisical/infisical-merge/packages/api" + "github.com/Infisical/infisical-merge/packages/util" + "github.com/google/uuid" + "github.com/rs/zerolog" +) + +const ( + hookPayloadVersion = "1" + + // Starts once the agent's body prefix is read, and covers the hook's whole answer, headers and body. + hookTimeout = 3 * time.Second + hookTLSHandshakeTimeout = 2 * time.Second + maxHookBodyPrefix = 1 << 20 + maxHookAnswer = 64 << 10 + maxHookMessage = 2 << 10 + maxHookReason = 512 + // connect reads 8 KB of a refusal, and the JSON encoder writes each <, > and & as six bytes. + maxNativeHookMessage = 1 << 10 + + hookAllow = "allow" + hookDeny = "deny" + hookFailed = "failed" + + hookCauseTimeout = "timeout" + hookCauseUnreachable = "unreachable" + hookCauseRedirect = "redirect" + hookCauseTooLarge = "too_large" + hookCauseInvalidJSON = "invalid_json" + hookCauseBadDecision = "bad_decision" + hookCauseBrokenConfig = "broken_config" + + hookDeniedRequestMessage = "Your organization's hook refused this request." + hookDeniedConnectionMessage = "Your organization's hook refused this connection." + hookFailedRequestMessage = "The hook server for this service didn't give a usable answer, so the request was not sent." + hookFailedConnectionMessage = "The hook server for this service didn't give a usable answer, so the connection was not opened." + hookFailedConnectionNote = "The hook didn't give a usable answer, so the connection wasn't opened." + hookDeniedConnectionNote = "The hook refused this connection" +) + +var ( + errHookDenied = errors.New("the service's hook refused the request") + errHookFailed = errors.New("the service's hook didn't give a usable answer") +) + +type serviceHook struct { + // Nil when the URL or secret Infisical sent can't be used, so every call fails rather than the hook being skipped. + url *url.URL + secret []byte +} + +func toServiceHook(wire *api.AgentVaultHook) *serviceHook { + if wire == nil { + return nil + } + hook := &serviceHook{secret: []byte(wire.Secret)} + parsed, err := url.Parse(wire.URL) + if err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && len(hook.secret) > 0 { + hook.url = parsed + } + return hook +} + +// Only the scheme and host are ever logged: a hook's path can carry a token. +func (h *serviceHook) origin() string { + if h.url == nil { + return "" + } + return h.url.Scheme + "://" + h.url.Host +} + +// Also what the session log records for a refusal, so nothing here may hold a header or body of the request. +type hookOutcome struct { + Decision string `json:"decision"` + Reason string `json:"reason,omitempty"` + Cause string `json:"cause,omitempty"` + DurationMs int64 `json:"durationMs"` + DeliveryID string `json:"deliveryId,omitempty"` + + message string + // Why the call failed, with the hook's URL taken out. + err error + origin string +} + +// Only a refusal is recorded: a row the hook allowed is described by what the upstream did. +func (o *hookOutcome) sessionLogHook() *hookOutcome { + if o == nil || o.Decision == hookAllow { + return nil + } + return o +} + +func (o *hookOutcome) logFields(event *zerolog.Event) *zerolog.Event { + event = event.Str("hook", o.Decision).Int64("hookMs", o.DurationMs).Str("deliveryId", o.DeliveryID) + if o.origin != "" { + event = event.Str("hookUrl", o.origin) + } + if o.Cause != "" { + event = event.Str("hookCause", o.Cause) + } + if o.Reason != "" { + event = event.Str("hookReason", o.Reason) + } + if o.err != nil { + event = event.AnErr("hookError", o.err) + } + return event +} + +type hookPayload struct { + Version string `json:"version"` + DeliveryID string `json:"deliveryId"` + Proxy hookPayloadProxy `json:"proxy"` + Session hookPayloadSession `json:"session"` + Service hookPayloadService `json:"service"` + Request *hookPayloadRequest `json:"request"` + MCP *hookPayloadMCP `json:"mcp"` + Connection *hookPayloadConnection `json:"connection"` +} + +type hookPayloadProxy struct { + ID string `json:"id"` + Version string `json:"version"` +} + +type hookPayloadSession struct { + ID string `json:"id"` + Label *string `json:"label"` + Actor *api.AgentVaultSessionActor `json:"actor"` + OnBehalfOf *api.AgentVaultSessionOnBehalfOf `json:"onBehalfOf"` +} + +type hookPayloadService struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + AccessBundle *hookPayloadBundle `json:"accessBundle"` +} + +type hookPayloadBundle struct { + ID string `json:"id"` + Name string `json:"name"` +} + +type hookPayloadRequest struct { + Method string `json:"method"` + Scheme string `json:"scheme"` + Host string `json:"host"` + Port string `json:"port"` + Path string `json:"path"` + Query string `json:"query"` + Headers map[string][]string `json:"headers"` + Body string `json:"body"` + BodyEncoding string `json:"bodyEncoding"` + BodyTruncated bool `json:"bodyTruncated"` +} + +type hookPayloadMCP struct { + Calls []hookMCPCall `json:"calls"` + Parsed bool `json:"parsed"` +} + +type hookMCPCall struct { + ID json.RawMessage `json:"id,omitempty"` + Method string `json:"method"` + Tool string `json:"tool,omitempty"` +} + +type hookPayloadConnection struct { + Host string `json:"host"` + Port string `json:"port"` + Database string `json:"database"` +} + +type hookAnswer struct { + Decision string `json:"decision"` + Message string `json:"message"` + Reason string `json:"reason"` +} + +type hookClient struct { + client *http.Client +} + +// Proxy is nil so a corporate proxy never sees the payload, which holds the agent's request body. +func newHookClient(rootCAs *x509.CertPool) *hookClient { + transport := &http.Transport{ + Proxy: nil, + DialContext: (&net.Dialer{Timeout: hookTimeout, KeepAlive: 30 * time.Second}).DialContext, + MaxIdleConnsPerHost: 64, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: hookTLSHandshakeTimeout, + TLSClientConfig: &tls.Config{RootCAs: rootCAs, MinVersion: tls.VersionTLS12}, + } + return &hookClient{client: &http.Client{ + Transport: transport, + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse + }, + }} +} + +// One pool for the life of the proxy, so hook calls reuse connections rather than paying a handshake each. +var sharedHookClient = sync.OnceValue(func() *hookClient { return newHookClient(nil) }) + +func (ps *proxyServer) newHookPayload(svc *resolvedService, found cacheLookup) hookPayload { + payload := hookPayload{ + Version: hookPayloadVersion, + Proxy: hookPayloadProxy{ID: ps.opts.ProxyID, Version: util.CLI_VERSION}, + Session: hookPayloadSession{ID: found.sessionID}, + Service: hookPayloadService{ID: svc.id, Name: svc.name, Type: toServiceKind(svc.kind)}, + } + if session := found.session; session != nil { + actor := session.Actor + payload.Session.Label, payload.Session.Actor, payload.Session.OnBehalfOf = session.Label, &actor, session.OnBehalfOf + } + if svc.accessBundleID != "" || svc.accessBundleName != "" { + payload.Service.AccessBundle = &hookPayloadBundle{ID: svc.accessBundleID, Name: svc.accessBundleName} + } + return payload +} + +// Standard Webhooks: the id and timestamp are signed with the body, so neither can be replaced on a replay. +func signHook(secret []byte, id, timestamp string, body []byte) string { + mac := hmac.New(sha256.New, secret) + mac.Write([]byte(id)) + mac.Write([]byte{'.'}) + mac.Write([]byte(timestamp)) + mac.Write([]byte{'.'}) + mac.Write(body) + return "v1," + base64.StdEncoding.EncodeToString(mac.Sum(nil)) +} + +// ask never returns nil, and anything but a well-formed allow or deny is a failure, so a hook can only let a +// request through by saying so. +func (c *hookClient) ask(parent context.Context, hook *serviceHook, payload *hookPayload) *hookOutcome { + start := time.Now() + id, err := uuid.NewV7() + if err != nil { + id = uuid.New() + } + out := &hookOutcome{DeliveryID: id.String(), origin: hook.origin()} + defer func() { out.DurationMs = time.Since(start).Milliseconds() }() + + fail := func(cause string, err error) *hookOutcome { + out.Decision, out.Cause, out.err = hookFailed, cause, err + return out + } + if hook.url == nil { + return fail(hookCauseBrokenConfig, errors.New("the hook URL or signing secret Infisical sent can't be used")) + } + + payload.DeliveryID = out.DeliveryID + body, ok := encodeJSON(payload) + if !ok { + return fail(hookCauseBrokenConfig, errors.New("the request could not be encoded for the hook")) + } + timestamp := strconv.FormatInt(time.Now().Unix(), 10) + + ctx, cancel := context.WithTimeout(parent, hookTimeout) + defer cancel() + // A bytes.Reader sets GetBody, which with Idempotency-Key lets Go resend on a pooled connection the hook closed. + req, err := http.NewRequestWithContext(ctx, http.MethodPost, hook.url.String(), bytes.NewReader(body)) + if err != nil { + return fail(hookCauseBrokenConfig, errors.New("the hook URL Infisical sent can't be used")) + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", "infisical-agent-vault-proxy/"+util.CLI_VERSION) + req.Header.Set("webhook-id", out.DeliveryID) + req.Header.Set("webhook-timestamp", timestamp) + req.Header.Set("webhook-signature", signHook(hook.secret, out.DeliveryID, timestamp, body)) + req.Header.Set("Idempotency-Key", out.DeliveryID) + + resp, err := c.client.Do(req) + if err != nil { + return fail(hookTransportCause(ctx, err)) + } + defer resp.Body.Close() + + // The Location header is never read: it is the hook server's URL, path and all. + if resp.StatusCode >= 300 && resp.StatusCode < 400 { + return fail(hookCauseRedirect, fmt.Errorf("it answered with a redirect (%d), which the proxy doesn't follow", resp.StatusCode)) + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return fail("status_"+strconv.Itoa(resp.StatusCode), fmt.Errorf("it answered with status %d", resp.StatusCode)) + } + raw, err := io.ReadAll(io.LimitReader(resp.Body, maxHookAnswer+1)) + if err != nil { + return fail(hookTransportCause(ctx, err)) + } + if len(raw) > maxHookAnswer { + return fail(hookCauseTooLarge, fmt.Errorf("its answer is larger than %d KB", maxHookAnswer>>10)) + } + var answer hookAnswer + if err := json.Unmarshal(raw, &answer); err != nil { + return fail(hookCauseInvalidJSON, errors.New("its answer isn't a JSON object with string fields")) + } + if answer.Decision != hookAllow && answer.Decision != hookDeny { + return fail(hookCauseBadDecision, errors.New(`its answer has no decision of "allow" or "deny"`)) + } + out.Decision = answer.Decision + out.message = cleanHookText(answer.Message, maxHookMessage) + out.Reason = cleanHookText(answer.Reason, maxHookReason) + return out +} + +// A url.Error names the whole hook URL, so only the error inside it is kept. +func hookTransportCause(ctx context.Context, err error) (string, error) { + var urlErr *url.Error + if errors.As(err, &urlErr) { + err = urlErr.Err + } + var netErr net.Error + if errors.Is(ctx.Err(), context.DeadlineExceeded) || (errors.As(err, &netErr) && netErr.Timeout()) { + return hookCauseTimeout, err + } + return hookCauseUnreachable, err +} + +// Control and invisible characters are dropped, line breaks become spaces, and the text is cut on a character +// boundary, so a hook's words can't forge log lines or hide text from whoever reads them. +func cleanHookText(s string, limit int) string { + var b strings.Builder + for _, r := range strings.ToValidUTF8(s, "") { + switch { + case r == '\n' || r == '\r' || r == '\t' || r == '
' || r == '
': + r = ' ' + case unicode.IsControl(r) || unicode.Is(unicode.Cf, r): + continue + } + if b.Len()+utf8.RuneLen(r) > limit { + break + } + b.WriteRune(r) + } + return strings.TrimSpace(b.String()) +} + +func capText(s string, limit int) string { + if len(s) <= limit { + return s + } + for limit > 0 && !utf8.RuneStart(s[limit]) { + limit-- + } + return s[:limit] +} + +// The agent's body up to limit bytes, and whether it holds more. The request keeps its whole body: one the proxy +// already holds is reused, one that fits is held so an OAuth retry can send it again, and a longer one is put back +// in front of the rest of the stream. +func hookBodyPrefix(req *http.Request, limit int) ([]byte, bool, error) { + if req.Body == nil || req.Body == http.NoBody { + return nil, false, nil + } + if buffered, ok := req.Body.(*bufferedBody); ok { + if len(buffered.data) > limit { + return buffered.data[:limit], true, nil + } + return buffered.data, false, nil + } + original := req.Body + read, err := io.ReadAll(io.LimitReader(original, int64(limit)+1)) + if err != nil { + req.Body = rechainedBody{Reader: io.MultiReader(bytes.NewReader(read), erroringReader{err}), Closer: original} + return nil, false, errBodyUnreadable + } + if len(read) > limit { + req.Body = rechainedBody{Reader: io.MultiReader(bytes.NewReader(read), original), Closer: original} + return read[:limit], true, nil + } + _ = original.Close() + if len(read) == 0 { + req.Body = http.NoBody + } else { + req.Body = &bufferedBody{Reader: bytes.NewReader(read), data: read} + } + return read, false, nil +} + +// UTF-8 text when the prefix is, else base64. A compressed body is always base64: its bytes are not the text. +func hookBodyText(prefix []byte, truncated bool, header http.Header) (string, string) { + encoding := header.Get("Content-Encoding") + if encoding == "" || strings.EqualFold(encoding, "identity") { + text := prefix + if truncated { + text = trimPartialRune(text) + } + if utf8.Valid(text) { + return string(text), "utf8" + } + } + return base64.StdEncoding.EncodeToString(prefix), "base64" +} + +// A prefix cut part way through a character would otherwise fail the UTF-8 check and go out as base64. +func trimPartialRune(b []byte) []byte { + for i := 1; i <= utf8.UTFMax && i <= len(b); i++ { + if utf8.RuneStart(b[len(b)-i]) { + if !utf8.FullRune(b[len(b)-i:]) { + return b[:len(b)-i] + } + return b + } + } + return b +} + +func hookHeaders(header http.Header) map[string][]string { + out := make(map[string][]string, len(header)) + for name, values := range header { + key := strings.ToLower(name) + out[key] = append(out[key], values...) + } + return out +} + +// The messages in a body the proxy read whole, so a refusal can answer each one. +func hookMCPOf(prefix []byte, truncated bool) (*hookPayloadMCP, []jsonrpcMessage) { + out := &hookPayloadMCP{Calls: []hookMCPCall{}} + if truncated { + return out, nil + } + messages, ok := parseJSONRPCMessages(prefix) + if !ok { + return out, nil + } + for _, message := range messages { + out.Calls = append(out.Calls, hookMCPCall{ID: message.id, Method: message.Method, Tool: message.Tool}) + } + out.Parsed = true + return out, messages +} + +// askHook runs after the proxy's own checks and before substitutions, custom headers and the credential, so the +// hook judges what the agent sent and never sees a value Infisical holds. +func (ps *proxyServer) askHook(req *http.Request, t tunnel, svc *resolvedService, found cacheLookup, outcome *forwardOutcome) error { + prefix, truncated, err := hookBodyPrefix(req, maxHookBodyPrefix) + if err != nil { + return err + } + body, encoding := hookBodyText(prefix, truncated, req.Header) + payload := ps.newHookPayload(svc, found) + payload.Request = &hookPayloadRequest{ + Method: req.Method, + Scheme: t.scheme, + Host: strings.ToLower(t.hostname), + Port: t.port, + Path: requestPath(req), + Query: req.URL.RawQuery, + Headers: hookHeaders(req.Header), + Body: body, + BodyEncoding: encoding, + BodyTruncated: truncated, + } + var messages []jsonrpcMessage + if svc.kind == serviceKindMCP { + payload.MCP, messages = hookMCPOf(prefix, truncated) + } + + result := sharedHookClient().ask(req.Context(), svc.hook, &payload) + outcome.hook = result + if result.Decision == hookAllow { + return nil + } + + denied := result.Decision == hookDeny + if denied && result.message == "" { + result.message = hookDeniedRequestMessage + } + // A JSON-RPC client needs a JSON-RPC answer; a GET for the event stream or a DELETE has no message to answer. + if svc.kind == serviceKindMCP && strings.EqualFold(req.Method, http.MethodPost) { + refusal := hookFailedMCPRefusal(messages, payload.MCP.Parsed, isBatch(prefix), streamableHTTPClient(req)) + if denied { + refusal = hookDeniedMCPRefusal(result.message, messages, payload.MCP.Parsed, isBatch(prefix), streamableHTTPClient(req)) + } + return ps.sseStreams.deliver(t.sessionToken, svc.id, req.URL, refusal) + } + if denied { + return errHookDenied + } + return errHookFailed +} + +func isBatch(body []byte) bool { + return bytes.HasPrefix(bytes.TrimSpace(body), []byte("[")) +} + +// Once per connection, before the tunnel opens. Open connections are not asked again. +func (ps *proxyServer) askNativeHook(ctx context.Context, svc *resolvedService, found cacheLookup) *hookOutcome { + payload := ps.newHookPayload(svc, found) + payload.Connection = &hookPayloadConnection{ + Host: svc.native.host, + Port: strconv.Itoa(svc.native.port), + Database: svc.native.database, + } + return sharedHookClient().ask(ctx, svc.hook, &payload) +} + +func nativeHookRefusal(result *hookOutcome) nativeRefusal { + if result.Decision == hookDeny { + body := capText(result.message, maxNativeHookMessage) + if body == "" { + body = hookDeniedConnectionMessage + } + note := hookDeniedConnectionNote + "." + if detail := firstNonEmpty(result.Reason, result.message); detail != "" { + note = hookDeniedConnectionNote + ": " + detail + } + return nativeRefusal{decision: decisionBlocked, status: http.StatusForbidden, code: errorCodeHookDenied, body: body, note: note, hook: result} + } + return nativeRefusal{decision: decisionError, status: http.StatusServiceUnavailable, code: errorCodeHookFailed, + body: hookFailedConnectionMessage, note: hookFailedConnectionNote, hook: result} +} + +func firstNonEmpty(values ...string) string { + for _, v := range values { + if v != "" { + return v + } + } + return "" +} diff --git a/packages/agentvault/hook_test.go b/packages/agentvault/hook_test.go new file mode 100644 index 000000000..4371b30dc --- /dev/null +++ b/packages/agentvault/hook_test.go @@ -0,0 +1,401 @@ +package agentvault + +import ( + "bytes" + "context" + "crypto/hmac" + "crypto/sha256" + "crypto/x509" + "encoding/base64" + "encoding/json" + "io" + "net" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "strings" + "sync" + "testing" + "time" + "unicode/utf8" + + "github.com/Infisical/infisical-merge/packages/api" + "github.com/google/uuid" + "github.com/rs/zerolog" +) + +const testHookSecret = "ascii-signing-secret-for-hooks-0123456789" + +type hookHit struct { + header http.Header + body []byte +} + +func (h hookHit) payload(t *testing.T) hookPayload { + t.Helper() + var p hookPayload + if err := json.Unmarshal(h.body, &p); err != nil { + t.Fatalf("the hook payload is not JSON (%v): %s", err, h.body) + } + return p +} + +// A hook server that records every call and answers with answer. +type fakeHook struct { + server *httptest.Server + mu sync.Mutex + hits []hookHit +} + +func newFakeHook(t *testing.T, answer http.HandlerFunc) *fakeHook { + t.Helper() + f := &fakeHook{} + f.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + f.mu.Lock() + f.hits = append(f.hits, hookHit{header: r.Header.Clone(), body: body}) + f.mu.Unlock() + answer(w, r) + })) + t.Cleanup(f.server.Close) + return f +} + +func (f *fakeHook) hook() *serviceHook { + u, _ := url.Parse(f.server.URL + "/agent-vault/hook") + return &serviceHook{url: u, secret: []byte(testHookSecret)} +} + +func (f *fakeHook) calls() []hookHit { + f.mu.Lock() + defer f.mu.Unlock() + return append([]hookHit(nil), f.hits...) +} + +func (f *fakeHook) only(t *testing.T) hookHit { + t.Helper() + calls := f.calls() + if len(calls) != 1 { + t.Fatalf("the hook was called %d times, want once", len(calls)) + } + return calls[0] +} + +func answerHook(body string) http.HandlerFunc { + return func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, body) + } +} + +func testHookPayload() *hookPayload { + return &hookPayload{Version: hookPayloadVersion, Service: hookPayloadService{ID: "svc-1", Name: "stripe", Type: serviceKindHTTP}} +} + +func TestTheHookSignatureMatchesStandardWebhooks(t *testing.T) { + // The Standard Webhooks reference vector, whose key is the base64 after whsec_. + key, _ := base64.StdEncoding.DecodeString("MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw") + if got := signHook(key, "msg_p5jXN8AQM9LWM0D4loKWxJek", "1614265330", []byte(`{"test": 2432232314}`)); got != "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=" { + t.Fatalf("signature = %s", got) + } + // An admin's secret is used as its own bytes, as the libraries' raw-key mode does. + got := signHook([]byte(testHookSecret), "018f3b4e-2a1c-7d10-9b2e-4c5d6e7f8091", "1731400000", []byte(`{"version":"1"}`)) + if got != "v1,Pj2Kn6ZHUNV4QQBD/0I/iER+DqTM4B6tGUwlrVb7W4g=" { + t.Fatalf("signature = %s", got) + } +} + +type getBodyRecorder struct { + next http.RoundTripper + mu sync.Mutex + seen bool + setAll bool +} + +func (r *getBodyRecorder) RoundTrip(req *http.Request) (*http.Response, error) { + r.mu.Lock() + r.seen, r.setAll = true, req.GetBody != nil + r.mu.Unlock() + return r.next.RoundTrip(req) +} + +func TestAHookCallIsSignedAndCanBeResent(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + client := newHookClient(nil) + recorder := &getBodyRecorder{next: client.client.Transport} + client.client.Transport = recorder + + before := time.Now().Unix() + result := client.ask(context.Background(), fake.hook(), testHookPayload()) + if result.Decision != hookAllow { + t.Fatalf("outcome = %+v (%v)", result, result.err) + } + if !recorder.seen || !recorder.setAll { + t.Fatal("the hook request had no GetBody, so Go can't resend it on a connection the hook closed") + } + + hit := fake.only(t) + id := hit.header.Get("webhook-id") + if parsed, err := uuid.Parse(id); err != nil || parsed.Version() != 7 { + t.Fatalf("webhook-id %q is not a uuid v7", id) + } + if hit.header.Get("Idempotency-Key") != id || hit.payload(t).DeliveryID != id || result.DeliveryID != id { + t.Fatalf("Idempotency-Key %q, deliveryId %q, outcome %q, want all %q", hit.header.Get("Idempotency-Key"), hit.payload(t).DeliveryID, result.DeliveryID, id) + } + ts := hit.header.Get("webhook-timestamp") + if n, err := strconv.ParseInt(ts, 10, 64); err != nil || n < before || n > time.Now().Unix() { + t.Fatalf("webhook-timestamp %q is not now in unix seconds", ts) + } + mac := hmac.New(sha256.New, []byte(testHookSecret)) + mac.Write([]byte(id + "." + ts + ".")) + mac.Write(hit.body) + if want := "v1," + base64.StdEncoding.EncodeToString(mac.Sum(nil)); hit.header.Get("webhook-signature") != want { + t.Fatalf("webhook-signature %q, want %q", hit.header.Get("webhook-signature"), want) + } + if hit.header.Get("Content-Type") != "application/json" || !strings.HasPrefix(hit.header.Get("User-Agent"), "infisical-agent-vault-proxy/") { + t.Fatalf("headers = %v", hit.header) + } +} + +// A hook server that drops idle connections is answered on a fresh one, with the same delivery and signature. +func TestAHookCallIsResentWhenThePooledConnectionWasClosed(t *testing.T) { + var mu sync.Mutex + var ids []string + requests := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.ReadAll(r.Body) + mu.Lock() + requests++ + n := requests + ids = append(ids, r.Header.Get("webhook-id")) + mu.Unlock() + if n == 2 { + conn, _, _ := w.(http.Hijacker).Hijack() + _ = conn.Close() + return + } + _, _ = io.WriteString(w, `{"decision":"allow"}`) + })) + defer srv.Close() + u, _ := url.Parse(srv.URL) + hook := &serviceHook{url: u, secret: []byte(testHookSecret)} + client := newHookClient(nil) + + for i := range 2 { + if result := client.ask(context.Background(), hook, testHookPayload()); result.Decision != hookAllow { + t.Fatalf("call %d: %+v (%v)", i, result, result.err) + } + } + mu.Lock() + defer mu.Unlock() + if len(ids) != 3 || ids[1] != ids[2] { + t.Fatalf("webhook-ids seen = %v, want the second call sent twice", ids) + } +} + +func TestAHookRedirectIsNotFollowed(t *testing.T) { + followed := false + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/elsewhere" { + followed = true + _, _ = io.WriteString(w, `{"decision":"allow"}`) + return + } + http.Redirect(w, r, "/elsewhere", http.StatusTemporaryRedirect) + })) + defer srv.Close() + u, _ := url.Parse(srv.URL + "/hook") + + result := newHookClient(nil).ask(context.Background(), &serviceHook{url: u, secret: []byte(testHookSecret)}, testHookPayload()) + if result.Decision != hookFailed || result.Cause != hookCauseRedirect || followed { + t.Fatalf("outcome = %+v, followed = %v", result, followed) + } +} + +func TestHookAnswersThatAreNotAllowOrDenyFail(t *testing.T) { + for _, tc := range []struct { + name string + answer http.HandlerFunc + cause string + }{ + {"server error", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) }, "status_500"}, + {"unauthorized", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusUnauthorized) }, "status_401"}, + {"not JSON", answerHook(`allow`), hookCauseInvalidJSON}, + {"empty", answerHook(``), hookCauseInvalidJSON}, + {"array", answerHook(`["allow"]`), hookCauseInvalidJSON}, + {"message not a string", answerHook(`{"decision":"allow","message":5}`), hookCauseInvalidJSON}, + {"no decision", answerHook(`{}`), hookCauseBadDecision}, + {"null", answerHook(`null`), hookCauseBadDecision}, + {"capitalised", answerHook(`{"decision":"Allow"}`), hookCauseBadDecision}, + {"unknown", answerHook(`{"decision":"maybe"}`), hookCauseBadDecision}, + {"over the cap", answerHook(`{"decision":"allow","reason":"` + strings.Repeat("a", maxHookAnswer) + `"}`), hookCauseTooLarge}, + } { + t.Run(tc.name, func(t *testing.T) { + fake := newFakeHook(t, tc.answer) + result := newHookClient(nil).ask(context.Background(), fake.hook(), testHookPayload()) + if result.Decision != hookFailed || result.Cause != tc.cause { + t.Fatalf("outcome = %+v (%v), want failed with %s", result, result.err, tc.cause) + } + }) + } +} + +func TestAHookAnswerJustUnderTheCapIsRead(t *testing.T) { + prefix, suffix := `{"decision":"deny","reason":"`, `"}` + fake := newFakeHook(t, answerHook(prefix+strings.Repeat("a", maxHookAnswer-len(prefix)-len(suffix))+suffix)) + result := newHookClient(nil).ask(context.Background(), fake.hook(), testHookPayload()) + if result.Decision != hookDeny || len(result.Reason) != maxHookReason { + t.Fatalf("outcome = %+v (%v)", result.Decision, result.err) + } +} + +func TestASlowHookTimesOut(t *testing.T) { + fake := newFakeHook(t, func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() }) + start := time.Now() + result := newHookClient(nil).ask(context.Background(), fake.hook(), testHookPayload()) + elapsed := time.Since(start) + if result.Decision != hookFailed || result.Cause != hookCauseTimeout { + t.Fatalf("outcome = %+v (%v)", result, result.err) + } + if elapsed < hookTimeout-100*time.Millisecond || elapsed > hookTimeout+2*time.Second { + t.Fatalf("the call took %v, want about %v", elapsed, hookTimeout) + } +} + +func TestAHookThatStallsAfterItsHeadersTimesOut(t *testing.T) { + fake := newFakeHook(t, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Length", "100") + w.WriteHeader(http.StatusOK) + _, _ = io.WriteString(w, `{"decision":`) + w.(http.Flusher).Flush() + <-r.Context().Done() + }) + result := newHookClient(nil).ask(context.Background(), fake.hook(), testHookPayload()) + if result.Decision != hookFailed || result.Cause != hookCauseTimeout { + t.Fatalf("outcome = %+v (%v)", result, result.err) + } +} + +func TestCancellingTheCallerEndsTheHookCall(t *testing.T) { + fake := newFakeHook(t, func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() }) + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(50*time.Millisecond, cancel) + start := time.Now() + result := newHookClient(nil).ask(ctx, fake.hook(), testHookPayload()) + if result.Decision != hookFailed || time.Since(start) > time.Second { + t.Fatalf("outcome = %+v after %v", result, time.Since(start)) + } +} + +func TestAHookInfisicalSentUnusableAlwaysFails(t *testing.T) { + for _, wire := range []struct{ url, secret string }{ + {"ftp://hooks.example.com/x", testHookSecret}, + {"https:///no-host", testHookSecret}, + {"://bad", testHookSecret}, + {"https://hooks.example.com/x", ""}, + } { + hook := toServiceHook(&api.AgentVaultHook{URL: wire.url, Secret: wire.secret}) + if hook == nil { + t.Fatalf("%+v mapped to no hook, which would skip it", wire) + } + result := newHookClient(nil).ask(context.Background(), hook, testHookPayload()) + if result.Decision != hookFailed || result.Cause != hookCauseBrokenConfig { + t.Fatalf("%+v: outcome = %+v", wire, result) + } + } + if toServiceHook(nil) != nil { + t.Fatal("a service with no hook got one") + } +} + +func TestAFailedHookCallLogsNoPathOrQuery(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + _ = ln.Close() + u, _ := url.Parse("http://" + addr + "/services/T123/tok-in-path?x=tok-in-query") + + result := newHookClient(nil).ask(context.Background(), &serviceHook{url: u, secret: []byte(testHookSecret)}, testHookPayload()) + if result.Decision != hookFailed || result.Cause != hookCauseUnreachable { + t.Fatalf("outcome = %+v (%v)", result, result.err) + } + var out bytes.Buffer + logger := zerolog.New(&out) + result.logFields(logger.Error()).Msg("agent-vault: request") + if strings.Contains(out.String(), "tok-in-path") || strings.Contains(out.String(), "tok-in-query") { + t.Fatalf("the hook URL's path reached the log: %s", out.String()) + } + if !strings.Contains(out.String(), `"hookUrl":"http://`+addr+`"`) || !strings.Contains(out.String(), `"hookCause":"unreachable"`) { + t.Fatalf("log line = %s", out.String()) + } +} + +func TestHookTextIsCleaned(t *testing.T) { + if got := cleanHookText(" ok\x00\x1b[31m​red‮\nnext\tline \xff", maxHookMessage); got != "ok[31mred next line" { + t.Fatalf("cleaned = %q", got) + } + long := cleanHookText(strings.Repeat("é", maxHookMessage), maxHookMessage) + if len(long) > maxHookMessage || !utf8.ValidString(long) || len(long) < maxHookMessage-1 { + t.Fatalf("cut to %d bytes, valid %v", len(long), utf8.ValidString(long)) + } + if got := capText("aé", 2); got != "a" { + t.Fatalf("capText cut inside a character: %q", got) + } +} + +func TestTheBodyPrefixKeepsTheWholeBodyForTheUpstream(t *testing.T) { + big := strings.Repeat("x", 10) + req := httptest.NewRequest(http.MethodPost, "/", io.NopCloser(strings.NewReader(big))) + prefix, truncated, err := hookBodyPrefix(req, 4) + if err != nil || !truncated || string(prefix) != "xxxx" { + t.Fatalf("prefix %q truncated %v err %v", prefix, truncated, err) + } + if rest, _ := io.ReadAll(req.Body); string(rest) != big { + t.Fatalf("the upstream would get %q", rest) + } + + req = httptest.NewRequest(http.MethodPost, "/", io.NopCloser(strings.NewReader("abc"))) + prefix, truncated, _ = hookBodyPrefix(req, 4) + if _, ok := req.Body.(*bufferedBody); !ok || truncated || string(prefix) != "abc" { + t.Fatalf("a body that fits should be held for a resend, got %T %q", req.Body, prefix) + } + if replay, ok := replayableBody(req); !ok || string(replay) != "abc" { + t.Fatal("a held body is not replayable") + } + again, truncated, _ := hookBodyPrefix(req, 2) + if string(again) != "ab" || !truncated { + t.Fatalf("a held body was read again as %q", again) + } +} + +func TestTheBodyIsSentAsTextOrBase64(t *testing.T) { + cut := []byte("h\xc3") + if body, encoding := hookBodyText(cut, true, http.Header{}); body != "h" || encoding != "utf8" { + t.Fatalf("a cut character made %q %s", body, encoding) + } + if _, encoding := hookBodyText(cut, false, http.Header{}); encoding != "base64" { + t.Fatal("a whole body that isn't UTF-8 went out as text") + } + if body, encoding := hookBodyText([]byte("plain"), false, http.Header{"Content-Encoding": {"gzip"}}); encoding != "base64" || body != base64.StdEncoding.EncodeToString([]byte("plain")) { + t.Fatalf("a compressed body went out as %s", encoding) + } +} + +func TestAnHTTPSHookIsVerifiedAgainstTheTrustedRoots(t *testing.T) { + srv := httptest.NewTLSServer(answerHook(`{"decision":"allow"}`)) + defer srv.Close() + u, _ := url.Parse(srv.URL) + hook := &serviceHook{url: u, secret: []byte(testHookSecret)} + + if result := newHookClient(nil).ask(context.Background(), hook, testHookPayload()); result.Decision != hookFailed || result.Cause != hookCauseUnreachable { + t.Fatalf("an untrusted certificate gave %+v", result) + } + pool := x509.NewCertPool() + pool.AddCert(srv.Certificate()) + if result := newHookClient(pool).ask(context.Background(), hook, testHookPayload()); result.Decision != hookAllow { + t.Fatalf("a trusted certificate gave %+v (%v)", result, result.err) + } +} diff --git a/packages/agentvault/mcp_tools.go b/packages/agentvault/mcp_tools.go index 0f1ef0c85..1ccc3ac7c 100644 --- a/packages/agentvault/mcp_tools.go +++ b/packages/agentvault/mcp_tools.go @@ -18,6 +18,8 @@ import ( const ( jsonrpcInvalidRequest = -32600 jsonrpcInvalidParams = -32602 + // Outside the range JSON-RPC and MCP reserve, which MCP asks of codes it doesn't define. + jsonrpcHookRefused = -31000 // Per message for a JSON response and per event for a stream. Anything larger is passed through as it // came, since a call to a hidden tool is still refused on the way in. @@ -33,11 +35,14 @@ const ( mcpNameHeader = "Mcp-Name" ) -// A request the service's tool allowlist refuses, answered by the proxy so the upstream never sees it. +// A request the service's tool allowlist or hook refuses, answered by the proxy so the upstream never sees it. type mcpToolRefusal struct { status int body []byte reason string + // Empty for the allowlist's own refusals, which are blocked and carry no code. + decision string + code string // For a legacy SSE client: the refusal can be sent on the event stream the server opened for this POST, // as the server's own answer would be. streamEvent is what goes on it, nil when there is nothing to // answer. @@ -48,6 +53,9 @@ type mcpToolRefusal struct { func (r *mcpToolRefusal) Error() string { return r.reason } func (r *mcpToolRefusal) write(w http.ResponseWriter) { + if r.code != "" { + w.Header().Set(HeaderError, r.code) + } if len(r.body) > 0 { w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Length", strconv.Itoa(len(r.body))) @@ -67,6 +75,22 @@ type jsonrpcError struct { Message string `json:"message"` } +type jsonrpcToolErrorResponse struct { + JSONRPC string `json:"jsonrpc"` + ID json.RawMessage `json:"id"` + Result mcpToolResult `json:"result"` +} + +type mcpToolResult struct { + IsError bool `json:"isError"` + Content []mcpTextContent `json:"content"` +} + +type mcpTextContent struct { + Type string `json:"type"` + Text string `json:"text"` +} + // Every POST to a service with an allowlist is read here, up to maxToolsAllowlistBody, whether or not session // logs are on, and refused whole when any call in it names a tool the list leaves out. A server may parse JSON // whatever its Content-Type, so a body that could be read as JSON is checked whatever its type, and refused @@ -261,6 +285,62 @@ func unlistedToolRefusal(svc *resolvedService, tool string, messages []jsonrpcMe return refusal } +func hookDeniedMCPRefusal(message string, messages []jsonrpcMessage, parsed, batch, streamable bool) *mcpToolRefusal { + return hookMCPRefusal(message, messages, parsed, batch, streamable, decisionBlocked, errorCodeHookDenied, http.StatusForbidden) +} + +func hookFailedMCPRefusal(messages []jsonrpcMessage, parsed, batch, streamable bool) *mcpToolRefusal { + return hookMCPRefusal(hookFailedRequestMessage, messages, parsed, batch, streamable, decisionError, errorCodeHookFailed, http.StatusServiceUnavailable) +} + +// A tool call gets a tool result with isError set rather than a JSON-RPC error: MCP clients hand a tool's error +// result to the model, but many replace a protocol error with a generic one, and the hook's message is for the +// model to act on. Every other request gets a JSON-RPC error with the same text, and delivery follows +// unlistedToolRefusal, including on the event stream of a legacy SSE client. +func hookMCPRefusal(text string, messages []jsonrpcMessage, parsed, batch, streamable bool, decision, code string, status int) *mcpToolRefusal { + refusal := &mcpToolRefusal{status: http.StatusOK, reason: text, decision: decision, code: code} + if !parsed { + refusal.status = status + refusal.body = marshalJSONRPC(newJSONRPCError(nil, jsonrpcHookRefused, text)) + return refusal + } + responses := make([]any, 0, len(messages)) + for _, message := range messages { + if message.id == nil { + continue + } + if message.Method == mcpMethodToolsCall { + responses = append(responses, jsonrpcToolErrorResponse{ + JSONRPC: "2.0", + ID: message.id, + Result: mcpToolResult{IsError: true, Content: []mcpTextContent{{Type: "text", Text: text}}}, + }) + continue + } + responses = append(responses, newJSONRPCError(message.id, jsonrpcHookRefused, text)) + } + + switch { + case len(responses) == 0 && streamable: + refusal.status = http.StatusAccepted + return refusal + case len(responses) == 0: + refusal.body = marshalJSONRPC(newJSONRPCError(nil, jsonrpcHookRefused, text)) + case batch: + refusal.body = marshalJSONRPC(responses) + default: + refusal.body = marshalJSONRPC(responses[0]) + } + if !streamable { + refusal.status = status + refusal.overStream = true + if len(responses) > 0 { + refusal.streamEvent = sseMessageEvent(refusal.body) + } + } + return refusal +} + func sseMessageEvent(data []byte) []byte { event := make([]byte, 0, len(data)+len("event: message\ndata: \n\n")) event = append(event, "event: message\ndata: "...) @@ -318,7 +398,8 @@ func streamableHTTPClient(req *http.Request) bool { // the legacy transport's results, which arrive on another connection, need no tracking. The proxy drops the // agent's Accept-Encoding so Go decompresses the response first; a body compressed anyway, one over the cap, // and anything that does not parse pass through untouched. An event stream's filter is returned, so the -// proxy can also send refusals on it. +// proxy can also send refusals on it. A nil allowlist hides nothing and only watches an event stream, for a +// hooked service whose refusals have to reach it. func hideUnlistedTools(resp *http.Response, allowed map[string]bool) *sseToolsFilter { if resp.Body == nil || resp.Body == http.NoBody { return nil @@ -332,7 +413,9 @@ func hideUnlistedTools(resp *http.Response, allowed map[string]bool) *sseToolsFi } switch mediaType { case "application/json": - hideUnlistedToolsInJSON(resp, allowed) + if allowed != nil { + hideUnlistedToolsInJSON(resp, allowed) + } case "text/event-stream": filter := newSSEToolsFilter(resp.Body, allowed) resp.Body = filter @@ -657,6 +740,9 @@ func isBlankSSELine(line []byte) bool { // An event whose data is a tools/list result is rebuilt with the filtered data on one data line where the // first one was; every other line keeps its place. Any other event is returned as it came. func rewriteSSEEvent(event []byte, allowed map[string]bool) []byte { + if allowed == nil { + return event + } lines := splitSSELines(event) var data [][]byte for _, line := range lines { @@ -783,7 +869,7 @@ func (r *sseStreamRegistry) deliver(session, service string, target *url.URL, re if filter == nil || (refusal.streamEvent != nil && !filter.inject(refusal.streamEvent)) { return refusal } - return &mcpToolRefusal{status: http.StatusAccepted, reason: refusal.reason} + return &mcpToolRefusal{status: http.StatusAccepted, reason: refusal.reason, decision: refusal.decision, code: refusal.code} } // The URL as the proxy forwards it, so an endpoint the server wrote with or without the default port, or in diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index c28afd140..b7927164d 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1008,3 +1008,144 @@ func TestARepeatedEndpointKeepsOneRegistration(t *testing.T) { t.Fatalf("the latest endpoint got %d", got.status) } } + +func hookedMCPService(host string, hook *serviceHook) *resolvedService { + svc := mcpService(host) + svc.name = "github-mcp" + svc.hook = hook + return svc +} + +func TestEveryMCPRequestAsksTheHook(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + up := newMCPUpstream(t) + client, _ := newMCPRecordingProxy(t, up, []*resolvedService{hookedMCPService(up.host(), fake.hook())}) + + for _, body := range []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + } { + if res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", body, http.Header{"Accept": {streamableAccept}}); res.status != http.StatusOK { + t.Fatalf("status %d", res.status) + } + if got := <-up.bodies; string(got) != body { + t.Fatalf("upstream got %s", got) + } + <-up.auth + } + calls := fake.calls() + if len(calls) != 2 { + t.Fatalf("the hook was asked %d times, want 2", len(calls)) + } + want := &hookPayloadMCP{Calls: []hookMCPCall{{ID: json.RawMessage("2"), Method: "tools/list"}}, Parsed: true} + if got := calls[1].payload(t).MCP; !reflect.DeepEqual(got, want) { + t.Fatalf("mcp = %+v", got) + } +} + +func TestAHookDenialOfAToolCallIsAToolErrorResult(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny","message":"Ask Ana first.","reason":"needs approval"}`)) + up := newMCPUpstream(t) + client, log := newMCPRecordingProxy(t, up, []*resolvedService{hookedMCPService(up.host(), fake.hook())}) + + body := `{"jsonrpc":"2.0","id":"abc","method":"tools/call","params":{"name":"merge_pull_request","arguments":{}}}` + res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", body, http.Header{"Accept": {streamableAccept}}) + assertUpstreamSawNothing(t, up) + want := `{"jsonrpc":"2.0","id":"abc","result":{"isError":true,"content":[{"type":"text","text":"Ask Ana first."}]}}` + if res.status != http.StatusOK || res.body != want { + t.Fatalf("status %d body\n%s\nwant\n%s", res.status, res.body, want) + } + if res.header.Get(HeaderError) != errorCodeHookDenied || res.header.Get(HeaderDecision) != decisionBlocked { + t.Fatalf("headers = %v", res.header) + } + if p := fake.only(t).payload(t).MCP; !reflect.DeepEqual(p.Calls, []hookMCPCall{{ID: json.RawMessage(`"abc"`), Method: "tools/call", Tool: "merge_pull_request"}}) { + t.Fatalf("mcp = %+v", p) + } + rec := drainOneRecord(t, log) + if rec.Decision != decisionBlocked || rec.Hook == nil || rec.Hook.Reason != "needs approval" { + t.Fatalf("record = %s", recordJSON(t, rec)) + } +} + +func TestAHookFailureOnAnotherMCPRequestIsAJSONRPCError(t *testing.T) { + fake := newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) }) + up := newMCPUpstream(t) + client, log := newMCPRecordingProxy(t, up, []*resolvedService{hookedMCPService(up.host(), fake.hook())}) + + res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", `{"jsonrpc":"2.0","id":7,"method":"tools/list"}`, http.Header{"Accept": {streamableAccept}}) + assertUpstreamSawNothing(t, up) + want := `{"jsonrpc":"2.0","id":7,"error":{"code":-31000,"message":"` + hookFailedRequestMessage + `"}}` + if res.status != http.StatusOK || res.body != want { + t.Fatalf("status %d body %s", res.status, res.body) + } + if res.header.Get(HeaderError) != errorCodeHookFailed || res.header.Get(HeaderDecision) != decisionError { + t.Fatalf("headers = %v", res.header) + } + if rec := drainOneRecord(t, log); rec.Decision != decisionError || rec.Hook == nil || rec.Hook.Cause != "status_500" { + t.Fatalf("record = %s", recordJSON(t, rec)) + } +} + +func TestAHookRefusalAnswersEveryIDInABatch(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny","message":"no"}`)) + up := newMCPUpstream(t) + client, _ := newMCPRecordingProxy(t, up, []*resolvedService{hookedMCPService(up.host(), fake.hook())}) + + batch := `[{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"a"}},{"jsonrpc":"2.0","id":2,"method":"tools/list"},{"jsonrpc":"2.0","method":"notifications/initialized"}]` + res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", batch, http.Header{"Accept": {streamableAccept}}) + want := `[{"jsonrpc":"2.0","id":1,"result":{"isError":true,"content":[{"type":"text","text":"no"}]}},{"jsonrpc":"2.0","id":2,"error":{"code":-31000,"message":"no"}}]` + if res.status != http.StatusOK || res.body != want { + t.Fatalf("status %d body %s", res.status, res.body) + } + + res = proxiedPost(t, client, up.server.URL+"/mcp", "application/json", `{"jsonrpc":"2.0","method":"notifications/initialized"}`, http.Header{"Accept": {streamableAccept}}) + if res.status != http.StatusAccepted || res.body != "" { + t.Fatalf("a notification got status %d body %s", res.status, res.body) + } + + res = proxiedPost(t, client, up.server.URL+"/mcp", "application/json", `{"jsonrpc":`, http.Header{"Accept": {streamableAccept}}) + if res.status != http.StatusForbidden || res.body != `{"jsonrpc":"2.0","id":null,"error":{"code":-31000,"message":"no"}}` { + t.Fatalf("an unreadable body got status %d body %s", res.status, res.body) + } + assertUpstreamSawNothing(t, up) +} + +func TestAHookRefusalReachesALegacySSEStream(t *testing.T) { + var fake *fakeHook + fake = newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { + calls := fake.calls() + var p hookPayload + if json.Unmarshal(calls[len(calls)-1].body, &p) == nil && p.Request != nil && p.Request.Method == http.MethodGet { + _, _ = io.WriteString(w, `{"decision":"allow"}`) + return + } + _, _ = io.WriteString(w, `{"decision":"deny","message":"Ask Ana first."}`) + }) + upstream, posts := newLegacySSEUpstream(t) + host := strings.TrimPrefix(upstream.URL, "https://") + _, client := newTLSProxy(t, upstream, fixedResolver{services: []*resolvedService{hookedMCPService(host, fake.hook())}}) + + resp, err := client.Get(upstream.URL + "/sse") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + stream := bufio.NewReader(resp.Body) + if event := readSSEEvent(t, stream); !strings.Contains(event, "/messages?sessionId=abc") { + t.Fatalf("endpoint event = %q", event) + } + + res := proxiedPost(t, client, upstream.URL+"/messages?sessionId=abc", "application/json", `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"merge"}}`, nil) + if res.status != http.StatusAccepted || res.body != "" || res.header.Get(HeaderError) != errorCodeHookDenied { + t.Fatalf("status %d body %s headers %v", res.status, res.body, res.header) + } + event := readSSEEvent(t, stream) + if !strings.Contains(event, `"id":4`) || !strings.Contains(event, `"isError":true`) || !strings.Contains(event, "Ask Ana first.") { + t.Fatalf("event = %q", event) + } + select { + case got := <-posts: + t.Fatalf("the upstream got a refused call: %s", got) + default: + } +} diff --git a/packages/agentvault/native.go b/packages/agentvault/native.go index 28d9ff5d3..724b43ee6 100644 --- a/packages/agentvault/native.go +++ b/packages/agentvault/native.go @@ -142,7 +142,8 @@ func (ps *proxyServer) handleNativeConnect(w http.ResponseWriter, r *http.Reques return } - services, grant, err := ps.cache.lookup(sessionToken, nil) + found, err := ps.cache.lookup(sessionToken, nil) + services, grant := found.services, found.sessionLog if err != nil { ps.denyAtGate(w, sessionToken, serviceName+NativeServiceSuffix, NativeServicePort, err) return @@ -161,7 +162,7 @@ func (ps *proxyServer) handleNativeConnect(w http.ResponseWriter, r *http.Reques ps.refuseNative(w, sessionToken, serviceName, body) return } - ps.refuseNativeService(w, sessionToken, grant, svc, body) + ps.refuseNativeService(w, sessionToken, grant, svc, serviceRefusal(body)) } if svc.personalValuesMissing { refuse(errPersonalValuesMissing(svc).Error()) @@ -199,6 +200,17 @@ func (ps *proxyServer) handleNativeConnect(w http.ResponseWriter, r *http.Reques } defer ps.cache.releaseNative(conn) + // After every local refusal, so the hook is only asked about a connection the proxy would open, and under the + // connection's context, so a revoke during the call cancels it. + var hook *hookOutcome + if svc.hook != nil { + hook = ps.askNativeHook(conn.ctx, svc, found) + if hook.Decision != hookAllow { + ps.refuseNativeService(w, sessionToken, grant, svc, nativeHookRefusal(hook)) + return + } + } + hijacker, ok := w.(http.Hijacker) if !ok { http.Error(w, "connection hijacking unsupported", http.StatusInternalServerError) @@ -220,8 +232,11 @@ func (ps *proxyServer) handleNativeConnect(w http.ResponseWriter, r *http.Reques logger := newNativeSessionLogger(ps, conn.key, grant, svc) defer logger.Close() - log.Info().Str("service", svc.name).Str("kind", svc.kind).Str("sessionKey", conn.key). - Msg("agent-vault: native connection opened") + opened := log.Info().Str("service", svc.name).Str("kind", svc.kind).Str("sessionKey", conn.key) + if hook != nil { + opened = hook.logFields(opened) + } + opened.Msg("agent-vault: native connection opened") client := &hangUpConn{Conn: clientConn} err = serveNative(conn.ctx, client, svc, tlsConfig, logger) if err != nil && conn.ctx.Err() == nil && !isClientHangUp(err) && !client.hungUp.Load() { @@ -256,17 +271,42 @@ func (c *hangUpConn) Read(p []byte) (int, error) { return n, err } +// What `connect` is told (body) and what the session log row says (note), which differ only for a hook's answer. +type nativeRefusal struct { + decision string + status int + code string + body string + note string + hook *hookOutcome +} + +func serviceRefusal(body string) nativeRefusal { + return nativeRefusal{decision: decisionBlocked, status: http.StatusForbidden, code: errorCodeServiceRefused, body: body, note: body} +} + func (ps *proxyServer) refuseNative(w http.ResponseWriter, sessionToken, serviceName, body string) { - log.Warn().Str("service", serviceName).Str("sessionKey", sessionKey(sessionToken)).Str("reason", body). - Msg("agent-vault: refused a database or SSH connection") - writeProxyError(w, http.StatusForbidden, decisionBlocked, errorCodeServiceRefused, body) + ps.writeNativeRefusal(w, sessionToken, serviceName, serviceRefusal(body)) +} + +func (ps *proxyServer) writeNativeRefusal(w http.ResponseWriter, sessionToken, serviceName string, refusal nativeRefusal) { + event := log.Warn() + if refusal.decision == decisionError { + event = log.Error() + } + event = event.Str("service", serviceName).Str("sessionKey", sessionKey(sessionToken)).Str("reason", refusal.body) + if refusal.hook != nil { + event = refusal.hook.logFields(event) + } + event.Msg("agent-vault: refused a database or SSH connection") + writeProxyError(w, refusal.status, refusal.decision, refusal.code, refusal.body) } // refuseNativeService refuses a connection to a service the session has, and records it so the session log shows // the attempt. -func (ps *proxyServer) refuseNativeService(w http.ResponseWriter, sessionToken string, grant *sessionLogGrant, svc *resolvedService, body string) { - newNativeSessionLogger(ps, sessionKey(sessionToken), grant, svc).recordOutcome(decisionBlocked, body) - ps.refuseNative(w, sessionToken, svc.name, body) +func (ps *proxyServer) refuseNativeService(w http.ResponseWriter, sessionToken string, grant *sessionLogGrant, svc *resolvedService, refusal nativeRefusal) { + newNativeSessionLogger(ps, sessionKey(sessionToken), grant, svc).recordRefusal(refusal.decision, refusal.note, refusal.hook.sessionLogHook()) + ps.writeNativeRefusal(w, sessionToken, svc.name, refusal) } func nativeTLSConfig(target nativeTarget) (*tls.Config, error) { diff --git a/packages/agentvault/native_logger.go b/packages/agentvault/native_logger.go index 04d0fcdfc..bd4c1af68 100644 --- a/packages/agentvault/native_logger.go +++ b/packages/agentvault/native_logger.go @@ -337,6 +337,10 @@ func (l *nativeSessionLogger) loggingOnLocked() bool { // recordOutcome writes a row for a connection that never reached the service, with the reason as its note. func (l *nativeSessionLogger) recordOutcome(decision, reason string) { + l.recordRefusal(decision, reason, nil) +} + +func (l *nativeSessionLogger) recordRefusal(decision, reason string, hook *hookOutcome) { l.mu.Lock() defer l.mu.Unlock() // The reasons are error text, which starts lowercase; a log row reads as a sentence. @@ -345,15 +349,15 @@ func (l *nativeSessionLogger) recordOutcome(decision, reason string) { } detail := &nativeDetail{} appendCapped(&detail.Note, reason, nativeLogInputCap, &detail.Truncated) - l.writeDecisionLocked(decision, detail, l.now()) + l.writeDecisionLocked(decision, detail, l.now(), hook) } // at is when the row's activity started, which is the time it shows, however much later it is written. func (l *nativeSessionLogger) writeLocked(detail *nativeDetail, at time.Time) { - l.writeDecisionLocked(decisionBrokered, detail, at) + l.writeDecisionLocked(decisionBrokered, detail, at, nil) } -func (l *nativeSessionLogger) writeDecisionLocked(decision string, detail *nativeDetail, at time.Time) { +func (l *nativeSessionLogger) writeDecisionLocked(decision string, detail *nativeDetail, at time.Time, hook *hookOutcome) { if !l.loggingOnLocked() { return } @@ -367,6 +371,7 @@ func (l *nativeSessionLogger) writeDecisionLocked(decision string, detail *nativ Service: &l.svc.name, ServiceType: &kind, Native: detail, + Hook: hook, at: at, } if l.svc.accessBundleName != "" { diff --git a/packages/agentvault/native_logger_test.go b/packages/agentvault/native_logger_test.go index 9cccedd78..53ee0b1f6 100644 --- a/packages/agentvault/native_logger_test.go +++ b/packages/agentvault/native_logger_test.go @@ -23,11 +23,11 @@ func newLoggerUnderTest(t *testing.T) (*nativeSessionLogger, *proxyServer) { ps := &proxyServer{} ps.cache = newSessionCache(grantingResolver{services: []*resolvedService{svc}}, ps.pollInterval) ps.sessionLogs = newSessionLogRecorder("proxy-1", &fakeShipper{}) - _, grant, err := ps.cache.lookup("agv_logger", nil) + found, err := ps.cache.lookup("agv_logger", nil) if err != nil { t.Fatal(err) } - return newNativeSessionLogger(ps, sessionKey("agv_logger"), grant, svc), ps + return newNativeSessionLogger(ps, sessionKey("agv_logger"), found.sessionLog, svc), ps } func loggedRecords(ps *proxyServer) []sessionLogRecord { diff --git a/packages/agentvault/native_proxy_test.go b/packages/agentvault/native_proxy_test.go index 25948d945..5d1ab0c26 100644 --- a/packages/agentvault/native_proxy_test.go +++ b/packages/agentvault/native_proxy_test.go @@ -723,3 +723,72 @@ func startFakeMySQL(t *testing.T) string { type quietMySQLHandler struct{ mysqlserver.EmptyHandler } func (quietMySQLHandler) UseDB(string) error { return nil } + +func hookedNativeService(addr string, hook *serviceHook) *resolvedService { + svc := nativeServiceAt(serviceKindPostgres, "db", addr) + svc.hook = hook + return svc +} + +func TestAProbeDoesNotAskTheHookButAConnectionDoes(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + upstream := startFakePostgres(t, "agent", "secret") + _, client, _ := newNativeProxy(t, []*resolvedService{hookedNativeService(upstream, fake.hook())}) + + if err := client.Probe(context.Background(), "db"); err != nil { + t.Fatal(err) + } + if got := len(fake.calls()); got != 0 { + t.Fatalf("a probe asked the hook %d times", got) + } + + tunnel, err := client.OpenTunnel(context.Background(), "db") + if err != nil { + t.Fatalf("an allowed connection was refused: %v", err) + } + _ = tunnel.Close() + p := fake.only(t).payload(t) + host, port, _ := net.SplitHostPort(upstream) + if p.Request != nil || p.MCP != nil || p.Connection == nil || *p.Connection != (hookPayloadConnection{Host: host, Port: port, Database: "app"}) || + p.Service.Type != serviceKindPostgres { + t.Fatalf("payload = %s", fake.only(t).body) + } +} + +func TestAHookRefusalOfAConnectionReachesConnectAndTheSessionLog(t *testing.T) { + long := strings.Repeat("", 400) + for _, tc := range []struct { + name string + answer http.HandlerFunc + status int + code string + message string + decision string + note string + }{ + {"deny", answerHook(`{"decision":"deny","message":"Ask Ana first.","reason":"after hours"}`), http.StatusForbidden, errorCodeHookDenied, + "Ask Ana first.", decisionBlocked, "The hook refused this connection: after hours"}, + {"deny without a reason", answerHook(`{"decision":"deny","message":"Ask Ana first."}`), http.StatusForbidden, errorCodeHookDenied, + "Ask Ana first.", decisionBlocked, "The hook refused this connection: Ask Ana first."}, + {"deny with a long message", answerHook(`{"decision":"deny","message":"` + long + `"}`), http.StatusForbidden, errorCodeHookDenied, + long[:maxNativeHookMessage], decisionBlocked, ""}, + {"failure", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) }, http.StatusServiceUnavailable, + errorCodeHookFailed, hookFailedConnectionMessage, decisionError, hookFailedConnectionNote}, + } { + t.Run(tc.name, func(t *testing.T) { + fake := newFakeHook(t, tc.answer) + ps, client, _ := newNativeProxy(t, []*resolvedService{hookedNativeService("127.0.0.1:1", fake.hook())}) + + _, err := client.OpenTunnel(context.Background(), "db") + var refused *ProxyRefusedError + if !errors.As(err, &refused) || refused.Status != tc.status || refused.Code != tc.code || refused.Message != tc.message { + t.Fatalf("open: %#v", err) + } + records := nativeRecords(t, ps, 1) + if len(records) != 1 || records[0].Decision != tc.decision || records[0].Hook == nil || records[0].Native == nil || + (tc.note != "" && records[0].Native.Note != tc.note) { + t.Fatalf("records = %s", recordJSON(t, records[0])) + } + }) + } +} diff --git a/packages/agentvault/proxy.go b/packages/agentvault/proxy.go index b64dea55f..8fa847973 100644 --- a/packages/agentvault/proxy.go +++ b/packages/agentvault/proxy.go @@ -680,8 +680,15 @@ func (ps *proxyServer) forwardHTTP(w http.ResponseWriter, r *http.Request, t tun code := "" var refusal *mcpToolRefusal switch { + case errors.Is(err, errHookDenied): + decision, status, code, body = decisionBlocked, http.StatusForbidden, errorCodeHookDenied, outcome.hook.message + case errors.Is(err, errHookFailed): + decision, status, code, body = decisionError, http.StatusServiceUnavailable, errorCodeHookFailed, hookFailedRequestMessage case errors.As(err, &refusal): decision, status = decisionBlocked, refusal.status + if refusal.decision != "" { + decision = refusal.decision + } case errors.Is(err, errHostBlocked): decision, status, code, body = decisionBlocked, http.StatusForbidden, errorCodeHostNotAllowed, err.Error() case errors.Is(err, errPolicyBlocked): @@ -728,6 +735,9 @@ func (ps *proxyServer) forwardHTTP(w http.ResponseWriter, r *http.Request, t tun if matched != nil { event = event.Str("service", matched.name).Str("accessBundle", matched.accessBundleName) } + if outcome.hook != nil { + event = outcome.hook.logFields(event) + } if outcome.brokered && !strings.EqualFold(scheme, "https") { event = event.Bool("plaintext", true) } @@ -759,6 +769,7 @@ func (ps *proxyServer) forwardHTTP(w http.ResponseWriter, r *http.Request, t tun AccessBundle: bundle, ServiceType: serviceType, MCP: outcome.mcp, + Hook: outcome.hook.sessionLogHook(), }) } @@ -828,16 +839,18 @@ type forwardOutcome struct { substituted []string sessionLog *sessionLogGrant mcp *mcpDetail + hook *hookOutcome } func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *resolvedService, forwardOutcome, error) { scheme, hostname, port, sessionToken := t.scheme, t.hostname, t.port, t.sessionToken var outcome forwardOutcome - services, grant, err := ps.cache.lookup(sessionToken, nil) + found, err := ps.cache.lookup(sessionToken, nil) if err != nil { return nil, nil, outcome, fmt.Errorf("%w: %w", errSessionResolve, err) } + services, grant := found.services, found.sessionLog outcome.sessionLog = grant // TRACE and TRACK make the upstream reflect the injected credential back in the response body. Upper @@ -917,6 +930,12 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re } } + if matched.hook != nil { + if err := ps.askHook(req, t, matched, found, &outcome); err != nil { + return nil, matched, outcome, err + } + } + // Substitutions first, so an injected real value can never itself be rewritten. The credential last, // so a custom header naming the credential's own header loses rather than replacing the token. substituted, err := applySubstitutions(req, matched.name, matched.substitutions) @@ -993,7 +1012,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re return nil, matched, outcome, err } } - if matched != nil && matched.kind == serviceKindMCP && matched.allowedTools != nil { + if matched != nil && matched.kind == serviceKindMCP && (matched.allowedTools != nil || matched.hook != nil) { if stream := hideUnlistedTools(resp, matched.allowedTools); stream != nil { ps.sseStreams.watch(stream, sessionToken, matched.id, *req.URL) } diff --git a/packages/agentvault/proxy_plaintext_test.go b/packages/agentvault/proxy_plaintext_test.go index 6c95f5db7..2adbe043c 100644 --- a/packages/agentvault/proxy_plaintext_test.go +++ b/packages/agentvault/proxy_plaintext_test.go @@ -161,3 +161,24 @@ func TestPlainHTTPToAnMCPServiceOnACustomPortIsRefused(t *testing.T) { t.Fatalf("the token reached the upstream in cleartext: %s", body) } } + +func TestThePlainDoorAsksTheHookWithoutTheSessionToken(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny","message":"no"}`)) + client, host := newPlaintextFixture(t, func(h string) *resolvedService { + svc := policyService(h, nil, nil, nil, nil) + svc.hook = fake.hook() + return svc + }) + + status, payload := do(t, client, http.MethodGet, "http://"+host+"/x", "") + if status != http.StatusForbidden || decodeProxyError(t, payload).Error != errorCodeHookDenied { + t.Fatalf("status %d: %s", status, payload) + } + hit := fake.only(t) + if p := hit.payload(t).Request; p.Scheme != "http" || p.Headers["proxy-authorization"] != nil { + t.Fatalf("request = %+v", p) + } + if bytes.Contains(hit.body, []byte("agv_tok")) { + t.Fatalf("the session token reached the hook: %s", hit.body) + } +} diff --git a/packages/agentvault/proxy_policy_test.go b/packages/agentvault/proxy_policy_test.go index 31316edec..c9af44f17 100644 --- a/packages/agentvault/proxy_policy_test.go +++ b/packages/agentvault/proxy_policy_test.go @@ -2,16 +2,21 @@ package agentvault import ( "bytes" + "compress/gzip" "crypto/tls" "crypto/x509" + "encoding/base64" "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "net/url" + "reflect" "strings" + "sync/atomic" "testing" + "unicode/utf8" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -497,3 +502,208 @@ func TestAnInjectedMethodOverrideCannotOutrankTheAllowlist(t *testing.T) { t.Fatalf("the service's own header reached the upstream as %v, so the allowlist can be outranked", v) } } + +// An echoing TLS upstream that counts what reaches it, behind a proxy whose one service has a hook and carries a +// credential, a custom header and a substitution, none of which the hook may see. +func newHookedPolicyFixture(t *testing.T, fake *fakeHook) (*proxyServer, *http.Client, string, *atomic.Int64) { + t.Helper() + hits := &atomic.Int64{} + upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + body, _ := io.ReadAll(r.Body) + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(echoed{Method: r.Method, Path: r.URL.EscapedPath(), Query: r.URL.RawQuery, Headers: r.Header, Body: string(body)}) + })) + t.Cleanup(upstream.Close) + host := strings.TrimPrefix(upstream.URL, "https://") + + svc := policyService(host, nil, nil, []customHeader{{name: "X-Tenant", value: []byte("tenant-value-secret")}}, + []substitution{{placeholder: "{{KEY}}", surfaces: map[string]bool{surfaceBody: true, surfaceHeader: true}, value: []byte("substituted-value-secret")}}) + svc.id, svc.accessBundleID = "svc-1", "bundle-1" + svc.credential = credential{kind: credentialBearer, headerName: "Authorization", headerPrefix: "Bearer", value: []byte("credential-value-secret")} + svc.hook = fake.hook() + + ps, client := newTLSProxy(t, upstream, grantingResolver{services: []*resolvedService{svc}}) + ps.opts.ProxyID = "proxy-1" + ps.sessionLogs = newSessionLogRecorder("proxy-1", &fakeShipper{}) + return ps, client, host, hits +} + +func doHooked(t *testing.T, client *http.Client, method, target string, body io.Reader, header http.Header) (*http.Response, string) { + t.Helper() + req, err := http.NewRequest(method, target, body) + if err != nil { + t.Fatal(err) + } + for name, values := range header { + req.Header[name] = values + } + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + payload, _ := io.ReadAll(resp.Body) + return resp, strings.TrimSpace(string(payload)) +} + +func TestAnAllowedRequestIsSentWithItsCredentialAndTheHookSawOnlyTheAgentsRequest(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + ps, client, host, _ := newHookedPolicyFixture(t, fake) + + resp, body := doHooked(t, client, http.MethodPost, "https://"+host+"/v1/Refunds?limit=2", strings.NewReader("amount={{KEY}}"), + http.Header{"Content-Type": {"application/x-www-form-urlencoded"}, "X-Agent": {"a", "b"}, "X-Key": {"{{KEY}}"}}) + if resp.StatusCode != http.StatusOK { + t.Fatalf("status %d: %s", resp.StatusCode, body) + } + got := decodeEcho(t, body) + if got.Headers["Authorization"][0] != "Bearer credential-value-secret" || got.Body != "amount=substituted-value-secret" { + t.Fatalf("the upstream got %+v", got) + } + + hit := fake.only(t) + for _, secret := range []string{"credential-value-secret", "tenant-value-secret", "substituted-value-secret"} { + if bytes.Contains(hit.body, []byte(secret)) { + t.Fatalf("the hook payload holds %q: %s", secret, hit.body) + } + } + p := hit.payload(t) + _, port, _ := strings.Cut(host, ":") + want := hookPayloadRequest{Method: "POST", Scheme: "https", Host: "127.0.0.1", Port: port, Path: "/v1/Refunds", Query: "limit=2", + Body: "amount={{KEY}}", BodyEncoding: "utf8"} + gotReq := *p.Request + gotReq.Headers = nil + if !reflect.DeepEqual(gotReq, want) { + t.Fatalf("request = %+v, want %+v", gotReq, want) + } + if h := p.Request.Headers; strings.Join(h["x-agent"], ",") != "a,b" || h["x-key"][0] != "{{KEY}}" || h["proxy-authorization"] != nil || h["authorization"] != nil || h["x-tenant"] != nil { + t.Fatalf("headers = %v", h) + } + if p.Version != "1" || p.Proxy.ID != "proxy-1" || p.Session.ID != "s1" || p.MCP != nil || p.Connection != nil { + t.Fatalf("payload = %s", hit.body) + } + if p.Service != (hookPayloadService{ID: "svc-1", Name: "github", Type: "http", AccessBundle: p.Service.AccessBundle}) || + p.Service.AccessBundle == nil || *p.Service.AccessBundle != (hookPayloadBundle{ID: "bundle-1", Name: "bundle"}) { + t.Fatalf("service = %s", hit.body) + } + + if rec := drainOneRecord(t, ps.sessionLogs); rec.Hook != nil || rec.Decision != decisionBrokered { + t.Fatalf("an allowed request's record = %s", recordJSON(t, rec)) + } +} + +func TestAHookDenialReachesTheAgent(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny","message":"Asked Ana, retry in a minute.","reason":"over budget"}`)) + ps, client, host, hits := newHookedPolicyFixture(t, fake) + logs := captureLogs(t) + + resp, body := doHooked(t, client, http.MethodGet, "https://"+host+"/v1/charges", nil, nil) + if resp.StatusCode != http.StatusForbidden || hits.Load() != 0 { + t.Fatalf("status %d, upstream hits %d: %s", resp.StatusCode, hits.Load(), body) + } + if resp.Header.Get(HeaderDecision) != decisionBlocked || resp.Header.Get(HeaderError) != errorCodeHookDenied { + t.Fatalf("headers = %v", resp.Header) + } + refusal := decodeProxyError(t, body) + if refusal.Error != errorCodeHookDenied || refusal.Message != "Asked Ana, retry in a minute." || refusal.Hint != proxyErrorHints[errorCodeHookDenied] { + t.Fatalf("body = %s", body) + } + + rec := drainOneRecord(t, ps.sessionLogs) + if rec.Decision != decisionBlocked || rec.Status != http.StatusForbidden || rec.Hook == nil || rec.Hook.Decision != hookDeny || + rec.Hook.Reason != "over budget" || rec.Hook.DeliveryID == "" { + t.Fatalf("record = %s", recordJSON(t, rec)) + } + line := logs.String() + if !strings.Contains(line, `"hook":"deny"`) || !strings.Contains(line, `"hookReason":"over budget"`) || !strings.Contains(line, `"deliveryId":"`+rec.Hook.DeliveryID+`"`) || + !strings.Contains(line, `"hookMs":`) || !strings.Contains(line, `"hookUrl":"`+fake.server.URL+`"`) || strings.Contains(line, "/agent-vault/hook") { + t.Fatalf("log = %s", line) + } +} + +func TestAHookDenialWithNoMessageGetsTheDefault(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny"}`)) + _, client, host, _ := newHookedPolicyFixture(t, fake) + _, body := doHooked(t, client, http.MethodGet, "https://"+host+"/", nil, nil) + if refusal := decodeProxyError(t, body); refusal.Message != hookDeniedRequestMessage { + t.Fatalf("body = %s", body) + } +} + +func TestEveryHookFailureRefusesTheRequest(t *testing.T) { + for _, tc := range []struct { + name string + answer http.HandlerFunc + cause string + }{ + {"server error", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusBadGateway) }, "status_502"}, + {"redirect", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/x", http.StatusFound) }, hookCauseRedirect}, + {"not JSON", answerHook(`ok`), hookCauseInvalidJSON}, + {"bad decision", answerHook(`{"decision":"yes"}`), hookCauseBadDecision}, + {"too large", answerHook(strings.Repeat(" ", maxHookAnswer+1)), hookCauseTooLarge}, + } { + t.Run(tc.name, func(t *testing.T) { + fake := newFakeHook(t, tc.answer) + ps, client, host, hits := newHookedPolicyFixture(t, fake) + resp, body := doHooked(t, client, http.MethodPost, "https://"+host+"/", strings.NewReader("x"), nil) + if resp.StatusCode != http.StatusServiceUnavailable || hits.Load() != 0 { + t.Fatalf("status %d, upstream hits %d", resp.StatusCode, hits.Load()) + } + if resp.Header.Get("Retry-After") != "5" || resp.Header.Get(HeaderError) != errorCodeHookFailed || resp.Header.Get(HeaderDecision) != decisionError { + t.Fatalf("headers = %v", resp.Header) + } + var fields map[string]any + _ = json.Unmarshal([]byte(body), &fields) + if len(fields) != 3 || fields["error"] != errorCodeHookFailed || fields["message"] != hookFailedRequestMessage || fields["hint"] != proxyErrorHints[errorCodeHookFailed] { + t.Fatalf("body = %s", body) + } + rec := drainOneRecord(t, ps.sessionLogs) + if rec.Decision != decisionError || rec.Hook == nil || rec.Hook.Decision != hookFailed || rec.Hook.Cause != tc.cause { + t.Fatalf("record = %s", recordJSON(t, rec)) + } + }) + } +} + +func TestLargeAndCompressedBodiesReachTheHookCutAndTheUpstreamWhole(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + _, client, host, _ := newHookedPolicyFixture(t, fake) + + big := strings.Repeat("é", 1<<20) + resp, body := doHooked(t, client, http.MethodPost, "https://"+host+"/upload", strings.NewReader(big), nil) + if resp.StatusCode != http.StatusOK || decodeEcho(t, body).Body != big { + t.Fatalf("status %d, the upstream did not get the whole 2 MB body", resp.StatusCode) + } + p := fake.only(t).payload(t).Request + if !p.BodyTruncated || p.BodyEncoding != "utf8" || len(p.Body) != maxHookBodyPrefix || !utf8.ValidString(p.Body) { + t.Fatalf("truncated %v, encoding %s, %d bytes", p.BodyTruncated, p.BodyEncoding, len(p.Body)) + } + + var zipped bytes.Buffer + zw := gzip.NewWriter(&zipped) + _, _ = zw.Write([]byte("compressed body")) + _ = zw.Close() + resp, _ = doHooked(t, client, http.MethodPost, "https://"+host+"/upload", bytes.NewReader(zipped.Bytes()), http.Header{"Content-Encoding": {"gzip"}}) + if resp.StatusCode != http.StatusOK { + t.Fatalf("status %d", resp.StatusCode) + } + calls := fake.calls() + p = calls[len(calls)-1].payload(t).Request + if decoded, _ := base64.StdEncoding.DecodeString(p.Body); p.BodyEncoding != "base64" || p.BodyTruncated || !bytes.Equal(decoded, zipped.Bytes()) { + t.Fatalf("a gzip body reached the hook as %s %q", p.BodyEncoding, p.Body) + } +} + +func TestTheProxysOwnRefusalsComeBeforeTheHook(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + ps, client, host, _ := newHookedPolicyFixture(t, fake) + services, _ := ps.cache.get("agv_tok") + services[0].allowedMethods = toMethodSet([]string{"GET"}) + + if resp, _ := doHooked(t, client, http.MethodDelete, "https://"+host+"/", nil, nil); resp.StatusCode != http.StatusForbidden { + t.Fatalf("status %d", resp.StatusCode) + } + if got := len(fake.calls()); got != 0 { + t.Fatalf("the hook was asked about a request the policy refused (%d calls)", got) + } +} diff --git a/packages/agentvault/resolve.go b/packages/agentvault/resolve.go index b7c08f25a..fd1dc3e63 100644 --- a/packages/agentvault/resolve.go +++ b/packages/agentvault/resolve.go @@ -23,6 +23,7 @@ type resolveResult struct { ExpiresAt *time.Time Services []*resolvedService SessionLog *sessionLogGrant + Session *api.AgentVaultResolvedSession } const sessionLogKeyBytes = 32 @@ -73,6 +74,7 @@ func (r *infisicalResolver) resolve(sessionToken string, held *sessionLogGrant) id: wire.ID, name: wire.Name, kind: kind, + accessBundleID: derefString(wire.AccessBundleID), accessBundleName: wire.AccessBundleName, hostPattern: wire.HostPattern, hostPatterns: hostPatterns, @@ -85,6 +87,7 @@ func (r *infisicalResolver) resolve(sessionToken string, held *sessionLogGrant) substitutions: toSubstitutions(wire.Substitutions), personalValuesMissing: wire.PersonalValuesMissing, memberProvided: wire.MemberProvided, + hook: toServiceHook(wire.Hook), }) } @@ -93,6 +96,7 @@ func (r *infisicalResolver) resolve(sessionToken string, held *sessionLogGrant) ExpiresAt: expiresAt, Services: services, SessionLog: toSessionLogGrant(res.SessionID, res.SessionLogs, held), + Session: res.Session, }, nil } @@ -138,6 +142,13 @@ func toSessionLogGrant(sessionID string, wire api.AgentVaultSessionLogGrant, hel return newSessionLogGrant(sessionID, key) } +func derefString(s *string) string { + if s == nil { + return "" + } + return *s +} + // Anything unrecognised is http, so a newer backend's type is brokered the way this proxy already knows how. func toServiceKind(wire string) string { switch wire { diff --git a/packages/agentvault/resolve_client_test.go b/packages/agentvault/resolve_client_test.go index f2288bd0e..9513b8cef 100644 --- a/packages/agentvault/resolve_client_test.go +++ b/packages/agentvault/resolve_client_test.go @@ -199,3 +199,44 @@ func TestADatabaseTargetKeepsTLSOnUnlessTheServerTurnsItOff(t *testing.T) { } } } + +func TestAResolveCarriesHooksAndTheSession(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"sessionId":"s1","services":[ + {"id":"a","name":"stripe","accessBundleId":"b-1","accessBundleName":"payments","hostPattern":"api.stripe.com","credential":{"type":"passthrough"},"hook":{"url":"https://hooks.example.com/stripe","secret":"` + testHookSecret + `"}}, + {"id":"b","name":"inline","accessBundleId":null,"hostPattern":"x.example","credential":{"type":"passthrough"},"hook":{"url":"not a url","secret":"` + testHookSecret + `"}}, + {"id":"c","name":"plain","accessBundleId":"b-1","hostPattern":"y.example","credential":{"type":"passthrough"},"hook":null}, + {"id":"d","name":"older","hostPattern":"z.example","credential":{"type":"passthrough"}} + ],"sessionLogs":{"enabled":false}, + "session":{"label":"Nightly refunds","actor":{"type":"machineIdentity","id":"m-1","name":"support-agent","email":null},"onBehalfOf":{"id":"u-1","name":"Ana Silva","email":"ana@acme.com"}}}`)) + })) + defer srv.Close() + old := config.INFISICAL_URL + config.INFISICAL_URL = srv.URL + "/api" + defer func() { config.INFISICAL_URL = old }() + + resolver, err := newInfisicalResolver(func() string { return "ptok" }) + if err != nil { + t.Fatal(err) + } + res, err := resolver.resolve("tok", nil) + if err != nil { + t.Fatal(err) + } + stripe, inline, plain, older := res.Services[0], res.Services[1], res.Services[2], res.Services[3] + if stripe.accessBundleID != "b-1" || stripe.hook == nil || stripe.hook.origin() != "https://hooks.example.com" || string(stripe.hook.secret) != testHookSecret { + t.Fatalf("stripe = %+v hook %+v", stripe, stripe.hook) + } + if inline.accessBundleID != "" || inline.hook == nil || inline.hook.url != nil { + t.Fatalf("an unusable hook must stay a hook that fails, got %+v", inline.hook) + } + if plain.hook != nil || older.hook != nil { + t.Fatal("a service with no hook got one") + } + s := res.Session + if s == nil || *s.Label != "Nightly refunds" || s.Actor.Type != "machineIdentity" || s.Actor.Email != nil || + s.OnBehalfOf == nil || *s.OnBehalfOf.Email != "ana@acme.com" { + t.Fatalf("session = %+v", s) + } +} diff --git a/packages/agentvault/session_endpoints.go b/packages/agentvault/session_endpoints.go index 7a6e89328..c2352738d 100644 --- a/packages/agentvault/session_endpoints.go +++ b/packages/agentvault/session_endpoints.go @@ -100,7 +100,7 @@ func sessionRouteFor(t tunnel, path string) (sessionRoute, bool) { // serveSessionEndpoint answers a request for one of the session's endpoints on the session the tunnel carries. The session // is checked here rather than at the door, so a refused session's reason arrives as JSON, inside TLS for https. func (ps *proxyServer) serveSessionEndpoint(w http.ResponseWriter, r *http.Request, t tunnel, route sessionRoute) { - _, grant, err := ps.cache.lookup(t.sessionToken, nil) + found, err := ps.cache.lookup(t.sessionToken, nil) if err != nil { refusal := classifyGateRefusal(err) refusal.event.Str("host", t.hostname).Str("sessionKey", sessionKey(t.sessionToken)).Str("decision", refusal.decision). @@ -114,7 +114,7 @@ func (ps *proxyServer) serveSessionEndpoint(w http.ResponseWriter, r *http.Reque method, path := truncateLogged(r.Method, maxLoggedMethodLen), truncatePath(requestPath(r)) log.Info().Str("method", method).Str("host", t.hostname).Str("path", path).Str("decision", decision). Int("status", status).Msg("agent-vault: request") - ps.sessionLogs.record(grant, sessionLogRecord{ + ps.sessionLogs.record(found.sessionLog, sessionLogRecord{ Method: method, Host: t.hostname, Port: t.port, diff --git a/packages/agentvault/session_log_spool.go b/packages/agentvault/session_log_spool.go index a699f5c93..8f4697dba 100644 --- a/packages/agentvault/session_log_spool.go +++ b/packages/agentvault/session_log_spool.go @@ -32,6 +32,8 @@ type sessionLogRecord struct { ServiceType *string `json:"serviceType"` MCP *mcpDetail `json:"mcp,omitempty"` Native *nativeDetail `json:"native,omitempty"` + // Set when the service's hook refused the request or failed to answer. Never the payload sent to it. + Hook *hookOutcome `json:"hook,omitempty"` at time.Time // An estimate of the record's size, so the spool can budget memory as well as rows. @@ -54,6 +56,9 @@ func (rec *sessionLogRecord) estimateSize() int { if n := rec.Native; n != nil { size += len(n.Statement) + len(n.Command) + len(n.Stdin) + len(n.File) + len(n.Result) + len(n.Output) + len(n.Note) } + if h := rec.Hook; h != nil { + size += 64 + len(h.Decision) + len(h.Reason) + len(h.Cause) + len(h.DeliveryID) + } return size } diff --git a/packages/agentvault/session_log_test.go b/packages/agentvault/session_log_test.go index a66d0a781..70d12d29e 100644 --- a/packages/agentvault/session_log_test.go +++ b/packages/agentvault/session_log_test.go @@ -1785,3 +1785,16 @@ func TestADrainedRingSlotHoldsNothing(t *testing.T) { } } } + +func TestAHookOutcomeCountsTowardARecordsSize(t *testing.T) { + rec := sessionLogRecord{Method: "GET", Decision: decisionBlocked} + without := rec.estimateSize() + rec.Hook = &hookOutcome{Decision: hookDeny, Reason: strings.Repeat("r", maxHookReason), DeliveryID: "018f3b4e-2a1c-7d10-9b2e-4c5d6e7f8091"} + if got := rec.estimateSize(); got < without+maxHookReason+36 { + t.Fatalf("size grew from %d to %d, less than the hook's fields", without, got) + } + b, _ := json.Marshal(rec.Hook) + if string(b) != `{"decision":"deny","reason":"`+strings.Repeat("r", maxHookReason)+`","durationMs":0,"deliveryId":"018f3b4e-2a1c-7d10-9b2e-4c5d6e7f8091"}` { + t.Fatalf("hook = %s", b) + } +} diff --git a/packages/agentvault/socks5_e2e_test.go b/packages/agentvault/socks5_e2e_test.go index 6b6244a41..fb81ac74b 100644 --- a/packages/agentvault/socks5_e2e_test.go +++ b/packages/agentvault/socks5_e2e_test.go @@ -881,3 +881,21 @@ func TestSocks5BrokersARequestToAnIPv6Target(t *testing.T) { t.Fatalf("status=%d body=%s, want the injected credential", status, body) } } + +func TestTheSocks5DoorAsksTheHook(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"deny","message":"not over socks5"}`)) + var f *socks5Fixture + f = newSocks5Fixture(t, resolverFunc(func() (*resolveResult, error) { + svc := bearerService("127.0.0.1:" + strconv.Itoa(int(f.upstreamPort(t)))) + svc.hook = fake.hook() + return &resolveResult{SessionID: "s1", Services: []*resolvedService{svc}}, nil + }), nil) + + status, body := f.get(t, "127.0.0.1", f.upstreamPort(t)) + if status != http.StatusForbidden || !strings.Contains(body, "not over socks5") || f.hits.Load() != 0 { + t.Fatalf("status=%d body=%s upstream hits=%d", status, body, f.hits.Load()) + } + if p := fake.only(t).payload(t).Request; p == nil || p.Path != "/x" { + t.Fatalf("request = %+v", p) + } +} diff --git a/packages/agentvault/upstream_auth_test.go b/packages/agentvault/upstream_auth_test.go index 00f68147e..d0314431c 100644 --- a/packages/agentvault/upstream_auth_test.go +++ b/packages/agentvault/upstream_auth_test.go @@ -2,6 +2,7 @@ package agentvault import ( "bufio" + "bytes" "crypto/sha256" "encoding/hex" "encoding/json" @@ -411,7 +412,7 @@ func TestARememberedRefreshFailureExpires(t *testing.T) { resolver := &oauthResolver{err: errors.New("dial tcp: connection refused")} cache := newSessionCache(resolver, func() time.Duration { return time.Minute }) resolver.services = []*resolvedService{oauthMCPService("mcp.example", "old-token")} - if _, _, err := cache.lookup("tok", nil); err != nil { + if _, err := cache.lookup("tok", nil); err != nil { t.Fatal(err) } sent := oauthToken("old-token") @@ -1390,3 +1391,24 @@ func TestAnMCPEventStreamIsForwardedBeforeItCompletes(t *testing.T) { t.Fatalf("a successful event stream triggered %d refreshes", got) } } + +func TestAnOAuthResendAsksTheHookOnce(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + resolver := &oauthResolver{issue: oauthToken("new-token"), status: oauthStatusConnected} + client, host, up := newOAuthFixture(t, resolver, func(h string) *resolvedService { + svc := oauthMCPService(h, "old-token") + svc.hook = fake.hook() + return svc + }, acceptOnly("new-token")) + + resp, body := doWith(t, client, http.MethodPost, fmt.Sprintf("https://%s/mcp", host), "application/json", strings.NewReader(mcpCallBody)) + if resp.StatusCode != http.StatusOK || body != "ok" { + t.Fatalf("the retried request should have succeeded, got %d: %s", resp.StatusCode, body) + } + if hits := up.recorded(); len(hits) != 2 || hits[1].body != mcpCallBody { + t.Fatalf("upstream hits = %+v, want the original and a resend with the same body", hits) + } + if hit := fake.only(t); bytes.Contains(hit.body, []byte("old-token")) || bytes.Contains(hit.body, []byte("new-token")) { + t.Fatalf("a token reached the hook: %s", hit.body) + } +} diff --git a/packages/api/agent_vault.go b/packages/api/agent_vault.go index 28c9756bd..59ce65eb9 100644 --- a/packages/api/agent_vault.go +++ b/packages/api/agent_vault.go @@ -122,6 +122,35 @@ type AgentVaultService struct { PersonalValuesMissing bool `json:"personalValuesMissing"` // Any credential field is left to each member. Older servers never send it, which decodes to false. MemberProvided bool `json:"memberProvided"` + // Nil for a service sent inline with the session. + AccessBundleID *string `json:"accessBundleId"` + // Asked before each request, or each database or SSH connection. Nil when the service has none. + Hook *AgentVaultHook `json:"hook"` +} + +type AgentVaultHook struct { + URL string `json:"url"` + Secret string `json:"secret"` +} + +// Who the session is for, as Infisical recorded it when the session was created. Older servers never send it. +type AgentVaultResolvedSession struct { + Label *string `json:"label"` + Actor AgentVaultSessionActor `json:"actor"` + OnBehalfOf *AgentVaultSessionOnBehalfOf `json:"onBehalfOf"` +} + +type AgentVaultSessionActor struct { + Type string `json:"type"` + ID string `json:"id"` + Name string `json:"name"` + Email *string `json:"email"` +} + +type AgentVaultSessionOnBehalfOf struct { + ID string `json:"id"` + Name string `json:"name"` + Email *string `json:"email"` } // Pointers, so a field the server leaves out is told apart from false and keeps the safe default. @@ -149,10 +178,11 @@ type ResolveAgentVaultSessionRequest struct { } type ResolveAgentVaultSessionResponse struct { - SessionID string `json:"sessionId"` - ExpiresAt string `json:"expiresAt"` - Services []AgentVaultService `json:"services"` - SessionLogs AgentVaultSessionLogGrant `json:"sessionLogs"` + SessionID string `json:"sessionId"` + ExpiresAt string `json:"expiresAt"` + Services []AgentVaultService `json:"services"` + SessionLogs AgentVaultSessionLogGrant `json:"sessionLogs"` + Session *AgentVaultResolvedSession `json:"session"` } func CallResolveAgentVaultSession(httpClient *resty.Client, sessionToken string, request ResolveAgentVaultSessionRequest) (ResolveAgentVaultSessionResponse, error) { From 982717cb1e8c8ef4b73425e3661aaf745b4fa45d Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:53:42 +0530 Subject: [PATCH 2/8] fix(agent-vault): short batch refusals, Retry-After on MCP hook failures, hook fixes from review --- packages/agentvault/discovery.go | 5 +- packages/agentvault/hook.go | 4 +- packages/agentvault/hook_test.go | 15 ++++++ packages/agentvault/mcp_tools.go | 15 +++++- packages/agentvault/mcp_tools_test.go | 64 +++++++++++++++++++++++- packages/agentvault/proxy.go | 4 +- packages/cmd/agent_vault_connect_test.go | 12 +++++ packages/cmd/agent_vault_errors.go | 5 +- 8 files changed, 115 insertions(+), 9 deletions(-) diff --git a/packages/agentvault/discovery.go b/packages/agentvault/discovery.go index 3dd26d136..bc92f9fdd 100644 --- a/packages/agentvault/discovery.go +++ b/packages/agentvault/discovery.go @@ -222,11 +222,14 @@ func writeUncheckedProxyError(w http.ResponseWriter, status int, decision, code, writeProxyErrorAPIURL(w, status, decision, code, message, false) } +// Seconds a client waits before retrying a request the hook couldn't answer. +const hookFailedRetryAfter = "5" + func writeProxyErrorAPIURL(w http.ResponseWriter, status int, decision, code, message string, namesAPIURL bool) { w.Header().Set(HeaderDecision, decision) w.Header().Set(HeaderError, code) if code == errorCodeHookFailed { - w.Header().Set("Retry-After", "5") + w.Header().Set("Retry-After", hookFailedRetryAfter) } if apiURL := proxyAPIURL(); apiURL != "" && namesAPIURL { w.Header().Set(HeaderAPIURL, apiURL) diff --git a/packages/agentvault/hook.go b/packages/agentvault/hook.go index ef79ec166..cce01e88b 100644 --- a/packages/agentvault/hook.go +++ b/packages/agentvault/hook.go @@ -111,7 +111,7 @@ func (o *hookOutcome) sessionLogHook() *hookOutcome { if o == nil || o.Decision == hookAllow { return nil } - return o + return &hookOutcome{Decision: o.Decision, Reason: o.Reason, Cause: o.Cause, DurationMs: o.DurationMs, DeliveryID: o.DeliveryID} } func (o *hookOutcome) logFields(event *zerolog.Event) *zerolog.Event { @@ -347,7 +347,7 @@ func cleanHookText(s string, limit int) string { var b strings.Builder for _, r := range strings.ToValidUTF8(s, "") { switch { - case r == '\n' || r == '\r' || r == '\t' || r == '
' || r == '
': + case r == '\n' || r == '\r' || r == '\t' || r == '\u2028' || r == '\u2029': r = ' ' case unicode.IsControl(r) || unicode.Is(unicode.Cf, r): continue diff --git a/packages/agentvault/hook_test.go b/packages/agentvault/hook_test.go index 4371b30dc..c811473c2 100644 --- a/packages/agentvault/hook_test.go +++ b/packages/agentvault/hook_test.go @@ -8,11 +8,13 @@ import ( "crypto/x509" "encoding/base64" "encoding/json" + "errors" "io" "net" "net/http" "net/http/httptest" "net/url" + "reflect" "strconv" "strings" "sync" @@ -399,3 +401,16 @@ func TestAnHTTPSHookIsVerifiedAgainstTheTrustedRoots(t *testing.T) { t.Fatalf("a trusted certificate gave %+v (%v)", result, result.err) } } + +func TestTheSessionLogCopyOfAHookOutcomeLeavesOutTheMessage(t *testing.T) { + o := &hookOutcome{Decision: hookDeny, Reason: "needs approval", Cause: "", DurationMs: 12, DeliveryID: "d1", + message: "Ask Ana first.", err: errors.New("boom"), origin: "https://hook.example"} + got := o.sessionLogHook() + want := &hookOutcome{Decision: hookDeny, Reason: "needs approval", DurationMs: 12, DeliveryID: "d1"} + if got == o || !reflect.DeepEqual(got, want) { + t.Fatalf("got %+v", got) + } + if (&hookOutcome{Decision: hookAllow}).sessionLogHook() != nil { + t.Fatal("an allowed call was recorded") + } +} diff --git a/packages/agentvault/mcp_tools.go b/packages/agentvault/mcp_tools.go index 1ccc3ac7c..9de97eae7 100644 --- a/packages/agentvault/mcp_tools.go +++ b/packages/agentvault/mcp_tools.go @@ -56,6 +56,9 @@ func (r *mcpToolRefusal) write(w http.ResponseWriter) { if r.code != "" { w.Header().Set(HeaderError, r.code) } + if r.code == errorCodeHookFailed { + w.Header().Set("Retry-After", hookFailedRetryAfter) + } if len(r.body) > 0 { w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Length", strconv.Itoa(len(r.body))) @@ -285,6 +288,8 @@ func unlistedToolRefusal(svc *resolvedService, tool string, messages []jsonrpcMe return refusal } +const hookBatchRefusedMessage = "This batch was refused by your organization's hook." + func hookDeniedMCPRefusal(message string, messages []jsonrpcMessage, parsed, batch, streamable bool) *mcpToolRefusal { return hookMCPRefusal(message, messages, parsed, batch, streamable, decisionBlocked, errorCodeHookDenied, http.StatusForbidden) } @@ -309,15 +314,21 @@ func hookMCPRefusal(text string, messages []jsonrpcMessage, parsed, batch, strea if message.id == nil { continue } + // Only the first answer carries the hook's text, so a long message in a long batch is not copied into + // every answer. + msg := text + if len(responses) > 0 { + msg = hookBatchRefusedMessage + } if message.Method == mcpMethodToolsCall { responses = append(responses, jsonrpcToolErrorResponse{ JSONRPC: "2.0", ID: message.id, - Result: mcpToolResult{IsError: true, Content: []mcpTextContent{{Type: "text", Text: text}}}, + Result: mcpToolResult{IsError: true, Content: []mcpTextContent{{Type: "text", Text: msg}}}, }) continue } - responses = append(responses, newJSONRPCError(message.id, jsonrpcHookRefused, text)) + responses = append(responses, newJSONRPCError(message.id, jsonrpcHookRefused, msg)) } switch { diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index b7927164d..34ffc9b0e 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1093,7 +1093,7 @@ func TestAHookRefusalAnswersEveryIDInABatch(t *testing.T) { batch := `[{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"a"}},{"jsonrpc":"2.0","id":2,"method":"tools/list"},{"jsonrpc":"2.0","method":"notifications/initialized"}]` res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", batch, http.Header{"Accept": {streamableAccept}}) - want := `[{"jsonrpc":"2.0","id":1,"result":{"isError":true,"content":[{"type":"text","text":"no"}]}},{"jsonrpc":"2.0","id":2,"error":{"code":-31000,"message":"no"}}]` + want := `[{"jsonrpc":"2.0","id":1,"result":{"isError":true,"content":[{"type":"text","text":"no"}]}},{"jsonrpc":"2.0","id":2,"error":{"code":-31000,"message":"` + hookBatchRefusedMessage + `"}}]` if res.status != http.StatusOK || res.body != want { t.Fatalf("status %d body %s", res.status, res.body) } @@ -1110,6 +1110,68 @@ func TestAHookRefusalAnswersEveryIDInABatch(t *testing.T) { assertUpstreamSawNothing(t, up) } +func TestOnlyTheFirstAnswerInAHookRefusedBatchCarriesTheHooksText(t *testing.T) { + text := strings.Repeat("x", 1000) + messages := make([]jsonrpcMessage, 0, 500) + for i := range 500 { + message := jsonrpcMessage{id: json.RawMessage(strconv.Itoa(i)), mcpCall: mcpCall{Method: "tools/list"}} + if i%2 == 0 { + message.Method = mcpMethodToolsCall + } + messages = append(messages, message) + } + refusal := hookDeniedMCPRefusal(text, messages, true, true, true) + if n := strings.Count(string(refusal.body), text); n != 1 { + t.Fatalf("the hook's text appears %d times", n) + } + if n := strings.Count(string(refusal.body), hookBatchRefusedMessage); n != 499 { + t.Fatalf("the batch line appears %d times, want 499", n) + } + if len(refusal.body) > 100_000 { + t.Fatalf("body is %d bytes", len(refusal.body)) + } + if !strings.HasPrefix(string(refusal.body), `[{"jsonrpc":"2.0","id":0,"result":{"isError":true,"content":[{"type":"text","text":"`+text+`"}]}},{"jsonrpc":"2.0","id":1,"error":{"code":-31000,"message":"`+hookBatchRefusedMessage+`"}},{"jsonrpc":"2.0","id":2,"result":{"isError":true,"content":[{"type":"text","text":"`+hookBatchRefusedMessage+`"}]}}`) { + t.Fatalf("body starts %s", refusal.body[:min(len(refusal.body), 1500)]) + } +} + +func TestAnMCPHookFailureTellsTheClientWhenToRetry(t *testing.T) { + fake := newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) }) + up := newMCPUpstream(t) + client, _ := newMCPRecordingProxy(t, up, []*resolvedService{hookedMCPService(up.host(), fake.hook())}) + + res := proxiedPost(t, client, up.server.URL+"/mcp", "application/json", `{"jsonrpc":`, http.Header{"Accept": {streamableAccept}}) + assertUpstreamSawNothing(t, up) + if res.status != http.StatusServiceUnavailable || res.header.Get(HeaderError) != errorCodeHookFailed { + t.Fatalf("status %d headers %v", res.status, res.header) + } + if got := res.header.Get("Retry-After"); got != hookFailedRetryAfter { + t.Fatalf("Retry-After = %q", got) + } +} + +func TestAHookedMCPServiceWithoutAnAllowlistStillDropsAcceptEncoding(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + sawEncoding := make(chan string, 1) + upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + sawEncoding <- r.Header.Get("Accept-Encoding") + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"jsonrpc":"2.0","id":3,"result":{}}`) + })) + t.Cleanup(upstream.Close) + host := strings.TrimPrefix(upstream.URL, "https://") + _, client := newTLSProxy(t, upstream, fixedResolver{services: []*resolvedService{hookedMCPService(host, fake.hook())}}) + + res := proxiedPost(t, client, upstream.URL+"/mcp", "application/json", `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + http.Header{"Accept": {streamableAccept}, "Accept-Encoding": {"br, deflate"}}) + if got := <-sawEncoding; got != "gzip" { + t.Fatalf("upstream saw Accept-Encoding %q, want the transport's own gzip", got) + } + if res.status != http.StatusOK { + t.Fatalf("status %d body %s", res.status, res.body) + } +} + func TestAHookRefusalReachesALegacySSEStream(t *testing.T) { var fake *fakeHook fake = newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { diff --git a/packages/agentvault/proxy.go b/packages/agentvault/proxy.go index 8fa847973..3087205d4 100644 --- a/packages/agentvault/proxy.go +++ b/packages/agentvault/proxy.go @@ -975,9 +975,9 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re ) } } - if matched.kind == serviceKindMCP && matched.allowedTools != nil { + if matched.kind == serviceKindMCP && (matched.allowedTools != nil || matched.hook != nil) { // Go then asks for gzip itself and hands back the response decompressed, so a tools/list result - // can be filtered whatever encodings the agent accepts. + // can be filtered, and a legacy SSE stream watched, whatever encodings the agent accepts. req.Header.Del("Accept-Encoding") } } diff --git a/packages/cmd/agent_vault_connect_test.go b/packages/cmd/agent_vault_connect_test.go index e26cc3641..713923a86 100644 --- a/packages/cmd/agent_vault_connect_test.go +++ b/packages/cmd/agent_vault_connect_test.go @@ -367,3 +367,15 @@ func TestAConnectWarningIsCleanedBeforeItPrints(t *testing.T) { t.Fatalf("warning %q, want it to start with the service name", got) } } + +func TestAServiceRefusalCapitalisesItsFirstLetterNotItsFirstByte(t *testing.T) { + err := classifyAgentVaultConnectError(&agentvault.ProxyRefusedError{Status: http.StatusForbidden, Message: "élevé privileges are required"}, "p:1") + var cliErr *util.CLIError + if !errors.As(err, &cliErr) || cliErr.Message != "Élevé privileges are required." { + t.Fatalf("got %v", err) + } + err = classifyAgentVaultConnectError(&agentvault.ProxyRefusedError{Status: http.StatusForbidden}, "p:1") + if !errors.As(err, &cliErr) || cliErr.Message != "." { + t.Fatalf("an empty message got %v", err) + } +} diff --git a/packages/cmd/agent_vault_errors.go b/packages/cmd/agent_vault_errors.go index 9f0893556..1c4b1a1a3 100644 --- a/packages/cmd/agent_vault_errors.go +++ b/packages/cmd/agent_vault_errors.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "strings" + "unicode" + "unicode/utf8" "github.com/Infisical/infisical-merge/packages/agentvault" "github.com/Infisical/infisical-merge/packages/api" @@ -232,7 +234,8 @@ func classifyAgentVaultConnectError(err error, proxyAddr string) error { } if errors.As(err, &refused) { message := strings.TrimSuffix(refused.Message, ".") + "." - return &util.CLIError{Code: errCodeServiceRefused, Cause: err, Message: strings.ToUpper(message[:1]) + message[1:]} + first, size := utf8.DecodeRuneInString(message) + return &util.CLIError{Code: errCodeServiceRefused, Cause: err, Message: string(unicode.ToUpper(first)) + message[size:]} } return &util.CLIError{Code: errCodeProxyUnreachable, Cause: err, Message: fmt.Sprintf("The connection to the Agent Vault proxy at %s failed: %s.", proxyAddr, strings.TrimSuffix(err.Error(), "."))} From 9b5ea5c966baffa05d040cac030e0833a0db3b88 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:41:27 +0530 Subject: [PATCH 3/8] fix(agent-vault): give a refused batch's hook message to its first tool call --- packages/agentvault/mcp_tools.go | 17 +++++++++++++---- packages/agentvault/mcp_tools_test.go | 12 ++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/agentvault/mcp_tools.go b/packages/agentvault/mcp_tools.go index 9de97eae7..c8526ae53 100644 --- a/packages/agentvault/mcp_tools.go +++ b/packages/agentvault/mcp_tools.go @@ -309,15 +309,24 @@ func hookMCPRefusal(text string, messages []jsonrpcMessage, parsed, batch, strea refusal.body = marshalJSONRPC(newJSONRPCError(nil, jsonrpcHookRefused, text)) return refusal } + // One answer carries the hook's text, so a long message in a long batch is not copied into every answer. It is + // the first tool call's when there is one: clients hand a tool result to the model but may hide an error. + carrier := -1 + for i, message := range messages { + if message.id == nil { + continue + } + if carrier == -1 || (message.Method == mcpMethodToolsCall && messages[carrier].Method != mcpMethodToolsCall) { + carrier = i + } + } responses := make([]any, 0, len(messages)) - for _, message := range messages { + for i, message := range messages { if message.id == nil { continue } - // Only the first answer carries the hook's text, so a long message in a long batch is not copied into - // every answer. msg := text - if len(responses) > 0 { + if i != carrier { msg = hookBatchRefusedMessage } if message.Method == mcpMethodToolsCall { diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index 34ffc9b0e..73721f5d7 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1135,6 +1135,18 @@ func TestOnlyTheFirstAnswerInAHookRefusedBatchCarriesTheHooksText(t *testing.T) } } +func TestAHookRefusedBatchGivesTheHooksTextToTheFirstToolCall(t *testing.T) { + messages := []jsonrpcMessage{ + {id: json.RawMessage("1"), mcpCall: mcpCall{Method: "tools/list"}}, + {id: json.RawMessage("2"), mcpCall: mcpCall{Method: mcpMethodToolsCall, Tool: "merge"}}, + } + refusal := hookDeniedMCPRefusal("Ask Ana first.", messages, true, true, true) + want := `[{"jsonrpc":"2.0","id":1,"error":{"code":-31000,"message":"` + hookBatchRefusedMessage + `"}},{"jsonrpc":"2.0","id":2,"result":{"isError":true,"content":[{"type":"text","text":"Ask Ana first."}]}}]` + if string(refusal.body) != want { + t.Fatalf("body %s", refusal.body) + } +} + func TestAnMCPHookFailureTellsTheClientWhenToRetry(t *testing.T) { fake := newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) }) up := newMCPUpstream(t) From 717ecdb01ded4820b37aece9f2f09eeb14849131 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:59:39 +0530 Subject: [PATCH 4/8] fix(agent-vault): leave the hook's MCP summary unparsed for a non-UTF-8 body --- packages/agentvault/hook.go | 9 +++++---- packages/agentvault/mcp_tools_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/agentvault/hook.go b/packages/agentvault/hook.go index cce01e88b..f13bf32bb 100644 --- a/packages/agentvault/hook.go +++ b/packages/agentvault/hook.go @@ -439,10 +439,11 @@ func hookHeaders(header http.Header) map[string][]string { return out } -// The messages in a body the proxy read whole, so a refusal can answer each one. -func hookMCPOf(prefix []byte, truncated bool) (*hookPayloadMCP, []jsonrpcMessage) { +// The messages in a body the proxy read whole, so a refusal can answer each one. A body declared in a charset other +// than UTF-8 is left unparsed, for the reason requestMediaType gives: the server may decode a different tool name. +func hookMCPOf(prefix []byte, truncated bool, header http.Header) (*hookPayloadMCP, []jsonrpcMessage) { out := &hookPayloadMCP{Calls: []hookMCPCall{}} - if truncated { + if _, isUTF8 := requestMediaType(header); truncated || !isUTF8 { return out, nil } messages, ok := parseJSONRPCMessages(prefix) @@ -479,7 +480,7 @@ func (ps *proxyServer) askHook(req *http.Request, t tunnel, svc *resolvedService } var messages []jsonrpcMessage if svc.kind == serviceKindMCP { - payload.MCP, messages = hookMCPOf(prefix, truncated) + payload.MCP, messages = hookMCPOf(prefix, truncated, req.Header) } result := sharedHookClient().ask(req.Context(), svc.hook, &payload) diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index 73721f5d7..1c6513ef5 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1135,6 +1135,18 @@ func TestOnlyTheFirstAnswerInAHookRefusedBatchCarriesTheHooksText(t *testing.T) } } +func TestTheHooksMCPSummaryIsLeftUnparsedForANonUTF8Charset(t *testing.T) { + body := []byte(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_file"}}`) + utf7 := http.Header{"Content-Type": {"application/json; charset=utf-7"}} + if mcp, messages := hookMCPOf(body, false, utf7); mcp.Parsed || len(mcp.Calls) != 0 || messages != nil { + t.Fatalf("utf-7 body summarised: %+v", mcp) + } + plain := http.Header{"Content-Type": {"application/json; charset=utf-8"}} + if mcp, _ := hookMCPOf(body, false, plain); !mcp.Parsed || len(mcp.Calls) != 1 || mcp.Calls[0].Tool != "read_file" { + t.Fatalf("utf-8 body not summarised: %+v", mcp) + } +} + func TestAHookRefusedBatchGivesTheHooksTextToTheFirstToolCall(t *testing.T) { messages := []jsonrpcMessage{ {id: json.RawMessage("1"), mcpCall: mcpCall{Method: "tools/list"}}, From 7e7935e354fdc0599f1ab412fe09cb8446c2fa20 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:09:05 +0530 Subject: [PATCH 5/8] fix(agent-vault): log the fixed refusal text for MCP hook refusals, not the hook's message --- packages/agentvault/mcp_tools.go | 8 +++++++- packages/agentvault/mcp_tools_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/agentvault/mcp_tools.go b/packages/agentvault/mcp_tools.go index c8526ae53..b8ff76828 100644 --- a/packages/agentvault/mcp_tools.go +++ b/packages/agentvault/mcp_tools.go @@ -303,7 +303,13 @@ func hookFailedMCPRefusal(messages []jsonrpcMessage, parsed, batch, streamable b // model to act on. Every other request gets a JSON-RPC error with the same text, and delivery follows // unlistedToolRefusal, including on the event stream of a legacy SSE client. func hookMCPRefusal(text string, messages []jsonrpcMessage, parsed, batch, streamable bool, decision, code string, status int) *mcpToolRefusal { - refusal := &mcpToolRefusal{status: http.StatusOK, reason: text, decision: decision, code: code} + // The error is what the request log line records, so it is the same fixed text an HTTP refusal logs: the hook's + // message is for the agent, and its reason already goes on the log line. + reason := errHookFailed.Error() + if code == errorCodeHookDenied { + reason = errHookDenied.Error() + } + refusal := &mcpToolRefusal{status: http.StatusOK, reason: reason, decision: decision, code: code} if !parsed { refusal.status = status refusal.body = marshalJSONRPC(newJSONRPCError(nil, jsonrpcHookRefused, text)) diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index 1c6513ef5..2f25368c3 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1135,6 +1135,20 @@ func TestOnlyTheFirstAnswerInAHookRefusedBatchCarriesTheHooksText(t *testing.T) } } +func TestAnMCPHookRefusalLogsTheFixedTextNotTheHooksMessage(t *testing.T) { + messages := []jsonrpcMessage{{id: json.RawMessage("1"), mcpCall: mcpCall{Method: mcpMethodToolsCall, Tool: "merge"}}} + denied := hookDeniedMCPRefusal("Customer Ana Silva's refund needs approval.", messages, true, false, true) + if denied.Error() != errHookDenied.Error() { + t.Fatalf("deny logs %q", denied.Error()) + } + if !strings.Contains(string(denied.body), "Customer Ana Silva's refund needs approval.") { + t.Fatalf("the agent lost the message: %s", denied.body) + } + if failed := hookFailedMCPRefusal(messages, true, false, true); failed.Error() != errHookFailed.Error() { + t.Fatalf("failure logs %q", failed.Error()) + } +} + func TestTheHooksMCPSummaryIsLeftUnparsedForANonUTF8Charset(t *testing.T) { body := []byte(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_file"}}`) utf7 := http.Header{"Content-Type": {"application/json; charset=utf-7"}} From 3fcf514d710b557e9f6d1f4fdb433008edb8c6d3 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Fri, 13 Nov 2026 04:16:47 +0530 Subject: [PATCH 6/8] fix(agent-vault): log a hook redirect to an unparsable target as a redirect, without the target --- packages/agentvault/hook.go | 4 ++++ packages/agentvault/hook_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/agentvault/hook.go b/packages/agentvault/hook.go index f13bf32bb..aee5265a0 100644 --- a/packages/agentvault/hook.go +++ b/packages/agentvault/hook.go @@ -334,6 +334,10 @@ func hookTransportCause(ctx context.Context, err error) (string, error) { if errors.As(err, &urlErr) { err = urlErr.Err } + // Go parses a redirect's Location before CheckRedirect runs, and quotes it when it can't. + if strings.HasPrefix(err.Error(), "failed to parse Location header") { + return hookCauseRedirect, errors.New("it answered with a redirect, which the proxy doesn't follow") + } var netErr net.Error if errors.Is(ctx.Err(), context.DeadlineExceeded) || (errors.As(err, &netErr) && netErr.Timeout()) { return hookCauseTimeout, err diff --git a/packages/agentvault/hook_test.go b/packages/agentvault/hook_test.go index c811473c2..1ce10732b 100644 --- a/packages/agentvault/hook_test.go +++ b/packages/agentvault/hook_test.go @@ -215,6 +215,26 @@ func TestAHookRedirectIsNotFollowed(t *testing.T) { } } +func TestAHookRedirectToABadURLLogsNoTarget(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Location", "/tok-in-redirect/%zz?x=tok-in-query") + w.WriteHeader(http.StatusTemporaryRedirect) + })) + defer srv.Close() + u, _ := url.Parse(srv.URL + "/hook") + + result := newHookClient(nil).ask(context.Background(), &serviceHook{url: u, secret: []byte(testHookSecret)}, testHookPayload()) + if result.Decision != hookFailed || result.Cause != hookCauseRedirect { + t.Fatalf("outcome = %+v (%v)", result, result.err) + } + var out bytes.Buffer + logger := zerolog.New(&out) + result.logFields(logger.Error()).Msg("agent-vault: request") + if strings.Contains(out.String(), "tok-in-redirect") || strings.Contains(out.String(), "tok-in-query") { + t.Fatalf("the redirect target reached the log: %s", out.String()) + } +} + func TestHookAnswersThatAreNotAllowOrDenyFail(t *testing.T) { for _, tc := range []struct { name string From 547095eb02aae26995c704226657784cb10634c2 Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Fri, 13 Nov 2026 05:38:42 +0530 Subject: [PATCH 7/8] improvement(agent-vault): watch only legacy SSE streams for hook refusals, share batch, text-cut and Retry-After helpers --- packages/agentvault/discovery.go | 11 +++++-- packages/agentvault/hook.go | 1 + packages/agentvault/mcp_tools.go | 12 ++++--- packages/agentvault/mcp_tools_test.go | 47 +++++++++++++++++---------- packages/agentvault/native_logger.go | 6 +--- packages/agentvault/proxy.go | 4 +-- 6 files changed, 49 insertions(+), 32 deletions(-) diff --git a/packages/agentvault/discovery.go b/packages/agentvault/discovery.go index bc92f9fdd..7c6e0eabc 100644 --- a/packages/agentvault/discovery.go +++ b/packages/agentvault/discovery.go @@ -225,12 +225,17 @@ func writeUncheckedProxyError(w http.ResponseWriter, status int, decision, code, // Seconds a client waits before retrying a request the hook couldn't answer. const hookFailedRetryAfter = "5" +// Shared by every refusal writer, so the same code always carries the same headers. +func setRetryAfter(h http.Header, code string) { + if code == errorCodeHookFailed { + h.Set("Retry-After", hookFailedRetryAfter) + } +} + func writeProxyErrorAPIURL(w http.ResponseWriter, status int, decision, code, message string, namesAPIURL bool) { w.Header().Set(HeaderDecision, decision) w.Header().Set(HeaderError, code) - if code == errorCodeHookFailed { - w.Header().Set("Retry-After", hookFailedRetryAfter) - } + setRetryAfter(w.Header(), code) if apiURL := proxyAPIURL(); apiURL != "" && namesAPIURL { w.Header().Set(HeaderAPIURL, apiURL) } diff --git a/packages/agentvault/hook.go b/packages/agentvault/hook.go index aee5265a0..3d4375399 100644 --- a/packages/agentvault/hook.go +++ b/packages/agentvault/hook.go @@ -364,6 +364,7 @@ func cleanHookText(s string, limit int) string { return strings.TrimSpace(b.String()) } +// Cut on a character boundary, so the text stays valid. func capText(s string, limit int) string { if len(s) <= limit { return s diff --git a/packages/agentvault/mcp_tools.go b/packages/agentvault/mcp_tools.go index b8ff76828..a6dfb0472 100644 --- a/packages/agentvault/mcp_tools.go +++ b/packages/agentvault/mcp_tools.go @@ -56,9 +56,7 @@ func (r *mcpToolRefusal) write(w http.ResponseWriter) { if r.code != "" { w.Header().Set(HeaderError, r.code) } - if r.code == errorCodeHookFailed { - w.Header().Set("Retry-After", hookFailedRetryAfter) - } + setRetryAfter(w.Header(), r.code) if len(r.body) > 0 { w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Length", strconv.Itoa(len(r.body))) @@ -142,7 +140,7 @@ func checkToolAllowlist(svc *resolvedService, req *http.Request, logging bool) ( if logging { detail = mcpDetailOf(messages) } - batch := bytes.HasPrefix(bytes.TrimSpace(body), []byte("[")) + batch := isBatch(body) if routingHeaders && !routingHeadersMatch(req.Header, messages, batch) { return detail, unreadableToolsRequest(svc.name) } @@ -404,6 +402,12 @@ func marshalJSONRPC(v any) []byte { return b } +// Only a legacy SSE client reads a hook's refusal from its event stream; a Streamable HTTP client gets it as the +// answer to its POST, so its streams need no watching. +func watchesHookStream(svc *resolvedService, req *http.Request) bool { + return svc.hook != nil && !streamableHTTPClient(req) +} + func streamableHTTPClient(req *http.Request) bool { var acceptsJSON, acceptsStream bool for _, value := range req.Header.Values("Accept") { diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index 2f25368c3..bceb8b963 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1188,25 +1188,36 @@ func TestAnMCPHookFailureTellsTheClientWhenToRetry(t *testing.T) { } } -func TestAHookedMCPServiceWithoutAnAllowlistStillDropsAcceptEncoding(t *testing.T) { - fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) - sawEncoding := make(chan string, 1) - upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - sawEncoding <- r.Header.Get("Accept-Encoding") - w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"jsonrpc":"2.0","id":3,"result":{}}`) - })) - t.Cleanup(upstream.Close) - host := strings.TrimPrefix(upstream.URL, "https://") - _, client := newTLSProxy(t, upstream, fixedResolver{services: []*resolvedService{hookedMCPService(host, fake.hook())}}) +func TestAHookedMCPServiceWithoutAnAllowlistDropsAcceptEncodingOnlyForLegacyClients(t *testing.T) { + for _, tc := range []struct { + name string + accept string + want string + }{ + {"streamable HTTP keeps the agent's encodings", streamableAccept, "br, deflate"}, + {"legacy SSE gets the transport's own gzip", "application/json", "gzip"}, + } { + t.Run(tc.name, func(t *testing.T) { + fake := newFakeHook(t, answerHook(`{"decision":"allow"}`)) + sawEncoding := make(chan string, 1) + upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + sawEncoding <- r.Header.Get("Accept-Encoding") + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"jsonrpc":"2.0","id":3,"result":{}}`) + })) + t.Cleanup(upstream.Close) + host := strings.TrimPrefix(upstream.URL, "https://") + _, client := newTLSProxy(t, upstream, fixedResolver{services: []*resolvedService{hookedMCPService(host, fake.hook())}}) - res := proxiedPost(t, client, upstream.URL+"/mcp", "application/json", `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, - http.Header{"Accept": {streamableAccept}, "Accept-Encoding": {"br, deflate"}}) - if got := <-sawEncoding; got != "gzip" { - t.Fatalf("upstream saw Accept-Encoding %q, want the transport's own gzip", got) - } - if res.status != http.StatusOK { - t.Fatalf("status %d body %s", res.status, res.body) + res := proxiedPost(t, client, upstream.URL+"/mcp", "application/json", `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + http.Header{"Accept": {tc.accept}, "Accept-Encoding": {"br, deflate"}}) + if got := <-sawEncoding; got != tc.want { + t.Fatalf("upstream saw Accept-Encoding %q, want %q", got, tc.want) + } + if res.status != http.StatusOK { + t.Fatalf("status %d body %s", res.status, res.body) + } + }) } } diff --git a/packages/agentvault/native_logger.go b/packages/agentvault/native_logger.go index bd4c1af68..b5ae9e1b4 100644 --- a/packages/agentvault/native_logger.go +++ b/packages/agentvault/native_logger.go @@ -389,12 +389,8 @@ func appendCapped(dst *string, s string, max int, truncated *bool) { return } if len(s) > room { - // Cut on a character boundary, so the record stays valid text. - for room > 0 && !utf8.RuneStart(s[room]) { - room-- - } // A copy, or the kept prefix would hold the whole original in memory while the spool counts only the prefix. - s = strings.Clone(s[:room]) + s = strings.Clone(capText(s, room)) *truncated = true } *dst += s diff --git a/packages/agentvault/proxy.go b/packages/agentvault/proxy.go index 3087205d4..17674e034 100644 --- a/packages/agentvault/proxy.go +++ b/packages/agentvault/proxy.go @@ -975,7 +975,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re ) } } - if matched.kind == serviceKindMCP && (matched.allowedTools != nil || matched.hook != nil) { + if matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchesHookStream(matched, req)) { // Go then asks for gzip itself and hands back the response decompressed, so a tools/list result // can be filtered, and a legacy SSE stream watched, whatever encodings the agent accepts. req.Header.Del("Accept-Encoding") @@ -1012,7 +1012,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re return nil, matched, outcome, err } } - if matched != nil && matched.kind == serviceKindMCP && (matched.allowedTools != nil || matched.hook != nil) { + if matched != nil && matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchesHookStream(matched, req)) { if stream := hideUnlistedTools(resp, matched.allowedTools); stream != nil { ps.sseStreams.watch(stream, sessionToken, matched.id, *req.URL) } From d5d5011e78ae1db4e16b56e33ee3bf6f192c830c Mon Sep 17 00:00:00 2001 From: saif <11242541+saifsmailbox98@users.noreply.github.com> Date: Fri, 13 Nov 2026 06:17:37 +0530 Subject: [PATCH 8/8] fix(agent-vault): decide whether to watch a hook's SSE stream from the agent's own Accept header --- packages/agentvault/mcp_tools_test.go | 36 +++++++++++++++++++++++++++ packages/agentvault/proxy.go | 8 ++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/agentvault/mcp_tools_test.go b/packages/agentvault/mcp_tools_test.go index bceb8b963..744cc0181 100644 --- a/packages/agentvault/mcp_tools_test.go +++ b/packages/agentvault/mcp_tools_test.go @@ -1188,6 +1188,42 @@ func TestAnMCPHookFailureTellsTheClientWhenToRetry(t *testing.T) { } } +func TestAHookRefusalReachesALegacySSEStreamWhenTheServiceSetsAccept(t *testing.T) { + var fake *fakeHook + fake = newFakeHook(t, func(w http.ResponseWriter, _ *http.Request) { + calls := fake.calls() + var p hookPayload + if json.Unmarshal(calls[len(calls)-1].body, &p) == nil && p.Request != nil && p.Request.Method == http.MethodGet { + _, _ = io.WriteString(w, `{"decision":"allow"}`) + return + } + _, _ = io.WriteString(w, `{"decision":"deny","message":"Ask Ana first."}`) + }) + upstream, _ := newLegacySSEUpstream(t) + host := strings.TrimPrefix(upstream.URL, "https://") + svc := hookedMCPService(host, fake.hook()) + svc.customHeaders = []customHeader{{name: "Accept", value: []byte(streamableAccept)}} + _, client := newTLSProxy(t, upstream, fixedResolver{services: []*resolvedService{svc}}) + + resp, err := client.Get(upstream.URL + "/sse") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + stream := bufio.NewReader(resp.Body) + if event := readSSEEvent(t, stream); !strings.Contains(event, "/messages?sessionId=abc") { + t.Fatalf("endpoint event = %q", event) + } + + res := proxiedPost(t, client, upstream.URL+"/messages?sessionId=abc", "application/json", `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"merge"}}`, nil) + if res.status != http.StatusAccepted || res.header.Get(HeaderError) != errorCodeHookDenied { + t.Fatalf("status %d body %s headers %v", res.status, res.body, res.header) + } + if event := readSSEEvent(t, stream); !strings.Contains(event, "Ask Ana first.") { + t.Fatalf("event = %q", event) + } +} + func TestAHookedMCPServiceWithoutAnAllowlistDropsAcceptEncodingOnlyForLegacyClients(t *testing.T) { for _, tc := range []struct { name string diff --git a/packages/agentvault/proxy.go b/packages/agentvault/proxy.go index 17674e034..2f348bfec 100644 --- a/packages/agentvault/proxy.go +++ b/packages/agentvault/proxy.go @@ -878,6 +878,9 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re // A credential that cannot be refreshed, injected into an MCP request: an upstream 401 then means the // stored credential is wrong, which the agent cannot fix by logging in itself. staticMCP := false + // Decided from the agent's own Accept, before a custom header or the credential can replace it, so it agrees + // with how a hook refusal is delivered. + watchHookStream := false if cfg.blocksOffBundle(matched, hostname, port) { return nil, nil, outcome, fmt.Errorf("no service covers host %q: %w", hostname, errHostBlocked) @@ -914,6 +917,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re } if matched != nil { + watchHookStream = watchesHookStream(matched, req) // Before anything is injected, so the names it records can only have come from the agent. Parsed // only for a session log or a tool allowlist; with neither the body is still buffered for an OAuth // credential, so a rejected token's request can be sent again. @@ -975,7 +979,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re ) } } - if matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchesHookStream(matched, req)) { + if matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchHookStream) { // Go then asks for gzip itself and hands back the response decompressed, so a tools/list result // can be filtered, and a legacy SSE stream watched, whatever encodings the agent accepts. req.Header.Del("Accept-Encoding") @@ -1012,7 +1016,7 @@ func (ps *proxyServer) forward(req *http.Request, t tunnel) (*http.Response, *re return nil, matched, outcome, err } } - if matched != nil && matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchesHookStream(matched, req)) { + if matched != nil && matched.kind == serviceKindMCP && (matched.allowedTools != nil || watchHookStream) { if stream := hideUnlistedTools(resp, matched.allowedTools); stream != nil { ps.sseStreams.watch(stream, sessionToken, matched.id, *req.URL) }