mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
EgressPolicy.CheckURL, the check WebFetch, the shared browser and the custom http tool run on a URL under ARTEX_SANDBOX=required, was fail-open in two ways: - a lookup error (or an empty answer) for a name in scope returned nil, so the URL was allowed; - a name answering several addresses was allowed as soon as one of them was public, the others being private, loopback, IPv6 or the cloud metadata address (169.254.169.254). CheckURL pins nothing. On a direct call the sandbox dialer resolves again and pins an allowed address, but through an explicit proxy the panel only dials the proxy, which resolves the target itself and may pick any answer (or reach a name the panel could not resolve); the shared browser resolves on its own as well. For those paths CheckURL is the only check on the target. CheckURL now denies a lookup error or an empty answer, and requires every answer to be allowed (in the scope CIDRs, or outside the default deny list): a dual-A [IMDS, public] answer is refused, in either order. The dialer path (ResolveAndCheck, also used by the egress proxy) is unchanged: it already fails closed on a lookup error and dials only the allowed address it pinned. sandboxHTTPTransport now turns keep-alive off on every transport it returns, not only on the one it builds when httpToolTransport returned nil: an operator proxy, or the recording proxy setting without a CA file, kept idle connections to the proxy. Two existing tests expected an in-scope name that does not resolve to be allowed, which only the fail-open lookup gave them: the scope drift test (its resolver reached the proxy policy only, not the policy WebFetch and the browser build) and the shared browser navigation test. Both now pass an offline resolver through the context (sandbox.WithResolver); their assertions are unchanged.
103 lines
4.5 KiB
Go
103 lines
4.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/netip"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/intercept"
|
|
"github.com/Autumn-27/artex/sandbox"
|
|
)
|
|
|
|
// dnsTable answers lookups from a table, without the network; a name it does
|
|
// not know fails as a lookup error would.
|
|
func dnsTable(answers map[string][]string) func(context.Context, string) ([]netip.Addr, error) {
|
|
return func(_ context.Context, host string) ([]netip.Addr, error) {
|
|
ips, ok := answers[host]
|
|
if !ok {
|
|
return nil, errors.New("lookup " + host + ": no such host")
|
|
}
|
|
out := make([]netip.Addr, 0, len(ips))
|
|
for _, ip := range ips {
|
|
out = append(out, netip.MustParseAddr(ip))
|
|
}
|
|
return out, nil
|
|
}
|
|
}
|
|
|
|
// proxyDNSCtx is a sandbox run whose scope lists allowed.example and the
|
|
// loopback proxy, with lookups answered by answers.
|
|
func proxyDNSCtx(answers map[string][]string) context.Context {
|
|
return sandbox.WithResolver(sandboxCtx([]string{"allowed.example"}, []string{"127.0.0.1/32"}), dnsTable(answers))
|
|
}
|
|
|
|
// Through an explicit proxy in scope, the panel dials the proxy and the proxy
|
|
// resolves the target: the target is checked by CheckURL alone. A target whose
|
|
// lookup fails is refused before the proxy is dialled; an in-scope target that
|
|
// resolves to a public address still goes through the same proxy.
|
|
func TestSandboxHTTPToolProxyRefusesTargetDNSError(t *testing.T) {
|
|
sandboxRequired(t)
|
|
proxy, proxyHits := canaryServer(t, "127.0.0.1", nil)
|
|
ctx := proxyDNSCtx(map[string][]string{"ok.allowed.example": {"203.0.113.10"}})
|
|
|
|
res := runHTTPSpec(t, ctx, map[string]any{"method": "GET", "url": "http://nxdomain.allowed.example/", "proxy": proxy.URL})
|
|
if !res.IsError || !strings.Contains(res.Flatten(), "egress denied") {
|
|
t.Fatalf("target with a failed lookup not refused: isError=%v text=%q", res.IsError, res.Flatten())
|
|
}
|
|
if n := proxyHits.Load(); n != 0 {
|
|
t.Fatalf("proxy received %d request(s) for a target whose lookup failed", n)
|
|
}
|
|
|
|
res = runHTTPSpec(t, ctx, map[string]any{"method": "GET", "url": "http://ok.allowed.example/", "proxy": proxy.URL})
|
|
if res.IsError || !strings.Contains(res.Flatten(), "CANARY_BODY") || proxyHits.Load() != 1 {
|
|
t.Fatalf("resolved in-scope target through the proxy: isError=%v text=%q proxy hits=%d", res.IsError, res.Flatten(), proxyHits.Load())
|
|
}
|
|
}
|
|
|
|
// A target answering the metadata address next to a public one is refused
|
|
// before the proxy, which would pick its own answer, is dialled.
|
|
func TestSandboxHTTPToolProxyRefusesDualAMetadataTarget(t *testing.T) {
|
|
sandboxRequired(t)
|
|
proxy, proxyHits := canaryServer(t, "127.0.0.1", nil)
|
|
ctx := proxyDNSCtx(map[string][]string{"dual.allowed.example": {"169.254.169.254", "203.0.113.10"}})
|
|
|
|
res := runHTTPSpec(t, ctx, map[string]any{"method": "GET", "url": "http://dual.allowed.example/latest/meta-data/", "proxy": proxy.URL})
|
|
if !res.IsError || !strings.Contains(res.Flatten(), "egress denied") {
|
|
t.Fatalf("dual-A metadata target not refused: isError=%v text=%q", res.IsError, res.Flatten())
|
|
}
|
|
if n := proxyHits.Load(); n != 0 {
|
|
t.Fatalf("proxy received %d request(s) for a dual-A metadata target", n)
|
|
}
|
|
}
|
|
|
|
// Every transport the sandbox builds for an http tool call has keep-alive off
|
|
// and the sandbox dialer: direct with the gate off or on, an operator proxy,
|
|
// and the recording proxy setting falling back to the global proxy.
|
|
func TestSandboxHTTPTransportKeepAlivesOff(t *testing.T) {
|
|
policy := agent.SandboxEgressPolicy(sandboxCtx([]string{"allowed.example"}, []string{"127.0.0.1/32"}))
|
|
gateOn := intercept.WithTargetScope(context.Background(), intercept.TargetScope{TaskID: "7", Enabled: func() bool { return true }})
|
|
s := &Server{m: &Manager{globalProxy: "http://127.0.0.1:3128"}}
|
|
direct := "http://api.allowed.example/"
|
|
for _, c := range []struct {
|
|
name string
|
|
ctx context.Context
|
|
spec httpExec
|
|
}{
|
|
{"direct, gate off", context.Background(), httpExec{URL: direct}},
|
|
{"direct, gate on", gateOn, httpExec{URL: direct}},
|
|
{"operator proxy", context.Background(), httpExec{URL: direct, Proxy: "http://127.0.0.1:8080"}},
|
|
{"operator proxy, gate on", gateOn, httpExec{URL: direct, Proxy: "127.0.0.1:8080"}},
|
|
{"recording proxy via the global proxy", context.Background(), httpExec{URL: direct, UseRecordingProxy: true}},
|
|
} {
|
|
tr := sandboxHTTPTransport(s.httpToolTransport(c.ctx, "probe", c.spec), policy)
|
|
if tr == nil || tr.DialContext == nil {
|
|
t.Fatalf("%s: transport %+v, want one with the sandbox dialer", c.name, tr)
|
|
}
|
|
if !tr.DisableKeepAlives {
|
|
t.Fatalf("%s: DisableKeepAlives = false, want keep-alive off on every sandbox transport", c.name)
|
|
}
|
|
}
|
|
}
|