mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 04:14:15 +00:00
A custom tool with Kind "http" bound to the worker ran its request from the panel process with no sandbox check: under ARTEX_SANDBOX=required a worker could reach a URL outside the task scope (a loopback service, an internal address, another host) and follow a redirect anywhere, because runHTTPTool and httpToolTransport never looked at SandboxRequired or at the task's egress policy. Decision: option 1, enforce the policy in the panel, as WebFetch does. WebFetch already has a usable path for the same situation (a request made by the panel, outside the worker container): the task's policy from SandboxRunInfo (sandbox.NewEgressPolicy), CheckURL on the input, and sandboxScopeDialContext on every connection, which resolves once, checks every address against the scope and the default deny list, and dials the pinned IP. Routing the tool through the per-task egress proxy is not needed for that, and WebFetch does not do it either. Under ARTEX_SANDBOX=required the http tool now: - checks the rendered URL with the task policy before any approval or request (scheme, scope domain or CIDR, names resolving only to private, loopback, metadata or IPv6 addresses); - checks every redirect hop with the same policy; a hop outside the scope fails the call instead of being followed; - dials through the WebFetch sandbox dialer: the target for a direct call (environment proxy dropped, pinned dialer of a gated scope kept underneath), the proxy for a call with a proxy, so a proxy outside the scope is not dialed. The policy resolution moves to agent.SandboxEgressPolicy, used by WebFetch and the http tool, and the dialer is exported as agent.SandboxScopeDialContext with a label; WebFetch keeps its messages. Sandbox off: nothing changes, the new code is skipped. The planner still does not receive a Kind "http" tool bound to the worker.
84 lines
3.5 KiB
Go
84 lines
3.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
|
|
"github.com/Autumn-27/artex/egress"
|
|
"github.com/Autumn-27/artex/intercept"
|
|
"github.com/Autumn-27/artex/sandbox"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// withWebFetch returns tools plus the WebFetch tool of an agent session. Every
|
|
// agent kind builds WebFetch here, one tool per run, so that its configuration
|
|
// can depend on the run: every redirect hop goes through the target gate
|
|
// (intercept.RedirectCheck), and every connection through the pinned dialer
|
|
// (intercept.PinnedDialer), on the client that uses proxy and on the direct
|
|
// retry client alike. ctx is the context of the run: when it carries a target
|
|
// scope the dialer never connects without one, and when that scope's gate is
|
|
// on the direct retry is turned off for the run, since it would leave without
|
|
// the proxy. The tool is added after the session's own tools, where agentcore
|
|
// adds it, so the tool list keeps its order. caCert is the recording proxy's
|
|
// CA, trusted to verify the HTTPS it re-signs. Each call goes through the shared
|
|
// egress governor (see governedFetch). tools is not modified.
|
|
func withWebFetch(ctx context.Context, tools []actool.CoreTool, proxy, caCert string) []actool.CoreTool {
|
|
scope, scoped := intercept.TargetScopeFromContext(ctx)
|
|
gated := scoped && scope.Enabled != nil && scope.Enabled()
|
|
dialer := intercept.PinnedDialer("WebFetch", scoped)
|
|
disableDirect := gated
|
|
var sbPolicy *sandbox.EgressPolicy
|
|
if SandboxRequired() {
|
|
pol := SandboxEgressPolicy(ctx)
|
|
sbPolicy = pol
|
|
dialer = sandboxScopeDialContext(pol, dialer)
|
|
disableDirect = true
|
|
}
|
|
webFetch := actool.NewWebFetch(actool.WebFetchConfig{Proxy: proxy, CACert: caCert, CheckRedirect: intercept.RedirectCheck("WebFetch"),
|
|
DialContext: dialer, DisableDirectRetry: disableDirect})
|
|
var wrapped actool.CoreTool = governedFetch{webFetch}
|
|
if SandboxRequired() {
|
|
wrapped = &sandboxScopeWebFetchTool{CoreTool: wrapped, policy: sbPolicy}
|
|
}
|
|
return append(tools[:len(tools):len(tools)], wrapped)
|
|
}
|
|
|
|
// egressGovernor resolves the governor of a WebFetch call, once per call so a
|
|
// changed noise profile applies to the next call; a test may pin it.
|
|
var egressGovernor = egress.Current
|
|
|
|
// governedFetch runs each WebFetch call as one send under the egress governor
|
|
// (off by default; then the call runs unchanged). Under a noise profile it
|
|
// paces the call by the rate, jitter and concurrency of the URL's host, and
|
|
// nothing else: WebFetch calls are neither coalesced nor cached, since each
|
|
// tool carries its own proxy and redirect and dial gate (see egress.Do).
|
|
// Norma owns the HTTP client, so redirect hops are not paced separately and a
|
|
// 429/503 is not retried.
|
|
type governedFetch struct {
|
|
actool.CoreTool
|
|
}
|
|
|
|
func (f governedFetch) Call(ctx context.Context, input json.RawMessage, tc *actool.ToolContext) (actool.Result, error) {
|
|
var in struct {
|
|
URL string `json:"url"`
|
|
}
|
|
if json.Unmarshal(input, &in) != nil {
|
|
return f.CoreTool.Call(ctx, input, tc)
|
|
}
|
|
u, err := url.Parse(in.URL)
|
|
if err != nil || u.Host == "" {
|
|
return f.CoreTool.Call(ctx, input, tc) // the tool reports the bad URL
|
|
}
|
|
return egress.Do(egressGovernor(), ctx, u, func() (actool.Result, error) {
|
|
return f.CoreTool.Call(ctx, input, tc)
|
|
})
|
|
}
|
|
|
|
// NewWebFetchTool builds a WebFetch tool configured for ctx (including
|
|
// ARTEX_SANDBOX=required scope enforcement when SandboxRequired() is true).
|
|
func NewWebFetchTool(ctx context.Context, proxy, caCert string) actool.CoreTool {
|
|
tools := withWebFetch(ctx, nil, proxy, caCert)
|
|
return tools[len(tools)-1]
|
|
}
|