Skip to content

feat(egress): getFromPublicAddress, the GET an agent's fetch goes through (TASK-267b) - #2242

Merged
lilyshen0722 merged 2 commits into
mainfrom
lily/fetch-url-guard
Oct 15, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
lily/fetch-url-guard

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Change

getFromPublicAddress(url) in backend/services/outboundUrlGuard.ts: the egress half of commonly_fetch_url (TASK-267b), built to the CTO's design on TASK-267. It's a GET twin of postToPublicAddress, held tighter because the caller is a model acting on outsider-written text.

rule how
https only every hop; credentials in the URL refused
public addresses only resolves and checks every answer (classifyAddress); the webhook private-address flag does not apply
no rebinding the socket gets a lookup that returns only the checked address
redirects followed by hand, each hop re-resolved and re-checked, at most 3
deadline one 10s signal over every hop and the body
content text/html, text/plain, application/json only, checked on the headers before any body byte is read; HTML reduced to text (htmlToText.ts)
size read to at most 1 MB, then cut off and reported truncated: true
headers only a fixed User-Agent and Accept; nothing a caller passes is merged in, so no cookie or Authorization can leave

Returns { ok, status, contentType, text, truncated, url }. A refusal throws the module's OutboundUrlRefusedError (isOutboundUrlRefused); a non-2xx final response is returned, not thrown.

One design choice for the gate: the cap truncates rather than refuses. The design says "1 MB streaming cap" without saying which. Ordinary pages run past 1 MB and their first megabyte is the useful part, so a refusal would make many fetches useless. The result says truncated: true, so the turn can tell. If you'd rather refuse, it's a one-word change (overflow: 'error'), and the arm flips.

Refactor: toPublicDestination and requestPinned now take a policy instead of hard-coding the webhook one. The webhook path keeps its exact behaviour: outboundUrlGuard.test.js and outboundUrlGuard.publicAddress.test.js pass unchanged.

Not in this PR: the tool definition, its opt-in, the after-a-lookup narrowing and the #2206 marker, which the CTO wires with 267a's search.

Proof

  • outboundUrlGuard.fetch.test.js: 32 arms. https.request is a fake and DNS a spy, so no arm touches the network.
    • Refusals made before any request: http, ftp, file, credentials, bad URL; private addresses with the webhook flag set; metadata by IP, IPv4-mapped IP and name; mixed public and private answers.
    • Pinning under rebinding.
    • Redirect hops: to a private address, to http, to metadata; more than 3 refused; exactly 3 followed; relative followed.
    • Content: text, HTML reduced, JSON; png, octet-stream, pdf and missing type refused with no body read; truncation at exactly 1 MB; non-2xx returned.
    • Exactly the fixed headers.
  • htmlToText.test.js: 9 arms covering dropped elements, blocks, whitespace, unclosed script, entities, invalid code points.
  • The four guard suites together: 193/193. tsc:check clean, eslint 0.
  • Mutations, each caught, restored from a copy:
mutation failing tests
agent fetch honours the private flag 8
http allowed 5
cap errors instead of truncating 1
no content-type check 4
4 redirects allowed 1
a Cookie merged into the headers 1

Gate: the CTO (security and code).

🤖 Generated with Claude Code

https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv

…ough (TASK-267b)

The egress half of commonly_fetch_url, built to the design on TASK-267: a GET
twin of postToPublicAddress, held tighter because the caller is a model acting
on outsider-written text.

- https only, on every hop; credentials in the URL refused.
- Public addresses only. The webhook private-address flag does not apply: a
  local receiver the operator trusts is not a page a model may read.
- The socket is pinned to the address that was checked, so a second DNS
  answer (rebinding) cannot substitute another.
- Redirects followed by hand and re-checked, at most 3.
- One 10s deadline over every hop and the body.
- text/html, text/plain and application/json only, checked on the response
  headers before any body byte is read; HTML is reduced to its text.
- The body is read to at most 1 MB and then cut off, reported as truncated,
  rather than refused: ordinary pages run past 1 MB and their start is useful.
- Only the fixed User-Agent and Accept headers are sent; nothing a caller
  passes is merged in, so no cookie or Authorization header can leave.

toPublicDestination and requestPinned take a policy instead of hard-coding the
webhook one; the webhook path keeps its exact behaviour (its two suites pass
unchanged). htmlToText is a reducer, not a sanitizer: its output is plain text
for a turn, guarded by the #2206 marker and the after-a-lookup narrowing.

The tool itself, its narrowing and the marker are wired separately.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security gate on #2242 at 51f802e (TASK-267b, getFromPublicAddress).

The fetch itself holds. Every hop is https and refuses credentials. Only public addresses pass, whatever the webhook flag says, and every DNS answer must pass. The socket is pinned to the checked address, so rebinding can't swap it. Redirects are manual and re-checked, three at most. One 10 s deadline covers all hops. The content type is checked on the headers before any body byte. Only the two fixed headers are sent, and nothing from callers is merged in. The webhook policy is unchanged. Truncate rather than refuse at 1 MB is the right call, so keep it.

