diff --git a/Cargo.lock b/Cargo.lock index ff8f41e..4bea359 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -8,7 +8,16 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" dependencies = [ - "gimli", + "gimli 0.29.0", +] + +[[package]] +name = "addr2line" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59317f77929f0e679d39364702289274de2f0f0b22cbf50b2b8cff2169a0b27a" +dependencies = [ + "gimli 0.33.0", ] [[package]] @@ -26,6 +35,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anstream" version = "0.6.14" @@ -75,6 +90,29 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arbitrary" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -87,20 +125,20 @@ version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ - "addr2line", + "addr2line 0.22.0", "cc", "cfg-if", "libc", "miniz_oxide", - "object", + "object 0.36.1", "rustc-demangle", ] [[package]] name = "bitflags" -version = "2.6.0" +version = "2.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da" [[package]] name = "block-buffer" @@ -111,6 +149,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +dependencies = [ + "allocator-api2", +] + [[package]] name = "byteorder" version = "1.5.0" @@ -125,9 +172,13 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" -version = "1.1.0" +version = "1.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaff6f8ce506b9773fa786672d63fc7a191ffea1be33f72bbd4aeacefca9ffc8" +checksum = "005ec2760ca554fae18df7a11195552ec576cd665632a881bc011d5bb2fd4d80" +dependencies = [ + "find-msvc-tools", + "shlex", +] [[package]] name = "cfg-if" @@ -135,6 +186,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.20", +] + [[package]] name = "colorchoice" version = "1.0.1" @@ -157,6 +217,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpp_demangle" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0667304c32ea56cb4cd6d2d7c0cfe9a2f8041229db8c033af7f8d69492429def" +dependencies = [ + "cfg-if", +] + [[package]] name = "cpufeatures" version = "0.2.12" @@ -166,6 +235,161 @@ dependencies = [ "libc", ] +[[package]] +name = "cranelift-assembler-x64" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9903e767cc0a95a59950de099a6b1d61e1476f774be1cdfa06d7ccdc9bfae56f" +dependencies = [ + "cranelift-assembler-x64-meta", +] + +[[package]] +name = "cranelift-assembler-x64-meta" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e777252ea3ec9daffa71408fbfe0793ab0a7b7bc3b2cf0f417dd0fa33964b8ef" +dependencies = [ + "cranelift-srcgen", +] + +[[package]] +name = "cranelift-bforest" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7828208a36e6627481cea61bd24c6234e31411d5c58b48e80ca094a1d593b944" +dependencies = [ + "cranelift-entity", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-bitset" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a58c8447cc59ef98c49096679f81392ad0acec2224cea2bef52011f5169f9d02" +dependencies = [ + "serde", + "serde_derive", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce520e899df1de583e7b564eafd66760fbc147d72894d2278ddac57a1489fcc0" +dependencies = [ + "bumpalo", + "cranelift-assembler-x64", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-control", + "cranelift-entity", + "cranelift-isle", + "gimli 0.33.0", + "hashbrown 0.17.1", + "libm", + "log", + "postcard", + "pulley-interpreter", + "regalloc2", + "rustc-hash", + "serde", + "serde_derive", + "sha2", + "smallvec", + "target-lexicon", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d8ab23e63d78ea6bbda18ae1ed9c958bc1cb88d90fb5e95f5ba62e9c019474b" +dependencies = [ + "cranelift-assembler-x64-meta", + "cranelift-codegen-shared", + "cranelift-srcgen", + "heck", + "pulley-interpreter", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24319a996b1ef40ebf8af69b39c868c790b35a42cfe38edacfd8c9deb75ea712" + +[[package]] +name = "cranelift-control" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9856d68cc2cefb83229cb2c55b620dd38427555c2fe87d73f8a4421616f628" +dependencies = [ + "arbitrary", +] + +[[package]] +name = "cranelift-entity" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0ec225d6b79c5d61358cb5ea081a1384ec541dbf8c1a8c618d187875af326ad" +dependencies = [ + "cranelift-bitset", + "serde", + "serde_derive", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-frontend" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d3a96a09bd9bd3cf6df2de2ea15d7418f493e946a8eaa1acfe39297a67ed3f3" +dependencies = [ + "cranelift-codegen", + "hashbrown 0.17.1", + "log", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-isle" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd28f593c36afbdc9aa17305c0bb66c39ce0530e1caffcf389fbec053950083e" + +[[package]] +name = "cranelift-native" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2987ca47affc261aa31ac643fb80dd7f8274cebf878003ba9f50cd6ff2c1055f" +dependencies = [ + "cranelift-codegen", + "libc", + "target-lexicon", +] + +[[package]] +name = "cranelift-srcgen" +version = "0.135.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7431dd8c9def94aca43b27915dee0d42103d9b13922cfb044febcd975547d68" + +[[package]] +name = "crc32fast" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8498c871161e1742aaa9d52551b2d6ebdd4c3d45a3be423e3728f33b955be550" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -192,6 +416,24 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "either" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "env_filter" version = "0.1.0" @@ -216,13 +458,19 @@ dependencies = [ ] [[package]] -name = "errno" -version = "0.3.9" +name = "equivalent" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -231,12 +479,24 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +[[package]] +name = "find-msvc-tools" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" + [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "foreign-types" version = "0.3.2" @@ -253,46 +513,78 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] -name = "futures-core" -version = "0.3.30" +name = "futures" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" [[package]] name = "futures-macro" -version = "0.3.30" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.5", ] [[package]] name = "futures-sink" -version = "0.3.30" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" [[package]] name = "futures-task" -version = "0.3.30" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" [[package]] name = "futures-util" -version = "0.3.30" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ + "futures-channel", "futures-core", + "futures-io", "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", - "pin-utils", "slab", ] @@ -323,6 +615,41 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +[[package]] +name = "gimli" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7f043f89559805f8c7cacc432749b2fa0d0a0a9ee46ce47164ed5ba7f126c" +dependencies = [ + "fnv", + "hashbrown 0.16.1", + "indexmap", + "stable_deref_trait", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", + "serde", + "serde_core", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -352,6 +679,18 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + [[package]] name = "ipnetwork" version = "0.20.0" @@ -367,6 +706,15 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -374,10 +722,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] -name = "libc" -version = "0.2.155" +name = "leb128fmt" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" [[package]] name = "linux-raw-sys" @@ -386,10 +746,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] -name = "log" -version = "0.4.22" +name = "linux-raw-sys" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" [[package]] name = "memchr" @@ -397,6 +769,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memfd" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57804b2c9b69967f1536a56f86297e367a33b19e98852ed624b84551cdbc0d90" +dependencies = [ + "rustix 1.1.4", +] + [[package]] name = "miniz_oxide" version = "0.7.4" @@ -447,6 +828,7 @@ dependencies = [ "tokio", "tokio-tungstenite", "toml", + "wasmtime", ] [[package]] @@ -468,6 +850,18 @@ dependencies = [ "memchr", ] +[[package]] +name = "object" +version = "0.39.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" +dependencies = [ + "crc32fast", + "hashbrown 0.17.1", + "indexmap", + "memchr", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -497,7 +891,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.119", ] [[package]] @@ -524,18 +918,24 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pkg-config" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -544,18 +944,41 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" dependencies = [ "unicode-ident", ] [[package]] -name = "quote" -version = "1.0.36" +name = "pulley-interpreter" +version = "48.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "f755d75e0809a4738a3e4880eeeb852fadbe1de5a437505b84f09c381657012a" +dependencies = [ + "cranelift-bitset", + "log", + "pulley-macros", + "wasmtime-internal-core", +] + +[[package]] +name = "pulley-macros" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e69fa1c4a555946f82e7894a7df88b0e9ee03b4266829b4811467c616ce51d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" dependencies = [ "proc-macro2", ] @@ -590,6 +1013,21 @@ dependencies = [ "getrandom", ] +[[package]] +name = "regalloc2" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "757712e8e61590d6d4f5d563483755538b5aa13467837a3b41cd9832509a7f85" +dependencies = [ + "allocator-api2", + "bumpalo", + "hashbrown 0.17.1", + "log", + "rustc-hash", + "serde", + "smallvec", +] + [[package]] name = "regex" version = "1.10.5" @@ -625,6 +1063,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + [[package]] name = "rustix" version = "0.38.34" @@ -634,10 +1078,23 @@ dependencies = [ "bitflags", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys 0.12.1", + "windows-sys 0.59.0", +] + [[package]] name = "ryu" version = "1.0.18" @@ -677,23 +1134,43 @@ dependencies = [ ] [[package]] -name = "serde" -version = "1.0.204" +name = "semver" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.204" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.5", ] [[package]] @@ -718,6 +1195,23 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + [[package]] name = "slab" version = "0.4.9" @@ -727,6 +1221,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9be42f50aa861c555654aa3a37f52f4b1074bacf4e48fe0ef7fa584e80f1f0f" +dependencies = [ + "serde", +] + [[package]] name = "socket2" version = "0.5.7" @@ -738,16 +1241,39 @@ dependencies = [ ] [[package]] -name = "syn" -version = "2.0.69" +name = "stable_deref_trait" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201fcda3845c23e8212cd466bfebf0bd20694490fc0356ae8e428e0824a915a6" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "syn" +version = "3.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" + [[package]] name = "tempfile" version = "3.12.0" @@ -757,17 +1283,35 @@ dependencies = [ "cfg-if", "fastrand", "once_cell", - "rustix", + "rustix 0.38.34", "windows-sys 0.59.0", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.61", +] + +[[package]] +name = "thiserror" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" +dependencies = [ + "thiserror-impl 2.0.20", ] [[package]] @@ -778,7 +1322,18 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.119", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", ] [[package]] @@ -806,7 +1361,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.119", ] [[package]] @@ -857,7 +1412,7 @@ dependencies = [ "native-tls", "rand", "sha1", - "thiserror", + "thiserror 1.0.61", "utf-8", ] @@ -903,6 +1458,224 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-encoder" +version = "0.254.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09480d646178e5fdd12bb06e812d0af9a3a191dbc9cd697fdc86687beade7393" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.254.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5769a29f799fbab136aaf65b4fe5384cd7d93fe6fc9ba0dcb6c8382a1f16e27" +dependencies = [ + "bitflags", + "hashbrown 0.17.1", + "indexmap", + "semver", + "serde", +] + +[[package]] +name = "wasmprinter" +version = "0.254.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e3ba11e024f504698f87b629b2b66c22a1231758de7607754963f7d198be39" +dependencies = [ + "anyhow", + "termcolor", + "wasmparser", +] + +[[package]] +name = "wasmtime" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd39f78fe9bc82cee4025ec9d52118269e9a1498016cc60119b14f2e1d86a8d" +dependencies = [ + "addr2line 0.26.1", + "async-trait", + "bitflags", + "bumpalo", + "cc", + "futures", + "libc", + "log", + "mach2", + "memfd", + "object 0.39.1", + "once_cell", + "postcard", + "pulley-interpreter", + "rustix 1.1.4", + "serde", + "serde_derive", + "smallvec", + "target-lexicon", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-core", + "wasmtime-internal-cranelift", + "wasmtime-internal-fiber", + "wasmtime-internal-jit-debug", + "wasmtime-internal-jit-icache-coherence", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-environ" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00c1070d95484d048994ec109cd42c4932ac07e6ee3cc1886fb90c7a18ec29da" +dependencies = [ + "anyhow", + "cpp_demangle", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-entity", + "gimli 0.33.0", + "hashbrown 0.17.1", + "indexmap", + "log", + "object 0.39.1", + "postcard", + "rustc-demangle", + "semver", + "serde", + "serde_derive", + "sha2", + "smallvec", + "target-lexicon", + "wasm-encoder", + "wasmparser", + "wasmprinter", + "wasmtime-internal-component-util", + "wasmtime-internal-core", +] + +[[package]] +name = "wasmtime-internal-component-util" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c0fe8292115fda08875f29064ae9f7f0b2c1ffbb323146eae538ae34b8a613f" + +[[package]] +name = "wasmtime-internal-core" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "821385794cf44baf2967fd2515368429ed2bff9b9b256f62d09a0f1301474fd8" +dependencies = [ + "hashbrown 0.17.1", + "libm", + "serde", +] + +[[package]] +name = "wasmtime-internal-cranelift" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0238b4a52773457ad1b8a1a26be90486e0ca0108cf46b9be16cc1d8d8cabdfa1" +dependencies = [ + "cranelift-codegen", + "cranelift-control", + "cranelift-entity", + "cranelift-frontend", + "cranelift-native", + "gimli 0.33.0", + "itertools", + "log", + "object 0.39.1", + "pulley-interpreter", + "smallvec", + "target-lexicon", + "thiserror 2.0.20", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-core", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", +] + +[[package]] +name = "wasmtime-internal-fiber" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "596f1d85e06cbf9c9add9c78ea135171c5753264ffe94a83dcc082a1ff49fc9b" +dependencies = [ + "cc", + "libc", + "rustix 1.1.4", + "wasmtime-environ", + "wasmtime-internal-versioned-export-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-internal-jit-debug" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "365faed74827bb0116785bab5872372df1baaec9ec0003c83bc99ccdc595f689" +dependencies = [ + "cc", + "wasmtime-internal-versioned-export-macros", +] + +[[package]] +name = "wasmtime-internal-jit-icache-coherence" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cce25afd9e7ac4957292a5c6a219951dd0f140b8417e7378a97f50b67c3b96f" +dependencies = [ + "libc", + "wasmtime-internal-core", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-internal-unwinder" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1fdea09d51e39c774984ca68a7d7486767e5b2935f5e686fa654aabcfc3c42d" +dependencies = [ + "cranelift-codegen", + "log", + "object 0.39.1", + "wasmtime-environ", +] + +[[package]] +name = "wasmtime-internal-versioned-export-macros" +version = "48.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97e48a5f514c38c2f74249724cb3501e45548d3311ca9e3881694859fd6ba116" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + [[package]] name = "windows-sys" version = "0.48.0" @@ -930,6 +1703,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-targets" version = "0.48.5" diff --git a/Cargo.toml b/Cargo.toml index 7caec96..0042854 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [features] forwarder = ["tokio-tungstenite", "tokio", "futures-util"] +# nscript: run user-submitted WASM note filters (Phase 0 spike) +nscript = ["wasmtime"] [dependencies] serde = { version = "1.0", features = ["derive"] } @@ -18,3 +20,6 @@ tokio = { version = "1.38.0", features = ["macros", "time", "sync", "rt-multi-th futures-util = { version = "0.3.30", optional = true } log = "0.4.22" env_logger = "0.11.3" + +# nscript deps +wasmtime = { version = "48", optional = true, default-features = false, features = ["cranelift", "runtime", "std"] } diff --git a/README.md b/README.md index 6958d34..e839a7a 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,57 @@ be queued if the connection goes down (up to the `queue_size` buffer limit) - `queue_size` *optional* - size of the note queue, this is used to buffer notes if the connection goes down. Default is 1000. +### NScript (experimental) + +* name: `nscript` + +Runs a user-supplied **WASM** module as a note filter. Instead of compiling +filters into noteguard, the logic lives in a sandboxed wasm guest that reads the +note through an *accessor ABI* — a set of imported host functions +(`note_pubkey`, `note_kind`, `note_targets_owner`, `is_following`, …) — rather +than parsing a note buffer itself. The note's byte format is therefore an +implementation detail of the host, and guests stay tiny (the example below is +under 300 bytes: no note parser is linked into it). + +The intent is programmable, per-npub server-side filtering (e.g. inbox / mention +spam control), eventually submitted to the relay as a signed event. This is a +**Phase 0 spike**: the guest is loaded from a local path and the owner context +comes from config. + +Build with the `nscript` feature: + +```sh +$ cargo build --features nscript +``` + +Config: + +- `module` — path to the compiled guest `.wasm` +- `owner` *optional* — hex pubkey the script is scoped to (its "inbox owner") +- `follows` *optional* — hex pubkeys the owner follows (backs `is_following`) +- `message` *optional* — message returned to the client on reject + +```toml +[filters.nscript] +module = "./follows_only.wasm" +owner = "1111…1111" +follows = ["2222…2222"] +message = "blocked: not in owner's follows" +``` + +Guests are bounded by wasm **fuel** and a **memory cap** per note, and noteguard +**fails open** (accepts) if a guest traps or exhausts its budget, so a broken or +runaway filter can't block writes or hang the relay. + +An example guest (`follows-only`, an inbox filter that rejects notes to the owner +from non-followed authors) lives in [`nscript-guests/`](nscript-guests). Build it +with: + +```sh +$ cd nscript-guests/follows-only +$ cargo build --release --target wasm32-unknown-unknown +``` + ## Read-policy (readPolicy) mode Everything above is the **write-side** plugin: it decides what gets *stored* (a @@ -201,7 +252,7 @@ subscription is passthrough). Filters run in order per event. ```toml [readpolicy] # compose the read filters, in order. Empty/absent => deliver all. -pipeline = ["max_events_per_conn", "read_bandwidth"] +pipeline = ["max_events_per_conn", "read_bandwidth", "nscript"] # connection-level throttle: max events delivered per connection. [readpolicy.filters.max_events_per_conn] @@ -219,6 +270,17 @@ bytes_per_minute = 5000000 # hex (optional). A whitelisted connection is never metered and takes the # passthrough fast path. whitelist = ["127.0.0.1", "0000…0000"] + +# guest wasm applied to subscriptions (owner = the authed subscriber). Requires +# the `nscript` build feature; the pipeline entry is skipped (fail open) in a +# build without it. +[readpolicy.filters.nscript] +module = "./mute_word.wasm" +# STUB for follow-set distribution: owner-hex -> hex pubkeys they follow. +# Backs the guest's is_following accessor until scripts/follows are sourced +# from signed on-relay submissions. +[readpolicy.filters.nscript.follows] +# "1111…1111" = ["2222…2222"] ``` Each filter is a [`ReadFilter`](src/read_filter.rs) — a lifecycle-aware, @@ -238,7 +300,8 @@ zero-copy fast path, never sending `req` batches (it still sends subscription, and the engine ANDs those: an empty pipeline is always passthrough; a configured throttle keeps the subscription non-passthrough (strfry must keep sending events for the counter/byte-meter to bound delivery); -and a bandwidth-whitelisted connection is passthrough (it is never metered). +a bandwidth-whitelisted connection is passthrough (it is never metered); and a +`nscript` filter is non-passthrough only when it actually loaded a guest. Emitting the field is forward-compatible: a strfry without passthrough support simply ignores it. @@ -261,6 +324,39 @@ For exact per-IP enforcement, set `relay.numThreads.reqWorker = 1` and (True cross-thread enforcement would require sharing bucket state across plugin processes, which this phase does not do.) +### NScript read filter (experimental) + +* name: `nscript` + +The read path can drive the same nscript WASM runtime as the write-side +[`nscript`](#nscript-experimental) filter, so it needs the `nscript` feature +(`cargo build --release --features nscript`); without it the pipeline entry is +logged and skipped (fail open). The subscriber (the NIP-42 `authed` pubkey) is +the script **owner**, so a per-npub feed filter runs scoped to whoever is +reading. The guest is compiled once per process and re-scoped per subscriber — +no recompile per subscription. The `Verdict` collapses on the read path to +**deliver** (`Accept`) vs **withhold** (anything else); there is no "shadow" +concept for reads. It fails open: a missing/failed module or a trapping guest +delivers everything. + +An unauthenticated subscription (no `authed`) still runs the guest, but with no +owner: an owner-scoped filter like `follows-only` then no-ops and delivers +everything, while an owner-independent filter still applies. The +[`mute-word`](nscript-guests/mute-word) example guest (withholds kind-1 notes +containing a muted word) is owner-independent, which makes it convenient for +exercising the read path without AUTH: + +```sh +$ cd nscript-guests/mute-word +$ cargo build --release --target wasm32-unknown-unknown +``` + +Script/follow **distribution is stubbed** for this phase (configured statically +via `module` and the `follows` map rather than sourced from signed submission +events on the relay); the point of the read mode today is the protocol + runtime +wiring. + + ## Testing You can test your filters like so: diff --git a/nscript-guests/follows-only/Cargo.lock b/nscript-guests/follows-only/Cargo.lock new file mode 100644 index 0000000..1a26c2b --- /dev/null +++ b/nscript-guests/follows-only/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "follows-only" +version = "0.1.0" diff --git a/nscript-guests/follows-only/Cargo.toml b/nscript-guests/follows-only/Cargo.toml new file mode 100644 index 0000000..fba4050 --- /dev/null +++ b/nscript-guests/follows-only/Cargo.toml @@ -0,0 +1,16 @@ +# Standalone crate (its own workspace) so it is not pulled into noteguard's build. +[workspace] + +[package] +name = "follows-only" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[profile.release] +opt-level = "s" +lto = true +panic = "abort" +strip = true diff --git a/nscript-guests/follows-only/src/lib.rs b/nscript-guests/follows-only/src/lib.rs new file mode 100644 index 0000000..a6db403 --- /dev/null +++ b/nscript-guests/follows-only/src/lib.rs @@ -0,0 +1,53 @@ +//! Example nscript guest: a follows-only inbox filter. +//! +//! Rejects notes addressed (p-tagged) to the scoped owner unless the author is +//! someone the owner follows. Notes that don't target the owner are accepted +//! untouched. +//! +//! Note how there is no note parser compiled into this module: it reads the note +//! purely through the host accessor ABI. Owner-relative context (who the owner +//! is, and their follow set) lives on the host, so this module is reusable logic +//! independent of any particular npub. + +#![no_std] + +use core::panic::PanicInfo; + +// ---- accessor ABI (imported from the host) ---- +#[link(wasm_import_module = "env")] +extern "C" { + /// Writes the 32-byte author pubkey into `out_ptr`; returns bytes written. + fn note_pubkey(out_ptr: *mut u8) -> i32; + /// 1 if the note p-tags the scoped owner, else 0. + fn note_targets_owner() -> i32; + /// 1 if the 32-byte pubkey at `pk_ptr` is in the owner's follow set. + fn is_following(pk_ptr: *const u8) -> i32; +} + +const ACCEPT: i32 = 0; +const REJECT: i32 = 1; + +/// The host calls this once per note; the current note is implicit. +#[no_mangle] +pub extern "C" fn filter_note() -> i32 { + unsafe { + // Only govern notes addressed to our owner; leave everything else alone. + if note_targets_owner() == 0 { + return ACCEPT; + } + + let mut author = [0u8; 32]; + note_pubkey(author.as_mut_ptr()); + + if is_following(author.as_ptr()) == 1 { + ACCEPT + } else { + REJECT + } + } +} + +#[panic_handler] +fn panic(_: &PanicInfo) -> ! { + loop {} +} diff --git a/nscript-guests/loop-forever/Cargo.lock b/nscript-guests/loop-forever/Cargo.lock new file mode 100644 index 0000000..095a048 --- /dev/null +++ b/nscript-guests/loop-forever/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "loop-forever" +version = "0.1.0" diff --git a/nscript-guests/loop-forever/Cargo.toml b/nscript-guests/loop-forever/Cargo.toml new file mode 100644 index 0000000..f932f4e --- /dev/null +++ b/nscript-guests/loop-forever/Cargo.toml @@ -0,0 +1,14 @@ +[workspace] + +[package] +name = "loop-forever" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[profile.release] +opt-level = "s" +panic = "abort" +strip = true diff --git a/nscript-guests/loop-forever/src/lib.rs b/nscript-guests/loop-forever/src/lib.rs new file mode 100644 index 0000000..1b4fa4b --- /dev/null +++ b/nscript-guests/loop-forever/src/lib.rs @@ -0,0 +1,16 @@ +//! Adversarial nscript guest: never returns. Must be stopped by host fuel. +#![no_std] +use core::panic::PanicInfo; + +#[no_mangle] +pub extern "C" fn filter_note() -> i32 { + let mut x: u64 = 0; + loop { + // volatile-ish spin so the optimizer can't fold it away + x = x.wrapping_add(1); + unsafe { core::ptr::read_volatile(&x); } + } +} + +#[panic_handler] +fn panic(_: &PanicInfo) -> ! { loop {} } diff --git a/nscript-guests/mute-word/Cargo.lock b/nscript-guests/mute-word/Cargo.lock new file mode 100644 index 0000000..2c0a480 --- /dev/null +++ b/nscript-guests/mute-word/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "mute-word" +version = "0.1.0" diff --git a/nscript-guests/mute-word/Cargo.toml b/nscript-guests/mute-word/Cargo.toml new file mode 100644 index 0000000..b637db2 --- /dev/null +++ b/nscript-guests/mute-word/Cargo.toml @@ -0,0 +1,16 @@ +# Standalone crate (its own workspace) so it is not pulled into noteguard's build. +[workspace] + +[package] +name = "mute-word" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[profile.release] +opt-level = "s" +lto = true +panic = "abort" +strip = true diff --git a/nscript-guests/mute-word/src/lib.rs b/nscript-guests/mute-word/src/lib.rs new file mode 100644 index 0000000..4433126 --- /dev/null +++ b/nscript-guests/mute-word/src/lib.rs @@ -0,0 +1,78 @@ +//! Example nscript guest: an owner-independent muted-word read filter. +//! +//! Withholds kind-1 notes whose content contains the muted word "spam"; +//! everything else is delivered. Unlike `follows-only`, this guest reads no +//! owner context, so it applies identically to every subscriber (authenticated +//! or not) — which makes it convenient for exercising the read-side +//! deliver/withhold path without NIP-42 AUTH. +//! +//! As with the other guests, there is no note parser compiled in: the content +//! is read through the host accessor ABI. + +#![no_std] + +use core::panic::PanicInfo; + +#[link(wasm_import_module = "env")] +extern "C" { + /// The note's kind. + fn note_kind() -> i64; + /// Copies up to `cap` bytes of the note content into `out_ptr`; returns the + /// content's true length (which may exceed `cap`). + fn note_content(out_ptr: *mut u8, cap: i32) -> i32; +} + +const ACCEPT: i32 = 0; +const REJECT: i32 = 1; + +/// The muted word. A note delivering it is withheld. +const MUTED: &[u8] = b"spam"; + +/// Scratch buffer for the note content. Content longer than this is truncated +/// for the scan (a muted word past the cutoff won't match — acceptable for a +/// spike; a real filter would stream or raise the cap). +const CAP: usize = 4096; + +#[no_mangle] +pub extern "C" fn filter_note() -> i32 { + unsafe { + // Only govern kind-1 (text notes); leave everything else alone. + if note_kind() != 1 { + return ACCEPT; + } + + let mut buf = [0u8; CAP]; + let true_len = note_content(buf.as_mut_ptr(), CAP as i32); + let n = if true_len < 0 { + 0 + } else { + (true_len as usize).min(CAP) + }; + + if contains(&buf[..n], MUTED) { + REJECT + } else { + ACCEPT + } + } +} + +/// Naive substring search (no_std, no allocator). +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + if needle.is_empty() || needle.len() > haystack.len() { + return needle.is_empty(); + } + let mut i = 0; + while i + needle.len() <= haystack.len() { + if &haystack[i..i + needle.len()] == needle { + return true; + } + i += 1; + } + false +} + +#[panic_handler] +fn panic(_: &PanicInfo) -> ! { + loop {} +} diff --git a/src/filters/mod.rs b/src/filters/mod.rs index da41573..f41a84e 100644 --- a/src/filters/mod.rs +++ b/src/filters/mod.rs @@ -8,6 +8,9 @@ mod whitelist; #[cfg(feature = "forwarder")] mod forwarder; +#[cfg(feature = "nscript")] +mod nscript; + pub use blacklist::Blacklist; pub use content::Content; pub use kinds::Kinds; @@ -17,3 +20,6 @@ pub use whitelist::Whitelist; #[cfg(feature = "forwarder")] pub use forwarder::Forwarder; + +#[cfg(feature = "nscript")] +pub use nscript::NScript; diff --git a/src/filters/nscript.rs b/src/filters/nscript.rs new file mode 100644 index 0000000..f295c3a --- /dev/null +++ b/src/filters/nscript.rs @@ -0,0 +1,63 @@ +use crate::nscript::{Runtime, Verdict}; +use crate::{Action, InputMessage, NoteFilter, OutputMessage}; +use serde::Deserialize; + +/// Runs a user-supplied WASM guest as a note filter via the accessor ABI. +/// +/// For the Phase 0 spike the guest is loaded from a local path and the owner +/// context comes from config; a later phase sources both from a signed +/// submission event on the relay. +#[derive(Deserialize, Default)] +pub struct NScript { + /// Path to the compiled guest `.wasm`. + module: String, + + /// Hex pubkey the script is scoped to (its "inbox owner"). + #[serde(default)] + owner: Option, + + /// Hex pubkeys the owner follows (backs `is_following`). + #[serde(default)] + follows: Vec, + + /// Message returned to the client on reject. + #[serde(default)] + message: Option, + + /// Compiled runtime, built lazily on first note. + #[serde(skip)] + runtime: Option, +} + +impl NoteFilter for NScript { + fn name(&self) -> &'static str { + "nscript" + } + + fn filter_note(&mut self, msg: &InputMessage) -> OutputMessage { + let id = msg.event.id.clone(); + + if self.runtime.is_none() { + match Runtime::load(&self.module, &self.owner, &self.follows) { + Ok(rt) => self.runtime = Some(rt), + Err(e) => { + // Fail open: a broken filter shouldn't block all writes. + log::error!("nscript: failed to load {}: {}", self.module, e); + return OutputMessage::new(id, Action::Accept, None); + } + } + } + + let rt = self.runtime.as_ref().expect("runtime just loaded"); + match rt.run(&msg.event) { + Ok(Verdict::Accept) => OutputMessage::new(id, Action::Accept, None), + Ok(Verdict::Reject) => OutputMessage::new(id, Action::Reject, self.message.clone()), + Ok(Verdict::ShadowReject) => OutputMessage::new(id, Action::ShadowReject, None), + Err(e) => { + // Fail open on guest trap / resource exhaustion. + log::error!("nscript: guest error: {}", e); + OutputMessage::new(id, Action::Accept, None) + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 60b9089..aa4f1f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,9 @@ pub mod readfilters; pub mod readmsg; pub mod readpolicy; +#[cfg(feature = "nscript")] +pub mod nscript; + pub use messages::{Action, InputMessage, OutputMessage}; pub use note_filter::{Note, NoteFilter}; pub use read_filter::{Admission, ConnContext, EventVerdict, OpenDecision, ReadFilter}; diff --git a/src/main.rs b/src/main.rs index 09676f7..afc7c2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,9 @@ use noteguard::filters::{Blacklist, Content, Kinds, ProtectedEvents, RateLimit, #[cfg(feature = "forwarder")] use noteguard::filters::Forwarder; +#[cfg(feature = "nscript")] +use noteguard::filters::NScript; + use log::info; use noteguard::{Action, InputMessage, NoteFilter, OutputMessage}; use serde::de::DeserializeOwned; @@ -54,6 +57,9 @@ impl Noteguard { #[cfg(feature = "forwarder")] self.register_filter::(); + + #[cfg(feature = "nscript")] + self.register_filter::(); } /// Run the loaded filters. You must call `load_config` before calling this, otherwise diff --git a/src/nscript.rs b/src/nscript.rs new file mode 100644 index 0000000..5825bca --- /dev/null +++ b/src/nscript.rs @@ -0,0 +1,290 @@ +//! Phase 0 spike of the nscript wasm filter runtime. +//! +//! The ABI is an *accessor interface* over an opaque note handle: the guest +//! never parses a note buffer, it calls imported `note_*` host functions to read +//! fields. The note's byte format is therefore an implementation detail — today +//! we back the accessors with the serde [`Note`]; later this becomes an +//! `ndb_note` (with an optional `note_raw` zero-copy fast path). Owner-relative +//! context (which npub the script is scoped to, and their follows) lives on the +//! host, so guests stay reusable, owner-agnostic logic. + +use crate::Note; +use std::collections::HashSet; +use wasmtime::{ + AsContext, AsContextMut, Caller, Config, Engine, Extern, Linker, Memory, Module, Store, + StoreLimits, StoreLimitsBuilder, +}; + +/// Guest action codes returned from `filter_note`. +const ACT_REJECT: i32 = 1; +const ACT_SHADOW_REJECT: i32 = 2; + +/// CPU budget (wasm fuel units) per note. Bounds runaway guests deterministically. +const DEFAULT_FUEL: u64 = 50_000_000; + +/// Max guest linear memory (bytes). +const MAX_MEMORY: usize = 16 * 1024 * 1024; + +/// The verdict a guest returns for a note. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Verdict { + Accept, + Reject, + ShadowReject, +} + +/// Per-invocation host state exposed to the accessor functions. +struct HostState { + note: Note, + owner: Option<[u8; 32]>, + follows: HashSet<[u8; 32]>, + limits: StoreLimits, +} + +/// A compiled nscript module plus the owner context it runs under. +pub struct Runtime { + engine: Engine, + module: Module, + linker: Linker, + owner: Option<[u8; 32]>, + follows: HashSet<[u8; 32]>, + fuel: u64, +} + +impl Runtime { + /// Compile the guest at `path` and wire up the accessor ABI. + /// + /// `owner` is the hex pubkey the script is scoped to and `follows` are hex + /// pubkeys that owner follows (backing for `note_targets_owner` and + /// `is_following`). + pub fn load(path: &str, owner: &Option, follows: &[String]) -> Result { + let mut config = Config::new(); + config.consume_fuel(true); + let engine = Engine::new(&config).map_err(|e| e.to_string())?; + let module = Module::from_file(&engine, path).map_err(|e| e.to_string())?; + + let mut linker = Linker::new(&engine); + register_host_functions(&mut linker).map_err(|e| e.to_string())?; + + let owner = owner.as_deref().and_then(hex32); + let follows = follows.iter().filter_map(|h| hex32(h)).collect(); + + Ok(Runtime { + engine, + module, + linker, + owner, + follows, + fuel: DEFAULT_FUEL, + }) + } + + /// Re-scope this already-compiled guest to a different owner, without + /// recompiling. `engine`, `module` and `linker` are owner-independent and + /// internally reference-counted, so cloning them is cheap — a per-subscriber + /// runtime costs no JIT compile or file read. + pub fn rescope(&self, owner: &Option, follows: &[String]) -> Self { + Runtime { + engine: self.engine.clone(), + module: self.module.clone(), + linker: self.linker.clone(), + owner: owner.as_deref().and_then(hex32), + follows: follows.iter().filter_map(|h| hex32(h)).collect(), + fuel: self.fuel, + } + } + + /// Run the guest filter against `note`, returning its verdict. + /// + /// A fresh [`Store`] is created per note (the compiled module is reused), so + /// guests get no state between invocations. Fuel and a memory cap bound the + /// resources a single call may consume. + pub fn run(&self, note: &Note) -> Result { + let state = HostState { + note: note.clone(), + owner: self.owner, + follows: self.follows.clone(), + limits: StoreLimitsBuilder::new().memory_size(MAX_MEMORY).build(), + }; + let mut store = Store::new(&self.engine, state); + store.limiter(|s| &mut s.limits); + store.set_fuel(self.fuel).map_err(|e| e.to_string())?; + + let instance = self + .linker + .instantiate(&mut store, &self.module) + .map_err(|e| e.to_string())?; + let func = instance + .get_typed_func::<(), i32>(&mut store, "filter_note") + .map_err(|e| e.to_string())?; + let code = func.call(&mut store, ()).map_err(|e| e.to_string())?; + + Ok(match code { + ACT_REJECT => Verdict::Reject, + ACT_SHADOW_REJECT => Verdict::ShadowReject, + _ => Verdict::Accept, + }) + } +} + +/// Register the accessor ABI on the linker under the `env` module. +/// +/// Low-level accessors (`note_pubkey`, `note_id`, `note_kind`, `note_created_at`, +/// `note_content`) expose raw note fields; higher-level owner-relative +/// capabilities (`note_targets_owner`, `is_following`) keep owner context on the +/// host. Variable-length reads use the `(out_ptr, cap) -> true_len` convention. +fn register_host_functions(linker: &mut Linker) -> Result<(), wasmtime::Error> { + // note_pubkey(out_ptr) -> 32 : raw author pubkey bytes (0 on error) + linker.func_wrap( + "env", + "note_pubkey", + |mut caller: Caller<'_, HostState>, out: i32| -> i32 { + let pk = match hex32(&caller.data().note.pubkey) { + Some(p) => p, + None => return 0, + }; + write_bytes(&mut caller, out, &pk) + }, + )?; + + // note_id(out_ptr) -> 32 : raw event id bytes (0 on error) + linker.func_wrap( + "env", + "note_id", + |mut caller: Caller<'_, HostState>, out: i32| -> i32 { + let id = match hex32(&caller.data().note.id) { + Some(p) => p, + None => return 0, + }; + write_bytes(&mut caller, out, &id) + }, + )?; + + // note_kind() -> kind + linker.func_wrap("env", "note_kind", |caller: Caller<'_, HostState>| -> i64 { + caller.data().note.kind + })?; + + // note_created_at() -> unix seconds + linker.func_wrap( + "env", + "note_created_at", + |caller: Caller<'_, HostState>| -> i64 { caller.data().note.created_at }, + )?; + + // note_content(out_ptr, cap) -> true_len : utf8 content, truncated to cap + linker.func_wrap( + "env", + "note_content", + |mut caller: Caller<'_, HostState>, out: i32, cap: i32| -> i32 { + let content = caller.data().note.content.clone(); + let bytes = content.as_bytes(); + let n = bytes.len().min(cap.max(0) as usize); + write_bytes(&mut caller, out, &bytes[..n]); + bytes.len() as i32 + }, + )?; + + // note_targets_owner() -> bool : does the note p-tag the scoped owner? + linker.func_wrap( + "env", + "note_targets_owner", + |caller: Caller<'_, HostState>| -> i32 { + let owner = match caller.data().owner { + Some(o) => o, + None => return 0, + }; + let hit = + caller.data().note.tags.iter().any(|t| { + t.len() >= 2 && t[0] == "p" && hex32(&t[1]).map_or(false, |p| p == owner) + }); + hit as i32 + }, + )?; + + // is_following(pk_ptr) -> bool : is this 32-byte pubkey in the owner's follows? + linker.func_wrap( + "env", + "is_following", + |mut caller: Caller<'_, HostState>, pk_ptr: i32| -> i32 { + let target = match read_array::<32>(&mut caller, pk_ptr) { + Some(b) => b, + None => return 0, + }; + caller.data().follows.contains(&target) as i32 + }, + )?; + + // log(ptr, len) : debug logging from the guest + linker.func_wrap( + "env", + "log", + |mut caller: Caller<'_, HostState>, ptr: i32, len: i32| { + if let Some(s) = read_str(&mut caller, ptr, len) { + log::info!("nscript: {}", s); + } + }, + )?; + + Ok(()) +} + +/// Resolve the guest's exported linear memory. +fn memory(caller: &mut Caller<'_, HostState>) -> Option { + match caller.get_export("memory") { + Some(Extern::Memory(m)) => Some(m), + _ => None, + } +} + +/// Write `data` into guest memory at `ptr`, returning the number of bytes +/// written (0 on failure). +fn write_bytes(caller: &mut Caller<'_, HostState>, ptr: i32, data: &[u8]) -> i32 { + let mem = match memory(caller) { + Some(m) => m, + None => return 0, + }; + match mem.write(caller.as_context_mut(), ptr as usize, data) { + Ok(()) => data.len() as i32, + Err(_) => 0, + } +} + +/// Read a fixed-size array from guest memory at `ptr`. +fn read_array(caller: &mut Caller<'_, HostState>, ptr: i32) -> Option<[u8; N]> { + let mem = memory(caller)?; + let mut buf = [0u8; N]; + mem.read(caller.as_context(), ptr as usize, &mut buf).ok()?; + Some(buf) +} + +/// Read a utf8 string of `len` bytes from guest memory at `ptr`. +fn read_str(caller: &mut Caller<'_, HostState>, ptr: i32, len: i32) -> Option { + let mem = memory(caller)?; + let mut buf = vec![0u8; len.max(0) as usize]; + mem.read(caller.as_context(), ptr as usize, &mut buf).ok()?; + String::from_utf8(buf).ok() +} + +/// Decode a single hex nibble. +fn hex_val(c: u8) -> Option { + match c { + b'0'..=b'9' => Some(c - b'0'), + b'a'..=b'f' => Some(c - b'a' + 10), + b'A'..=b'F' => Some(c - b'A' + 10), + _ => None, + } +} + +/// Decode a 64-char hex string into a 32-byte array. +fn hex32(s: &str) -> Option<[u8; 32]> { + let b = s.as_bytes(); + if b.len() != 64 { + return None; + } + let mut out = [0u8; 32]; + for (i, byte) in out.iter_mut().enumerate() { + *byte = (hex_val(b[2 * i])? << 4) | hex_val(b[2 * i + 1])?; + } + Some(out) +} diff --git a/src/read_filter.rs b/src/read_filter.rs index 6d418d8..375743e 100644 --- a/src/read_filter.rs +++ b/src/read_filter.rs @@ -86,8 +86,8 @@ pub enum Admission { /// The engine ANDs `!needs_events` across all filters to decide passthrough: a /// subscription is passthrough (strfry skips per-event marshaling) only if *no* /// filter needs per-event work. `needs_events` is evaluated per subscription, so -/// it may depend on per-sub state established in `req_open` (e.g. whether this -/// connection is bandwidth-whitelisted). +/// it may depend on per-sub state established in `req_open` (e.g. whether a guest +/// actually loaded, or whether this connection is bandwidth-whitelisted). #[derive(Debug, Clone, PartialEq, Eq)] pub struct OpenDecision { pub admission: Admission, diff --git a/src/readfilters/mod.rs b/src/readfilters/mod.rs index 4f704d0..fa4c25e 100644 --- a/src/readfilters/mod.rs +++ b/src/readfilters/mod.rs @@ -5,5 +5,11 @@ mod bandwidth; mod max_events; +#[cfg(feature = "nscript")] +mod nscript; + pub use bandwidth::ReadBandwidth; pub use max_events::MaxEventsPerConn; + +#[cfg(feature = "nscript")] +pub use nscript::NScriptRead; diff --git a/src/readfilters/nscript.rs b/src/readfilters/nscript.rs new file mode 100644 index 0000000..78e5d01 --- /dev/null +++ b/src/readfilters/nscript.rs @@ -0,0 +1,143 @@ +use crate::nscript::{Runtime, Verdict}; +use crate::read_filter::{ConnContext, EventVerdict, OpenDecision, ReadFilter}; +use crate::readmsg::UnparsedNote; +use serde::Deserialize; +use std::collections::HashMap; + +/// Runs a user-supplied WASM guest as a per-subscription deliverability filter, +/// scoped to the subscriber (the NIP-42 `authed` pubkey = the script owner). +/// +/// The guest is compiled once per process and re-scoped (no recompile) to each +/// subscriber at `req_open`. The [`Verdict`] collapses on the read path to +/// deliver (`Accept`) vs withhold (anything else) — there is no shadow concept +/// for reads. Fails open: a missing/failed module or a trapping guest delivers. +/// +/// The `module` + `follows` fields stub out the eventual script-distribution +/// system: today the guest and its owner's follow-set are configured statically; +/// later they come from a signed submission event on the relay. +#[derive(Deserialize, Default)] +pub struct NScriptRead { + /// Path to the compiled guest `.wasm`, applied to every subscription (owner = + /// the authed subscriber). If unset, deliver everything. + pub module: Option, + + /// STUB for follow-set distribution: owner-hex -> the hex pubkeys that owner + /// follows (backs the guest's `is_following` accessor). + #[serde(default)] + pub follows: HashMap>, + + /// The compiled guest, built once on the first `req_open` and re-scoped per + /// subscriber. Stays `None` when no module is configured or the load failed + /// (fail open — deliver all); `load_attempted` keeps us from retrying a + /// broken module (and re-logging) on every subscription. + #[serde(skip)] + compiled: Option, + #[serde(skip)] + load_attempted: bool, + + /// `connId` -> `subId` -> that subscriber's scoped guest. Only subscriptions + /// that actually have a guest get an entry; everything else delivers all. + #[serde(skip)] + runtimes: HashMap>, +} + +impl NScriptRead { + /// The compiled guest, compiling it on first use. Returns `None` — meaning + /// deliver-all — when no module is configured or the load failed (fail + /// open); the failure is logged once, not once per subscription. + fn compiled(&mut self) -> Option<&Runtime> { + if !self.load_attempted { + self.load_attempted = true; + if let Some(module) = &self.module { + // Compiled owner-less; `rescope` binds each subscriber's owner + // and follow-set without recompiling. + match Runtime::load(module, &None, &[]) { + Ok(rt) => self.compiled = Some(rt), + Err(e) => log::error!( + "readpolicy: failed to load {} (delivering all): {}", + module, + e + ), + } + } + } + self.compiled.as_ref() + } +} + +impl ReadFilter for NScriptRead { + fn name(&self) -> &'static str { + "nscript" + } + + /// An unauthenticated subscription (`authed` == `None`) still runs the + /// guest, but with no owner: an owner-scoped filter (like `follows-only`) + /// then no-ops and delivers everything, while an owner-independent filter + /// (e.g. content muting) still applies. + fn req_open(&mut self, ctx: &ConnContext, _filter: Option<&serde_json::Value>) -> OpenDecision { + let owner = ctx.authed.map(str::to_string); + let follows = ctx + .authed + .and_then(|owner| self.follows.get(owner)) + .cloned() + .unwrap_or_default(); + + // Only a subscription that actually has a guest does per-event work; a + // deliver-all sub (no module / failed load) can take passthrough. + let Some(runtime) = self.compiled().map(|rt| rt.rescope(&owner, &follows)) else { + return OpenDecision::passthrough(); + }; + + self.runtimes + .entry(ctx.conn_id) + .or_default() + .insert(ctx.sub_id.to_string(), runtime); + OpenDecision::needs_events() + } + + fn filter_event(&mut self, ctx: &ConnContext, ev: &UnparsedNote) -> EventVerdict { + // No runtime (no module / failed load / never opened): deliver. + let Some(rt) = self + .runtimes + .get(&ctx.conn_id) + .and_then(|subs| subs.get(ctx.sub_id)) + else { + return EventVerdict::Deliver; + }; + + // Only the guest path pays for a full parse. + let note = match ev.parse() { + Ok(note) => note, + // Fail open on a malformed event, matching strfry's stance. + Err(e) => { + log::error!("readpolicy: unparseable event (delivering): {}", e); + return EventVerdict::Deliver; + } + }; + + match rt.run(¬e) { + Ok(Verdict::Accept) => EventVerdict::Deliver, + // Reject / ShadowReject both collapse to withhold-this-event on the + // read path — there is no shadow concept for deliverability. + Ok(_) => EventVerdict::Skip, + Err(e) => { + // Fail open on guest trap / resource exhaustion. + log::error!("readpolicy: guest error (delivering): {}", e); + EventVerdict::Deliver + } + } + } + + fn req_close(&mut self, conn_id: u64, sub_id: &str) { + if let Some(subs) = self.runtimes.get_mut(&conn_id) { + subs.remove(sub_id); + if subs.is_empty() { + self.runtimes.remove(&conn_id); + } + } + } + + fn conn_close(&mut self, conn_id: u64) { + self.runtimes.remove(&conn_id); + } +} diff --git a/src/readmsg.rs b/src/readmsg.rs index fdd3c5e..a556148 100644 --- a/src/readmsg.rs +++ b/src/readmsg.rs @@ -41,14 +41,14 @@ impl UnparsedNote { self.0.get().len() } - /// Fully parse the event into a [`Note`]. Only used by a filter that + /// Fully parse the event into a [`Note`]. Only used when a guest filter /// actually needs the event's fields. pub fn parse(&self) -> serde_json::Result { serde_json::from_str(self.0.get()) } /// Cheaply parse just the event `id` for the accept-list, avoiding a full - /// [`Note`] parse on the bandwidth-only path. + /// [`Note`] parse on the bandwidth-only (no-guest) path. pub fn id(&self) -> serde_json::Result { #[derive(Deserialize)] struct IdOnly { diff --git a/src/readpolicy.rs b/src/readpolicy.rs index 28926d0..1548532 100644 --- a/src/readpolicy.rs +++ b/src/readpolicy.rs @@ -45,6 +45,9 @@ use serde::Deserialize; use std::collections::HashMap; use std::io::{self, BufRead, Write}; +#[cfg(feature = "nscript")] +use crate::readfilters::NScriptRead; + /// The `[readpolicy]` config table (read from `noteguard.toml`, or the file /// named by `$NOTEGUARD_CONFIG`). Mirrors the write-side `Config`: an ordered /// `pipeline` of filter names plus a `filters` map of each filter's own config. @@ -113,10 +116,14 @@ impl ReadPolicy { ); } - /// All built-in read filters, available on every new [`ReadPolicy`]. + /// All built-in read filters, available on every new [`ReadPolicy`]. The + /// guest (`nscript`) filter is registered only in `nscript` builds. fn register_builtin_filters(&mut self) { self.register_filter::(); self.register_filter::(); + + #[cfg(feature = "nscript")] + self.register_filter::(); } /// Compose the pipeline from config. Unlike the write side (which panics on a