// packages/wasm_plugin_sdk/examples/demo.vk // Self-contained verification suite & runnable example for `wasm_plugin_sdk`. use [ sdk = wasm_plugin_sdk fs hex = codec/hex x25 = crypto/x25519 ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] println (str/join ['[PASS] ' label] '') .ok ] run-demo = fn [] enum [ tmp_dir = 'packages/wasm_plugin_sdk/.demo_tmp' _ = fs/mkdir-all tmp_dir defer [ _ = fs/remove-all tmp_dir ] // --- 1. `VKPW` Binary + JSON Host-Guest RPC Envelope Roundtrip & CRC-32 Integrity --- call_wire = sdk/encode-rpc-call 'calc-discount' { subtotal_cents = 25000, tier = 'gold' } { max_pct = 20 } dec_call = sdk/decode-rpc-frame call_wire mut bad_wire = bytes/with-cap (len call_wire) _ = bytes/push-all mut bad_wire call_wire _ = bytes/set mut bad_wire 12 (bad_wire[12] ^ 0xff) crc_rejected = err? (sdk/decode-rpc-frame/or bad_wire [e -> e]) rpc_ok = dec_call.kind == (sdk/FRAME-CALL) and dec_call.fn_name == 'calc-discount' and (map/get dec_call.input 'subtotal_cents') == 25000 and crc_rejected _ = assert-true rpc_ok 'VKPW binary+JSON RPC envelope roundtrip and CRC-32 tamper rejection' // --- 2. Compile Real Tenant Pricing-Rule `.vk` Guest Plugin -> Wasm 3.0 + Ed25519 Sign --- // Notice the guest speaks the exact VKPW binary protocol over its confined sandbox files! pricing_guest_src = str/join [ 'use [bits, buf]\n' 'args = os/args\n' 'wire = os/read-bytes args[0]\n' 'blen = bytes/get-u32-be wire 6\n' 'req = json/decode (bytes/to-str wire[10..(10 + blen)])\n' 'inp = map/get req \'input\'\n' 'cfg = map/get req \'config\'\n' 'subtotal = as-i64 (map/get inp \'subtotal_cents\')\n' 'tier = str/append \'\' (map/get inp \'tier\')\n' 'rate = if tier == \'enterprise\' [ as-i64 (map/get cfg \'enterprise_pct\') ] else [ 5 ]\n' 'disc = (subtotal * rate) / 100\n' 'resp_json = json/encode { output = { discount_cents = disc, final_cents = (subtotal - disc), applied_pct = rate } }\n' 'jb = bytes/from-str resp_json\n' 'mut w = buf/writer-new ((len jb) + 14)\n' '_ = buf/write-str mut w \'VKPW\'\n' '_ = buf/write-u8 mut w 1\n' '_ = buf/write-u8 mut w 2\n' '_ = buf/write-u32-be mut w (len jb)\n' '_ = buf/write-bytes mut w jb\n' 'wo = buf/writer-bytes w\n' 'crc = bits/crc32 wo\n' 'mut out = buf/writer-new ((len wo) + 4)\n' '_ = buf/write-bytes mut out wo\n' '_ = buf/write-u32-be mut out crc\n' '_ = os/write-file args[1] (buf/writer-bytes out)\n' ] '' pricing_wasm_path = str/join [tmp_dir '/tenant_pricing.wasm'] '' wasm_bytes = sdk/compile-vk-plugin pricing_guest_src pricing_wasm_path signer_seed = bytes/from-str 'veko-plugin-ed25519-seed-32bytes' signer_pub_hex = hex/encode (x25/ed25519-pubkey signer_seed) plugin_sig_hex = sdk/sign-plugin-bytes signer_seed wasm_bytes mut host = sdk/new-plugin-host/work-dir (str/join [tmp_dir '/work'] '') manifest = sdk/PluginManifest { plugin_id = 'tenant-acme-pricing' version = '1.0.0' allowed_hosts = ['fx.acme.internal' '*.pricing.veko.dev'] allowed_paths = ['/catalog/'] max_fuel = 2000000 max_mem_pages = 64 config = { enterprise_pct = 15 } ed25519_signer = signer_pub_hex } // Verify forged signature is rejected before registering with authentic signature bad_reg = sdk/register-plugin/signature/or mut host manifest pricing_wasm_path (str/append 'ff' plugin_sig_hex[2..128]) [e -> e] ok_reg = sdk/register-plugin/signature mut host manifest pricing_wasm_path plugin_sig_hex _ = assert-true ((sdk/valid-wasm-magic? wasm_bytes) and (err? bad_reg) and ok_reg == .ok) 'Real .vk -> Wasm 3.0 compilation and Ed25519 code-signing verification' // --- 3. Host-Call Allowlist Enforcement (Permit Allowed Host, Block Unauthorized Host & Traversal) --- _ = sdk/register-host-fn/cap/check-host mut host 'http_fetch_rate' (fn [a map] any [ { rate = 1.08, source = (map/get a 'host') } ]) 'net' true _ = sdk/register-host-fn/cap/check-path mut host 'kv_read_catalog' (fn [a map] any [ { sku = 'VK-ENT', path = (map/get a 'path') } ]) 'fs' true allowed_hc = sdk/dispatch-host-call/target-host mut host 'tenant-acme-pricing' 'http_fetch_rate' { host = 'eu.pricing.veko.dev' } 'eu.pricing.veko.dev' blocked_hc = sdk/dispatch-host-call/target-host/or mut host 'tenant-acme-pricing' 'http_fetch_rate' { host = 'evil.exfil.io' } 'evil.exfil.io' [e -> e] blocked_path = sdk/dispatch-host-call/target-path/or mut host 'tenant-acme-pricing' 'kv_read_catalog' { path = '/etc/passwd' } '/etc/passwd' [e -> e] hc_ok = (map/get allowed_hc 'rate') == 1.08 and (err? blocked_hc) and (err? blocked_path) _ = assert-true hc_ok 'Host-function allowlist permits *.pricing.veko.dev and blocks evil.exfil.io & /etc/passwd' // --- 4. Execute Tenant Pricing Plugin Inside Wasm 3.0 Sandbox with JSON Marshaling --- inv_res = sdk/invoke-plugin mut host 'tenant-acme-pricing' 'evaluate-order' { subtotal_cents = 40000 tier = 'enterprise' } disc_c = as-i64 (map/get inv_res.output 'discount_cents') final_c = as-i64 (map/get inv_res.output 'final_cents') inv_ok = disc_c == 6000 and final_c == 34000 and inv_res.metrics.wasm_bytes > 1000 and inv_res.metrics.response_bytes > 20 _ = assert-true inv_ok 'Wasm 3.0 tenant pricing plugin execution (40000 -> 6000 discount, 34000 final)' // --- 5. Deterministic Fuel Exhaustion Terminates Runaway Infinite-Loop Wasm 3.0 Plugin --- runaway_src = 'mut acc = 0\nwhile true [\n\tacc = acc + 1\n]\n' runaway_wasm = str/join [tmp_dir '/runaway_loop.wasm'] '' _ = sdk/compile-vk-plugin runaway_src runaway_wasm runaway_manifest = sdk/PluginManifest { plugin_id = 'tenant-runaway-loop' version = '1.0.0' max_fuel = 5000 max_mem_pages = 32 } _ = sdk/register-plugin mut host runaway_manifest runaway_wasm trap_res = sdk/invoke-plugin/or mut host 'tenant-runaway-loop' 'spin' {} [e -> e] _ = assert-true (err? trap_res) 'Wasm 3.0 --fuel=5000 quota deterministically terminates runaway infinite-loop plugin' // --- 6. Audit Trail Inspection --- logs = sdk/host-audit-log host // Expected entries: // 0: bad_signature (denied) // 1: register tenant-acme-pricing (ok) // 2: http_fetch_rate eu.pricing.veko.dev (ok) // 3: http_fetch_rate evil.exfil.io (denied_host) // 4: kv_read_catalog /etc/passwd (denied_path) // 5: invoke tenant-acme-pricing (ok) // 6: register tenant-runaway-loop (ok) // 7: invoke tenant-runaway-loop (trapped_or_fuel_exhausted) audit_ok_1 = (len logs) == 8 and logs[0].outcome == 'bad_signature' and (not logs[0].allowed) audit_ok_2 = logs[2].outcome == 'ok' and logs[3].outcome == 'denied_host' and logs[4].outcome == 'denied_path' audit_ok_3 = logs[5].outcome == 'ok' and logs[7].outcome == 'trapped_or_fuel_exhausted' and (not logs[7].allowed) _ = assert-true (audit_ok_1 and audit_ok_2 and audit_ok_3) 'Deterministic AuditEntry log captures signature, host/path policy, execution, and fuel trap events' .ok ] _ = run-demo println 'wasm_plugin_sdk: all verification checks passed'