mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
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.
This commit is contained in:
+35
-2
@@ -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
|
||||
|
||||
@@ -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