From 9f892e4e95448e85d340da31b276414516ddc8a8 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:46:56 +0100 Subject: [PATCH] fix(intercept): make target pins name-only behind any proxy The gate treated a call as direct whenever only the recording proxy or a custom tool's own proxy was in the path, so it stored IP pins the proxy did not enforce. ProxyAddr now covers the recording and global proxies, a per-tool check covers a custom http tool's own proxy, and the gate marks the pin name-only for any of them. Closes #368. --- CHANGELOG.md | 1 + intercept/intercept.go | 12 +++++ intercept/target_gate.go | 10 ++-- intercept/target_gate_test.go | 25 +++++++++ server/manager.go | 37 ++++++++++++- server/manager_tool_proxy_test.go | 86 +++++++++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 server/manager_tool_proxy_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7671b7e..381d3fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **Target gate behind proxies**: a target approval is name-only whenever a proxy is in the path, including the recording proxy, the global egress proxy and a custom tool's own proxy, so the gate never claims an IP binding the proxy does not enforce. - **Remote MCP response bound**: Streamable HTTP JSON-RPC bodies and SSE data payloads are refused above 8 MiB, so a remote MCP server cannot force an unbounded decode. - **LLM response capture bound**: each recorded provider attempt keeps at most 1 MiB, and a call keeps at most 8 attempts, so a large or retrying stream cannot grow the recorder without bound. The live stream is unchanged. - **Intercept audit ring**: `preToolUse` records allow only after intercept and the target gate decide, so a blocked call no longer leaves a leftover allow row in `Guard.Audit()`. diff --git a/intercept/intercept.go b/intercept/intercept.go index 70bbf90..c765f02 100644 --- a/intercept/intercept.go +++ b/intercept/intercept.go @@ -211,6 +211,9 @@ type Interceptor struct { // upstreamProxy reports whether target traffic leaves through an upstream // egress proxy (nil = no). Injected by the server layer. upstreamProxy func() bool + // toolProxy reports whether one tool's own configuration routes its traffic + // through a proxy (nil = no). Injected by the server layer. + toolProxy func(string) bool // derivedExempt lists the endpoints of the platform's own services (nil = // none). Injected by the server layer. See SetDerivedExemptions. derivedExempt func() []string @@ -234,6 +237,15 @@ func (i *Interceptor) SetUpstreamProxyCheck(f func() bool) { i.mu.Unlock() } +// SetToolProxyCheck installs the function that reports whether a tool sends its +// own traffic through a proxy. The proxy then chooses the address it connects +// to, so a target approval for that call is name-only. Passing nil disables it. +func (i *Interceptor) SetToolProxyCheck(f func(string) bool) { + i.mu.Lock() + i.toolProxy = f + i.mu.Unlock() +} + // SetReviewer installs the LLM fallback judge callback. Passing nil disables it. func (i *Interceptor) SetReviewer(r Reviewer) { i.mu.Lock() diff --git a/intercept/target_gate.go b/intercept/target_gate.go index de7b8e2..bd2a475 100644 --- a/intercept/target_gate.go +++ b/intercept/target_gate.go @@ -208,11 +208,13 @@ func (c *targetCheck) run(targets []Target) ([]TargetRequest, TargetResult) { i.mu.RLock() proxied := i.upstreamProxy + toolProxy := i.toolProxy i.mu.RUnlock() - // Behind the upstream proxy the proxy chooses the address it connects to, so - // every pin is name-only for this call, whatever the pin row says: the proxy - // can be turned on after a pin was approved. - nameOnly := proxied != nil && proxied() + // Behind a proxy the proxy chooses the address it connects to, so every pin + // is name-only for this call, whatever the pin row says: the proxy can be + // turned on after a pin was approved. The recording proxy, the global egress + // proxy and a custom tool's own proxy all count. + nameOnly := (proxied != nil && proxied()) || (toolProxy != nil && toolProxy(c.tool)) var unpinned []TargetRequest var uses []func() diff --git a/intercept/target_gate_test.go b/intercept/target_gate_test.go index a4d4376..8665121 100644 --- a/intercept/target_gate_test.go +++ b/intercept/target_gate_test.go @@ -975,6 +975,31 @@ func TestUpstreamProxyMakesPinsNameOnlyAtCallTime(t *testing.T) { g.passes("name-only pin, no proxy", g.fetch(appURL)) } +// A custom tool that carries its own proxy cannot enforce an approved IP, so +// its pins are name-only at call time, the same as behind the upstream proxy. +func TestToolProxyMakesPinsNameOnlyAtCallTime(t *testing.T) { + g := newGate(t) + proxied := false + g.ic.SetToolProxyCheck(func(string) bool { return proxied }) + const tool = "my_http_tool" + const input = `{"url":"https://app.example.com/login"}` + g.res.set("app.example.com", "203.0.113.7") + g.pin("app.example.com", 443, "203.0.113.7") + + // The address changes. A direct call asks. + g.res.set("app.example.com", "198.51.100.99") + g.expire() + g.requests("changed address, direct tool", g.check(tool, input)) + + // The same pin covers the name for the proxied tool, and the first-use row + // records that the connection was not bound. + proxied = true + g.passes("changed address, proxied tool", g.check(tool, input)) + if got := g.store.outcomes(); len(got) != 1 || got[0] != "allowed/pin_match" || !g.store.logged[0].Audit.Targets[0].NameOnlyEffective { + t.Fatalf("rows %v, target %+v; want a name-only first use", got, g.store.logged[0].Audit.Targets[0]) + } +} + // The first call that a pin version covers is logged once per process. func TestPinFirstUseLoggedOncePerVersion(t *testing.T) { g := newGate(t) diff --git a/server/manager.go b/server/manager.go index f314626..650625e 100644 --- a/server/manager.go +++ b/server/manager.go @@ -430,8 +430,11 @@ func NewManager(dir, proxyAddr string) (*Manager, error) { log.Printf("[llmusage] create table: %v", err) } m := &Manager{dir: dir, pg: pg, assets: pg.Assets(), tasks: map[string]*Task{}, interceptor: intercept.New(pg)} - // Behind the global egress proxy a target approval is name-only. - m.interceptor.SetUpstreamProxyCheck(func() bool { return m.GlobalProxy() != "" }) + // Behind any proxy in the path (the recording proxy, the global egress proxy, + // or a custom tool's own proxy) the proxy chooses the address, so a target + // approval is name-only. + m.interceptor.SetUpstreamProxyCheck(func() bool { return m.ProxyAddr() != "" }) + m.interceptor.SetToolProxyCheck(m.toolUsesProxy) // The platform's own endpoints need no target approval. m.interceptor.SetDerivedExemptions(m.targetGateEndpoints) if proxyAddr != "" { @@ -832,6 +835,36 @@ func (m *Manager) ProxyAddr() string { return m.globalProxy } +// toolUsesProxy reports whether a custom tool's own configuration sends its +// traffic through a proxy. That call cannot enforce an approved IP, so the +// target gate treats its pins as name-only. A recording-proxy tool is also +// covered by ProxyAddr() when capture is on; the per-tool check covers a +// tool-specific proxy while capture is off. +func (m *Manager) toolUsesProxy(key string) bool { + if m.pg == nil { + return false + } + t, err := m.pg.GetTool(key) + if err != nil { + // A failed read cannot prove the call is direct; treat it as proxied. + return true + } + if t == nil || t.Kind != "http" { + return false + } + var spec struct { + Proxy string `json:"proxy"` + UseRecordingProxy bool `json:"use_recording_proxy"` + } + if json.Unmarshal(t.Exec, &spec) != nil { + return false + } + if strings.TrimSpace(spec.Proxy) != "" { + return true + } + return spec.UseRecordingProxy && m.ProxyAddr() != "" +} + // ProxyCACert returns the CA cert path agents must trust to verify HTTPS through // the egress proxy. Non-empty ONLY when traffic capture is on (the MITM re-signs // certs): the global proxy used directly (capture off) is a plain forwarder that diff --git a/server/manager_tool_proxy_test.go b/server/manager_tool_proxy_test.go new file mode 100644 index 0000000..af79b30 --- /dev/null +++ b/server/manager_tool_proxy_test.go @@ -0,0 +1,86 @@ +package server + +import ( + "encoding/json" + "fmt" + "testing" + "time" + + "github.com/Autumn-27/artex/db" + "github.com/Autumn-27/artex/traffic" +) + +// ProxyAddr is the one source of truth for "is there a proxy in the path": +// the recording proxy while capture is on, the global proxy otherwise. +func TestProxyAddrCoversRecordingAndGlobal(t *testing.T) { + m := &Manager{} + if got := m.ProxyAddr(); got != "" { + t.Fatalf("empty manager ProxyAddr = %q", got) + } + m.globalProxy = "http://127.0.0.1:9" + if got := m.ProxyAddr(); got != m.globalProxy { + t.Fatalf("global ProxyAddr = %q", got) + } + + tr, err := traffic.Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + m.traffic = tr + m.trafficOn = true + if got := m.ProxyAddr(); got == "" { + t.Fatal("recording proxy not returned by ProxyAddr") + } +} + +// A custom http tool with its own proxy cannot enforce an approved IP; a +// recording-proxy tool counts only when a proxy address is actually in the path. +func TestToolUsesProxy(t *testing.T) { + g := newGateServer(t) + pg := g.m.pg + suffix := fmt.Sprintf("_%d", time.Now().UnixNano()) + add := func(key, kind string, exec map[string]any) string { + t.Helper() + key += suffix + raw, err := json.Marshal(exec) + if err != nil { + t.Fatal(err) + } + if err := pg.CreateCustomTool(&db.Tool{Key: key, Kind: kind, Enabled: true, Exec: raw}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = pg.DeleteCustomTool(key) }) + return key + } + + proxied := add("t_proxy_http", "http", map[string]any{ + "url": "http://example.com/", "proxy": "http://127.0.0.1:9"}) + direct := add("t_direct_http", "http", map[string]any{"url": "http://example.com/"}) + recording := add("t_rec_http", "http", map[string]any{ + "url": "http://example.com/", "use_recording_proxy": true}) + command := add("t_proxy_cmd", "command", map[string]any{ + "command": "curl http://example.com/", "proxy": "http://127.0.0.1:9"}) + + if !g.m.toolUsesProxy(proxied) { + t.Fatal("per-tool proxy not detected") + } + if g.m.toolUsesProxy(direct) { + t.Fatal("direct http tool reported as proxied") + } + if g.m.toolUsesProxy(recording) { + t.Fatal("recording tool reported as proxied with capture off and no global proxy") + } + if g.m.toolUsesProxy(command) { + t.Fatal("command tool reported as proxied") + } + if g.m.toolUsesProxy("t_missing" + suffix) { + t.Fatal("missing tool reported as proxied") + } + + // With a proxy address in the path, the recording flag counts. + g.m.globalProxy = "http://127.0.0.1:9" + if !g.m.toolUsesProxy(recording) { + t.Fatal("recording tool not proxied with a global proxy") + } +}