wifi-densepose/v2/crates/homecore-plugins
ruv 0ca903b497 feat(homecore-plugins): enforce plugin signature + capability isolation (ADR-162 P4/P5)
ADR-161 honestly relabelled the manifest's wasm_module_hash / wasm_module_sig /
publisher_key as "(P4 — not yet enforced)" and the homecore_permissions claims
as deferred P5 authority isolation. This makes both real and tested.

P4 (signature/integrity verification, SECURITY):
- New `verify` module: SHA-256 module-hash check + Ed25519 signature
  verification over the digest against publisher_key, with a PluginPolicy
  trust allowlist and an explicit AllowUnsigned dev escape hatch (loud warn).
  Secure default rejects unsigned / unknown-publisher / tampered modules.
- Reuses the in-repo cog-ha-matter::witness_signing Ed25519 pattern; sha2 is a
  workspace dep, ed25519-dalek/hex/base64 already in the lock — no new external
  dep tree (only new edges in homecore-plugins).
- WasmtimeRuntime::load_plugin verifies before instantiation; legacy load_wasm
  retained for trusted/test modules.

P5 (authority/capability isolation, SECURITY):
- New `permissions` module: PermissionSet distilled from homecore_permissions
  (state:write:<glob> or bare entity glob). hc_state_set now consults it and
  returns a typed -3 to the guest on an undeclared write (no host panic).

Tests (fail on old code, which had no load_plugin/verify and an unchecked
hc_state_set): tampered module rejected; valid sig from trusted key loads;
valid sig from untrusted key rejected; unsigned rejected by default and loads
only under AllowUnsigned; light.* plugin writes light.kitchen but is denied
lock.front_door; no-permission plugin can write nothing. Real deterministic
keypair signs real bytes.

Manifest doc updated: P4/P5 now ENFORCED (was "not yet enforced").

homecore-plugins --features wasmtime: 32 passed (lib 23, integration 9), 0 failed.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-06-12 01:33:52 -04:00
..
src feat(homecore-plugins): enforce plugin signature + capability isolation (ADR-162 P4/P5) 2026-06-12 01:33:52 -04:00
tests feat(homecore-plugins): enforce plugin signature + capability isolation (ADR-162 P4/P5) 2026-06-12 01:33:52 -04:00
Cargo.toml feat(homecore-plugins): enforce plugin signature + capability isolation (ADR-162 P4/P5) 2026-06-12 01:33:52 -04:00
README.md docs(homecore-plugins): comprehensive README — WASM plugin runtime + InProcess registry 2026-05-25 23:10:35 -04:00

README.md

homecore-plugins

WASM integration plugin runtime for HOMECORE with native Rust runtime (P1) and Wasmtime JIT sandbox support (P2).

Crates.io License MSRV: 1.89+ Tests ADR-128

P1 scaffold: manifest parsing, plugin traits, and in-memory native Rust plugin registry. Wasmtime sandbox (P2) and hot-reload (P3) are deferred.

What this crate does

