Skip to content

feat(intercept): pinned dialer for WebFetch and the custom http tool (S3b 2.1) - #186

Merged
BaaRai merged 1 commit into
mainfrom
feat/target-gate-pinned-dialer
Nov 11, 2026
Merged

BaaRai merged 1 commit into
mainfrom
feat/target-gate-pinned-dialer

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 11, 2026

Copy link
Copy Markdown
Owner

S3b step 2.1 (plan v6, section 11): in a gated scope, WebFetch and the custom http tool connect only to addresses that a pin or a stored exemption covers at the moment of the connection, on every hop. Gate off is unchanged.

What changes

  • intercept.PinnedDialer(tool, scoped) (new intercept/target_dial.go): the DialContext of both clients. It reads the target scope from the context net/http passes to the dial (D-1).

    • No scope, client built without one, or gate off: plain dial of the address received.
    • Client built for a scope, scope missing at the dial: fail closed, TARGET_GATE_UNAVAILABLE, nothing dialled.
    • The gate can't decide (nil interceptor, bad task id, rules, exemptions or pins can't be read, unparsable address, non-tcp network): TARGET_GATE_UNAVAILABLE, plus one decided row where there is a store.
  • dialTarget: runs the existing gate decision (targetCheck.run) on the dialled host:port with a fresh lookup (fresh). That covers block rules first, then exemptions, pins and port precedence. URL rules are not re-matched. Then it acts on what covered the target:

    • derived exemption or name-only pin (D-2): plain dial of the address.
    • stored exemption that holds, or pin with R ⊆ P (D-3, D-4): dial only the addresses it compared, in canonical order, first success wins, and never the name.
    • not covered: find or open the request (new_ip / no_pin, wait bound 0, input {"dial":"host:port"}) and fail at once with TARGET_APPROVAL_PENDING.

    Redirect hops are dialled against the hop's own (host, port) (D-5).

  • targetCheck: fields fresh and checked; checkedTarget.cover. When fresh is set, the second lookup before a request is skipped because the answer is already fresh.

  • Name-only (D-2): the request reason adds "The platform does not bind the connection to these addresses."; the first-use pin_match row records pin.NameOnlyEffective || nameOnly.

  • WebFetch: withWebFetch(ctx, ...) on all four agent kinds sets DialContext: intercept.PinnedDialer("WebFetch", scoped) and DisableDirectRetry: gated (D-8). withWebFetch is kept (OPEN-7).

  • Custom http tool (D-6): new httpToolTransport. A direct call in a gated scope gets a http.DefaultTransport clone with the pinned dialer, Proxy = nil and keep-alive off. Gate off: client.Transport stays nil. Calls through a proxy (proxy or use_recording_proxy) keep their transport without a dialer; the recording proxy is step 2.2.

  • OPEN-9: a derived exemption URL without a port gets the port net/http dials. Only socks5/socks5h → 1080 was missing; http/https already had 80/443.

  • Base-dialer seam: Interceptor.SetBaseDialer (test seam, beside SetResolver).

  • Norma: go.mod replace moved to github.com/BaaRai/Norma v0.4.8 (19a39e5), go.sum by go mod tidy, same form as chore: bump Norma to v0.4.7 #184.

No schema change, no new table, no change in guard.

Tests

Named in plan 11.8:

  • intercept unit tests:
    • Gate off, scope and failure: TestPinnedDialGateOffIsPlainDial, TestPinnedDialScopeLostFailsClosed, TestPinnedDialScopeUnreadableFailsClosed, TestPinnedDialResolveFailureBlocksWithoutRequest, TestPinnedDialBlockRuleOnDialAddress.
    • Which address is dialled: TestPinnedDialConnectsOnlyToPinnedAddresses, TestPinnedDialDerivedExemptionDialsByName, TestPinnedDialNameOnlyPinDialsByName, TestPinnedDialNormalisesLikeTheCheck, TestPinnedDialFreshLookupCollapsed, TestPinnedDialTriesAddressesInOrder.
    • Standard-library behaviour: TestPinnedDialSeesScopeInDialContext (S1), TestPinnedDialHTTPSVerifiesName (S4), TestPinnedDialNoReuseAcrossCalls (S6), TestRedirectContextCarriesScope (S5).
    • Name-only: TestNameOnlyReasonSaysConnectionNotBound, TestPinUsedRecordsEffectiveNameOnly.
  • intercept PostgreSQL tests: TestPinAddressChangedBetweenCheckAndDialNotReachable, TestPinnedDialPartialOverlapNotReachable, TestPinnedDialRevokedBetweenCheckAndDial, TestPinnedDialStoredExemptionDialsCheckedAddresses, TestPinnedDialIPLiteralPin, TestPinnedDialPortPrecedence, TestRedirectHopDialledAgainstHopPinNotOrigin (three cases), TestDialMismatchOpensOneNewIPRequest.
  • agent: TestWebFetchDialerWiredOnEveryAgentKind, TestWebFetchGatedScopeHasNoDirectRetry, TestWebFetchProxiedDialIsTheProxy (S2), TestWebFetchRetryAfterLiveSwitchIsPinned.
  • server: TestCustomHTTPDirectDialPinned, TestCustomHTTPGateOffTransportUnchanged, TestCustomHTTPRedirectHopDialPinned, TestCustomToolTestEndpointDialUngated, TestBypass_CustomHTTPProxyPeerNotPinned, TestTargetGateEndToEndDialRebindBlocked.

