Compare commits

..
Author SHA1 Message Date
BaaRai 9f892e4e95 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.
2026-11-19 15:47:36 +01:00
6 changed files with 165 additions and 6 deletions
+1
View File
@@ -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()`.
+12
View File
@@ -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()
+6 -4
View File
@@ -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()
+25
View File
@@ -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)
+35 -2
View File
@@ -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
+86
View File
@@ -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")
}
}