Skip to content

imcp2-deploy 1/6: an icp-project host for a bundle, treated as untrusted input - #230

Closed
raymondk wants to merge 2 commits into
mainfrom
deploy-1-bundle-host
Closed

raymondk wants to merge 2 commits into
mainfrom
deploy-1-bundle-host

Conversation

@raymondk

@raymondk raymondk commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the imcp2-deploy crate and the library's Host for a bundle, the .tar.gz that icp project bundle writes. Nothing drives it yet.

The bundle is untrusted input. A bundle is handed over by an agent, so the host takes it for exactly what a bundle is and no more:

  • extract_bundle unpacks it into a scratch directory under a compressed-size limit (256 MiB) and a decompressed-stream limit (1 GiB), refusing symlinks and other non-file entries, more than 250,000 files and directories (counting the parents an entry's path implies), entry paths over 4096 bytes, and anything under .icp/ in any letter case, the id and artifact state a deploy seeds itself.
  • The FileSystem the library reads the project through is the library's own files::Confined: the root is opened once as a directory handle and every read is made beneath it, so a path that leaves the root (by climbing, by being absolute elsewhere, or by a symlink) is refused, and so is a path through a symlink wherever it leads. The project loader reads through a metered view of it, with a 256 MiB budget on what it retains.
  • The loaded project is vetted by the library's untrusted rule (icp_project::untrusted::Vetted, what icp deploy --untrusted applies): each build step a pre-built module, each sync step a plugin, each file named a regular file the confined filesystem will give. A script step, a module or plugin fetched from a URL, or a recipe (untrusted::NoRecipes) fails the load, before any phase.
  • IcMainnet is the network seam: the reserved ic network as the loader supplies it, nothing else. A manifest declaring another network that claims the mainnet root key over an endpoint of its own is refused.
  • BundledWasm serves local wasm sources from the bundle, sha256 verified. NoScripts is the script runner the syncer is composed with; it spawns nothing.
  • check_bundle validates the whole bundle before a deploy would run: one pre-built step per canister naming a wasm in the archive that hashes to its recorded checksum (and, gzipped, expands to no more than 100 MiB), canister names that differ by more than letter case, every sync plugin in the archive with the bytes its files: declare held to a sum, Candid text held to core's guard_candid_text, an environment on ic.

Dependencies. The four icp CLI crates (icp-project, icp-events, icp-sync-plugin, icp-canister-interfaces) come from crates.io at 0.1, which carries dfinity/icp-cli#873 on #868. icp-sync-plugin brings wasmtime into the workspace, which is why this is its own crate and not a module of imcp2-core. The crate is publish = false. The manifest lists only what this PR uses; later PRs in the stack add theirs.

What changed in this PR at the re-cut

  • The icp CLI crates are crates.io 0.1 releases, where this PR had a git pin on icp-cli's main. Cargo.lock grows by 96 packages over main, where the git pin added 129.
  • One commit, where there were thirteen following the upstream pin.
  • host.rs is in its final form: the Candid guard on a bundle's arguments and interfaces, which the pipeline PR used to add here, is in.
  • src/lib.rs is a stub that names the module; the crate's account of itself arrives with the tools in imcp2-deploy 5/6: the MCP tools, served from the local server #232.

Review so far

  • aterga asked whether some of the third-party crates can be avoided, and for an analysis of the pinned versions. The move to crates.io releases answers the pin half in part: versions are released ones, resolved in Cargo.lock. No per-crate analysis has been written; that is still open.

Changes

  • crates/imcp2-deploy (new): Cargo.toml, src/lib.rs, src/host.rs (915 lines of code, 748 of tests).
  • Workspace Cargo.toml adds the member; Cargo.lock.
  • imcp2-core: guard_candid_text made public and re-exported.

Testing

  • cargo build --locked --workspace --all-targets
  • cargo test --locked --workspace --all-targets
  • cargo fmt --all -- --check

The tests are offline: the network seam, every extraction refusal named above, the confined filesystem serving what the bundle carries and nothing outside it, the load refusing a script step, a remote module, a recipe and a module named through a link, a sync step's declared paths, the loader's read budget, and a module checked once however many canisters name it.

The stack

Six stacked PRs that bring icp deploy to the MCP servers through the icp CLI's own project library. Re-cut on 2026-11-18 so that each file arrives once, in the form it has at the top; the tree at the top is unchanged from before the re-cut, rebased onto current main.

PR What
1 #230 The crate and the icp-project host for an untrusted bundle
2 #246 Signing: canister calls over ic-agent, an identity that refreshes
3 #231 The deploy pipeline, its events and its interruption
4 #247 The upload-ticket store
5 #232 The MCP tools, and the local server serving them
6 #243 The hosted server serving them

#242 (streamed phases, interruption) and #245 (one bundle source per server, crates.io) are closed: their changes are folded into the PRs above.

Checklist

  • I have read the Contributing guidelines.
  • Docs (README / comments) updated for any user-visible change.
  • No secrets, credentials, or internal-only information are included.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical findings permit archive-controlled state injection, host-file access, and requests to arbitrary network endpoints, while mismatched crate versions block releases.

4 open issues
What changed in this PR

Adds an unpublished imcp2-deploy crate implementing an icp-project host for untrusted project bundles. This establishes the foundation for the deployment pipeline and local MCP tools in the next two PRs.

Changes:

  • Adds bounded archive extraction, an index-backed filesystem, and bundle validation.
  • Implements network, wasm, and build adapters, with script execution refused and offline tests included.
  • Registers the crate and locks the pinned icp-cli dependencies, including the Wasmtime runtime.
File Description
crates/​imcp2-deploy/​src/​lib.rs Documents the crate’s scope and exports the host module.
crates/​imcp2-deploy/​src/​host.rs Implements bundle extraction, host adapters, validation, and tests.
crates/​imcp2-deploy/​Cargo.toml Defines the unpublished crate and pins icp-cli dependencies.
Cargo.toml Adds the workspace member and changes root version declarations.
Cargo.lock Records the new dependency tree and package version updates.

Review effort: Balanced

Comment thread Cargo.toml Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
@raymondk
raymondk added this pull request to stack #233 November 3, 2026 19:07
Copilot AI balanced review requested due to automatic review settings November 3, 2026 19:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical flaws allow case-insensitive aliases to introduce unintended deployment targets or substitute Wasm artifacts, while unbounded decompression and quadratic index scans expose the server to memory and CPU exhaustion.

4 open issues
4 resolved since last review

Review effort: Balanced

Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 3, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Critical resource-budget gaps in archive extraction, plugin execution, and project loading allow crafted bundles to exhaust server memory, disk space, or inodes.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

High severity Repeated file-backed arguments bypass the project-loading memory limit

crates/​imcp2-deploy/​src/​host.rs:361

Project loading can exhaust memory before check_bundle runs: the pinned loader reads each file-backed argument into a separate owned value retained by its canister. Fifty canisters referencing the same 100 MiB binary argument retain roughly 5 GiB despite staying within the extraction limits. Enforce a cumulative project-loading read budget, counting repeated reads, before allocating and returning these buffers to ProjectLoadImpl.

High severity Plugin execution lacks a store memory resource limiter

crates/​imcp2-deploy/​src/​host.rs:744

The pinned Wasmtime runner executes bundle-supplied plugins in the server process with stack and epoch limits but no store resource limiter. A small component can allocate and touch multiple GiB of linear memory, potentially exhausting server memory before the timeout; archive limits do not constrain these allocations. Use a runner with aggregate memory, table, and instance budgets, or run plugins in an OS-resource-limited subprocess.

High severity Implicit archive directories bypass the filesystem-object limit

crates/​imcp2-deploy/​src/​host.rs:827

The entry cap counts archive records, but unpack_in also creates every missing parent directory. A bundle of empty files beneath distinct, deep directory chains can stay within both byte limits and the entry cap while creating millions of directories, exhausting disk space or inodes. Count distinct implicit parent directories against a filesystem-object budget before unpacking each entry.

4 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 3, 2026 22:34
@raymondk

raymondk commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

The three "previously missed" findings in the last review, addressed in ec3c3c3:

  • Repeated file-backed arguments bypass the project-loading memory limit. The project loader now reads through a metered view of the bundle (Metered in host.rs), charged by file size before each read, with MAX_PROJECT_BYTES (256 MiB) for everything it loads: manifests, argument files and variable files, counted once per canister that names them. The phases keep reading the bundle itself, so a module read several times over a deploy is not charged. Tested at loading_the_project_is_held_to_its_read_budget (two canisters naming one file: the second read is what trips the budget).
  • Implicit archive directories bypass the filesystem-object limit. Extraction now counts files and directories alike against MAX_BUNDLE_ENTRIES, charging each directory an entry's path implies once (read off the index and the directory entries written so far). Since that walk costs a lookup per component, an entry's path is first held to MAX_ENTRY_PATH_BYTES (4096, Linux's PATH_MAX). Tested at the_directories_an_archive_implies_count_against_its_cap and an_entry_path_past_the_length_limit_is_refused.
  • Plugin execution lacks a store memory resource limiter. Not bounded here, and host_for's docs now say so. The runner is icp_sync_plugin::Wasmtime, which builds its own Engine and Store inside a private function; nothing on this side of its Run trait can attach a StoreLimits, and the real plugins (e.g. the certified-assets plugin) declare no memory maximum, so a static check on the component would refuse them. The fix is a store limiter in icp-sync-plugin's runner upstream, after which this crate bumps the pinned rev.

#231 and #232 are rebased on top locally and get pushed next.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unbounded plugin input loading and repeated module decompression allow untrusted bundles to exhaust host memory or monopolize the server.

6 open issues

Review effort: Balanced

Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/Cargo.toml Outdated
# The extraction directory is resolved to the spelling the disk knows it by
# (see `extract_bundle`); `dunce` keeps a Windows path free of the `\\?\`
# prefix, as icp-project's own host filesystem does.
dunce = "1"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we avoid adding some of these 3rd party crates?

For the ones that we cannot replicate, let's have an explicit analysis of the pinned version, since this repo is particularly sensitive to supply chain vulnerabilities.

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.

Our own additions are two crates, camino and tar (flate2, tempfile and async-trait were already in the tree). The other ~125 new packages come through the four icp-cli crates: ~60 are wasmtime/cranelift via icp-sync-plugin (the CLI runs sync steps, asset upload included, as wasm plugins), ~10 are cap-std via icp-project's confined filesystem, ~10 are ic-cdk/ledger types via icp-canister-interfaces. Those can't be dropped without reimplementing the CLI's sync runtime. For the rest, I can add a cargo-vet/cargo-deny policy in CI or a per-version review of the pinned set — say which.

@raymondk
raymondk force-pushed the deploy-1-bundle-host branch from d5a789c to a820722 Compare November 11, 2026 22:06
@raymondk
raymondk marked this pull request as ready for review November 12, 2026 12:57
Copilot AI balanced review requested due to automatic review settings November 12, 2026 12:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Untrusted plugins can exhaust MCP server memory because the runner lacks hard guest/store memory limits, and existing archive, module-expansion, and file budgets do not constrain guest allocations.

1 open issue
6 resolved since last review

Review effort: Balanced

syncer: Arc::new(Syncer::new(
Arc::new(NoScripts),
wasm.clone(),
Arc::new(icp_sync_plugin::Wasmtime),
…ted input

The crate takes the icp CLI's project library from crates.io at 0.1
(icp-project, icp-events, icp-sync-plugin, icp-canister-interfaces) and is
not published itself. `host` extracts the archive `icp project bundle`
writes under bounds on its size, entries and paths, reads it through the
library's confined filesystem, and composes the rule `icp --untrusted`
applies: no recipes, no scripts, modules and plugins from the bundle alone,
the `ic` network only.

imcp2-core makes `guard_candid_text` public, for the Candid text a bundle
carries.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@raymondk
raymondk force-pushed the deploy-1-bundle-host branch from a820722 to 46dd8cc Compare November 18, 2026 02:00
Copilot AI balanced review requested due to automatic review settings November 18, 2026 02:00
@raymondk
raymondk removed this pull request from stack #233 November 18, 2026 02:01
@raymondk
raymondk added this pull request to stack #248 November 18, 2026 02:02
@raymondk raymondk changed the title imcp2-deploy 1/3: an icp-project host for a bundle, treated as untrusted input imcp2-deploy 1/6: an icp-project host for a bundle, treated as untrusted input Nov 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Untrusted plugins can exhaust server memory without an aggregate runtime limit, and deeply nested resolved interface types can bypass the text scanner and crash deploy workers through stack overflow.

2 open issues

Review effort: Balanced

Comment on lines +642 to +644
if let Some(interface) = extract_candid_service(&bytes) {
guard_candid_text(&format!("the `candid:service` interface of '{path}'"), &interface)
.map_err(Unsupported)?;
…what it refuted

A deploy's upgrade compatibility check is candid's subtype check, run
between a bundle's `candid:service` interface and the deployed canister's,
both the caller's to write. On 0.10.35 a chain of two dozen `opt` aliases
whose leaves differ, half a kilobyte and well inside `guard_candid_text`,
ran exponentially long on a thread nothing can interrupt, and a deeper
chain panicked where the check unwrapped its own recursion-limit error.
0.10.39 removes both.

What is left: the check is polynomial in the resolved depth, about cubic,
so an interface the guard admits can still cost seconds (about 18 s
measured for 1,023 aliases of three `opt`s each on a 2 MiB stack).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 03:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because missing bundle-level coverage leaves Candid argument dispatch and interface validation behavior insufficiently verified.

2 open issues

Review effort: Balanced

@raymondk

Copy link
Copy Markdown
Contributor Author

Superseded by the stack #254 → #259, which carries the same code in six PRs from main, with the deploy tools living in imcp2-core instead of a separate crate. The bundle host is in #254.

@raymondk raymondk closed this Nov 19, 2026
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.

3 participants