Also added: TestDerivedExemptionsDefaultProxyPort (OPEN-9).

Changed tests:

  • TestWebFetchRedirectCheckWiredOnEveryAgentKind now pins the origin 127.0.0.1:<start port> before the run. The hooks in that test let every call through, so the gate never checked the origin, and the pinned dialer now refuses a target nobody pinned. The assertions are unchanged.
  • TestWebFetchRedirectCheckOnDirectRetry is replaced by TestWebFetchGatedScopeHasNoDirectRetry: in a gated scope the direct retry no longer exists. With a refusing proxy that is a derived exemption, the fetch fails with the proxy error, the origin gets no request, and no request row is written.
  • TestWebFetchRedirectGateOffUnchanged is kept as-is (plan name TestWebFetchGateOffKeepsDirectRetry). It still compares with Norma's own tool output.

Differences from the plan:

  • Tests that reach a request (RequestTargets writes through *db.DB) run against PostgreSQL rather than the fake store.
  • checkedTarget.pin was not added, because nothing reads it.
  • No task-settings text about the gate exists in the repo (the flag is API-only), so there was nothing to update for that exit-criterion line.

Verification (WSL, go1.26.10, fresh PostgreSQL 16.15)

  • go build ./...: ok.
  • go vet ./...: ok.
  • go test -count=1 -p 1 ./...: all packages ok.
  • go test -race -count=1 -p 1 on the CI package list: all ok.

Mutation checks (each reverted after):

  • Dial by name instead of the compared addresses: 3 tests fail.
  • No decision at the dial: 2 tests fail.
  • A lost scope dials plain: TestPinnedDialScopeLostFailsClosed fails.
  • Cached lookup at the dial: 2 tests fail.
  • WebFetch without the dialer: all 4 kinds in TestWebFetchDialerWiredOnEveryAgentKind fail.
  • Direct retry kept when gated: TestWebFetchGatedScopeHasNoDirectRetry fails.
  • Custom http without the dialer: TestCustomHTTPDirectDialPinned fails.

Left for 2.2

  • The recording-proxy addon (go-mitmproxy) that rejects unapproved hosts and unpinned peers. It binds WebFetch and custom http traffic that goes through the recording proxy, after the hook-order spike.
  • D10: routing gate-on HTTP through the proxy.
  • The Monitor proxy environment (D12).
  • TestBypass_CustomHTTPProxyPeerNotPinned flips for the recording-proxy half. An operator-written proxy stays unbound (OPEN-6).

Noted, not changed

httpProxyTransport (server/customtool.go:501) builds "http://" + s.m.ProxyAddr(), but ProxyAddr() already returns a URL with a scheme (traffic/traffic.go:307-309, and a validated global_proxy). So use_recording_proxy produces http://http://.... This bug predates this PR. The bypass test gives that path a bare host:port to document it.

…(S3b 2.1)

In a gated scope, the two HTTP clients the platform owns connect only to
addresses that a pin or a stored exemption covers at the moment of the
connection, on every hop.

- intercept.PinnedDialer(tool, scoped): the DialContext of both clients.
  It reads the target scope from the context net/http passes to the dial.
  Gate off, or no scope in a client built without one: plain dial, as
  today. A client built for a scope whose scope is missing at the dial
  fails closed with TARGET_GATE_UNAVAILABLE.
- dialTarget runs the gate decision on the dialled host:port with a fresh
  lookup (block rules first, exemptions, pins, port precedence) and
  connects only to the addresses it compared, never to the name. Derived
  exemptions and name-only pins dial the address as received. An address
  no pin covers finds or opens the request (new_ip / no_pin, wait 0) and
  fails at once with TARGET_APPROVAL_PENDING. Redirect hops are dialled
  against the hop's own (host, port).
- WebFetch (withWebFetch, now given the run context): DialContext set on
  every agent kind; DisableDirectRetry when the run's scope is gated.
- Custom http tool: a direct call in a gated scope gets a DefaultTransport
  clone with the pinned dialer, no environment proxy and keep-alive off.
  Proxied calls are unchanged (the recording proxy is step 2.2).
- Name-only: request reason says the platform does not bind the
  connection; the first-use pin_match row records the effective state.
- Derived exemptions: a proxy URL without a port is listed with the port
  net/http dials (1080 for socks5) (OPEN-9).
- go.mod: Norma v0.4.8 (DialContext, DisableDirectRetry).
@BaaRai
BaaRai merged commit e38e48e into main Nov 11, 2026
4 checks passed
@BaaRai BaaRai mentioned this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant