mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
With the target gate on, only the tool URL was checked and the proxy in the tool's proxy field was dialled without a check, so an approved target could be fetched through any host and port. Read the proxy as a target, as a proxy named on a Bash command line is: the call waits for its approval, the request records the proxy without credentials, and the proxy is dialled through the pinned dialer. The recording and global proxies, required mode and the gate-off paths are unchanged.
142 lines
6.8 KiB
Go
142 lines
6.8 KiB
Go
package server
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// Gate on: the tool's own proxy is a target. An approved target URL does not
|
|
// let the call reach an unapproved proxy: the proxy is not dialled and a
|
|
// request opens on its host:port, recorded without the proxy credentials.
|
|
func TestCustomHTTPGatedOwnProxyNotDialed(t *testing.T) {
|
|
g := newGateServer(t)
|
|
on := true
|
|
task := g.newTask(t, db.TaskCreateOptions{TargetGate: &on})
|
|
_, targetHits, host, port := countingServer(t)
|
|
_, proxyHits, _, proxyPort := countingServer(t)
|
|
targetURL := "http://" + host + ":" + port + "/"
|
|
direct := g.s.buildCustomTool(&db.Tool{Key: "probe_own_proxy", Kind: "http", Exec: execJSON(t, map[string]any{"url": targetURL})})
|
|
approveTarget(t, g, task, direct, func() (string, bool) { return callTool(t, direct, gatedCtx(task, 0), `{}`, nil) }, host+":"+port)
|
|
if out, isErr := callTool(t, direct, gatedCtx(task, 0), `{}`, nil); isErr || targetHits.Load() != 1 {
|
|
t.Fatalf("approved direct call: error=%v out=%q", isErr, out)
|
|
}
|
|
|
|
proxyKey := "127.0.0.1:" + proxyPort
|
|
tool := g.s.buildCustomTool(&db.Tool{Key: "probe_own_proxy", Kind: "http",
|
|
Exec: execJSON(t, map[string]any{"url": targetURL, "proxy": "http://user:pw-secret@" + proxyKey})})
|
|
out, isErr := callTool(t, tool, gatedCtx(task, 0), `{}`, nil)
|
|
if !isErr || !strings.Contains(out, "TARGET_APPROVAL_PENDING") || !strings.Contains(out, proxyKey) {
|
|
t.Fatalf("unapproved proxy: error=%v out=%q", isErr, out)
|
|
}
|
|
if n := proxyHits.Load(); n != 0 {
|
|
t.Fatalf("unapproved proxy received %d request(s)", n)
|
|
}
|
|
if n := targetHits.Load(); n != 1 {
|
|
t.Fatalf("target received %d request(s), want only the direct one", n)
|
|
}
|
|
_, toolName, input, ok := openTargetRequest(t, g.m.pg, task.ID, proxyKey)
|
|
if !ok || toolName != "probe_own_proxy" || strings.Contains(input, "pw-secret") || strings.Contains(input, "user") {
|
|
t.Fatalf("request on the proxy: ok=%v tool=%q input=%s", ok, toolName, input)
|
|
}
|
|
}
|
|
|
|
// A saved tool's own proxy is a direct hop: the approval pins the addresses it
|
|
// resolved, and a proxy name that later answers another address is refused
|
|
// instead of being dialled unpinned (DNS rebinding). The tool is saved so the
|
|
// manager knows it uses a proxy, as in production.
|
|
func TestCustomHTTPOwnProxyHostnameRebindingRefused(t *testing.T) {
|
|
g := newGateServer(t)
|
|
on := true
|
|
task := g.newTask(t, db.TaskCreateOptions{TargetGate: &on})
|
|
_, targetHits, host, port := countingServer(t)
|
|
proxy, proxyHits, _, _ := countingServer(t)
|
|
targetKey := host + ":" + port
|
|
proxyKey := "proxy.example.com:3128"
|
|
res := &countingResolver{answers: map[string][]string{"proxy.example.com": {"203.0.113.7"}}}
|
|
g.m.interceptor.SetResolver(res)
|
|
dialer := newRouteDialer(g)
|
|
dialer.route("203.0.113.7:3128", proxy.Listener.Addr().String())
|
|
tool := &db.Tool{Key: "probe_rebind_proxy", Kind: "http", Description: "probe",
|
|
Exec: execJSON(t, map[string]any{"url": "http://" + targetKey + "/", "proxy": "http://" + proxyKey})}
|
|
if err := g.m.pg.CreateCustomTool(tool); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = g.m.pg.DeleteCustomTool(tool.Key) })
|
|
built := g.s.buildCustomTool(tool)
|
|
call := func() (string, bool) { return callTool(t, built, gatedCtx(task, 0), `{}`, nil) }
|
|
|
|
approveTarget(t, g, task, built, call, targetKey)
|
|
approveTarget(t, g, task, built, call, proxyKey)
|
|
if out, isErr := call(); isErr || !strings.Contains(out, "probe-reached") || proxyHits.Load() != 1 {
|
|
t.Fatalf("approved proxy: error=%v out=%q hits=%d", isErr, out, proxyHits.Load())
|
|
}
|
|
pins, err := g.m.pg.ActiveTargetPins(taskRef(t, task), "proxy.example.com", 3128)
|
|
if err != nil || len(pins) != 1 || pins[0].NameOnlyEffective ||
|
|
len(pins[0].PinnedIPs) != 1 || pins[0].PinnedIPs[0] != "203.0.113.7" {
|
|
t.Fatalf("proxy pin: %+v, %v", pins, err)
|
|
}
|
|
|
|
res.set("proxy.example.com", "203.0.113.9")
|
|
beforeTarget, beforeProxy, beforeCalls := targetHits.Load(), proxyHits.Load(), len(dialer.calls())
|
|
out, isErr := call()
|
|
if !isErr || !strings.Contains(out, "TARGET_APPROVAL_PENDING") || !strings.Contains(out, proxyKey) {
|
|
t.Fatalf("rebound proxy: error=%v out=%q", isErr, out)
|
|
}
|
|
if proxyHits.Load() != beforeProxy || targetHits.Load() != beforeTarget {
|
|
t.Fatalf("rebound proxy was reached: proxy=%d target=%d",
|
|
proxyHits.Load()-beforeProxy, targetHits.Load()-beforeTarget)
|
|
}
|
|
if got := len(dialer.calls()); got != beforeCalls {
|
|
t.Fatalf("dialled %v after the rebind", dialer.calls()[beforeCalls:])
|
|
}
|
|
}
|
|
|
|
// Gate off (sandbox off): the proxy is not checked, as on c03939c1. The tool
|
|
// URL itself is not checked in that mode either.
|
|
func TestCustomHTTPOwnProxyGateOffUnchanged(t *testing.T) {
|
|
g := newGateServer(t)
|
|
off := false
|
|
task := g.newTask(t, db.TaskCreateOptions{TargetGate: &off})
|
|
_, hits, host, port := countingServer(t)
|
|
tool := g.s.buildCustomTool(&db.Tool{Key: "probe_gate_off", Kind: "http",
|
|
Exec: execJSON(t, map[string]any{"url": "http://api.example.com/", "proxy": "http://" + host + ":" + port})})
|
|
if out, isErr := callTool(t, tool, gatedCtx(task, 0), `{}`, nil); isErr || !strings.Contains(out, "probe-reached") || hits.Load() != 1 {
|
|
t.Fatalf("gate off, loopback proxy: error=%v out=%q hits=%d", isErr, out, hits.Load())
|
|
}
|
|
}
|
|
|
|
// ARTEX_SANDBOX=required with the gate on: the proxy is checked by the sandbox
|
|
// egress policy only, as on c03939c1. A proxy inside the sandbox scope and
|
|
// never approved in the gate is dialled, and no gate request opens on it.
|
|
func TestSandboxHTTPToolGatedProxyUnchanged(t *testing.T) {
|
|
g := newGateServer(t)
|
|
on := true
|
|
task := g.newTask(t, db.TaskCreateOptions{TargetGate: &on})
|
|
target, targetHits := canaryServer(t, "127.0.0.1", nil)
|
|
proxy, proxyHits := canaryServer(t, "127.0.0.2", nil)
|
|
targetKey := strings.TrimPrefix(target.URL, "http://")
|
|
proxyKey := strings.TrimPrefix(proxy.URL, "http://")
|
|
sandboxRequired(t)
|
|
call := func(exec map[string]any) (string, bool) {
|
|
tool := g.s.buildCustomTool(&db.Tool{Key: "probe_sb_proxy", Kind: "http", Exec: execJSON(t, exec)})
|
|
ctx := agent.WithSandboxRunInfo(gatedCtx(task, 0), agent.SandboxRunInfo{
|
|
TaskID: task.ID, ScopeCIDRs: []string{"127.0.0.1/32", "127.0.0.2/32"},
|
|
})
|
|
return callTool(t, tool, ctx, `{}`, nil)
|
|
}
|
|
direct := map[string]any{"method": "GET", "url": target.URL + "/"}
|
|
directTool := g.s.buildCustomTool(&db.Tool{Key: "probe_sb_proxy", Kind: "http", Exec: execJSON(t, direct)})
|
|
approveTarget(t, g, task, directTool, func() (string, bool) { return call(direct) }, targetKey)
|
|
before := targetHits.Load()
|
|
out, isErr := call(map[string]any{"method": "GET", "url": target.URL + "/", "proxy": proxy.URL})
|
|
if isErr || !strings.Contains(out, "CANARY_BODY") || proxyHits.Load() != 1 || targetHits.Load() != before {
|
|
t.Fatalf("required mode, in-scope proxy: error=%v out=%q proxy hits=%d", isErr, out, proxyHits.Load())
|
|
}
|
|
if _, _, _, ok := openTargetRequest(t, g.m.pg, task.ID, proxyKey); ok {
|
|
t.Fatal("required mode opened a gate request on the proxy")
|
|
}
|
|
}
|