Skip to content

feat(ocap-kernel): integrate Snaps network endowment factory#942

Open
sirtimid wants to merge 4 commits intomainfrom
sirtimid/integrate-snaps-network-endowment
Open

feat(ocap-kernel): integrate Snaps network endowment factory#942
sirtimid wants to merge 4 commits intomainfrom
sirtimid/integrate-snaps-network-endowment

Conversation

@sirtimid
Copy link
Copy Markdown
Contributor

@sirtimid sirtimid commented Apr 22, 2026

Closes #936.

Integrates the Snaps @metamask/snaps-execution-environments/endowments network factory into vat globals. Replaces the platform-level fetch capability with a per-vat endowment that ships fetch/Request/Headers/Response, wires teardown for in-flight requests and open body streams, and enforces a per-vat host allowlist (VatConfig.network.allowedHosts) applied at #initVat via makeHostCaveat. fetch without an allowlist fails init — no implicit allow-all. file:// URLs are rejected by the caveat with a hint to use the fs platform capability.

Summary

  • Add fetch, Request, Headers, Response to the default vat endowments via the Snaps factory; teardown aggregates into VatSupervisor.terminate().
  • New VatConfig.network: { allowedHosts: string[] }; host matching is URL.hostname-only (ports and schemes ignored).
  • Wire the Snaps factory's notify callback to the vat logger at debug; transport failures surface via console.error.
  • Remove the entire packages/kernel-platforms/src/capabilities/fetch/ directory (BREAKING for direct consumers of fetchConfigStruct, FetchCapability, makeHostCaveat, makeCaveatedFetch).
  • Migrate consumers: kernel-node-runtime drops platformOptions.fetch.fromFetch; nodejs-test-workers/mock-fetch stubs globalThis.fetch directly; evm-wallet-experiment cluster config and docker e2e helper move to globals: ['fetch', ...] + network.allowedHosts.
  • Update docs/kernel-guide.md endowment table, docs/usage.md, and packages/evm-wallet-experiment/docs/setup-guide.md.

Test plan

  • Unit: network-caveat.test.ts covers host/hostname matching, port-insensitivity, file:// rejection (string + Request input), malformed URL propagation, and a positive+negative end-to-end chain through makeCaveatedFetch.
  • endowments.test.ts verifies the new globals list, the notify wiring, logger-transport error containment, and that teardown aborts an in-flight fetch (stubs globalThis.fetch with an abort-respecting promise, asserts the abort signal propagates and teardown returns cleanly).
  • VatSupervisor.test.ts exercises both the fetch-without-allowlist throw path and the positive-path where network.allowedHosts is supplied (asserts absence of the guard error in dispatch).
  • types.test.ts validates the new network field, rejecting non-array and non-string entries.
  • Integration: kernel-test/src/endowments.test.ts exercises allowed/disallowed hosts end-to-end through a real VatSupervisor and asserts Request/Headers/Response constructors are available in the vat compartment.
  • Lint + changelog validate clean across all three touched packages.

🤖 Generated with Claude Code


Note

High Risk
High risk because it changes how outbound network access is granted/enforced (new per-vat allowlist guard) and introduces breaking config/API changes (platformConfig.fetch removal, MakeAllowedGlobals signature change) that affect multiple packages and tests.

Overview
Adds Snaps network endowments to vats by including fetch plus hardened Request/Headers/Response in the default allowed globals and wiring their teardown into vat termination.

Enforces a new per-vat outbound host allowlist via VatConfig.network.allowedHosts; VatSupervisor now rejects vats that request fetch without this config and wraps fetch with a hostname caveat (including explicit file:// rejection).

BREAKING migration: removes the fetch platform capability entirely from @metamask/kernel-platforms, drops platformOptions.fetch usage in kernel-node-runtime, updates consumers/tests to request globals: ['fetch', ...] + network.allowedHosts, and changes MakeAllowedGlobals/createDefaultEndowments to accept a { logger } options bag (used for Snaps network notify logging).

Reviewed by Cursor Bugbot for commit fd045f4. Bugbot is set up for automated code reviews on this repo. Configure here.

sirtimid and others added 2 commits April 22, 2026 19:46
Add fetch/Request/Headers/Response to default vat endowments via
@metamask/snaps-execution-environments's network factory, with per-vat
host allowlist threaded through a new VatConfig.network field. Remove
the platform-level fetch capability entirely.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Reject file:// URLs in network caveat with a hint to use the fs
  platform capability (closes a filesystem capability leak)
- Match host allowlist on URL.hostname, ignoring ports
- Surface logger-transport failures in the notify bridge via
  console.error so a broken audit trail is visible
- Add tests: teardown aborts in-flight fetch, caveated-fetch
  end-to-end chain, positive-path initVat past the fetch guard,
  malformed URLs, file:// via Request input

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sirtimid sirtimid requested a review from a team as a code owner April 22, 2026 18:04
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 22, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 70.95%
⬇️ -0.04%
8124 / 11450
🔵 Statements 70.78%
⬇️ -0.05%
8259 / 11667
🔵 Functions 71.91%
⬇️ -0.03%
1972 / 2742
🔵 Branches 64.5%
⬇️ -0.09%
3290 / 5100
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/evm-wallet-experiment/src/cluster-config.ts 100%
🟰 ±0%
83.33%
⬇️ -6.67%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-node-runtime/src/vat/vat-worker.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
8-27
packages/kernel-platforms/src/browser.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-platforms/src/nodejs.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-platforms/src/platform-test.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-platforms/src/capabilities/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-test/src/vats/endowment-fetch.ts 0%
🟰 ±0%
0%
⬇️ -100.00%
0%
🟰 ±0%
0%
🟰 ±0%
16-48
packages/nodejs-test-workers/src/workers/mock-fetch.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
6-34
packages/ocap-kernel/src/types.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/vats/VatSupervisor.ts 79.2%
⬆️ +4.20%
59.09%
⬆️ +9.09%
53.33%
🟰 ±0%
79.2%
⬆️ +4.20%
152, 164, 186, 257, 295-299, 310, 319-320, 342-344, 347, 351-353, 383-384, 395-397, 432, 449-457
packages/ocap-kernel/src/vats/endowments.ts 100%
🟰 ±0%
80%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/vats/network-caveat.ts 100% 100% 100% 100%
Generated in workflow #4357 for commit fd045f4 by the Vitest Coverage Report Action

- VatSupervisor: add positive-path assertion (init returns a result)
  alongside the existing negation to prevent vacuous pass
- endowments: replace globalThis.fetch mutation with vi.stubGlobal +
  unstubAllGlobals in afterEach; removes the require-atomic-updates
  disable and avoids cross-test bleed
- endowments: assert console.error fallback in the notify-swallow
  test so a silent-swallow regression would fail
- network-caveat: add data:/blob: scheme cases to pin the empty-
  hostname rejection path; rename $scheme it.each label to $label
  for accuracy

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

Integrate Snaps network endowment factory into vat globals

1 participant