Commit 37db357
fix(ocap-kernel): request only the URL the fetch caveat approved (#1026)
Fixes MetaMask/MetaMask-planning#7557.
## Problem
A vat granted `fetch` could reach any host regardless of its
`network.allowedHosts`. Three routes, one shape: **the URL the caveat
approves is not the URL that gets requested.**
1. **TOCTOU (CWE-367 — the reported PoC).** Both enforcement points
resolved the vat's input to a URL for validation and then handed **the
input itself** to `fetch`, which resolved it a second time. An input
answering differently on each read was validated as an allowed host and
requested as a forbidden one. `harden()` does not help: it freezes the
wrapper, not the vat's argument.
2. **Redirects.** `redirect` defaults to `follow` and the caveat only
ever saw the pre-flight URL, so an allowed host answering `302 Location:
http://169.254.169.254/latest/meta-data/` sent the vat's request outside
the allowlist and handed the vat the response body. Not merely a risk
from a compromised third party: an allowlist may perfectly well name a
host **the vat itself controls**, which makes this a general escape.
3. **Transport replacement.** undici honours a `dispatcher` in `init`,
and a dispatcher decides where the bytes go whatever URL the caveat
approved. Found while fixing (2); not in the report.
## Fix
New in `@metamask/kernel-utils`:
- **`resolveFetchInput`** resolves an input exactly once and returns the
resolved URL plus a stand-in to forward in its place.
- **`makeGuardedFetch`** wraps a `fetch` so a guard runs before
**every** request it makes, following redirects itself, one hop at a
time.
Both enforcement points route through them —
`makeCaveatedFetch`/`makeHostCaveat` (`ocap-kernel`) and
`makeHostRestrictedFetch` (`kernel-language-model-service`) — and are
now one-line delegations.
The caveat takes a **`URL` and nothing else**. The deleted `FetchCaveat`
handed every policy the raw fetch args and made it re-derive the URL
itself, via the now-deleted `resolveUrl` — the exact CWE-367 read. A
guard author now has no raw input to mis-resolve, and no
partially-accurate `init` to mis-read: the first request's `init` is
assembled from the caller's arguments while a hop's comes from the
redirect rewrite, so an `init`-reading policy would have been sound only
after the first hop — the hop every request makes. `requestOnce`
likewise takes the resolved `{ url, input }` pair rather than a
destination and the URL it is claimed to be, so passing the caller's own
input is a type error rather than a comment violation.
### Closed
| Vector | Handling |
| --- | --- |
| Stringifier answering differently on successive reads (the PoC) |
Rejected, rather than serviced at whichever URL it showed first |
| `Request` subclass overriding its `url` getter | URL read through the
genuine accessor |
| `Request` state in a mutable own property (undici on Node 22) |
`Request` copied, then rebuilt around the resolved URL as a string |
| Redirect out of the allowlist | Refused; the forbidden host is never
contacted |
| `redirect: 'follow'` in `init` **or** on a `Request` | Overridden, not
merged |
| `dispatcher` in `init` | Refused — from a snapshot of `init`, so an
accessor cannot answer the check with `undefined` and the transport with
a dispatcher |
| `dispatcher` planted as an own property of a `Request` | Shed by the
copy that precedes the rebuild |
| `baseFetch` that walks a chain itself despite `redirect: 'manual'` |
Refused, rather than returned as the approved resource |
### Semantics
- **Follow per hop, don't reject outright.** A redirect that stays
inside `allowedHosts` is followed as `fetch` would have, so
`http`→`https` and `/x`→`/x/` keep working. `response.url` names where
the chain ended and `response.redirected` reports the chain. Getting
`redirected` right needs a proxy: the vat `fetch` endowment **hardens**
its response, so the flag cannot be written onto it. The proxy is not
the response — `Response.prototype.text.call(view)` throws where
`view.text()` works — which the JSDoc records.
- **Except in a browser, where no redirect can be followed at all.**
Checking a hop requires `redirect: 'manual'`, and a browser answers that
with an opaque-redirect response — status 0, no headers — which hides
the target instead of exposing it. `VatSupervisor` runs in an iframe, so
this is the extension's behaviour, not a corner case: a redirected
request fails there rather than being followed. Failing closed is the
only option; the alternative is approving a URL and requesting whatever
the `Location` said. Non-redirected requests are unaffected.
- **Only `follow` is overridden.** It is the one mode that could walk to
an unapproved host. `manual` and `error` ask for *less*, so they are
obeyed rather than silently upgraded. The mode is answered on the status
alone, before the `Location` is read, as the spec orders it — so `error`
fails a redirect that names nowhere to go rather than passing it off as
an ordinary response, which is also what undici does.
- **Three things fail closed rather than quietly:**
- a hop that **keeps the body** when that body cannot be sent again.
Stricter than the spec, which replays a stream whose *source* it kept —
a `Request`'s body is a stream however it was built, so `fetch(new
Request(url, {method:'POST', body:'x'}))` across a 307 now errors where
`fetch` would have replayed it. Buffering every body up front to avoid
that would make an unbounded upload an unbounded allocation; the error
names the hop and the remedy.
- a hop to a **scheme `fetch` will not follow** (`data:`, `file:`,
`blob:`), refused by name rather than incidentally as a nameless host.
- a **`baseFetch` that followed a redirect below the guard**. Every
request here asks for `manual`, so a response reporting `redirected`
means a chain was walked that the guard never saw — silently degrading
back to the pre-flight-only checking this PR replaces.
- Spec rewrite otherwise: 303 (and 301/302 from a POST) → bodyless GET,
credentials dropped when a hop leaves the origin, 20-hop limit.
## Verification
Against live `http.createServer` instances on **Node 22.20 and 24.18**
(both in the CI matrix; `engines: >=22`), exercising the built `dist`
under lockdown — **49 checks pass on both**, and **13 of them fail
against the pre-fix build**, with the forbidden server recording `GET
/secrets`.
Covered: single cross-host redirect; chain ending on an allowed host;
excursion to another allowed host and back; redirect loop; vat-supplied
`follow`/`manual`/`error` and a `Request` carrying its own mode; 307
with string and stream bodies; 303 POST→GET; cross-origin credential
stripping; non-fetchable scheme; empty `Location`; `dispatcher` in
`init`, planted on a `Request`, and hidden behind an accessor. Plus the
non-regressions — GET, POST string/stream bodies, `Request` reuse,
`Request` + `init` override, `AbortController` propagation across a hop
— all with bodies intact.
**All 33 mutations of `guarded-fetch.ts` are caught by its tests**,
including each arm of the origin comparison, each entry of both header
lists, the 303 HEAD exemption, and the `body: null` and method-case edge
cases. Deleting `resolveFetchInput`'s defensive `new Request(input)`
copy is caught too — that copy is what sheds a planted `dispatcher`, and
asserting it needs a live server, since the rebuilt `Request` keeps a
dispatcher out of reach whether or not it carried one. Coverage is
layered: real-server tests in `kernel-utils`, unit tests at both
enforcement points, and an end-to-end test in `kernel-test` driving
every attack from a real vat through the real supervisor and the
hardened Snaps `ResponseWrapper`.
## Not addressed
- **DNS rebinding.** The caveat matches the hostname a vat names, so an
allowlisted name resolving to a loopback or link-local address reaches
it. Which address a name resolves to at connect time is undici's
business and not reachable from here.
- **No audit trail.** A refused hop throws; the vat sees the error and
may swallow it, leaving no kernel-side record that an allowlist
violation was attempted. The signal is currently inverted — successful
egress reaches `logger.debug`, a denial reaches nothing — and the
success path carries no URL either, so there is no egress-destination
trail in either direction. Worth a follow-up: an `onRefusal` on
`makeGuardedFetch` would also catch the non-guard refusals (bad
`Location`, non-fetchable scheme, hop cap, unreplayable body).
- **`cache` is not carried across a hop.** This package compiles against
undici's `RequestInit`, which has no `cache` field because undici
ignores cache mode, so a hop in a browser realm reverts to the default.
- **Stream request bodies in a browser.** The `Request` rebuild passes
the original through as a `RequestInit`, which needs `duplex` when the
body is a stream. undici exposes `Request.prototype.duplex`; browsers do
not, so a browser vat's stream-bodied `Request` fails before any request
is made. Fail-closed, and narrow — streaming uploads need HTTPS and
HTTP/2 anyway.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **High Risk**
> Changes security-critical vat network confinement and
redirect/integrity semantics; incorrect behavior could allow
exfiltration or break legitimate fetches.
>
> **Overview**
> **Security fix** for vat `fetch` escaping `network.allowedHosts`
(CWE-367): the allowlist was checked on one URL resolution while the
underlying `fetch` could read the input again or follow redirects to
forbidden hosts.
>
> Adds **`resolveFetchInput`** and **`makeGuardedFetch`** in
`@metamask/kernel-utils` so the guard runs on the URL that will actually
be requested, with a safe stand-in input forwarded to `baseFetch`.
Redirects are handled manually (`redirect: 'manual'` on every hop) so
each `Location` is host-checked; caller `redirect: 'follow'` is
overridden, `manual`/`error` honored. Also rejects `dispatcher` in init,
blocks non-replayable bodies on body-preserving redirects, and verifies
**`integrity`** against the final response body (not intermediate 3xx
bodies).
>
> **`makeCaveatedFetch` / `makeHostCaveat`** (`ocap-kernel`) and
**`makeHostRestrictedFetch`** (Ollama) now delegate to
`makeGuardedFetch`; the host caveat takes a **`URL` only** (removed
`resolveUrl` / raw-args caveat). Regression coverage spans unit tests,
live HTTP servers, and kernel vat endowment tests.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
282fbaf. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 180e6ac commit 37db357
21 files changed
Lines changed: 3018 additions & 112 deletions
File tree
- packages
- kernel-language-model-service
- src/ollama
- kernel-test/src
- vats
- kernel-utils
- src
- nodejs-test-workers/src/workers
- ocap-kernel
- src/vats
- repo-tools
- src/test-utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
10 | 15 | | |
Lines changed: 95 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
93 | 98 | | |
94 | 99 | | |
95 | 100 | | |
96 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
97 | 107 | | |
98 | 108 | | |
99 | 109 | | |
100 | 110 | | |
101 | 111 | | |
102 | 112 | | |
103 | 113 | | |
104 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
105 | 196 | | |
106 | 197 | | |
107 | 198 | | |
| |||
Lines changed: 18 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
15 | 19 | | |
16 | | - | |
| 20 | + | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
68 | 233 | | |
0 commit comments