# Outbound network proxy

A direct, peer-pinned connection cannot reach services available only through the user's proxy. Fake-IP proxies such as Clash also answer local DNS from `198.18.0.0/15`, which a local-resolver policy refuses as `NON_GLOBAL_ADDRESS` though the real resolution happens in the proxy.

## Who follows the proxy

| Consumer | Examples | Route |
| --- | --- | --- |
| Arbitrary public URLs | `tool.web_fetch`, `tool.image_result.download`, channel attachments and assets | ordinary proxy |
| Fixed audited HTTPS services | search APIs including DuckDuckGo, image APIs, updater, skills catalog, office assets, channel APIs, TTS | ordinary proxy |
| Providers | `provider.*`, `webui.model_listing.*` | ordinary proxy only when the exact origin is in the provider API or OAuth inventory |
| Configured custom origins, loopback callbacks | private services an owner exception allows | direct, peer-pinned |

The route is chosen before the target policy, for every request and redirect hop. A `NO_PROXY` match, or no route, connects directly with the full local address check.

## Targets the proxy resolves

| Target | Decision |
| --- | --- |
| IP literal | Checked directly; private, loopback, reserved, fake-IP and metadata refused |
| Single-label, `localhost`, or `.localhost .local .lan .home.arpa .internal .intranet .corp .home` | Refused before any lookup |
| Metadata hostname | `METADATA_ADDRESS` |
| Local answer is a real private, loopback, link-local, reserved or metadata address | Refused |
| Local answer is fake-IP (`198.18.0.0/15`), or the lookup fails | Left to the proxy |
| Malformed answer | Refused |

The decision carries `requires_proxy`; building a direct pool or socket for it raises `PROXY_ROUTE_REQUIRED`. Fixed audited services also accept fake-IP on direct connections, since TLS verifies a hostname the caller cannot choose. Untrusted public URLs never do.

## Which proxy

1. Explicit `security.outbound_url.policy_proxy`, with its enforcement requirement; ordinary routing is then unused.
2. `OPENPROGRAM_PROXY_URL`, keeping `NO_PROXY`.
3. Standard variables through HTTPX, with Python's macOS and Windows system settings as fallback. Loopback is always bypassed.

| Aspect | Behaviour |
| --- | --- |
| Pools | httpcore proxy pools with a decision-aware backend, for HTTP, HTTPS and SOCKS5 |
| Forwarding | HTTPS targets tunnelled by hostname with CONNECT; plain HTTP in absolute form |
| Proxy address | Owner-selected; metadata and link-local forbidden |
| Credentials | Proxy authentication goes only to the handshake; redirects do not forward credentials across origins |
| Verification | TLS checks the original hostname; Host comes from the validated origin |
| Failure | Never falls back to direct |
| Client cache | Routing map is immutable within a client and part of its cache identity; a new call after a change retires stale clients |
| Explicit transport-security objects | Do not inherit ambient routing |

**Choices and residual risk**

| Choice | Decision |
| --- | --- |
| Environment parsing | HTTPX's parsing and URLPattern precedence are reused; no second `NO_PROXY` matcher |
| `PolicyProxyConfig` | Stays apart from ordinary routing: a normal Clash proxy does not enforce URL policy, so local checks still run in front of it |
| Left out | New profile, disabling URL protection, OAuth change, routing of custom services |
| CA bundles | Stay under outbound security configuration |

| Risk | Position |
| --- | --- |
| Final resolution | Behind a proxy, it is the proxy's |
| Public hostname with a private real answer | Caught only when the local resolver returns that answer; behind a fake-IP resolver, or with DNS rebinding between the check and the proxy's lookup, the proxy's rules decide |
| Still applied | Read-only methods, the 80/443 limit for untrusted URLs, credential stripping |
| For a strict guarantee | An enforcing policy proxy, or hosts in `NO_PROXY` |

**Acceptance**

- Sync and async public entries use HTTP, HTTPS and SOCKS5 routes.
- `web_fetch` behind a fake-IP resolver reaches the target by hostname.
- IP literals, local names and real private answers stay refused when proxied; `NO_PROXY` hosts keep the direct check.
- A proxy-resolved decision never opens a direct socket.
- `NO_PROXY` and override precedence hold; shared clients are replaced when routes change.
- Unauthorized target and proxy addresses are rejected before network activity; service and proxy credentials stay apart; policy-proxy behaviour and cleanup are retained.

**How OpenClaw does it**

Sources: `references/openclaw/src/infra/net/proxy-env.ts`, `proxy-fetch.ts`, `src/infra/net/proxy/`, [its proxy docs](https://docs.openclaw.ai/cli/proxy/).

- One environment resolver mirroring undici's `EnvHttpProxyAgent`: lowercase first; HTTPS prefers `https_proxy`, then `http_proxy`; `ALL_PROXY` as fallback. A full reimplemented `NO_PROXY` matcher with IPv4 CIDR gates every decision.
- One override: `--proxy-url`, `proxy.proxyUrl`, `OPENCLAW_PROXY_URL`, optional `--proxy-ca-file`, over undici `ProxyAgent`, with a managed proxy lifecycle.
- Provider helpers route through these; the SSRF guard uses the same `matchesNoProxy`.

OpenProgram borrows one documented reading of the standard variables and adds a separate policy-proxy boundary. OpenClaw's proxy lifecycle validation is not needed here.

## Implementation status

In `openprogram/host/security/url_policy.py` (`proxy_resolves`, `requires_proxy`), `safe_http.py` (`uses_ambient_proxy`, route before policy) and `load_outbound_security_config`. Not yet: the sandbox command proxy (`openprogram/host/sandbox/network_policy.py`) keeps its own direct public-address rule and does not recognise fake-IP answers.