homecore-plugins provides a trait-based plugin system that can host both native Rust plugins (in-process) and WASM plugins (Wasmtime sandbox, P2). It defines:

  • PluginManifest — JSON schema for plugin metadata (superset of Home Assistant's manifest.json), validated at load time
  • HomeCorePlugin trait — async lifecycle hooks (setup, teardown, state changed handlers)
  • PluginRuntime trait — abstraction over execution environments (native vs WASM)
  • InProcessRuntime — built-in runtime for first-party Rust plugins (P1)
  • PluginRegistry — manages loading, unloading, and querying plugins
  • Host ABI (stubs) — C-compatible function signatures for WASM ↔ homecore calls (wiring in P2)

The system is designed to be feature-gated: compile with --features wasmtime to unlock JIT sandbox support for untrusted third-party plugins.

Features

  • Native Rust plugins — first-party integrations compiled into the binary, zero sandbox overhead (P1)
  • WASM plugin framework — trait-based abstraction ready for Wasmtime JIT (P2) or wasm3 interpreter (P3)
  • PluginManifest validation — required fields enforced at load time; superset of HA manifest fields
  • Async plugin lifecyclesetup() and teardown() for resource management
  • State change subscriptions — plugins can subscribe to entity state changes with handler callbac
  • Config entry lifecycle — plugin receives config when registered; P3 adds hot-reload
  • Feature-gated runtimes — Wasmtime (30 MB, P2) and wasm3 (50 kB, P3) are optional dependencies
  • Manifest inheritance from Home Assistantcodeowners, requirements, documentation, issue_tracker, IoT classification

Capabilities

Capability Type Method Notes
Load native plugin Runtime InProcessRuntime::load(manifest, handler) Sync; handler is a Rust type implementing HomeCorePlugin
Load WASM plugin Runtime WasmtimeRuntime::load(wasm_bytes, manifest) (P2) Async; JIT compiles via Cranelift; requires --features wasmtime
List loaded plugins Registry PluginRegistry::list() Returns Vec<(PluginId, PluginManifest)>
Query plugin config Registry PluginRegistry::get_config(plugin_id) Returns Arc<ConfigEntryJson>
Call plugin handler Host ABI hc_state_changed(event) (P2) WASM plugin receives state change events via exported function
Unload plugin Registry PluginRegistry::unload(plugin_id) Calls teardown(), frees memory (P3 = hot-reload)

Comparison to Home Assistant

Aspect Home Assistant homecore-plugins
Plugin language Python (.py integrations) Rust (P1) + WASM (P2+)
Sandbox None (all Python in same process) None (P1); Wasmtime sandbox (P2)
Plugin discovery homeassistant/components/ directory PluginManifest JSON + registry
Config lifecycle YAML + dynamic reload Config entry + manifest (hot-reload P3)
Host ABI CPython C API C types + Wasmtime exported functions (P2)
Manifest format Home Assistant's manifest.json subset Superset with ioc_class, cog_publisher
Feature gating Integration-specific Feature flags: wasmtime, wasm3

Performance

  • Native plugin overhead — same as regular Rust function calls; no sandbox cost
  • WASM plugin sandbox — Wasmtime JIT ~5 ms per call (after warmup); memory overhead ~10 MB per instance
  • Manifest parsing — < 1 ms (serde_json)
  • Registry operations — O(1) plugin lookup (DashMap); O(n) for list()
  • No per-crate benchmarks yet — a follow-up issue tracks baseline measurements

Usage

Native plugin (P1):

use homecore_plugins::{HomeCorePlugin, PluginManifest, InProcessRuntime};
use async_trait::async_trait;

struct MyPlugin;

#[async_trait]
impl HomeCorePlugin for MyPlugin {
    async fn setup(&mut self) -> Result<(), homecore_plugins::PluginError> {
        println!("Plugin setup");
        Ok(())
    }

    async fn teardown(&mut self) -> Result<(), homecore_plugins::PluginError> {
        println!("Plugin teardown");
        Ok(())
    }

    async fn on_state_changed(&mut self, _event: &homecore_plugins::StateChangedEventJson) -> Result<(), homecore_plugins::PluginError> {
        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let manifest = PluginManifest {
        domain: "my_plugin".to_string(),
        name: "My Plugin".to_string(),
        ..Default::default()
    };

    let mut runtime = InProcessRuntime::new();
    let plugin_id = runtime.load(manifest.clone(), MyPlugin).await.expect("load plugin");
    println!("Loaded plugin: {:?}", plugin_id);
    runtime.unload(&plugin_id).await.ok();
}

WASM plugin (P2 example):

# Build a WASM plugin (requires --features wasmtime)
cargo build -p homecore-plugin-example --target wasm32-unknown-unknown --release

# The WasmtimeRuntime will be available at P2:
# let mut runtime = WasmtimeRuntime::new();
# let plugin_id = runtime.load(wasm_bytes, manifest).await?;

Relation to other HOMECORE crates

homecore-plugins (plugin registry + runtime abstraction)
├─ homecore (state machine; plugins receive state changes)
├─ homecore-plugin-example (reference WASM plugin)
├─ homecore-server (loads plugins at startup)
└─ homecore-automation (can invoke handlers via service calls)

Security Notes

P1 (this release): No sandbox. Native Rust plugins have full process access.

P2 (planned): Wasmtime JIT sandbox is opt-in via --features wasmtime. WASM plugins run in isolated memory with explicit host ABI calls to access homecore state. The host ABI is frozen before P2 begins (ADR-128 §8 risk mitigation).

P4+: Ed25519 signature verification and permission enforcement for third-party Cog registry distribution.

References