HOLD: htmlToText is quadratic on hostile input, and the page is attacker-chosen. Two patterns cause it: the tag pattern <\/?([a-z][a-z0-9-]*)\b[^>]*> and the dropped-element opener <${tag}\b[^>]*>. On input with many < and no >, each < starts a scan to the end of the input. Measured on this head's file (Node 26, laptop):

input size time
normal page 977 KB 25–35 ms
'<a ' × 10k / 20k / 40k 29 / 59 / 117 KB 91 / 380 / 1505 ms
'<script ' × 40k / 3 104 KB 958 ms

Doubling the input quadruples the time. At the 1 MB the fetch allows, that comes to roughly 115 s of synchronous work on the API process's event loop, from one page a model was steered to. The 10 s deadline doesn't bound it, because it runs after the body is read.

Fix, tested: stop each tag scan at the next < as well as at >. Change [^>]*> to [^<>]*> in both patterns:

text = text.replace(new RegExp(`<${tag}\\b[^<>]*>[\\s\\S]*?(</${tag}\\s*>|$)`, 'gi'), ' ');
...
text = text.replace(/<\/?([a-z][a-z0-9-]*)\b[^<>]*>/gi, (_tag, name: string) => (

With that change, 1,025 KB of '<a ' takes 20 ms, 911 KB of '<script ' takes 6 ms, and the normal page is unchanged. All four of your suites pass on it, 193/193 (I applied it in a scratch worktree; nothing was pushed). Please add an arm: about 1 MB of '<a ' repeated must reduce in under a second. Today's code fails that, and the fix passes it.

Not holding on this, a note: the agent policy allows any port on a public host. Pinning agent fetches to 443 would close off using us to reach odd public ports. Your call.

GATE: HOLD

…etches stay on port 443

The CTO's HOLD on #2242: the tag pattern and the dropped-element opener used
`[^>]*`, so a "<" with no ">" sent each attempt scanning to the end of the
input. That's quadratic: 117 KB of "<a " took 1.5 s, and the 1 MB a fetch may
return would block the event loop for about two minutes. `[^<>]*` stops each
attempt at the next "<". Measured on "<a " repeated: the old pattern goes 16 KB
29 ms, 32 KB 150 ms, 64 KB 465 ms; the new one handles 1 MB in 2 ms.

Six hostile 1 MB inputs ("<a ", "<script ", bare "<", "<!--", "<p", "&#") now
each have an arm bounded at 1 s; they run in 3-17 ms.

Also from the review: an agent fetch refuses any non-default port, on the
first hop and on every redirect (the URL parser drops an explicit :443).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security re-gate on #2242 at 86023bb. My HOLD 5537153226 at 51f802e is resolved.

The quadratic is fixed. Both patterns now stop a tag scan at the next < ([^<>]*>). That covers the tag pattern and the dropped-element opener, and the changed lines match the change I tested. The other hostile shapes you added (<, <!--, &#) are linear by construction: the comment and dropped-element bodies consume to the end on their first unclosed match, and entity bodies are bounded at 9 characters.

The bound has teeth. I ran it at this head in a scratch worktree:

  • All four suites pass, 203/203.

  • I then reverted only the two patterns to [^>]*> and re-ran htmlToText.test.js with the jest timeout raised. Three of your 1 MB arms fail:

    input time
    <a 186 s
    <script 138 s
    <p 231 s

    The other 12 pass. I restored the file afterwards. This is the red jest run your note said you couldn't get, and it matches the ~2 minutes I extrapolated.

Port 443 is taken. httpsOnly now refuses a non-default port on the first hop and on every redirect. The URL parser normalises an explicit :443 to '', so url.port is set only for a non-default port. The arms cover :8443, :80, an explicit :443 and a redirect to :8080. The webhook policy is untouched.

Unchanged since my first gate and still holding: https only with no credentials, public addresses only regardless of the webhook flag, every DNS answer checked, the socket pinned to the checked address, manual redirects re-checked (3 at most), one 10 s deadline, the content type checked before any body byte, the two fixed headers only, and truncation at 1 MB.

GATE: PASS

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Oct 15, 2026
Merged via the queue into main with commit 68ae527 Oct 15, 2026
17 checks passed
samxu01 pushed a commit that referenced this pull request Oct 16, 2026
…K-267b)

A native turn that declares commonly_fetch_url can read one web page through
getFromPublicAddress (#2242). The address of a GET reaches its host, so the
model never composes one: it may open only an address that appears, as
written, in text the turn was given (the prompt, the message it answers,
other authors' pod messages, search results, pages already read). Its own
words and its own posts never count (webAddressProvenance).

A page is third-party text: it reaches the model behind the #2206 marker,
cut to 24,000 characters, and narrows the rest of the turn exactly as a
search does (in-pod read and post plus lookups). Not offered, and refused at
dispatch, in a pod bridged to an external chat. At most 3 pages a turn,
counted on the AgentRun as webFetches. A guard refusal reaches the model and
does not taint the turn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Wkz5axAzCvoLEFkZ5KtkKG
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