Skip to content

feat(ffi)!: decrypt encrypted ledgers via a host capability (#1667, WIT 3.2.0) - #1670

Merged
robcohen merged 1 commit into
mainfrom
feat/wasi-host-decrypt
Jul 1, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/wasi-host-decrypt

Conversation

@robcohen

@robcohen robcohen commented Jul 1, 2026

Copy link
Copy Markdown
Member

Closes #1667. Breaking WIT change: rustledger:ledger 3.1.0 → 3.2.0 — adds the host.decrypt import; embedders (rustfava) must provide it.

Problem

GPG-encrypted ledgers (.gpg/.asc) were detected but couldn't be decrypted through the WASI component — decryption shells out to gpg (Command::new), which a WASI guest can't do (no process spawn, no keyring). Load failed with failed to decrypt, so encrypted ledgers were unusable via the component (rustfava).

Approach — decryption as a host capability (b2)

Grounded in prior art + best practice (see the #1667 discussion): the WASI capability model says the host mediates authority including secrets, and beancount itself decrypts on the host (gpg --batch --decrypt) then loads the plaintext. So decryption belongs to the host, not the sandbox — and secret keys should never enter the guest.

This implements the clean component-model idiom: the component imports a decrypt capability the host satisfies.

  • loader: FileSystem::decrypt trait method — default shells out to gpg, so the native CLI path is unchanged. New process::load_with_fs for filesystem injection.
  • wit (3.2.0): interface host { decrypt: func(ciphertext: list<u8>) -> result<string, string> } + import host.
  • ffi-component: HostDecryptFs reads the ciphertext in-sandbox (WASI fs) and calls host.decrypt; load_file injects it.
  • ffi-wasi: load_file_with_fs threads the caller-provided filesystem.

load-file keeps handling encryption transparently — the keyring authority just lives in the host now.

Breaking host-contract change

The component now imports host.decrypt, so an embedder must provide it in its wasmtime linker — e.g.:

rustledger::ledger::host::add_to_linker::<_, HasSelf<Host>>(&mut linker, |h| h)?;
// impl Host { fn decrypt(&mut self, ct: Vec<u8>) -> Result<String,String> { /* gpg / gpgme */ } }

rustfava must add this to load encrypted ledgers. The native CLI is unaffected (it uses the loader's default gpg path).

Tests

  • An encrypted .gpg file loads via a host-provided decrypt (the test host returns a fixed plaintext — no real gpg/keyring needed in CI).
  • 92 loader + 19 FFI parity tests pass; clippy -D warnings + fmt clean; the version_matches_wit_package gate confirms API_VERSION 3.2 ↔ WIT 3.2.0.

Release

Breaking FFI/WIT → wants a 0.19.0; rustfava then adds the host.decrypt provider and regenerates bindings.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings July 1, 2026 19:24
@robcohen
robcohen requested a review from a team as a code owner July 1, 2026 19:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces host-mediated decryption for GPG-encrypted ledgers in the WASI component, by adding a new WIT import (host.decrypt) and routing loader decryption through an injectable filesystem. This enables .gpg/.asc ledgers to load in sandboxed environments where spawning gpg and accessing the keyring is not possible.

Changes:

  • Add FileSystem::decrypt and thread it through the loader pipeline (including a new load_with_fs entrypoint) so decryption can be delegated by sandboxed filesystems.
  • Bump WIT package rustledger:ledger to 3.2.0 and add the host.decrypt import to the component world.
  • Implement a component-side filesystem (HostDecryptFs) that reads ciphertext in-sandbox and calls the imported host.decrypt, plus parity coverage for the new capability.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/rustledger-loader/src/vfs.rs Adds FileSystem::decrypt with a default gpg --decrypt implementation.
crates/rustledger-loader/src/process.rs Adds load_with_fs to allow injecting a filesystem (used by WASI component).
crates/rustledger-loader/src/lib.rs Routes encrypted reads through self.fs.decrypt(path) and exposes load_with_fs; makes decrypt_gpg_file usable by vfs.
crates/rustledger-ffi-wasi/src/helpers.rs Adds load_file_with_fs so callers can thread an injected filesystem through the canonical loader pipeline.
crates/rustledger-ffi-component/wit/world.wit Bumps WIT package to 3.2.0 and introduces/imports interface host { decrypt(...) }.
crates/rustledger-ffi-component/src/lib.rs Updates component API version constant to 3.2.
crates/rustledger-ffi-component/src/convert.rs Implements HostDecryptFs that calls host.decrypt and injects it into load_file.
crates/rustledger-ffi-component/Cargo.toml Adds rustledger-loader dependency for DiskFileSystem/FileSystem usage.
crates/rustledger-ffi-component-tests/tests/parity.rs Links the new host import and adds a test validating encrypted loads via host decrypt.
Cargo.lock Locks the new crate dependency edge for the component crate.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-loader/src/lib.rs Resolved
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 5.88235% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/process.rs 0.00% 12 Missing ⚠️
crates/rustledger-loader/src/vfs.rs 0.00% 3 Missing ⚠️
crates/rustledger-loader/src/lib.rs 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.6ms ± 0.9ms 23.5 MB
beancount 769.5ms ± 6ms 50.6 MB
Comparison 21x faster 2.2x less

vs main branch (2026-07-01)

Metric main this PR Change
Time 36.5ms 36.6ms +0.3% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

WIT rustledger:ledger 3.1.0 -> 3.2.0 (adds the host.decrypt import); API_VERSION
-> 3.2.

GPG-encrypted ledgers (.gpg/.asc) were detected but could not be decrypted
through the WASI component: decryption shelled out to gpg (Command::new), which a
WASI guest cannot do (no process spawn, no keyring access), so load failed with
"failed to decrypt". Encrypted ledgers were unusable via the component (rustfava).

Fix follows the WASI capability model + beancount's own architecture: decryption
is a host capability, not something the sandbox does. Decryption is now a
FileSystem trait method (default: gpg subprocess — native CLI unchanged); the
component injects a filesystem whose decrypt reads the ciphertext in-sandbox and
calls the new host.decrypt import, which the embedder satisfies with its keyring
access. Secret keys never enter the guest.

- loader: FileSystem::decrypt (default gpg) + process::load_with_fs (fs injection)
- wit: interface host { decrypt: func(list<u8>) -> result<string,string> }; import host
- ffi-component: HostDecryptFs delegates decrypt to the host import; load_file uses it
- ffi-wasi: load_file_with_fs threads the caller fs

BREAKING (host contract): the component now imports host.decrypt, so an embedder
must provide it in its wasmtime linker (e.g. shelling out to gpg / gpgme). The
native CLI is unaffected.

Tests: an encrypted .gpg file loads via a host-provided decrypt (no gpg/keyring in
the sandbox). 92 loader + 19 FFI parity pass; clippy -D warnings + fmt clean.

Closes #1667

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen force-pushed the feat/wasi-host-decrypt branch from 0b29f69 to 38b9074 Compare July 1, 2026 20:18
@robcohen
robcohen merged commit 34d87f7 into main Jul 1, 2026
47 checks passed
@robcohen
robcohen deleted the feat/wasi-host-decrypt branch July 1, 2026 20:42
@robcohen robcohen mentioned this pull request Jul 1, 2026
robcohen added a commit that referenced this pull request Jul 1, 2026
Minor (breaking-FFI) release: WIT contract rustledger:ledger 3.2.0 — the component
now imports host.decrypt so encrypted ledgers are decrypted by the embedder
(#1667/#1670); embedders must provide the import and regenerate bindings. Also
carries the booking/validate diagnostics fixes (#1666, #1668 via #1669): lot-label
preserved on reductions, dedicated balance-currency diagnostic, deduped oversell
error. Workspace 0.18.0 -> 0.19.0; packages/mcp-server bumped to match.

Co-authored-by: Claude Opus 4.8 <[email protected]>
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.

GPG decryption fails: WASI component can't reach the keyring (failed to decrypt)

2 participants