# web_fetch

Status: built 2026-11-27 (ticket 90, first half; search follows). Tier: bundled (`src/webfetch.c`,
`src/htmltext.c`), over the transport's web mode in core (`src/net.c`). Clock: taste. Record: none
new; a call is an effect, replay-safe. Default: on.

The tool is a registration (`pic_webfetch_tool`, [the tool registry](../design/keel.md)): a GET of
one URL through `pic_http_get`, redirects followed by the tool, not the transport, so every hop is
parsed, checked and counted by one function.

## The address check

A model chooses the URL, so the fetch must not reach the user's machine or network. The check is
`pic_net_web` in `net.c`, made on each address `pic_net_connect` is about to connect to, after the
name lookup. Checking the connected address, and not the URL's host, is what defeats a name that
resolves to a private address, a name whose answer changes between a check and the connect, and a
redirect: each hop is a new connection with its own check. Refused: IPv4 0/8, 10/8, 100.64/10,
127/8, 169.254/16 (metadata included), 172.16/12, 192.0.0/24, 192.0.2/24, 192.88.99/24,
192.168/16, 198.18/15, 198.51.100/24, 203.0.113/24 and 224/3; IPv6 outside 2000::/3 (so fc00::/7,
fe80::/10, ff00::/8, ::1 and ::), Teredo, 2001:db8::/32; and the IPv6 forms that carry an IPv4
address (::ffff:a.b.c.d, 64:ff9b::/96, 2002::/16) by that address. `wf_parse` also writes an IPv4
host given as a number, in octal or hex, or short (`127.1`) as its dotted quad, so every resolver
reads it alike. A URL with a user name or password is refused.

Web mode also lets plain `http://` go to any host, which the transport otherwise allows to loopback
only. It is off unless the tool turns it on for its own connection.

Test seams (dev builds only, never in a release build): `PIC_TEST_WEB_LOOPBACK=PORT` lets the one
loopback port of the fake server through the check; `PIC_TEST_WEB_DEADLINE=SECONDS` shortens the
30 second deadline. `test/pic/webfetch.sh --no-faults` checks that a release build refuses
loopback.

## Limits

Five redirects (the sixth fails), 5 MB of body (refused on a Content-Length over it, and when a
body without one grows past it), 30 seconds for the whole call across all hops, text types only
(`text/*`, JSON, XML, JavaScript, YAML, and a body with no type that has no NUL byte). The result
is cut to `max_chars`; the whole converted text goes to `pic_tool_hooks.output`, kept as
`pic:output/N` for `read`.

## web_search

Status: built 2026-11-27 (ticket 90, second half). Tier: bundled (`src/websearch.c`). Clock: vendor
(Brave's API). Record: none new; a call is an effect, replay-safe. Default: on when a key exists (`ws_on`: the sealed row or the variable, no unlock needed).

One GET of `https://api.search.brave.com/res/v1/web/search?q=...&count=N` with the key in
`X-Subscription-Token`. The key is the credential `brave-search` (`pic login brave-search`, which
`known_provider` in `main.c` accepts through `pic_model_keyname`, which the terminal's `/login` and `auth status` use too; routing rules and imports refuse it) or `BRAVE_API_KEY`, read
through `pic_cred_get`, wiped after the call and scrubbed from any error. Results are taken with
SQLite's JSON functions, so a reply that is not JSON, or has no `web.results`, is an answer and not
a crash; each field goes through `pic_web_utf8` (controls dropped) and `pic_html_plain` (Brave puts
`<strong>` and entities in snippets). Caps: 10 results, 400 query characters, 2 MB, 20 seconds.
`PIC_TEST_WEB_SEARCH_URL` (dev builds only) replaces the endpoint, so the tests use the fake server.
It is not a registered read-only tool (`read_only` 0): the query leaves the machine.
