mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f892e4e95 |
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
|
|||||||
|
|
||||||
### Fixed
|
### 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.
|
- **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.
|
- **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()`.
|
- **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()`.
|
||||||
|
|||||||
@@ -211,6 +211,9 @@ type Interceptor struct {
|
|||||||
// upstreamProxy reports whether target traffic leaves through an upstream
|
// upstreamProxy reports whether target traffic leaves through an upstream
|
||||||
// egress proxy (nil = no). Injected by the server layer.
|
// egress proxy (nil = no). Injected by the server layer.
|
||||||
upstreamProxy func() bool
|
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 =
|
// derivedExempt lists the endpoints of the platform's own services (nil =
|
||||||
// none). Injected by the server layer. See SetDerivedExemptions.
|
// none). Injected by the server layer. See SetDerivedExemptions.
|
||||||
derivedExempt func() []string
|
derivedExempt func() []string
|
||||||
@@ -234,6 +237,15 @@ func (i *Interceptor) SetUpstreamProxyCheck(f func() bool) {
|
|||||||
i.mu.Unlock()
|
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.
|
// SetReviewer installs the LLM fallback judge callback. Passing nil disables it.
|
||||||
func (i *Interceptor) SetReviewer(r Reviewer) {
|
func (i *Interceptor) SetReviewer(r Reviewer) {
|
||||||
i.mu.Lock()
|
i.mu.Lock()
|
||||||
|
|||||||
@@ -208,11 +208,13 @@ func (c *targetCheck) run(targets []Target) ([]TargetRequest, TargetResult) {
|
|||||||
|
|
||||||
i.mu.RLock()
|
i.mu.RLock()
|
||||||
proxied := i.upstreamProxy
|
proxied := i.upstreamProxy
|
||||||
|
toolProxy := i.toolProxy
|
||||||
i.mu.RUnlock()
|
i.mu.RUnlock()
|
||||||
// Behind the upstream proxy the proxy chooses the address it connects to, so
|
// Behind a proxy the proxy chooses the address it connects to, so every pin
|
||||||
// every pin is name-only for this call, whatever the pin row says: the proxy
|
// is name-only for this call, whatever the pin row says: the proxy can be
|
||||||
// can be turned on after a pin was approved.
|
// turned on after a pin was approved. The recording proxy, the global egress
|
||||||
nameOnly := proxied != nil && proxied()
|
// proxy and a custom tool's own proxy all count.
|
||||||
|
nameOnly := (proxied != nil && proxied()) || (toolProxy != nil && toolProxy(c.tool))
|
||||||
|
|
||||||
var unpinned []TargetRequest
|
var unpinned []TargetRequest
|
||||||
var uses []func()
|
var uses []func()
|
||||||
|
|||||||
@@ -975,6 +975,31 @@ func TestUpstreamProxyMakesPinsNameOnlyAtCallTime(t *testing.T) {
|
|||||||
g.passes("name-only pin, no proxy", g.fetch(appURL))
|
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.
|
// The first call that a pin version covers is logged once per process.
|
||||||
func TestPinFirstUseLoggedOncePerVersion(t *testing.T) {
|
func TestPinFirstUseLoggedOncePerVersion(t *testing.T) {
|
||||||
g := newGate(t)
|
g := newGate(t)
|
||||||
|
|||||||
+35
-2
@@ -430,8 +430,11 @@ func NewManager(dir, proxyAddr string) (*Manager, error) {
|
|||||||
log.Printf("[llmusage] create table: %v", err)
|
log.Printf("[llmusage] create table: %v", err)
|
||||||
}
|
}
|
||||||
m := &Manager{dir: dir, pg: pg, assets: pg.Assets(), tasks: map[string]*Task{}, interceptor: intercept.New(pg)}
|
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.
|
// Behind any proxy in the path (the recording proxy, the global egress proxy,
|
||||||
m.interceptor.SetUpstreamProxyCheck(func() bool { return m.GlobalProxy() != "" })
|
// 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.
|
// The platform's own endpoints need no target approval.
|
||||||
m.interceptor.SetDerivedExemptions(m.targetGateEndpoints)
|
m.interceptor.SetDerivedExemptions(m.targetGateEndpoints)
|
||||||
if proxyAddr != "" {
|
if proxyAddr != "" {
|
||||||
@@ -832,6 +835,36 @@ func (m *Manager) ProxyAddr() string {
|
|||||||
return m.globalProxy
|
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
|
// 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
|
// 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
|
// certs): the global proxy used directly (capture off) is a plain forwarder that
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user