lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit b3a2e94e988a79b8323a837f8aea4f608db1de65
parent 386e2d7e8cca95bdd0f6ce3444b5e741ea61ef8a
Author: triesap <tyson@radroots.org>
Date:   Fri, 16 Oct 2026 20:22:07 +0000

transport_nostr: Adopt maintained client with explicit event admission

- Pin reviewed immutable upstream SDK and utility sources to replace the vulnerable LRU dependency closure.
- Preserve exact signed delivery, explicit authentication, hardened connections, bounded fetches and receiver-local notification gaps.
- Reject noncanonical wire identities and changed signatures under cached event IDs through private admission controls.
- Verify macOS and Linux transport execution, unchanged public API, owning contracts and strict dependency policy without new exemptions.

Diffstat:
MCargo.lock | 306+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
MCargo.toml | 7+++----
Mcontracts/architecture/deviations.toml | 31+++++++++++++++++++++++++++++++
Mcontracts/releases/supply_chain.toml | 23++++++++++-------------
Mcrates/transport_nostr/Cargo.toml | 1-
Mcrates/transport_nostr/README.md | 8++++++++
Mcrates/transport_nostr/src/auth.rs | 40++++++++++++++++++----------------------
Mcrates/transport_nostr/src/client.rs | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mcrates/transport_nostr/src/exact_delivery.rs | 76++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Acrates/transport_nostr/src/native_backend_tests.rs | 189+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/transport_nostr/src/relay.rs | 184+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Mcrates/transport_nostr/src/sink.rs | 11+++++------
Mcrates/transport_nostr/src/socket_write.rs | 2+-
Mcrates/transport_nostr/src/socket_write_tests.rs | 2+-
Mcrates/transport_nostr/src/source.rs | 298++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mcrates/transport_nostr/src/subscription.rs | 82+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mcrates/transport_nostr/tests/exact_delivery.rs | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mcrates/transport_nostr/tests/fetch_bounds.rs | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mcrates/transport_nostr/tests/fetch_raw_budget.rs | 31++++++++++++++++++++-----------
Mcrates/transport_nostr/tests/fetch_windows.rs | 10+++++-----
Mcrates/transport_nostr/tests/local_io.rs | 6+++---
Mcrates/transport_nostr/tests/network_hardening.rs | 2+-
Mcrates/transport_nostr/tests/package_boundary.rs | 1+
Mdeny.toml | 6++++--
Msupply-chain/audits.toml | 318+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msupply-chain/config.toml | 16+++++++++++++++-
Mtools/xtask/src/supply_chain_qualification.rs | 143++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
27 files changed, 1612 insertions(+), 396 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -132,7 +132,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -143,7 +143,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -253,8 +253,7 @@ dependencies = [ [[package]] name = "async-utility" version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "188f83b9a198af8c336e505611edb00d6d2ac5c694241c5a4f9a12316938cfe9" +source = "git+https://github.com/yukibtc/async-utility?rev=b323ae0c1c1424b78bf8bb8832b95e86771f1dee#b323ae0c1c1424b78bf8bb8832b95e86771f1dee" dependencies = [ "futures-util", "gloo-timers", @@ -264,20 +263,21 @@ dependencies = [ [[package]] name = "async-wsocket" -version = "0.13.2" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c92385c7c8b3eb2de1b78aeca225212e4c9a69a78b802832759b108681a5069" +checksum = "2c713e1f14c7b82e32ea159af1c6e2f070cfadbdf23fb2512acce9af0a26f1a2" dependencies = [ - "async-utility", "futures", "futures-util", "js-sys", "tokio", + "tokio-happy-eyeballs", "tokio-rustls", "tokio-socks", "tokio-tungstenite", "url", "wasm-bindgen", + "wasm-bindgen-futures", "web-sys", ] @@ -291,12 +291,6 @@ dependencies = [ ] [[package]] -name = "atomic-destructor" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef49f5882e4b6afaac09ad239a4f8c70a24b8f2b0897edb1f706008efd109cf4" - -[[package]] name = "atomic-waker" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -304,9 +298,9 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" [[package]] name = "backtrace" @@ -363,6 +357,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32637268377fc7b10a8c6d51de3e7fba1ce5dd371a96e342b34e6078db558e7f" [[package]] +name = "bech32" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efbd3e1070bbdf4cd88a75264e18e8a26f7cb5c6949eadf0ceb85fb159cf08f8" + +[[package]] name = "bindgen" version = "0.69.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -391,7 +391,7 @@ version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90dbd31c98227229239363921e60fcf5e558e43ec69094d46fc4996f08d1d5bc" dependencies = [ - "bitcoin_hashes", + "bitcoin_hashes 0.14.1", "serde", "unicode-normalization", ] @@ -412,6 +412,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" [[package]] +name = "bitcoin-consensus-encoding" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9daa31138eb443d5751b207f3f64154e2bb09cd59960562ccc7a7112be38147f" +dependencies = [ + "bitcoin-internals 0.7.0", +] + +[[package]] +name = "bitcoin-internals" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d573f4cf32996a8dce612e4348cece65a241f1882ed594047c9ba348e8869fa5" + +[[package]] +name = "bitcoin-internals" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8bea3a9f0cfece4564e37cb49a38cc245ca5184719e50d7d0dda3268722c4e2" + +[[package]] name = "bitcoin-io" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -424,7 +445,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26ec84b80c482df901772e931a9a681e26a1b9ee2302edeff23cb30328745c8b" dependencies = [ "bitcoin-io", - "hex-conservative", + "hex-conservative 0.2.2", + "serde", +] + +[[package]] +name = "bitcoin_hashes" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5304e53726dbe5f93141535e102ed97b5bf4714fbecefdda8f9fb98d7fdaff0e" +dependencies = [ + "bitcoin-consensus-encoding", + "bitcoin-internals 0.6.0", + "hex-conservative 1.3.0", "serde", ] @@ -947,6 +980,46 @@ dependencies = [ ] [[package]] +name = "defmt" +version = "0.3.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4deef977660e373c458df605a913d4ab121db0ccd18d1ad6976dff7d37cddb53" +dependencies = [ + "defmt 1.2.0", +] + +[[package]] +name = "defmt" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4a0795ac740cb54d7b3775c9c17345f6aaecf884c01733b39d6017738e0e3e1" +dependencies = [ + "bitflags 1.3.2", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "840bb056bd99c495c9188198a258ab787c79f306d864db3e6e8451442d6d8366" +dependencies = [ + "defmt-parser", + "proc-macro2", + "quote", + "syn 3.0.2", +] + +[[package]] +name = "defmt-parser" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71958f69fb2de856c5dd51ac9377e986ca1aa5919f931bf5395a807277f889a0" +dependencies = [ + "thiserror 2.0.18", +] + +[[package]] name = "der" version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1188,7 +1261,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -1214,6 +1287,18 @@ dependencies = [ ] [[package]] +name = "faster-hex" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04839bdf9d8c10f66806fad16b852fc72aab80873aebc3cb69d85b4fa41543ed" +dependencies = [ + "autocfg", + "defmt 0.3.101", + "heapless", + "serde", +] + +[[package]] name = "fastrand" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1601,6 +1686,15 @@ dependencies = [ ] [[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] name = "hashbrown" version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1641,6 +1735,16 @@ dependencies = [ ] [[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "stable_deref_trait", +] + +[[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1662,6 +1766,15 @@ dependencies = [ ] [[package]] +name = "hex-conservative" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271e0d19bcb473b6675739a2b536076b24a082316cb5199ad918edce10c599e8" +dependencies = [ + "arrayvec", +] + +[[package]] name = "hkdf" version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2285,9 +2398,9 @@ checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" [[package]] name = "lru" -version = "0.16.4" +version = "0.18.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f66e8d5d03f609abc3a39e6f08e4164ebf1447a732906d39eb9b99b7919ef39" +checksum = "ef9ac18847474e638e3702b76c65d4eb93428471a74778ef0f1be711717f89b5" [[package]] name = "lru-slab" @@ -2380,9 +2493,9 @@ checksum = "c7d3d987ea7078dc36947cde532637c472a229426702e4331dd7667325378bd9" dependencies = [ "aes", "base64", - "bech32", + "bech32 0.11.1", "bip39", - "bitcoin_hashes", + "bitcoin_hashes 0.14.1", "cbc", "chacha20 0.9.1", "chacha20poly1305", @@ -2390,7 +2503,7 @@ dependencies = [ "hex", "instant", "scrypt", - "secp256k1", + "secp256k1 0.29.1", "serde", "serde_json", "unicode-normalization", @@ -2399,56 +2512,67 @@ dependencies = [ ] [[package]] -name = "nostr-database" -version = "0.44.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7462c9d8ae5ef6a28d66a192d399ad2530f1f2130b13186296dbb11bdef5b3d1" +name = "nostr" +version = "0.45.5" +source = "git+https://github.com/nostrdevkit/nostr.git?rev=de3031aa99986eb29b3f34580355f9a099e4c927#de3031aa99986eb29b3f34580355f9a099e4c927" dependencies = [ - "lru", - "nostr", - "tokio", + "base64", + "bech32 0.12.0", + "bip39", + "bitcoin_hashes 1.2.0", + "cbc", + "chacha20 0.9.1", + "chacha20poly1305", + "faster-hex", + "opaquerr", + "rand 0.10.1", + "secp256k1 0.30.0", + "serde", + "serde_json", + "unicode-normalization", + "universal-time", + "url", + "zeroize", ] [[package]] -name = "nostr-gossip" -version = "0.44.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade30de16869618919c6b5efc8258f47b654a98b51541eb77f85e8ec5e3c83a6" +name = "nostr-database" +version = "0.45.2" +source = "git+https://github.com/nostrdevkit/nostr.git?rev=de3031aa99986eb29b3f34580355f9a099e4c927#de3031aa99986eb29b3f34580355f9a099e4c927" dependencies = [ - "nostr", + "nostr 0.45.5", + "opaquerr", ] [[package]] -name = "nostr-relay-pool" -version = "0.44.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c85c54d6ca9aae4ae2bf19a7663ba9db5f45f783f1d24aff55f006386b8b99a1" +name = "nostr-gossip" +version = "0.45.1" +source = "git+https://github.com/nostrdevkit/nostr.git?rev=de3031aa99986eb29b3f34580355f9a099e4c927#de3031aa99986eb29b3f34580355f9a099e4c927" dependencies = [ - "async-utility", - "async-wsocket", - "atomic-destructor", - "hex", - "lru", - "negentropy", - "nostr", - "nostr-database", - "tokio", - "tracing", + "nostr 0.45.5", + "opaquerr", ] [[package]] name = "nostr-sdk" -version = "0.44.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "471732576710e779b64f04c55e3f8b5292f865fea228436daf19694f0bf70393" +version = "0.45.4" +source = "git+https://github.com/nostrdevkit/nostr.git?rev=de3031aa99986eb29b3f34580355f9a099e4c927#de3031aa99986eb29b3f34580355f9a099e4c927" dependencies = [ "async-utility", - "nostr", + "async-wsocket", + "faster-hex", + "futures", + "lru", + "negentropy", + "nostr 0.45.5", "nostr-database", "nostr-gossip", - "nostr-relay-pool", + "opaquerr", + "rand 0.10.1", "tokio", + "tokio-stream", "tracing", + "universal-time", ] [[package]] @@ -2474,7 +2598,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -2606,6 +2730,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] +name = "opaquerr" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f933a4265d5cdad61d19bbdfc972ea5726d56cd8d3d57b8f2d3c365dd42bee9" + +[[package]] name = "openssl" version = "0.10.76" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2882,7 +3012,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -2983,13 +3113,13 @@ name = "radroots_event_codec" version = "0.1.0-alpha" dependencies = [ "hex", - "nostr", + "nostr 0.44.7", "radroots_blossom", "radroots_core", "radroots_event", "radroots_identity", "radroots_protocol", - "secp256k1", + "secp256k1 0.29.1", "serde", "serde_json", "sha2", @@ -3000,7 +3130,7 @@ name = "radroots_event_codec_wasm" version = "0.1.0-alpha" dependencies = [ "hex", - "nostr", + "nostr 0.44.7", "radroots_blossom", "radroots_core", "radroots_event", @@ -3076,7 +3206,7 @@ name = "radroots_nostr" version = "0.1.0-alpha" dependencies = [ "base64", - "nostr", + "nostr 0.44.7", "radroots_blossom", "radroots_event", "radroots_event_codec", @@ -3093,7 +3223,7 @@ dependencies = [ name = "radroots_nostr_connect" version = "0.1.0-alpha" dependencies = [ - "nostr", + "nostr 0.44.7", "radroots_event", "radroots_identity", "radroots_nostr", @@ -3111,7 +3241,7 @@ version = "0.1.0-alpha" dependencies = [ "futures", "hex", - "nostr", + "nostr 0.44.7", "nostrdb", "radroots_nostr", "serde_json", @@ -3180,7 +3310,7 @@ version = "0.1.0-alpha" dependencies = [ "base64", "hex", - "nostr", + "nostr 0.44.7", "radroots_core", "radroots_event", "radroots_event_codec", @@ -3250,7 +3380,7 @@ dependencies = [ name = "radroots_sdk" version = "0.1.0-alpha" dependencies = [ - "nostr", + "nostr 0.44.7", "radroots_blossom", "radroots_core", "radroots_event", @@ -3361,7 +3491,7 @@ name = "radroots_signing" version = "0.1.0-alpha" dependencies = [ "hex", - "nostr", + "nostr 0.44.7", "radroots_blossom", "radroots_event", "radroots_event_codec", @@ -3501,7 +3631,7 @@ dependencies = [ "radroots_storage_sqlite", "radroots_trade", "radroots_transport", - "secp256k1", + "secp256k1 0.29.1", "serde", "serde_json", "sha2", @@ -3553,7 +3683,6 @@ version = "0.1.0-alpha" dependencies = [ "async-wsocket", "futures", - "nostr-relay-pool", "nostr-sdk", "radroots_event_codec", "radroots_nostr", @@ -3904,7 +4033,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4049,6 +4178,17 @@ dependencies = [ ] [[package]] +name = "secp256k1" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252" +dependencies = [ + "bitcoin_hashes 0.14.1", + "rand 0.8.5", + "secp256k1-sys", +] + +[[package]] name = "secp256k1-sys" version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4297,7 +4437,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4525,7 +4665,7 @@ dependencies = [ "getrandom 0.3.4", "once_cell", "rustix 1.1.4", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4652,6 +4792,15 @@ dependencies = [ ] [[package]] +name = "tokio-happy-eyeballs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8564c32dfb6f4257f8bc6edfc178a34af97520e0b7b9815500c55eb3d092f29f" +dependencies = [ + "tokio", +] + +[[package]] name = "tokio-macros" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4693,13 +4842,14 @@ dependencies = [ "futures-core", "pin-project-lite", "tokio", + "tokio-util", ] [[package]] name = "tokio-tungstenite" -version = "0.26.2" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9daff607c6d2bf6c16fd681ccb7eecc83e4e2cdc1ca067ffaadfca5de7f084" +checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857" dependencies = [ "futures-util", "log", @@ -4860,9 +5010,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tungstenite" -version = "0.26.2" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4793cb5e56680ecbb1d843515b23b6de9a75eb04b66643e256a396d43be33c13" +checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442" dependencies = [ "bytes", "data-encoding", @@ -5054,6 +5204,12 @@ dependencies = [ ] [[package]] +name = "universal-time" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47a939edecc3c5a7b83c02e5f6b3c31d2bc69eabcc9a87ab12c6d37ee6dbc856" + +[[package]] name = "untrusted" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5435,7 +5591,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml @@ -164,7 +164,7 @@ aes-gcm = { version = "0.10.3", default-features = false, features = [ "aes", "alloc", ] } -async-wsocket = { version = "0.13.2" } +async-wsocket = { version = "0.17" } base64 = { version = "0.22", default-features = false, features = ["alloc"] } bincode = { version = "1.3.3" } bytes = { version = "1" } @@ -203,8 +203,7 @@ keyring = { version = "3.6.3", default-features = false, features = [ libc = { version = "0.2" } libsqlite3-sys = { version = "=0.37.0", default-features = false } nostr = { version = "0.44.7", default-features = false } -nostr-relay-pool = { version = "0.44.0" } -nostr-sdk = { version = "0.44.1" } +nostr-sdk = { git = "https://github.com/nostrdevkit/nostr.git", rev = "de3031aa99986eb29b3f34580355f9a099e4c927", version = "=0.45.4" } num_cpus = { version = "1.17.0" } poly1305 = { version = "0.8", default-features = false } prettyplease = { version = "0.2.37" } @@ -255,7 +254,7 @@ thiserror = { version = "1" } tokio = { version = "1" } tokio-rustls = { version = "0.26.4", default-features = false } tokio-util = { version = "0.7", features = ["rt"] } -tokio-tungstenite = { version = "0.26.2", default-features = false, features = [ +tokio-tungstenite = { version = "0.28.0", default-features = false, features = [ "connect", "rustls-tls-webpki-roots", ] } diff --git a/contracts/architecture/deviations.toml b/contracts/architecture/deviations.toml @@ -2,6 +2,37 @@ schema_version = 1 architecture_id = "radroots.crates.release.v1" [[deviation]] +id = "RCRV1-DEV-043" +date = "2026-10-16" +status = "closed" +approval = "The user authorizes the complete shared SDK refactor, required supported dependency repairs, verified source commits and GitHub master integration." +affected_steps = ["030", "040", "070", "130"] +spec_anchors = ["contracts/crates/release_v1/radroots_crates_release_v1.toml#release_policy"] +source_evidence = [ + "The current nostr-database 0.44.0 and nostr-relay-pool 0.44.3 manifests require lru 0.16. The current RustSec policy reports RUSTSEC-2026-0253 fixed at lru 0.18.2 or later; the published 0.44 family has no resolving patch.", + "Checksum-verified published nostr-sdk 0.45.4 integrates relay ownership and admits lru 0.18. Nostr 0.45.5 and nostr-database 0.45.2 are the selected supported family. The native upstream WebSocket SPI and futures remain Send, which supplies no browser-local portability proof.", + "The initial native compile proves that replacing the independently consumed pure nostr 0.44 protocol package also removes its current public constructors, signer and ancillary helpers. Those source bodies do not own lru. Preserve them exactly until the approved domain/protocol replacement; use the neutral event/wire boundary to the maintained private native SDK instead.", + "Published SDK 0.45.1 through 0.45.4 notification streams silently discard receiver overflow errors. Public upstream commit 1c687bb852eb6dc90cd5e05a9571d0b20becc4b8 adds receiver-local with_gaps reporting. Select exact published upstream de3031aa99986eb29b3f34580355f9a099e4c927, which retains that fix and corrects duplicate queued REQs on reconnect, and qualify the actual affected source delta.", + "The selected private protocol's fixed-array hex helper accepts a longer even-sized string and decodes only the expected prefix. Before upstream normalization, the bounded native WebSocket adapter must enforce canonical exact-length EVENT id/pubkey/signature and OK event-id fields. Prove malformed acknowledgment and inbound-event refusal against actual loopback sockets; retain unmodified valid wire bytes and the existing single client owner.", + "The selected SDK caches successful verification by event ID only and skips subsequent verification on a cache hit. Test repeated actual relay delivery with a changed signature under a previously accepted ID; require complete event verification before native admission even on cache hits. Keep verification private to the existing upstream client admission SPI, with constant errors and no second decoder or network owner." +] +replacement_action = "Adopt the exact maintained public upstream client commit in the concrete native transport and require with_gaps on correctness-sensitive notification consumers. Preserve the exact current pure protocol adapter source and cross the private SDK boundary through neutral event/wire values. Replace removed native client APIs directly while preserving public Nostr interoperability, exact signed-event and ACK identity, explicit authentication, hardened DNS/address/TLS/frame/ingress admission, configured deadlines, bounded per-relay effects and meaningful retained regressions. Refresh affected current source-policy and dependency evidence without rewriting history, adding policy exceptions, a generic relay pool, unsafe Send wrappers or package publication authority. The complete approved domain/protocol successor remains required." +verification = [ + "Validate this prospective record with the existing standalone architecture command before dependency or runtime edits.", + "Review the actual selected source/checksums and every resolved dependency delta; check and test the complete affected native models, signer and transport closure, including real loopback transport controls.", + "Run strict lint/docs, owning architecture/contracts, affected portable feature checks, public API freshness and routine advisory/license/source policy. Bind unsupported/deferred browser and final candidate scope explicitly; publish the provider before any exact consumer pin.", +] +unresolved_risk = "Only the current native private-client source repair is qualified. Supported caller and generated-input adoption, the complete shared SDK successor, bounded browser implementation, resource and diagnostic policy, actual host/device matrix and final release qualification remain required. Automated use-scoped reviews do not provide independent security acceptance or qualification of unused dependency APIs and platforms." +normative_architecture_change = false +adr_required = false +closure_evidence = [ + "The selected immutable public SDK and utility source replace the lru 0.16 dependency closure without changing the current pure protocol adapter or introducing another relay pool. Actual fixed-output Nix fetches verify both upstream inputs.", + "Actual original-wire ACK/event identifier and cached-ID changed-signature regressions fail before correction. The hardened connector enforces original canonical identifier lengths, and private client admission verifies each complete event even when the upstream verification cache hits. Valid continuation and exact signed outbound wire behavior are retained.", + "All 163 current transport tests pass on macOS aarch64 and actual Linux x86_64 under Rust 1.99.0. Locked all-target/all-feature compilation, strict Clippy/Rustdoc and transport doctests pass. The actual pinned-nightly producer remains byte-identical to the existing 163-line public API baseline.", + "Strict frozen cargo-vet succeeds after automated source/use review without adding or modifying any exemption. The exact unchanged resolved public graph retains its executed advisory/license/ban/source result with zero errors and the existing exact spin yank and unused license allowances. Current architecture, contracts and API boundaries pass; unchanged tooling/pure package evidence retains only its original source scope." +] + +[[deviation]] id = "RCRV1-DEV-042" date = "2026-10-16" status = "closed" diff --git a/contracts/releases/supply_chain.toml b/contracts/releases/supply_chain.toml @@ -33,15 +33,6 @@ mitigation = "radroots_transport_nostr canonicalizes every relay URL with url >= remove_when = "nostr >=0.45.0 stable" [[advisory_exception]] -id = "RUSTSEC-2026-0243" -package = "nostr-relay-pool" -affected_version = "0.44.3" -introduced_by = "nostr-sdk 0.44.1 and radroots_transport_nostr" -classification = "unmaintained" -mitigation = "The advisory reports no vulnerability; retain the locked release line only until the governed Nostr 0.45 migration." -remove_when = "nostr >=0.45.0 stable" - -[[advisory_exception]] id = "CARGO-YANKED-SPIN-0.9.8" package = "spin" affected_version = "0.9.8" @@ -51,10 +42,16 @@ mitigation = "The package has no RustSec vulnerability; the gate rejects every y remove_when = "sqlx no longer resolves flume 0.12.0 with spin 0.9.8" [[git_source]] -url = "https://github.com/rust-nostr/nostr.git" -revision = "5bba5163eb77107f82c4a8262cf29d7f33a73219" -packages = ["nostr", "nostr-relay-builder", "nostr-sdk"] -removal_when = "nostr 0.45 stable satisfies Studio compatibility tests" +url = "https://github.com/nostrdevkit/nostr.git" +revision = "de3031aa99986eb29b3f34580355f9a099e4c927" +packages = ["nostr", "nostr-database", "nostr-gossip", "nostr-sdk"] +removal_when = "A registry SDK retains receiver-local notification gaps and passes the complete native transport controls." + +[[git_source]] +url = "https://github.com/yukibtc/async-utility" +revision = "b323ae0c1c1424b78bf8bb8832b95e86771f1dee" +packages = ["async-utility"] +removal_when = "The selected upstream SDK uses a registry async-utility with the same required behavior." [[package]] name = "radroots_core" diff --git a/crates/transport_nostr/Cargo.toml b/crates/transport_nostr/Cargo.toml @@ -45,7 +45,6 @@ radroots_transport = { workspace = true, default-features = false } async-wsocket = { workspace = true } futures = { workspace = true } nostr-sdk = { workspace = true } -nostr-relay-pool = { workspace = true } rustls = { workspace = true } serde_json = { workspace = true, features = ["std"] } sha2 = { workspace = true, default-features = false } diff --git a/crates/transport_nostr/README.md b/crates/transport_nostr/README.md @@ -79,6 +79,14 @@ of message text, including `duplicate` or `already have`. Closed or replaced connections do not grant fresh send authority to a retained writer. Missing acknowledgements remain unknown or unavailable evidence. +Inbound `EVENT` identifiers, public keys and signatures, and `OK` event IDs, +must have their canonical lowercase hexadecimal lengths on the original wire. +The hardened connector refuses invalid fields before the SDK can normalize +them. A longer identifier sharing a valid prefix grants no acceptance. +Every inbound event also passes complete ID and signature verification through +the client's private admission policy, including repeated IDs already present +in the upstream verification cache. + Callers cannot forge or mutate prepared authority: ```compile_fail diff --git a/crates/transport_nostr/src/auth.rs b/crates/transport_nostr/src/auth.rs @@ -34,11 +34,11 @@ pub(crate) trait AuthClient: Send + Sync { #[derive(Clone, Debug)] pub(crate) struct LiveAuthClient { - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, } impl LiveAuthClient { - pub(crate) const fn new(client: nostr_sdk::Client) -> Self { + pub(crate) const fn new(client: nostr_sdk::client::Client) -> Self { Self { client } } } @@ -52,12 +52,13 @@ impl AuthClient for LiveAuthClient { let expected = relay.as_str().trim_end_matches('/'); let output = self .client - .send_msg_to([relay.as_str()], ClientMessage::auth(event)) + .send_msg(ClientMessage::auth(event)) + .to([relay.as_str()]) .await .map_err(|_| Error::AuthTransport)?; if output .success - .iter() + .keys() .any(|url| url.to_string().trim_end_matches('/') == expected) { Ok(()) @@ -83,8 +84,7 @@ impl AuthFlow { #[cfg(test)] pub(crate) fn isolated() -> Self { - let client = nostr_sdk::Client::default(); - client.automatic_authentication(false); + let client = nostr_sdk::client::Client::default(); Self::new(Arc::new(LiveAuthClient::new(client))) } @@ -219,8 +219,8 @@ impl NostrTransport { { return Err(Error::AuthResponseMismatch); } - let event = radroots_nostr::event::to_nostr(signed.envelope()) - .map_err(|_| Error::AuthResponseInvalid)?; + let event = + Event::from_json(signed.raw_json()).map_err(|_| Error::AuthResponseInvalid)?; self.auth.client.submit(relay.clone(), event).await?; self.auth.remove(relay, challenge) }) @@ -266,9 +266,7 @@ fn has_exact_tag(tags: &[Vec<String>], name: &str, value: &str) -> bool { mod tests { use super::*; use crate::{Config, RelayUrlPolicy}; - use nostr_sdk::prelude::{ - EventBuilder, JsonUtil, Keys, RelayUrl as UpstreamRelayUrl, Timestamp, - }; + use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys, Kind, Tag, Timestamp}; use std::sync::atomic::{AtomicUsize, Ordering}; #[derive(Debug)] @@ -302,17 +300,15 @@ mod tests { } fn signed_response(relay: &RelayUrl, challenge: &str, created_at: u64) -> String { - EventBuilder::auth( - challenge, - relay - .as_str() - .parse::<UpstreamRelayUrl>() - .expect("upstream relay"), - ) - .custom_created_at(Timestamp::from_secs(created_at)) - .sign_with_keys(&Keys::generate()) - .expect("signed auth event") - .as_json() + EventBuilder::new(Kind::Authentication, "") + .tags([ + Tag::parse(["relay", relay.as_str()]).expect("relay tag"), + Tag::parse(["challenge", challenge]).expect("challenge tag"), + ]) + .custom_created_at(Timestamp::from_secs(created_at)) + .finalize(&Keys::generate()) + .expect("signed auth event") + .as_json() } #[test] diff --git a/crates/transport_nostr/src/client.rs b/crates/transport_nostr/src/client.rs @@ -213,6 +213,45 @@ fn validate_timeout(field: &'static str, value_ms: u64) -> Result<(), Error> { Ok(()) } +pub(crate) async fn connect_relay( + client: &nostr_sdk::client::Client, + url: &str, + timeout: std::time::Duration, +) -> Result<nostr_sdk::relay::Relay, nostr_sdk::error::Error> { + let relay = client.relay(url).await?.ok_or_else(|| { + nostr_sdk::error::Error::with_static_message( + nostr_sdk::error::ErrorKind::NotFound, + "configured relay is unavailable", + ) + })?; + relay.try_connect().timeout(timeout).await?; + Ok(relay) +} + +#[derive(Debug)] +struct VerifiedEventAdmission; + +impl nostr_sdk::policy::AdmitPolicy for VerifiedEventAdmission { + fn admit_event<'a>( + &'a self, + _relay_url: &'a nostr_sdk::prelude::RelayUrl, + _subscription_id: &'a nostr_sdk::prelude::SubscriptionId, + event: &'a nostr_sdk::prelude::Event, + ) -> radroots_transport::BoxFuture< + 'a, + Result<nostr_sdk::policy::AdmitStatus, nostr_sdk::error::Error>, + > { + Box::pin(async move { + // The SDK verification cache keys only the ID, not the received body or signature. + Ok(if event.verify().is_ok() { + nostr_sdk::policy::AdmitStatus::Success + } else { + nostr_sdk::policy::AdmitStatus::Rejected { reason: None } + }) + }) + } +} + /// Concrete Nostr implementation of the transport source and sink SPIs. #[derive(Clone)] pub struct NostrTransport { @@ -228,13 +267,20 @@ pub struct NostrTransport { impl NostrTransport { /// Creates an inert transport from validated explicit configuration. pub fn new(config: Config) -> Self { - let connector = crate::relay::HardenedWebsocketTransport::new(config.endpoints()); + let connector = crate::relay::HardenedWebsocketTransport::new( + config.endpoints(), + std::time::Duration::from_millis(config.connect_timeout_ms()), + ); let writers = connector.writers.clone(); let ingress = connector.ingress.clone(); - let client = nostr_sdk::Client::builder() + let client = nostr_sdk::client::Client::builder() .websocket_transport(connector) + .admit_policy(VerifiedEventAdmission) + .max_relays(std::num::NonZeroUsize::new(config.endpoints().len())) + .connect_timeout(std::time::Duration::from_millis( + config.connect_timeout_ms(), + )) .build(); - client.automatic_authentication(false); let status = Arc::new(crate::status::StatusTracker::new(&config)); Self { config, @@ -337,6 +383,10 @@ impl fmt::Debug for NostrTransport { } #[cfg(test)] +#[path = "native_backend_tests.rs"] +mod native_backend_tests; + +#[cfg(test)] mod tests { use super::*; diff --git a/crates/transport_nostr/src/exact_delivery.rs b/crates/transport_nostr/src/exact_delivery.rs @@ -1,8 +1,9 @@ //! Exact retained EVENT framing and connection-local acknowledgement handling. use crate::{relay::MAX_WIRE_MESSAGE_BYTES, socket_write::WriterRegistry, status}; -use nostr_relay_pool::RelayNotification; -use nostr_sdk::{EventId, RelayMessage}; +use futures::StreamExt; +use nostr_sdk::prelude::{EventId, RelayMessage}; +use nostr_sdk::relay::{RelayNotification, RelayStatus}; use radroots_transport::{DeliveryRequest, outcome::DeliveryOutcome}; use std::sync::Arc; @@ -16,22 +17,27 @@ impl ExactEvent { pub(crate) fn from_request(request: &DeliveryRequest) -> Option<Self> { let signed = request.payload().event(); let raw = signed.raw_json(); - let event = radroots_nostr::event::to_nostr(signed.envelope()).ok()?; + u16::try_from(signed.envelope().kind_u32()).ok()?; + let id = EventId::from_slice(signed.envelope().id().as_bytes()).ok()?; Some(Self { - id: event.id, + id, frame: frame(raw)?, }) } pub(crate) async fn publish( &self, - client: &nostr_sdk::Client, + client: &nostr_sdk::client::Client, writers: &WriterRegistry, url: &str, ) -> Result<DeliveryOutcome, String> { - let relay = client.relay(url).await.map_err(|error| error.to_string())?; + let relay = client + .relay(url) + .await + .map_err(|error| error.to_string())? + .ok_or_else(|| "configured relay is unavailable".to_owned())?; // Subscribe before sending on the same connection; no success from enqueue alone. - let mut notifications = relay.notifications(); + let mut notifications = relay.notifications().with_gaps(); let writer = writers.get(url).map_err(|error| error.to_string())?; writer .send(async_wsocket::Message::Text(self.frame.to_string())) @@ -39,8 +45,9 @@ impl ExactEvent { .map_err(|error| error.to_string())?; loop { let notification = notifications - .recv() + .next() .await + .ok_or_else(|| "relay acknowledgement unavailable".to_owned())? .map_err(|_| "relay acknowledgement unavailable".to_owned())?; if let Some(outcome) = acknowledgement(self.id, notification) { return Ok(outcome); @@ -57,25 +64,24 @@ fn frame(raw: &str) -> Option<Arc<str>> { fn acknowledgement(id: EventId, notification: RelayNotification) -> Option<DeliveryOutcome> { match notification { - RelayNotification::Message { - message: - RelayMessage::Ok { - event_id, - status: accepted, - message, - }, - } if event_id == id => Some(if accepted { - DeliveryOutcome::accepted() - } else { - status::delivery_failure(&message) - }), + RelayNotification::Message { message } => match *message { + RelayMessage::Ok { + event_id, + status: accepted, + message, + } if event_id == id => Some(if accepted { + DeliveryOutcome::accepted() + } else { + status::delivery_failure(&message) + }), + _ => None, + }, RelayNotification::RelayStatus { - status: - nostr_sdk::RelayStatus::Disconnected - | nostr_sdk::RelayStatus::Terminated - | nostr_sdk::RelayStatus::Banned, + status: RelayStatus::Disconnected | RelayStatus::Idle | RelayStatus::Banned, } => Some(status::delivery_failure("relay disconnected")), - RelayNotification::Shutdown => Some(status::delivery_failure("relay shutdown")), + RelayNotification::RelayStatus { + status: RelayStatus::Shutdown, + } => Some(status::delivery_failure("relay shutdown")), _ => None, } } @@ -103,11 +109,11 @@ mod tests { let id = EventId::from_byte_array([1; 32]); let other = EventId::from_byte_array([2; 32]); let ok = |event_id, status, message: &'static str| RelayNotification::Message { - message: RelayMessage::Ok { + message: Box::new(RelayMessage::Ok { event_id, status, message: message.into(), - }, + }), }; assert!(acknowledgement(id, ok(other, true, "")).is_none()); assert_eq!( @@ -129,9 +135,9 @@ mod tests { )); } for status in [ - nostr_sdk::RelayStatus::Disconnected, - nostr_sdk::RelayStatus::Terminated, - nostr_sdk::RelayStatus::Banned, + nostr_sdk::relay::RelayStatus::Disconnected, + nostr_sdk::relay::RelayStatus::Idle, + nostr_sdk::relay::RelayStatus::Banned, ] { assert!(!super::status::delivery_succeeded( &acknowledgement(id, RelayNotification::RelayStatus { status }).unwrap() @@ -141,13 +147,19 @@ mod tests { acknowledgement( id, RelayNotification::RelayStatus { - status: nostr_sdk::RelayStatus::Connected + status: nostr_sdk::relay::RelayStatus::Connected } ) .is_none() ); assert!(!status::delivery_succeeded( - &acknowledgement(id, RelayNotification::Shutdown).unwrap() + &acknowledgement( + id, + RelayNotification::RelayStatus { + status: RelayStatus::Shutdown + } + ) + .unwrap() )); } } diff --git a/crates/transport_nostr/src/native_backend_tests.rs b/crates/transport_nostr/src/native_backend_tests.rs @@ -0,0 +1,189 @@ +use futures::{SinkExt, StreamExt}; +use nostr_sdk::{ + client::{Client, ClientNotification}, + prelude::RelayMessage, + relay::RelayNotification, +}; +use std::{num::NonZeroUsize, time::Duration}; +use tokio_tungstenite::{accept_async, tungstenite::Message}; + +const WATCHDOG: Duration = Duration::from_secs(5); + +#[tokio::test] +async fn queued_subscription_is_sent_once_after_a_real_socket_reconnect() { + use nostr_sdk::prelude::{ClientMessage, Filter, Kind, SubscriptionId}; + tokio::time::timeout(WATCHDOG, async { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let endpoint = crate::RelayEndpoint::new( + &url, + crate::RelayUrlPolicy::Local, + crate::RelayAccess::ReadOnly, + ) + .unwrap(); + let connector = + crate::relay::HardenedWebsocketTransport::new(&[endpoint], Duration::from_secs(1)); + let client = Client::builder().websocket_transport(connector).build(); + client.add_relay(&url).await.unwrap(); + let (closed, closure) = tokio::sync::oneshot::channel(); + let (observed, frames) = tokio::sync::oneshot::channel(); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut first = accept_async(tcp).await.unwrap(); + while let Some(message) = first.next().await { + match message { + Ok(Message::Close(_)) | Err(_) => break, + _ => first.flush().await.unwrap(), + } + } + closed.send(()).unwrap(); + let (tcp, _) = listener.accept().await.unwrap(); + let mut second = accept_async(tcp).await.unwrap(); + let mut requests = Vec::new(); + while let Some(message) = second.next().await { + match message.unwrap() { + Message::Text(text) => { + let value: serde_json::Value = serde_json::from_str(&text).unwrap(); + if value[0] == "CLOSE" && value[1] == "sent-barrier" { + observed.send(requests).unwrap(); + break; + } + requests.push(value); + } + _ => second.flush().await.unwrap(), + } + } + // Keep the second connection alive until explicit shutdown. + while let Some(message) = second.next().await { + if matches!(message, Ok(Message::Close(_)) | Err(_)) { + break; + } + second.flush().await.unwrap(); + } + }); + let relay = super::connect_relay(&client, &url, Duration::from_secs(1)) + .await + .unwrap(); + relay.disconnect(); + closure.await.unwrap(); + let id = SubscriptionId::new("retained-live"); + let filter = Filter::new().kind(Kind::TextNote); + relay + .subscribe(filter.clone()) + .with_id(id.clone()) + .await + .unwrap(); + super::connect_relay(&client, &url, Duration::from_secs(1)) + .await + .unwrap(); + relay + .send_msg(ClientMessage::close(SubscriptionId::new("sent-barrier"))) + .wait_until_sent(Duration::from_secs(1)) + .await + .unwrap(); + let frames = frames.await.unwrap(); + assert_eq!( + frames, + vec![ + serde_json::from_str::<serde_json::Value>( + &ClientMessage::req(id, filter).as_json() + ) + .unwrap() + ] + ); + client.shutdown().await; + server.await.unwrap(); + }) + .await + .unwrap(); +} + +#[tokio::test] +async fn actual_native_receivers_report_local_repeated_gaps_and_continue() { + tokio::time::timeout(WATCHDOG, async { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let endpoint = crate::RelayEndpoint::new( + &url, + crate::RelayUrlPolicy::Local, + crate::RelayAccess::ReadOnly, + ) + .unwrap(); + let connector = + crate::relay::HardenedWebsocketTransport::new(&[endpoint], Duration::from_secs(1)); + let client = Client::builder() + .websocket_transport(connector) + .notification_channel_size(NonZeroUsize::new(2).unwrap()) + .build(); + client + .add_relay(&url) + .notification_channel_size(2) + .await + .unwrap(); + let (send, mut receive) = tokio::sync::mpsc::channel::<usize>(1); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(tcp).await.unwrap(); + while let Some(sequence) = receive.recv().await { + let frame = serde_json::json!(["NOTICE", sequence.to_string()]).to_string(); + socket.send(Message::Text(frame.into())).await.unwrap(); + } + socket.close(None).await.unwrap(); + }); + let relay = super::connect_relay(&client, &url, Duration::from_secs(1)) + .await + .unwrap(); + let mut fast_relay = relay.notifications().with_gaps(); + let mut slow_relay = relay.notifications().with_gaps(); + let mut fast_client = client.notifications().with_gaps(); + let mut slow_client = client.notifications().with_gaps(); + + for round in 0..2 { + for sequence in round * 8..(round + 1) * 8 { + send.send(sequence).await.unwrap(); + let RelayNotification::Message { message } = + fast_relay.next().await.unwrap().unwrap() + else { + panic!("relay message") + }; + assert!( + matches!(*message, RelayMessage::Notice(value) if value == sequence.to_string()) + ); + let ClientNotification::Message { relay_url, message } = + fast_client.next().await.unwrap().unwrap() + else { + panic!("client message") + }; + assert_eq!(relay_url.as_str(), url); + assert!( + matches!(*message, RelayMessage::Notice(value) if value == sequence.to_string()) + ); + } + assert_eq!(slow_relay.next().await.unwrap().unwrap_err().skipped, 6); + assert_eq!(slow_client.next().await.unwrap().unwrap_err().skipped, 6); + for sequence in (round + 1) * 8 - 2..(round + 1) * 8 { + let RelayNotification::Message { message } = + slow_relay.next().await.unwrap().unwrap() + else { + panic!("retained relay message") + }; + assert!( + matches!(*message, RelayMessage::Notice(value) if value == sequence.to_string()) + ); + let ClientNotification::Message { message, .. } = + slow_client.next().await.unwrap().unwrap() + else { + panic!("retained client message") + }; + assert!( + matches!(*message, RelayMessage::Notice(value) if value == sequence.to_string()) + ); + } + } + drop(send); + server.await.unwrap(); + client.shutdown().await; + }) + .await + .unwrap(); +} diff --git a/crates/transport_nostr/src/relay.rs b/crates/transport_nostr/src/relay.rs @@ -6,9 +6,8 @@ use async_wsocket::futures_util::stream::SplitSink; use async_wsocket::futures_util::{Sink, SinkExt, StreamExt}; use core::fmt; use core::pin::Pin; -use nostr_relay_pool::ConnectionMode; -use nostr_relay_pool::transport::error::TransportError; -use nostr_relay_pool::transport::websocket::{WebSocketSink, WebSocketStream, WebSocketTransport}; +use nostr_sdk::error::Error as TransportError; +use nostr_sdk::transport::websocket::{WebSocketSink, WebSocketStream, WebSocketTransport}; use radroots_transport::{BoxFuture, Target, TargetNetworkPolicy, TransportId}; use std::collections::BTreeMap; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; @@ -99,13 +98,15 @@ impl fmt::Display for RelayUrl { #[derive(Clone, Debug)] pub(crate) struct HardenedWebsocketTransport { policies: Arc<BTreeMap<String, RelayUrlPolicy>>, + connect_timeout: Duration, pub(crate) writers: crate::socket_write::WriterRegistry, pub(crate) ingress: crate::source_ingress::IngressRegistry, } impl HardenedWebsocketTransport { - pub(crate) fn new(endpoints: &[RelayEndpoint]) -> Self { + pub(crate) fn new(endpoints: &[RelayEndpoint], connect_timeout: Duration) -> Self { Self { + connect_timeout, ingress: crate::source_ingress::IngressRegistry::new( endpoints .iter() @@ -137,11 +138,10 @@ impl WebSocketTransport for HardenedWebsocketTransport { fn connect<'a>( &'a self, url: &'a Url, - mode: &'a ConnectionMode, - timeout: Duration, + proxy: Option<SocketAddr>, ) -> BoxFuture<'a, Result<(WebSocketSink, WebSocketStream), TransportError>> { Box::pin(async move { - if !matches!(mode, ConnectionMode::Direct) { + if proxy.is_some() { return Err(policy_error( "proxy and Tor connection modes are not configured", )); @@ -185,7 +185,7 @@ impl WebSocketTransport for HardenedWebsocketTransport { let tls = tokio_rustls::TlsConnector::from(Arc::new(tls_config)) .connect(server_name, tcp) .await - .map_err(TransportError::backend)?; + .map_err(TransportError::transport)?; MaybeTlsStream::Rustls(tls) } else { MaybeTlsStream::Plain(tcp) @@ -200,20 +200,22 @@ impl WebSocketTransport for HardenedWebsocketTransport { Some(config), ) .await - .map_err(TransportError::backend)?; + .map_err(TransportError::transport)?; let (tx, rx) = stream.split(); let writer = - crate::socket_write::SocketWriter::new(Box::new(HardenedTransportSink(tx))); + crate::socket_write::SocketWriter::new(Box::pin(HardenedTransportSink(tx))); self.writers.install(relay.as_str(), &writer)?; let sink: WebSocketSink = - Box::new(crate::socket_write::SharedSocketSink::new(writer)); - let stream: WebSocketStream = Box::pin( - rx.map(|result| result.map_err(TransportError::backend).and_then(from_wire)), - ); + Box::pin(crate::socket_write::SharedSocketSink::new(writer)); + let stream: WebSocketStream = Box::pin(rx.map(|result| { + result + .map_err(TransportError::transport) + .and_then(from_wire) + })); Ok((sink, stream)) }; - tokio::time::timeout(timeout, connect) + tokio::time::timeout(self.connect_timeout, connect) .await .map_err(|_| policy_error("relay connection deadline elapsed"))? }) @@ -270,7 +272,7 @@ impl fmt::Display for NetworkPolicyError { impl std::error::Error for NetworkPolicyError {} pub(crate) fn policy_error(message: &'static str) -> TransportError { - TransportError::backend(NetworkPolicyError(message)) + TransportError::policy(NetworkPolicyError(message)) } type MeteredSocket = TokioWebSocketStream<crate::source_wire::MeteredIo<MaybeTlsStream<TcpStream>>>; @@ -279,7 +281,10 @@ struct HardenedTransportSink(SplitSink<MeteredSocket, WireMessage>); fn from_wire(message: WireMessage) -> Result<Message, TransportError> { Ok(match message { - WireMessage::Text(text) => Message::Text(text.to_string()), + WireMessage::Text(text) => { + validate_wire_identifiers(&text)?; + Message::Text(text.to_string()) + } WireMessage::Binary(bytes) => Message::Binary(bytes.to_vec()), WireMessage::Ping(bytes) => Message::Ping(bytes.to_vec()), WireMessage::Pong(bytes) => Message::Pong(bytes.to_vec()), @@ -288,6 +293,40 @@ fn from_wire(message: WireMessage) -> Result<Message, TransportError> { }) } +fn validate_wire_identifiers(text: &str) -> Result<(), TransportError> { + if text.len() > MAX_WIRE_MESSAGE_BYTES { + return Err(policy_error("relay message exceeds the wire limit")); + } + // Enforce original wire identities before the upstream fixed-array decoder + // can truncate hex to its expected prefix. Leave other decoding to the SDK. + let Ok(serde_json::Value::Array(fields)) = serde_json::from_str(text) else { + return Ok(()); + }; + let canonical_hex = |value: Option<&serde_json::Value>, length| { + value + .and_then(serde_json::Value::as_str) + .is_some_and(|value| { + value.len() == length + && value + .bytes() + .all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f')) + }) + }; + let valid = match fields.first().and_then(serde_json::Value::as_str) { + Some("OK") => canonical_hex(fields.get(1), 64), + Some("EVENT") => fields.get(2).is_some_and(|event| { + canonical_hex(event.get("id"), 64) + && canonical_hex(event.get("pubkey"), 64) + && canonical_hex(event.get("sig"), 128) + }), + _ => true, + }; + if !valid { + return Err(policy_error("relay message has a noncanonical identifier")); + } + Ok(()) +} + impl Sink<Message> for HardenedTransportSink { type Error = TransportError; @@ -298,14 +337,14 @@ impl Sink<Message> for HardenedTransportSink { ) -> Poll<Result<(), Self::Error>> { Pin::new(&mut self.0) .poll_ready_unpin(context) - .map_err(TransportError::backend) + .map_err(TransportError::transport) } #[cfg_attr(coverage_nightly, coverage(off))] fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> { Pin::new(&mut self.0) .start_send_unpin(item.into()) - .map_err(TransportError::backend) + .map_err(TransportError::transport) } #[cfg_attr(coverage_nightly, coverage(off))] @@ -315,7 +354,7 @@ impl Sink<Message> for HardenedTransportSink { ) -> Poll<Result<(), Self::Error>> { Pin::new(&mut self.0) .poll_flush_unpin(context) - .map_err(TransportError::backend) + .map_err(TransportError::transport) } #[cfg_attr(coverage_nightly, coverage(off))] @@ -325,7 +364,7 @@ impl Sink<Message> for HardenedTransportSink { ) -> Poll<Result<(), Self::Error>> { Pin::new(&mut self.0) .poll_close_unpin(context) - .map_err(TransportError::backend) + .map_err(TransportError::transport) } } @@ -436,6 +475,54 @@ mod tests { use super::*; #[test] + fn original_wire_identifiers_are_exact_canonical_and_never_rewritten() { + use serde_json::json; + let event = json!({"id": "ab".repeat(32), "pubkey": "cd".repeat(32), + "sig": "ef".repeat(64), "extension": true}); + let text = format!(" \n{} \t", json!(["EVENT", "bounded", event])); + assert_eq!( + from_wire(WireMessage::Text(text.clone().into())).unwrap(), + Message::Text(text) + ); + for field in ["id", "pubkey", "sig"] { + let original = event[field].as_str().unwrap(); + for invalid in [ + json!(format!("{original}00")), + json!(&original[2..]), + json!(original.to_ascii_uppercase()), + json!(format!("g{}", &original[1..])), + json!(null), + json!(32), + ] { + let mut malformed = event.clone(); + malformed[field] = invalid; + let text = json!(["EVENT", "bounded", malformed]).to_string(); + assert!( + from_wire(WireMessage::Text(text.into())).is_err(), + "{field}" + ); + } + } + for id in [json!("aa".repeat(33)), json!("AA".repeat(32)), json!(null)] { + let text = json!(["OK", id, true, ""]).to_string(); + assert!(from_wire(WireMessage::Text(text.into())).is_err()); + } + for control in [ + json!(["OK", "aa".repeat(32), true, ""]), + json!(["AUTH", "arbitrary challenge"]), + json!(["EOSE", "arbitrary subscription"]), + json!(["NOTICE", "arbitrary notice"]), + ] { + let text = control.to_string(); + assert_eq!( + from_wire(WireMessage::Text(text.clone().into())).unwrap(), + Message::Text(text) + ); + } + assert!(validate_wire_identifiers(&" ".repeat(MAX_WIRE_MESSAGE_BYTES + 1)).is_err()); + } + + #[test] fn metered_socket_conversion_preserves_data_control_and_close_messages() { for message in [ WireMessage::Text("text".into()), @@ -479,11 +566,10 @@ mod tests { [endpoint.as_str()], ) .expect("local TLS profile"); - let transport = HardenedWebsocketTransport::new(profile.endpoints()); + let transport = + HardenedWebsocketTransport::new(profile.endpoints(), Duration::from_secs(5)); let url = Url::parse(&endpoint).expect("URL"); - let result = transport - .connect(&url, &ConnectionMode::Direct, Duration::from_secs(5)) - .await; + let result = transport.connect(&url, None).await; assert!( result.is_err(), "plaintext peer must not complete a secure relay connection" @@ -536,7 +622,10 @@ mod tests { ["wss://relay.example.com"], ) .expect("profile"); - assert!(HardenedWebsocketTransport::new(profile.endpoints()).support_ping()); + assert!( + HardenedWebsocketTransport::new(profile.endpoints(), Duration::from_secs(5)) + .support_ping() + ); assert!(!policy_error("denied").to_string().is_empty()); let root = Url::parse("wss://relay.example/").expect("root URL"); @@ -555,6 +644,49 @@ mod tests { ); } + #[tokio::test] + async fn proxy_refusal_precedes_socket_effects() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let url = format!("ws://{address}"); + let endpoint = + crate::RelayEndpoint::new(&url, RelayUrlPolicy::Local, crate::RelayAccess::ReadOnly) + .unwrap(); + let transport = HardenedWebsocketTransport::new(&[endpoint], Duration::from_secs(1)); + let url = Url::parse(&url).unwrap(); + assert!(transport.connect(&url, Some(address)).await.is_err()); + assert!( + tokio::time::timeout(Duration::from_millis(20), listener.accept()) + .await + .is_err() + ); + } + + #[tokio::test] + async fn configured_connector_timeout_bounds_an_unfinished_websocket_handshake() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let endpoint = + crate::RelayEndpoint::new(&url, RelayUrlPolicy::Local, crate::RelayAccess::ReadOnly) + .unwrap(); + let transport = HardenedWebsocketTransport::new(&[endpoint], Duration::from_millis(50)); + let (accepted, observed) = tokio::sync::oneshot::channel(); + let (release, wait) = tokio::sync::oneshot::channel::<()>(); + let peer = tokio::spawn(async move { + let (_socket, _) = listener.accept().await.unwrap(); + accepted.send(()).unwrap(); + wait.await.unwrap(); + }); + let url = Url::parse(&url).unwrap(); + let result = tokio::time::timeout(Duration::from_secs(2), transport.connect(&url, None)) + .await + .expect("the configured connector deadline must expire"); + assert!(result.is_err()); + observed.await.unwrap(); + release.send(()).unwrap(); + peer.await.unwrap(); + } + #[test] fn device_network_policy_matches_the_shared_conformance_vectors() { let document: serde_json::Value = serde_json::from_str(include_str!( diff --git a/crates/transport_nostr/src/sink.rs b/crates/transport_nostr/src/sink.rs @@ -60,13 +60,13 @@ pub(crate) trait RelayClient: Send + Sync { #[derive(Clone, Debug)] pub(crate) struct LiveRelayClient { - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, writers: crate::socket_write::WriterRegistry, } impl LiveRelayClient { pub(crate) const fn new( - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, writers: crate::socket_write::WriterRegistry, ) -> Self { Self { client, writers } @@ -74,8 +74,7 @@ impl LiveRelayClient { #[cfg(test)] pub(crate) fn isolated() -> Self { - let client = nostr_sdk::Client::default(); - client.automatic_authentication(false); + let client = nostr_sdk::client::Client::default(); Self::new( client, crate::socket_write::WriterRegistry::new(std::iter::empty()), @@ -109,10 +108,10 @@ impl RelayClient for LiveRelayClient { attempted = true; self.client .add_relay(url.as_str()) + .connect_timeout(connect_timeout) .await .map_err(|error| error.to_string())?; - self.client - .try_connect_relay(url.as_str(), connect_timeout) + crate::client::connect_relay(&self.client, url.as_str(), connect_timeout) .await .map_err(|error| error.to_string())?; event.publish(&self.client, &self.writers, &url).await diff --git a/crates/transport_nostr/src/socket_write.rs b/crates/transport_nostr/src/socket_write.rs @@ -7,7 +7,7 @@ use core::{ task::{Context, Poll}, }; use futures::{Sink, SinkExt, lock::Mutex}; -use nostr_relay_pool::transport::{error::TransportError, websocket::WebSocketSink}; +use nostr_sdk::{error::Error as TransportError, transport::websocket::WebSocketSink}; use radroots_transport::BoxFuture; use std::{ collections::BTreeMap, diff --git a/crates/transport_nostr/src/socket_write_tests.rs b/crates/transport_nostr/src/socket_write_tests.rs @@ -8,7 +8,7 @@ fn message(text: &str) -> Message { fn channel_writer() -> (Arc<SocketWriter>, mpsc::Receiver<Message>) { let (tx, rx) = mpsc::channel(0); ( - SocketWriter::new(Box::new(tx.sink_map_err(TransportError::backend))), + SocketWriter::new(Box::pin(tx.sink_map_err(TransportError::transport))), rx, ) } diff --git a/crates/transport_nostr/src/source.rs b/crates/transport_nostr/src/source.rs @@ -3,10 +3,10 @@ use crate::{NostrTransport, RelayCursor, RelayUrl, status}; use core::cmp::Ordering; use core::time::Duration; -use futures::{StreamExt, stream}; +use futures::{Stream, StreamExt, stream}; use nostr_sdk::prelude::{ - Filter, JsonUtil, Kind, RelayMessage, RelayNotification, ReqExitPolicy, SingleLetterTag, - SubscribeAutoCloseOptions, SubscribeOptions, SubscriptionId, Timestamp, + Filter, Kind, PublicKey, RelayMessage, RelayNotification, ReqExitPolicy, SingleLetterTag, + SubscribeAutoCloseOptions, SubscriptionId, Timestamp, }; use radroots_transport::{ BoxFuture, EventSource, FetchPage, FetchRequest, @@ -63,13 +63,13 @@ pub(crate) trait RelaySourceClient: Send + Sync { #[derive(Clone, Debug)] pub(crate) struct LiveRelaySourceClient { - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, ingress: crate::source_ingress::IngressRegistry, } impl LiveRelaySourceClient { pub(crate) const fn new( - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, ingress: crate::source_ingress::IngressRegistry, ) -> Self { Self { client, ingress } @@ -77,8 +77,7 @@ impl LiveRelaySourceClient { #[cfg(test)] pub(crate) fn isolated() -> Self { - let client = nostr_sdk::Client::default(); - client.automatic_authentication(false); + let client = nostr_sdk::client::Client::default(); Self::new(client, crate::source_ingress::IngressRegistry::default()) } } @@ -162,9 +161,7 @@ impl RelaySourceClient for LiveRelaySourceClient { let authors = selector .authors() .iter() - .filter_map(|author| { - radroots_nostr::key::public_key_to_nostr(*author).ok() - }) + .filter_map(|author| PublicKey::from_slice(author.as_bytes()).ok()) .collect::<Vec<_>>(); if authors.len() != selector.authors().len() { return Ok(RelayFetchResult::Complete(Vec::new())); @@ -186,42 +183,40 @@ impl RelaySourceClient for LiveRelaySourceClient { filter = filter.until(Timestamp::from_secs(until)); } let connected = tokio::time::timeout_at(deadline, async { - self.client - .try_connect_relay( - url.as_str(), - connect_timeout.min( - deadline - .saturating_duration_since(tokio::time::Instant::now()), - ), - ) - .await - .map_err(|error| error.to_string()) + crate::client::connect_relay( + &self.client, + url.as_str(), + connect_timeout.min( + deadline.saturating_duration_since(tokio::time::Instant::now()), + ), + ) + .await + .map_err(|error| error.to_string()) }) .await; - match connected { + let relay = match connected { Ok(result) => result?, Err(_) => return Ok(RelayFetchResult::Timeout(Vec::new())), - } - let relay = self - .client - .relay(url.as_str()) - .await - .map_err(|error| error.to_string())?; - let mut notifications = relay.notifications(); + }; + let mut notifications = relay + .notifications() + .with_gaps() + .map(|item| item.map_err(|_| ())); let subscription_id = SubscriptionId::generate(); let remaining = deadline.saturating_duration_since(tokio::time::Instant::now()); if remaining.is_zero() { return Ok(RelayFetchResult::Timeout(Vec::new())); } - let options = SubscribeOptions::default().close_on(Some( - SubscribeAutoCloseOptions::default() - .exit_policy(ReqExitPolicy::ExitOnEOSE) - .timeout(Some(remaining)), - )); + let options = SubscribeAutoCloseOptions::default() + .exit_policy(ReqExitPolicy::ExitOnEOSE) + .timeout(Some(remaining)); match tokio::time::timeout_at( deadline, - relay.subscribe_with_id(subscription_id.clone(), filter, options), + relay + .subscribe(filter) + .with_id(subscription_id.clone()) + .close_on(options), ) .await { @@ -266,7 +261,7 @@ impl RelaySourceClient for LiveRelaySourceClient { } async fn admit_fetch_connections( - client: &nostr_sdk::Client, + client: &nostr_sdk::client::Client, relays: &[RelayUrl], deadline: tokio::time::Instant, ) -> Result<FetchConnections, RelayFetchResult> { @@ -288,7 +283,12 @@ async fn admit_fetch_connections( client .relay(relay.as_str()) .await - .map_err(|error| RelayFetchResult::Failed(error.to_string()))?, + .map_err(|error| RelayFetchResult::Failed(error.to_string()))? + .ok_or_else(|| { + RelayFetchResult::Failed(String::from( + "configured relay is unavailable", + )) + })?, ) .map_err(RelayFetchResult::Failed)?; } @@ -332,10 +332,10 @@ fn finish_fetch( // The SDK owns its socket tasks. Its synchronous disconnect signal disables // reconnect and tears down those tasks, including when the fetch future drops. #[derive(Default)] -struct FetchConnections(std::sync::Mutex<Vec<nostr_sdk::Relay>>); +struct FetchConnections(std::sync::Mutex<Vec<nostr_sdk::relay::Relay>>); impl FetchConnections { - fn retain(&self, relay: nostr_sdk::Relay) -> Result<(), String> { + fn retain(&self, relay: nostr_sdk::relay::Relay) -> Result<(), String> { self.0 .lock() .map_err(|_| String::from("fetch connection state unavailable"))? @@ -363,7 +363,7 @@ impl Drop for FetchConnections { } async fn collect_until_eose( - notifications: &mut tokio::sync::broadcast::Receiver<RelayNotification>, + notifications: &mut (impl Stream<Item = Result<RelayNotification, ()>> + Unpin), subscription_id: &SubscriptionId, deadline: tokio::time::Instant, budget: &FetchBudget, @@ -379,57 +379,59 @@ async fn collect_until_eose( let received = tokio::select! { biased; _ = budget.wait_exhausted() => return RelayFetchResult::ResourceLimit(events), - received = tokio::time::timeout_at(deadline, notifications.recv()) => received, + received = tokio::time::timeout_at(deadline, notifications.next()) => received, }; let notification = match received { - Ok(Ok(notification)) => notification, - Ok(Err(error)) => return RelayFetchResult::Failed(error.to_string()), + Ok(Some(Ok(notification))) => notification, + Ok(Some(Err(()))) => { + return RelayFetchResult::Failed(String::from("relay notification gap")); + } + Ok(None) => { + return RelayFetchResult::Failed(String::from("relay notifications unavailable")); + } Err(_) => return RelayFetchResult::Timeout(events), }; if !budget.notification() { return RelayFetchResult::ResourceLimit(events); } match notification { - RelayNotification::Message { - message: - RelayMessage::Event { - subscription_id: observed_subscription, - event, - }, - } if observed_subscription.as_ref() == subscription_id => { - if events.len() >= UPSTREAM_FETCH_LIMIT { - return RelayFetchResult::ResourceLimit(events); + RelayNotification::Message { message } => match *message { + RelayMessage::Event { + subscription_id: observed_subscription, + event, + } if observed_subscription.as_ref() == subscription_id => { + if events.len() >= UPSTREAM_FETCH_LIMIT { + return RelayFetchResult::ResourceLimit(events); + } + let raw = event.as_ref().as_json(); + if !budget.event(raw.len()) { + return RelayFetchResult::ResourceLimit(events); + } + events.push(raw); } - let raw = event.as_ref().as_json(); - if !budget.event(raw.len()) { - return RelayFetchResult::ResourceLimit(events); + RelayMessage::EndOfStoredEvents(observed_subscription) + if observed_subscription.as_ref() == subscription_id => + { + return if budget.exhausted() { + RelayFetchResult::ResourceLimit(events) + } else if tokio::time::Instant::now() < deadline { + RelayFetchResult::Complete(events) + } else { + RelayFetchResult::Timeout(events) + }; } - events.push(raw); - } - RelayNotification::Message { - message: RelayMessage::EndOfStoredEvents(observed_subscription), - } if observed_subscription.as_ref() == subscription_id => { - return if budget.exhausted() { - RelayFetchResult::ResourceLimit(events) - } else if tokio::time::Instant::now() < deadline { - RelayFetchResult::Complete(events) - } else { - RelayFetchResult::Timeout(events) - }; - } - RelayNotification::Message { - message: - RelayMessage::Closed { - subscription_id: observed_subscription, - message, - }, - } if observed_subscription.as_ref() == subscription_id => { - return RelayFetchResult::Failed(message.into_owned()); - } + RelayMessage::Closed { + subscription_id: observed_subscription, + message, + } if observed_subscription.as_ref() == subscription_id => { + return RelayFetchResult::Failed(message.into_owned()); + } + _ => {} + }, RelayNotification::RelayStatus { status: nostr_sdk::prelude::RelayStatus::Disconnected - | nostr_sdk::prelude::RelayStatus::Terminated + | nostr_sdk::prelude::RelayStatus::Idle | nostr_sdk::prelude::RelayStatus::Banned, } => { return RelayFetchResult::Failed(String::from("relay disconnected")); @@ -437,7 +439,9 @@ async fn collect_until_eose( RelayNotification::AuthenticationFailed => { return RelayFetchResult::Failed(String::from("relay authentication failed")); } - RelayNotification::Shutdown => { + RelayNotification::RelayStatus { + status: nostr_sdk::prelude::RelayStatus::Shutdown, + } => { return RelayFetchResult::Failed(String::from("relay shutdown")); } _ => {} @@ -1114,12 +1118,14 @@ mod tests { let (sender, mut receiver) = tokio::sync::broadcast::channel(2); sender .send(RelayNotification::Message { - message: RelayMessage::EndOfStoredEvents(Cow::Owned(subscription_id.clone())), + message: Box::new(RelayMessage::EndOfStoredEvents(Cow::Owned( + subscription_id.clone(), + ))), }) .expect("EOSE"); assert_eq!( collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &subscription_id, tokio::time::Instant::now() + Duration::from_secs(1), &FetchBudget::default(), @@ -1131,7 +1137,7 @@ mod tests { let (_sender, mut receiver) = tokio::sync::broadcast::channel(2); assert_eq!( collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &subscription_id, tokio::time::Instant::now(), &FetchBudget::default() @@ -1147,14 +1153,16 @@ mod tests { let (sender, mut receiver) = tokio::sync::broadcast::channel(1); sender .send(RelayNotification::Message { - message: RelayMessage::EndOfStoredEvents(Cow::Owned(subscription_id.clone())), + message: Box::new(RelayMessage::EndOfStoredEvents(Cow::Owned( + subscription_id.clone(), + ))), }) .unwrap(); let budget = FetchBudget::default(); assert!(!budget.wire(budget::MAX_FETCH_BYTES + 1, 0, 0)); assert_eq!( collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &subscription_id, tokio::time::Instant::now() + Duration::from_secs(1), &budget @@ -1165,6 +1173,32 @@ mod tests { } #[tokio::test] + async fn receiver_gap_cannot_turn_a_retained_eose_into_complete_evidence() { + let id = SubscriptionId::generate(); + let (sender, mut receiver) = tokio::sync::broadcast::channel(1); + sender + .send(RelayNotification::RelayStatus { + status: nostr_sdk::prelude::RelayStatus::Connected, + }) + .unwrap(); + sender + .send(RelayNotification::Message { + message: Box::new(RelayMessage::EndOfStoredEvents(Cow::Owned(id.clone()))), + }) + .unwrap(); + assert_eq!( + collect_until_eose( + &mut test_notifications(&mut receiver), + &id, + tokio::time::Instant::now() + Duration::from_secs(1), + &FetchBudget::default(), + ) + .await, + RelayFetchResult::Failed(String::from("relay notification gap")) + ); + } + + #[tokio::test] async fn exhaustion_after_eose_before_finalization_retains_events_and_earlier_evidence() { let relays = ["wss://one.example", "wss://two.example"]; let registry = @@ -1180,21 +1214,21 @@ mod tests { let (sender, mut receiver) = tokio::sync::broadcast::channel(2); sender .send(RelayNotification::Message { - message: RelayMessage::Event { + message: Box::new(RelayMessage::Event { subscription_id: Cow::Owned(id.clone()), event: Cow::Owned(nostr_sdk::prelude::Event::from_json(FIRST).unwrap()), - }, + }), }) .unwrap(); sender .send(RelayNotification::Message { - message: RelayMessage::EndOfStoredEvents(Cow::Owned(id.clone())), + message: Box::new(RelayMessage::EndOfStoredEvents(Cow::Owned(id.clone()))), }) .unwrap(); batches.push(RelayFetchBatch { relay: RelayUrl::parse(url, RelayUrlPolicy::Public).unwrap(), result: collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &id, tokio::time::Instant::now() + Duration::from_secs(1), &budget, @@ -1278,9 +1312,9 @@ mod tests { #[tokio::test] async fn completed_connection_ownership_is_retained_until_the_whole_query_finishes() { - let client = nostr_sdk::Client::default(); + let client = nostr_sdk::client::Client::default(); client.add_relay("wss://one.example").await.unwrap(); - let relay = client.relay("wss://one.example").await.unwrap(); + let relay = client.relay("wss://one.example").await.unwrap().unwrap(); let connections = FetchConnections::default(); connections.retain(relay.clone()).unwrap(); assert_eq!(connections.0.lock().unwrap().len(), 1); @@ -1292,7 +1326,7 @@ mod tests { #[tokio::test] async fn complete_connection_inventory_is_inert_and_preserves_shared_sockets_on_success() { - let client = nostr_sdk::Client::default(); + let client = nostr_sdk::client::Client::default(); let relays = ["wss://one.example", "wss://two.example"] .map(|url| RelayUrl::parse(url, RelayUrlPolicy::Public).unwrap()); let connections = admit_fetch_connections( @@ -1305,7 +1339,12 @@ mod tests { assert_eq!(connections.0.lock().unwrap().len(), 2); for relay in &relays { assert_eq!( - client.relay(relay.as_str()).await.unwrap().status(), + client + .relay(relay.as_str()) + .await + .unwrap() + .unwrap() + .status(), nostr_sdk::prelude::RelayStatus::Initialized ); } @@ -1313,7 +1352,12 @@ mod tests { drop(connections); for relay in &relays { assert_eq!( - client.relay(relay.as_str()).await.unwrap().status(), + client + .relay(relay.as_str()) + .await + .unwrap() + .unwrap() + .status(), nostr_sdk::prelude::RelayStatus::Initialized ); } @@ -1321,7 +1365,7 @@ mod tests { #[tokio::test] async fn expired_inventory_performs_no_sdk_admission() { - let client = nostr_sdk::Client::default(); + let client = nostr_sdk::client::Client::default(); let relay = RelayUrl::parse("wss://one.example", RelayUrlPolicy::Public).unwrap(); for relays in [vec![relay], Vec::new()] { assert!(matches!( @@ -1334,11 +1378,8 @@ mod tests { #[tokio::test] async fn failed_inventory_releases_every_handle_already_admitted() { - let client = nostr_sdk::Client::builder() - .opts( - nostr_sdk::ClientOptions::default() - .pool(nostr_sdk::prelude::RelayPoolOptions::default().max_relays(Some(1))), - ) + let client = nostr_sdk::client::Client::builder() + .max_relays(std::num::NonZeroUsize::new(1)) .build(); let relays = ["wss://one.example", "wss://two.example"] .map(|url| RelayUrl::parse(url, RelayUrlPolicy::Public).unwrap()); @@ -1353,8 +1394,13 @@ mod tests { )); assert_eq!(client.relays().await.len(), 1); assert_eq!( - client.relay(relays[0].as_str()).await.unwrap().status(), - nostr_sdk::prelude::RelayStatus::Terminated + client + .relay(relays[0].as_str()) + .await + .unwrap() + .unwrap() + .status(), + nostr_sdk::prelude::RelayStatus::Idle ); } @@ -1381,12 +1427,12 @@ mod tests { .unwrap() }) .collect::<Vec<_>>(); - let connector = crate::relay::HardenedWebsocketTransport::new(&endpoints); + let connector = + crate::relay::HardenedWebsocketTransport::new(&endpoints, Duration::from_secs(1)); let ingress = connector.ingress.clone(); - let client = nostr_sdk::Client::builder() + let client = nostr_sdk::client::Client::builder() .websocket_transport(connector) .build(); - client.automatic_authentication(false); let (started, observed) = tokio::sync::oneshot::channel(); let first_server = tokio::spawn(async move { let (tcp, _) = first.accept().await.unwrap(); @@ -1404,7 +1450,14 @@ mod tests { let malformed = serde_json::to_string(&( "EVENT", &value[1], - serde_json::json!({"invalid": "x".repeat(384 * 1024)}), + // Keep primitive wire identities admissible so missing + // event fields reach the aggregate raw-ingress budget. + serde_json::json!({ + "id": "00".repeat(32), + "pubkey": "11".repeat(32), + "sig": "22".repeat(64), + "invalid": "x".repeat(384 * 1024), + }), )) .unwrap(); for _ in 0..24 { @@ -1453,11 +1506,10 @@ mod tests { let _ = closed.send(()); }); client.add_relay(urls[1].as_str()).await.unwrap(); - client - .try_connect_relay(urls[1].as_str(), Duration::from_secs(1)) + crate::client::connect_relay(&client, urls[1].as_str(), Duration::from_secs(1)) .await .unwrap(); - let queued = client.relay(urls[1].as_str()).await.unwrap(); + let queued = client.relay(urls[1].as_str()).await.unwrap().unwrap(); let source = LiveRelaySourceClient::new(client, ingress); let query = SourceQuery { relays: urls @@ -1504,7 +1556,7 @@ mod tests { assert_eq!(queued_requests.load(AtomicOrdering::SeqCst), 0); assert_eq!( status, - nostr_sdk::prelude::RelayStatus::Terminated, + nostr_sdk::prelude::RelayStatus::Idle, "queued preexisting socket must lose automatic reconnect authority" ); closed.unwrap().unwrap(); @@ -1709,7 +1761,7 @@ mod tests { } assert_eq!( collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &id, tokio::time::Instant::now() + Duration::from_secs(10), &FetchBudget::default() @@ -1727,15 +1779,15 @@ mod tests { for _ in 0..=UPSTREAM_FETCH_LIMIT { sender .send(RelayNotification::Message { - message: RelayMessage::Event { + message: Box::new(RelayMessage::Event { subscription_id: Cow::Owned(id.clone()), event: Cow::Owned(event.clone()), - }, + }), }) .unwrap(); } let result = collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &id, tokio::time::Instant::now() + Duration::from_secs(10), &FetchBudget::default(), @@ -1799,20 +1851,20 @@ mod tests { for _ in 0..17 { sender .send(RelayNotification::Message { - message: RelayMessage::Event { + message: Box::new(RelayMessage::Event { subscription_id: Cow::Owned(id.clone()), event: Cow::Owned(event.clone()), - }, + }), }) .unwrap(); } sender .send(RelayNotification::Message { - message: RelayMessage::EndOfStoredEvents(Cow::Owned(id.clone())), + message: Box::new(RelayMessage::EndOfStoredEvents(Cow::Owned(id.clone()))), }) .unwrap(); let result = collect_until_eose( - &mut receiver, + &mut test_notifications(&mut receiver), &id, tokio::time::Instant::now() + Duration::from_secs(10), &shared, @@ -1905,4 +1957,18 @@ mod tests { 1 ); } + + fn test_notifications( + receiver: &mut tokio::sync::broadcast::Receiver<RelayNotification>, + ) -> std::pin::Pin<Box<dyn Stream<Item = Result<RelayNotification, ()>> + Send + '_>> { + Box::pin(futures::stream::unfold(receiver, |receiver| async { + match receiver.recv().await { + Ok(value) => Some((Ok(value), receiver)), + Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => { + Some((Err(()), receiver)) + } + Err(tokio::sync::broadcast::error::RecvError::Closed) => None, + } + })) + } } diff --git a/crates/transport_nostr/src/subscription.rs b/crates/transport_nostr/src/subscription.rs @@ -1,9 +1,10 @@ //! Bounded Nostr live-subscription adapter. use crate::{NostrTransport, RelayCursor, RelayUrl}; +use futures::StreamExt; use nostr_sdk::prelude::{ - ClientMessage, Filter, JsonUtil, Kind, RelayMessage, RelayPoolNotification, ReqExitPolicy, - SubscribeAutoCloseOptions, SubscribeOptions, SubscriptionId, Timestamp, + ClientMessage, ClientNotification, Filter, Kind, PublicKey, RelayMessage, ReqExitPolicy, + SubscribeAutoCloseOptions, SubscriptionId, Timestamp, }; use radroots_transport::{ BoxFuture, BoxSubscription, EventSubscriber, EventSubscription, SubscriptionEnd, @@ -60,18 +61,17 @@ pub(crate) trait RelaySubscriptionClient: Send + Sync { #[derive(Clone, Debug)] pub(crate) struct LiveRelaySubscriptionClient { - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, } impl LiveRelaySubscriptionClient { - pub(crate) const fn new(client: nostr_sdk::Client) -> Self { + pub(crate) const fn new(client: nostr_sdk::client::Client) -> Self { Self { client } } #[cfg(test)] pub(crate) fn isolated() -> Self { - let client = nostr_sdk::Client::default(); - client.automatic_authentication(false); + let client = nostr_sdk::client::Client::default(); Self::new(client) } } @@ -83,7 +83,7 @@ impl RelaySubscriptionClient for LiveRelaySubscriptionClient { query: RelaySubscriptionQuery, ) -> BoxFuture<'_, Result<Box<dyn RelaySubscriptionSession>, ()>> { Box::pin(async move { - let notifications = self.client.notifications(); + let notifications = self.client.notifications().with_gaps(); let subscription_id = SubscriptionId::new(query.id); let mut targeted = Vec::with_capacity(query.targets.len()); let mut relay_lookup = BTreeMap::new(); @@ -91,13 +91,16 @@ impl RelaySubscriptionClient for LiveRelaySubscriptionClient { for target in query.targets { let url = target.relay.as_str().to_owned(); let filter = subscription_filter(&query.selector, target.since_unix_seconds)?; - self.client.add_relay(url.as_str()).await.map_err(|_| ())?; self.client - .try_connect_relay(url.as_str(), query.connect_timeout) + .add_relay(url.as_str()) + .connect_timeout(query.connect_timeout) + .await + .map_err(|_| ())?; + crate::client::connect_relay(&self.client, url.as_str(), query.connect_timeout) .await .map_err(|_| ())?; relay_lookup.insert(url.clone(), target.relay); - targeted.push((url, filter)); + targeted.push((url, vec![filter])); } let auto_close = SubscribeAutoCloseOptions::default() @@ -105,22 +108,18 @@ impl RelaySubscriptionClient for LiveRelaySubscriptionClient { .timeout(Some(query.timeout)); let output = self .client - .subscribe_targeted( - subscription_id.clone(), - targeted, - SubscribeOptions::default().close_on(Some(auto_close)), - ) + .subscribe(targeted) + .with_id(subscription_id.clone()) + .close_on(auto_close) .await .map_err(|_| ())?; if !output.failed.is_empty() || output.success.len() != relay_lookup.len() { let _ = self .client - .send_msg_to( - relay_lookup.keys().map(String::as_str), - ClientMessage::close(subscription_id.clone()), - ) + .send_msg(ClientMessage::close(subscription_id.clone())) + .to(relay_lookup.keys().map(String::as_str)) .await; - self.client.unsubscribe(&subscription_id).await; + let _ = self.client.unsubscribe(&subscription_id).await; return Err(()); } @@ -139,9 +138,9 @@ impl RelaySubscriptionClient for LiveRelaySubscriptionClient { } struct LiveRelaySubscriptionSession { - client: nostr_sdk::Client, + client: nostr_sdk::client::Client, subscription_id: SubscriptionId, - notifications: Option<tokio::sync::broadcast::Receiver<RelayPoolNotification>>, + notifications: Option<nostr_sdk::stream::NotificationStreamWithGaps<ClientNotification>>, relay_lookup: BTreeMap<String, RelayUrl>, cancelled: bool, } @@ -154,19 +153,19 @@ impl RelaySubscriptionSession for LiveRelaySubscriptionSession { return Ok(RelaySubscriptionItem::Shutdown); }; loop { - let notification = match notifications.recv().await { - Ok(notification) => notification, - Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => return Err(()), - Err(tokio::sync::broadcast::error::RecvError::Closed) => { + let notification = match notifications.next().await { + Some(Ok(notification)) => notification, + Some(Err(_)) => return Err(()), + None => { return Ok(RelaySubscriptionItem::Shutdown); } }; match notification { - RelayPoolNotification::Message { relay_url, message } => { + ClientNotification::Message { relay_url, message } => { let Some(relay) = self.relay_lookup.get(relay_url.as_str()).cloned() else { continue; }; - match message { + match *message { RelayMessage::Event { subscription_id, event, @@ -184,10 +183,10 @@ impl RelaySubscriptionSession for LiveRelaySubscriptionSession { _ => {} } } - RelayPoolNotification::Shutdown => { + ClientNotification::Shutdown => { return Ok(RelaySubscriptionItem::Shutdown); } - RelayPoolNotification::Event { .. } => {} + ClientNotification::Event { .. } => {} } } }) @@ -199,16 +198,21 @@ impl RelaySubscriptionSession for LiveRelaySubscriptionSession { if !self.cancelled { let output = self .client - .send_msg_to( - self.relay_lookup.keys().map(String::as_str), - ClientMessage::close(self.subscription_id.clone()), - ) + .send_msg(ClientMessage::close(self.subscription_id.clone())) + .to(self.relay_lookup.keys().map(String::as_str)) .await .map_err(|_| ())?; if !output.failed.is_empty() || output.success.len() != self.relay_lookup.len() { return Err(()); } - self.client.unsubscribe(&self.subscription_id).await; + let output = self + .client + .unsubscribe(&self.subscription_id) + .await + .map_err(|_| ())?; + if !output.failed.is_empty() { + return Err(()); + } self.cancelled = true; self.notifications = None; } @@ -614,7 +618,7 @@ fn subscription_filter( let authors = selector .authors() .iter() - .map(|author| radroots_nostr::key::public_key_to_nostr(*author).map_err(|_| ())) + .map(|author| PublicKey::from_slice(author.as_bytes()).map_err(|_| ())) .collect::<Result<Vec<_>, _>>()?; let mut filter = Filter::new(); if !kinds.is_empty() { @@ -745,7 +749,7 @@ mod tests { use crate::{ Config, RelayAccess, RelayEndpoint, RelayProfile, RelayProfileKind, RelayUrlPolicy, }; - use nostr_sdk::prelude::{EventBuilder, Keys}; + use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys}; use radroots_transport::{ EventSubscriber, Target, TargetSet, source::{FetchSelector, SubscriptionBounds, SubscriptionCheckpoint}, @@ -868,9 +872,9 @@ mod tests { } fn signed_event(content: &str, created_at: u64) -> String { - EventBuilder::text_note(content) + EventBuilder::new(Kind::TextNote, content) .custom_created_at(Timestamp::from_secs(created_at)) - .sign_with_keys(&Keys::parse(FIXTURE_SECRET_KEY).expect("fixture keys")) + .finalize(&Keys::parse(FIXTURE_SECRET_KEY).expect("fixture keys")) .expect("signed event") .as_json() } diff --git a/crates/transport_nostr/tests/exact_delivery.rs b/crates/transport_nostr/tests/exact_delivery.rs @@ -1,5 +1,5 @@ use futures::{FutureExt, SinkExt, StreamExt}; -use nostr_sdk::prelude::{EventBuilder, JsonUtil, Keys, Kind, Tag, Timestamp}; +use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys, Kind, Tag, Timestamp}; use radroots_transport::{ DeliveryRequest, EventSink, EventSource, FetchRequest, TargetSet, policy::{SatisfactionClass, SatisfactionPolicy, TargetPolicy}, @@ -27,9 +27,9 @@ fn now() -> u64 { } fn raw_event() -> String { - let event = EventBuilder::text_note("harvest café\n🌱") + let event = EventBuilder::new(Kind::TextNote, "harvest café\n🌱") .custom_created_at(Timestamp::from_secs(1_800_000_000)) - .sign_with_keys(&Keys::parse(KEY).unwrap()) + .finalize(&Keys::parse(KEY).unwrap()) .unwrap(); let mut value: Value = serde_json::from_str(&event.as_json()).unwrap(); value["extension"] = json!({"retained": true}); @@ -203,7 +203,7 @@ async fn exact_signed_bytes_read_requests_and_auth_use_the_same_connection() { Tag::parse(["challenge", "challenge"]).unwrap(), ]) .custom_created_at(Timestamp::from_secs(at / 1_000)) - .sign_with_keys(&Keys::parse(KEY).unwrap()) + .finalize(&Keys::parse(KEY).unwrap()) .unwrap(); transport .complete_authentication(relay, "challenge", Some(&auth.as_json()), at) @@ -223,6 +223,63 @@ async fn exact_signed_bytes_read_requests_and_auth_use_the_same_connection() { } #[tokio::test] +async fn an_overlong_ack_identifier_never_accepts_the_matching_prefix() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let config = config(&format!("ws://{}", listener.local_addr().unwrap()), 250); + let raw = raw_event(); + let (done, wait) = tokio::sync::oneshot::channel(); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(tcp).await.unwrap(); + let event: Value = serde_json::from_str(&text(&mut socket).await).unwrap(); + let overlong = format!("{}00", event[1]["id"].as_str().unwrap()); + reply(&mut socket, json!(["OK", overlong, true, ""])).await; + wait.await.unwrap(); + }); + let request = request(&config, &raw); + let receipt = tokio::time::timeout( + Duration::from_secs(5), + NostrTransport::new(config).deliver(request.clone()), + ) + .await + .unwrap() + .unwrap(); + done.send(()).unwrap(); + server.await.unwrap(); + assert!(!receipt.is_satisfied(&request).unwrap()); +} + +#[tokio::test] +async fn relay_challenge_never_creates_an_implicit_authentication_response() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let config = config(&format!("ws://{}", listener.local_addr().unwrap()), 2_000); + let raw = raw_event(); + let expected = format!("[\"EVENT\",{raw}]"); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(tcp).await.unwrap(); + let wire = text(&mut socket).await; + assert_eq!(wire, expected); + let event: Value = serde_json::from_str(&wire).unwrap(); + reply(&mut socket, json!(["AUTH", "explicit-signer-required"])).await; + assert!( + tokio::time::timeout(Duration::from_millis(100), text(&mut socket)) + .await + .is_err() + ); + reply(&mut socket, json!(["OK", event[1]["id"], true, ""])).await; + }); + let transport = NostrTransport::new(config.clone()); + let request = request(&config, &raw); + let receipt = tokio::time::timeout(Duration::from_secs(5), transport.deliver(request.clone())) + .await + .unwrap() + .unwrap(); + server.await.unwrap(); + assert!(receipt.is_satisfied(&request).unwrap()); +} + +#[tokio::test] async fn rejection_lost_ack_and_disconnect_never_invent_acceptance() { for mode in ["reject", "lost", "disconnect"] { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); diff --git a/crates/transport_nostr/tests/fetch_bounds.rs b/crates/transport_nostr/tests/fetch_bounds.rs @@ -1,5 +1,5 @@ use futures::{SinkExt, StreamExt}; -use nostr_sdk::prelude::{EventBuilder, JsonUtil, Keys}; +use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys, Kind}; use radroots_transport::{ EventSource, FetchRequest, TargetSet, outcome::FetchTargetState, source::FetchBounds, }; @@ -57,8 +57,8 @@ async fn listener() -> (TcpListener, String) { fn event(content: &str) -> Value { let keys = Keys::parse("0000000000000000000000000000000000000000000000000000000000000001").unwrap(); - let event = EventBuilder::text_note(content) - .sign_with_keys(&keys) + let event = EventBuilder::new(Kind::TextNote, content) + .finalize(&keys) .unwrap(); serde_json::from_str(&event.as_json()).unwrap() } @@ -103,6 +103,94 @@ async fn serve( } #[tokio::test(flavor = "multi_thread")] +async fn a_cached_event_id_never_admits_a_changed_signature() { + let (listener, url) = listener().await; + let valid = event("cached signature admission"); + let mut changed = valid.clone(); + changed["sig"] = Value::String("0".repeat(128)); + let (release, released) = oneshot::channel(); + let server = tokio::spawn(async move { + let (stream, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(stream).await.unwrap(); + for payload in [valid, changed, event("valid continuation after rejection")] { + loop { + let message = socket.next().await.unwrap().unwrap(); + let Message::Text(text) = message else { + socket.flush().await.unwrap(); + continue; + }; + let values: Value = serde_json::from_str(&text).unwrap(); + if values[0] != "REQ" { + continue; + } + socket + .send(Message::Text( + serde_json::to_string(&("EVENT", &values[1], &payload)) + .unwrap() + .into(), + )) + .await + .unwrap(); + socket + .send(Message::Text( + serde_json::to_string(&("EOSE", &values[1])).unwrap().into(), + )) + .await + .unwrap(); + break; + } + } + released.await.unwrap(); + }); + let (transport, request) = transport(&[url], 1); + let pages = tokio::time::timeout(Duration::from_secs(5), async { + let first = transport.fetch(request.clone()).await.unwrap(); + let repeated = transport.fetch(request.clone()).await.unwrap(); + let continuation = transport.fetch(request).await.unwrap(); + (first, repeated, continuation) + }) + .await + .unwrap(); + release.send(()).unwrap(); + server.await.unwrap(); + assert_eq!(pages.0.events().len(), 1); + assert!( + pages.1.events().is_empty(), + "changed signature reached fetch output" + ); + assert_eq!(pages.2.events().len(), 1); + assert!( + [&pages.0, &pages.1, &pages.2] + .into_iter() + .all(|page| page.target_outcomes()[0].state() == FetchTargetState::Complete) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn overlong_event_identifiers_never_become_normalized_fetch_results() { + for field in ["id", "pubkey", "sig"] { + let (listener, url) = listener().await; + let mut payload = event("strict wire identifiers"); + payload[field] = Value::String(format!("{}00", payload[field].as_str().unwrap())); + let calls = Arc::new(AtomicUsize::new(0)); + let server = tokio::spawn(serve(listener, Some(payload), true, Arc::clone(&calls))); + let (transport, request) = transport(&[url], 1); + let page = tokio::time::timeout(Duration::from_secs(5), transport.fetch(request)) + .await + .unwrap() + .unwrap(); + server.abort(); + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert!(page.events().is_empty(), "{field}"); + assert_ne!( + page.target_outcomes()[0].state(), + FetchTargetState::Complete, + "{field}" + ); + } +} + +#[tokio::test(flavor = "multi_thread")] async fn queued_relays_do_not_receive_a_fresh_timeout_after_a_stalled_relay() { let (first, first_url) = listener().await; let (second, second_url) = listener().await; diff --git a/crates/transport_nostr/tests/fetch_raw_budget.rs b/crates/transport_nostr/tests/fetch_raw_budget.rs @@ -52,6 +52,19 @@ async fn listener() -> (TcpListener, String) { (listener, url) } +fn malformed_event() -> Value { + // Canonical primitive fields pass wire admission, while absent event + // fields still exercise malformed traffic in the aggregate ingress budget. + let value = serde_json::json!({ + "id": "00".repeat(32), + "pubkey": "11".repeat(32), + "sig": "22".repeat(64), + "invalid_event": "x".repeat(384 * 1024), + }); + assert!(serde_json::from_value::<nostr_sdk::prelude::Event>(value.clone()).is_err()); + value +} + // A WebSocket pong is produced only after the client's receive stream has // consumed all preceding messages. No scheduling sleep is an admission barrier. async fn barrier(socket: &mut WebSocketStream<TcpStream>, sequence: usize) -> bool { @@ -93,12 +106,7 @@ async fn serve_malformed(listener: TcpListener, frames: usize) { if values[0] != "REQ" { continue; } - let malformed = serde_json::to_string(&( - "EVENT", - &values[1], - serde_json::json!({ "invalid_event": "x".repeat(384 * 1024) }), - )) - .unwrap(); + let malformed = serde_json::to_string(&("EVENT", &values[1], malformed_event())).unwrap(); assert!(malformed.len() < 512 * 1024); for sequence in 0..frames { if socket @@ -194,12 +202,13 @@ async fn unsolicited_malformed_traffic_is_bounded_before_the_request() { let server = tokio::spawn(async move { let (stream, _) = listener.accept().await.unwrap(); let mut socket = accept_async(stream).await.unwrap(); + let malformed = + serde_json::to_string(&("EVENT", "unsolicited", malformed_event())).unwrap(); for sequence in 0..24 { - let malformed = format!( - "[\"EVENT\",\"unsolicited\",{{\"invalid\":\"{}\"}}]", - "x".repeat(384 * 1024) - ); - if socket.send(Message::Text(malformed.into())).await.is_err() + if socket + .send(Message::Text(malformed.clone().into())) + .await + .is_err() || !barrier(&mut socket, sequence).await { return; diff --git a/crates/transport_nostr/tests/fetch_windows.rs b/crates/transport_nostr/tests/fetch_windows.rs @@ -1,5 +1,5 @@ use futures::{SinkExt, StreamExt}; -use nostr_sdk::prelude::{EventBuilder, JsonUtil, Keys, Timestamp}; +use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys, Kind, Timestamp}; use radroots_transport::{ EventSource, FetchRequest, TargetSet, outcome::FetchTargetState, @@ -27,9 +27,9 @@ async fn real_capped_eose_preserves_received_ties_and_yields_explicit_older_hist Keys::parse("0000000000000000000000000000000000000000000000000000000000000001").unwrap(); let mut history = (0..1001) .map(|index| { - let event = EventBuilder::text_note(format!("equal-time {index}")) + let event = EventBuilder::new(Kind::TextNote, format!("equal-time {index}")) .custom_created_at(Timestamp::from_secs(100)) - .sign_with_keys(&keys) + .finalize(&keys) .unwrap(); serde_json::from_str::<Value>(&event.as_json()).unwrap() }) @@ -39,9 +39,9 @@ async fn real_capped_eose_preserves_received_ties_and_yields_explicit_older_hist .iter() .map(|event| event["id"].as_str().unwrap().to_owned()) .collect::<BTreeSet<_>>(); - let older = EventBuilder::text_note("older") + let older = EventBuilder::new(Kind::TextNote, "older") .custom_created_at(Timestamp::from_secs(99)) - .sign_with_keys(&keys) + .finalize(&keys) .unwrap(); history.push(serde_json::from_str(&older.as_json()).unwrap()); let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); diff --git a/crates/transport_nostr/tests/local_io.rs b/crates/transport_nostr/tests/local_io.rs @@ -103,11 +103,11 @@ async fn simulator_profile_proves_read_capability_against_a_real_loopback_socket #[tokio::test(flavor = "multi_thread")] async fn simulator_profile_streams_and_cancels_a_real_live_subscription() { - use nostr_sdk::prelude::{EventBuilder, JsonUtil, Keys, Timestamp}; + use nostr_sdk::prelude::{EventBuilder, FinalizeEvent, Keys, Kind, Timestamp}; - let event = EventBuilder::text_note("bounded live event") + let event = EventBuilder::new(Kind::TextNote, "bounded live event") .custom_created_at(Timestamp::from_secs(1_800_000_100)) - .sign_with_keys(&Keys::parse(FIXTURE_SECRET_KEY).expect("fixture keys")) + .finalize(&Keys::parse(FIXTURE_SECRET_KEY).expect("fixture keys")) .expect("signed event"); let expected_id = event.id.to_hex(); let event: Value = serde_json::from_str(event.as_json().as_str()).expect("event JSON"); diff --git a/crates/transport_nostr/tests/network_hardening.rs b/crates/transport_nostr/tests/network_hardening.rs @@ -22,7 +22,7 @@ fn tls_verification_and_pinned_dns_are_non_configurable_live_defaults() { ".max_frame_size(Some(MAX_WIRE_MESSAGE_BYTES))", "validate_resolved_addresses(", "connect_pinned(addresses.as_slice())", - "ConnectionMode::Direct", + "if proxy.is_some()", ] { assert!( RELAY_SOURCE.contains(required), diff --git a/crates/transport_nostr/tests/package_boundary.rs b/crates/transport_nostr/tests/package_boundary.rs @@ -321,6 +321,7 @@ fn adapter_owns_no_storage_outbox_or_orchestration_surface() { "error.rs".to_owned(), "exact_delivery.rs".to_owned(), "lib.rs".to_owned(), + "native_backend_tests.rs".to_owned(), "profile.rs".to_owned(), "relay.rs".to_owned(), "sink.rs".to_owned(), diff --git a/deny.toml b/deny.toml @@ -16,7 +16,6 @@ all-features = true ignore = [ "RUSTSEC-2024-0384", # `instant` is an unmaintained, non-vulnerability Nostr 0.44 dependency. "RUSTSEC-2024-0421", - "RUSTSEC-2026-0243", # `nostr-relay-pool` is unmaintained and moves into `nostr-sdk` at 0.45. ] [licenses] @@ -60,7 +59,10 @@ skip-tree = [] unknown-registry = "deny" unknown-git = "deny" allow-registry = ["https://github.com/rust-lang/crates.io-index"] -allow-git = ["https://github.com/rust-nostr/nostr.git"] +allow-git = [ + "https://github.com/nostrdevkit/nostr.git", + "https://github.com/yukibtc/async-utility", +] [sources.allow-org] github = [] diff --git a/supply-chain/audits.toml b/supply-chain/audits.toml @@ -17,8 +17,326 @@ implies = "safe-to-deploy" description = "The package's secret lifecycle, redaction, zeroization, persistence, keyring boundary, and error behavior were reviewed for the Radroots use case." implies = "safe-to-deploy" +[[audits.async-utility]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.3.2 -> 0.3.2@git:b323ae0c1c1424b78bf8bb8832b95e86771f1dee" +notes = "Reviewed the complete registry-to-Git Rust delta and manifest change. Native task handles delegate polling, completion and abort to Tokio; ordinary drop detaches while AbortOnDrop requests cancellation. Native timeout/runtime modules are unchanged and parse no network bytes. The WASM implementation uses local spawning, an Abortable future, oneshot output and an acquire/release completion guard; its source review does not qualify a browser runtime. Added tests cover cancellation before polling, repeated abort, retained completed output, detached ownership and non-Unpin/non-Send values. This automated delta review inherits the existing registry 0.3.2 exemption and makes no independent audit or universal task-termination claim." + +[[audits.async-wsocket]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.17.0" +notes = "Reviewed the complete native production modules, message/socket conversions, manifest and selected SDK WebSocket SPI, plus browser actor/stream ownership. Native errors and sink/stream polling delegate to Tokio/Tungstenite; no new unsafe Send wrapper is introduced. The Radroots adapter supplies its own DNS/address/TLS policy, exact wire-identifier admission, configured connection deadline, fixed frame bounds and metered ingress, so default proxy/SOCKS dialing is not admitted. Both native loopback suites exercise the actual composition. The new browser actor uses unbounded channels; it is not admitted as the bounded Radroots browser implementation. This focused automated native-use review does not qualify those browser queues or unrestricted default dialing." + +[[audits.autocfg]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.5.1" +notes = "Reviewed the complete production sources and the published 1.5.0 -> 1.5.1 delta. This build-only helper accepts trusted build-script snippets and compiler/wrapper/flag inputs, invokes Command without a shell, checks the output directory, writes compiler IR there and removes only its generated probe path. Version/IO/process failures remain errors; it has no runtime network parser. The release changes the wrapper test harness, not production probing. Current Rust 1.99 compilation executes the selected build dependency. This automated review assumes governed compiler/build inputs and is not an independent audit or permission to compile untrusted network text." + +[[audits.bech32]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.12.0" +notes = "Reviewed the manifest, top-level decode entry, HRP admission, character/separator checking and checksum-to-byte-iterator path. HRPs have a fixed 83-byte buffer; invalid characters, mixed case, missing separators, insufficient checksum and code-length violations return errors before collecting decoded bytes. Allocating decoding is proportional to admitted input, so callers still need input bounds. The private native Nostr path receives bounded frames and requires canonical hexadecimal EVENT/OK identities before SDK parsing; it does not accept Bech32 aliases as wire identity. This focused automated native-use review does not qualify Bitcoin witness policies, checksum selection by arbitrary callers or unbounded NIP-19 data." + +[[audits.bitcoin-consensus-encoding]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.3.0" +notes = "Reviewed the manifest and feature boundaries, slice decoder drivers, fixed ArrayDecoder and byte/array encoders, and complete compact-size byte admission and minimal-encoding checks. The selected native dependency has no alloc/std/serde features; bitcoin_hashes uses fixed-size array codec traits, not a Bitcoin peer or transaction parser. Fixed buffers advance by the available remaining bytes and reject incomplete completion; exact slice decoding rejects trailing input. Arbitrary user-supplied decoders must make progress, and optional allocating collection/IO drivers are outside this admitted use. This focused automated native-use review does not qualify those optional APIs or provide a general resource bound for caller-defined codecs." + +[[audits.bitcoin-internals]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.6.0" +notes = "Reviewed all production modules and the build script/manifest: fixed-array/slice transformations, the Copy-only ArrayVec safety boundary, bounded serde insertion, error/storage macros and integer casts. ArrayVec tracks initialized elements within static capacity; safe insertion refuses excess capacity and its raw slice view covers only initialized elements. Slice chunking checks nonzero constant chunk sizes. Some caller APIs can panic on invalid ranges/capacity, and allocating errors can retain raw input. Native hash callers use fixed block/array sizes and public adapter errors normalize upstream causes. The build script runs the governed rustc directly and writes only OUT_DIR/rust_version.rs. This automated source review is not a general unsafe-code proof or redaction guarantee for arbitrary callers." + +[[audits.bitcoin-internals]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.6.0 -> 0.7.0" +notes = "Reviewed the complete production delta and manifest: shared array/slice safety code and build script remain unchanged, input-error formatting escapes debug characters, an integer widening cast is added, and U256 uses two fixed u128 words. U256 hex/byte serde requires exactly 32 bytes; decimal parsing returns overflow/invalid-input errors, formatting uses fixed buffers, and arithmetic/division retain documented panic conditions. Test-only serde dependencies move out of the production feature surface. Current native dependencies select no optional serde/alloc features on either internals version and do not expose U256 arithmetic as Radroots agreement values. This automated delta inherits the reviewed 0.6 source and does not qualify arbitrary arithmetic operands or monetary semantics." + +[[audits.bitcoin_hashes]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.2.0" +notes = "Reviewed the selected feature manifest, fixed-hash construction/serde/hex macros, SHA256 buffering and finalization, normal software and CPU-dispatched block-processing paths, and native call sites. Fixed hash byte decoding requires exact array length; string decoding uses the reviewed fixed-size hex decoder. SHA256 buffers at most one 64-byte block and dispatches intrinsic implementations only after CPU feature detection; their four 16-byte block loads are reached with complete blocks. Native adapter ingress is bounded before hashing and its interoperation tests compare event identities across the independent codecs on macOS and Linux. This focused automated native-use review does not independently certify the cryptographic algorithm, every hash family, batch hashing API, unlimited incremental input, or another platform." + +[[audits.bitflags]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.3.2" +notes = "Reviewed the manifest and complete production macro expansion and attribute filtering. Generated values contain a caller-selected integer; checked from_bits rejects unknown bits, truncate masks them, and remove uses bit-clear while toggle uses XOR. The unsafe constructor preserves bits and requires the caller's documented validity invariant. There is no byte decoder, allocation, filesystem or process access. This same-version package was already locked with a safe-to-deploy exemption; its newly inherited network criterion is evaluated for the current native composition, where the optional embedded logging edge is not selected. This is automated source review, not application authorization or optional-feature execution evidence." + +[[audits.byteorder]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.5.0" +notes = "Reviewed feature boundaries, fixed integer reads/writes, variable-byte-length admission and slice conversion macros used by the hash32 edge. The selected no_std feature set excludes I/O extension helpers. Primitive reads use exact bounded slices and native endian conversions; invalid caller buffer lengths panic, rather than supplying partial values. This edge is compiled through heapless, while the admitted faster-hex std/alloc runtime uses its allocating implementation and no hash32 parser. This focused automated native-use review qualifies no arbitrary-length reader, dynamic allocation policy or invalid-call panic freedom." + +[[audits.chacha20]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.10.2" +notes = "Reviewed manifest and feature selection, fixed-size state/seed and RNG entry points and CPU-dispatch boundaries. This optional rand software-generator dependency is absent from both selected native graphs: the SDK selects SysRng, not StdRng/ThreadRng or this package's cipher/RNG APIs. The package owns no network parser; its internal cipher constructors require trusted fixed key/nonce sizes. This automated review admits the inactive dependency in the current macOS/Linux composition only, and does not independently certify a new stream-cipher algorithm, optional RNG state serialization, architecture backend or secret lifecycle." + +[[audits.cpufeatures]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.2.17 -> 0.3.0" +notes = "Reviewed the complete production release delta and manifest: trusted CPU detection tokens gain initialization helpers; Apple sysctl reads retain fixed-size outputs and null write arguments; x86 adds CPUID leaf 7 subleaf 1 feature bits; LoongArch feature detection combines CPUCFG and OS capability bits. No network text, collection lengths, parser or writable system setting is introduced. The 0.3 dependency belongs to an optional software RNG edge absent from the current native feature graphs. This automated delta review inherits the existing 0.2.17 exemption and does not qualify cryptographic algorithms or execute the changed optional architecture paths." + +[[audits.defmt]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.3.101" +notes = "Reviewed the complete production source and manifest. This compatibility package reexports defmt 1.x macros, traits and encoding helpers and forwards its feature flags; it has no independent build script, runtime parser or network owner. Both selected native public graphs exclude it. This automated review admits that inactive reexport package for the current composition and does not qualify embedded logging, arbitrary global logger implementations or another platform." + +[[audits.defmt]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.2.0" +notes = "Reviewed manifest/module boundaries, the complete build script, logger safety contract and raw-frame/rzcobs encoder ownership. The script reads the packaged linker template and writes only its generated linker script under Cargo OUT_DIR; the encoder consumes caller slices with fixed run/zero counters, not a network decoding API. Global logger thread/interrupt safety remains the implementing host's obligation. This package and build script are absent from both selected native public graphs. This focused automated review admits the inactive dependency for that composition, not embedded runtime qualification, every formatter implementation or a global logger safety proof." + +[[audits.defmt-macros]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.2.0" +notes = "Reviewed manifest/build entry, macro dispatch and format-string argument parsing, log expansion/code generation, symbol construction and compile-time environment filtering. The build script emits only DEFMT_LOG rerun tracking; macros accept Rust token streams and literal format strings and generate typed Rust/linker metadata. The selected native graphs exclude this package. Format argument indices and generated code are trusted compiler inputs, not admitted network text; optional logger/panic attributes still require their host safety contracts. This focused automated inactive-use review does not certify all macro expansions or bounded parsing of arbitrary untrusted Rust/format strings." + +[[audits.defmt-parser]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.1.0" +notes = "Reviewed all production parsing modules: format fragments, type names, display hints, array/range integers, brace admission and argument consistency. Parsing follows UTF-8 character boundaries and reports malformed types/ranges/braces. Argument indices control vector resizing and index-plus-one arithmetic, so this parser is unsuitable for untrusted network input without separate input/index bounds. The only relevant dependency route is optional compile-time defmt macros with trusted literals, and both selected native graphs exclude it. This automated inactive-use review supplies no runtime parser/resource qualification or permission to route network strings through this API." + +[[audits.faster-hex]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.10.1" +notes = "Reviewed all production encoder, decoder, dispatch, error, serde and build-probe source and the actual Nostr fixed-array call sites. CPU dispatch checks x86 processor/OS state or aarch64 NEON support; vector reads and writes require complete source and destination chunks, with scalar tails. Encoding returns only its initialized ASCII prefix. Checked decoding rejects odd/nonhex input but permits a longer even source than the destination: it is not an exact-identifier validator. The bounded native adapter therefore checks canonical exact-length EVENT and OK identifiers before upstream normalization; real socket regressions retain the previously observed failures. Allocating helpers assume caller-sized inputs, and the admitted messages retain the adapter's frame/ingress limits. This automated native-use review does not claim general constant-time behavior, unrestricted allocation safety, optional embedded logging or browser qualification." + +[[audits.getrandom]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.4.2" +notes = "Reviewed feature/backend selection, the public fill/u32/u64 and SysRng interfaces, initialization helpers and the native macOS and Linux implementations, including the Linux file fallback. macOS uses at most 256-byte getentropy chunks; Linux handles partial reads and EINTR, rejects zero/invalid progress and selects the fixed OS source rather than network-supplied paths. Initialized slice conversion follows successful complete fill. Native SDK callers request fixed-size values for ping/jitter and protocol contexts, and no packet selects a backend or buffer length. System entropy can block or fail, and the upstream UnwrapErr adapter can panic on that failure. This focused automated native-use review does not claim nonblocking entropy, panic freedom, cryptographic implementation audit or qualification of alternate Windows/UEFI/WASI/browser backends." + +[[audits.hash32]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.3.1" +notes = "Reviewed all three production modules and the manifest: trait forwarding, stateless hasher construction, FNV wrapping accumulation and the Murmur3 block/tail implementation. This is a non-cryptographic hash dependency, not an event-identity or signature algorithm. The only selected reverse edge is heapless through faster-hex; the native faster-hex std/alloc implementation invokes neither those collections nor either hasher. Murmur3's potentially uninitialized buffer references are outside the admitted native call path and require separate review/proving inputs before any use. This automated use-scoped review does not certify that unsafe implementation, adversarial collision resistance or an embedded runtime." + +[[audits.heapless]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.8.0" +notes = "Reviewed module/feature and build-probe boundaries plus fixed-capacity Vec/String insertion, initialization and UTF-8 conversion paths referenced by faster-hex's non-alloc alternative. Checked push/extend refuse excess capacity and strings preserve UTF-8 after safe insertion; unsafe constructors retain caller initialization/encoding requirements. The build script compiles a fixed trusted probe under OUT_DIR with governed compiler/wrapper arguments and owns no runtime network parser. The only selected reverse edge is faster-hex, whose actual std/alloc implementation uses alloc Vec/String instead; optional embedded logging and non-alloc containers are not invoked by this native composition. This focused automated review does not qualify every pool/queue/map API or the hash32 Murmur3 implementation." + +[[audits.hex-conservative]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "1.3.0" +notes = "Reviewed manifest/feature boundaries, fixed/variable decoding entries, complete hex iterators and their initialized-output handling, Char representation/table conversion and the bounded buffer encoder. Fixed-array decoding checks exactly N*2 bytes before writing; invalid/odd/nonhex input returns typed errors, and decoded Vec length is published only after initialization succeeds. Unchecked ASCII conversions originate from the closed ASCII Char variants. Allocating helpers scale with caller input, so they are not standalone network size limits. The native graph supplies bounded frames and fixed-size public hash values. This focused automated source review does not qualify unbounded formatting/collection by arbitrary callers or every optional feature." + +[[audits.lru]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.16.4 -> 0.18.5" +notes = "Reviewed the complete published release delta: the returned mutable reference now borrows the cache, pop detaches before a potentially panicking key destructor, allocation replaces unchecked NonNull construction, and sparse/find-and-promote/retain preserve list ownership. Reviewed the borrowed return and added retention, promotion and destructor regressions. Native SDK callers retain bounded capacities and immutable EventId keys. This delta inherits the existing 0.16.4 exemption; it is not an independent full-crate audit or a general unsafe-code proof." + [[audits.negentropy]] who = "Codex (automated single-owner source review)" criteria = "network-parser-reviewed" delta = "0.5.0 -> 0.5.2" notes = "Reviewed only the published 0.5.0 -> 0.5.2 delta: count admission before allocation, equivalent accumulator carry and duplicate ordering, private helper visibility and const additions. Protocol 0x61 and MIT licensing remain. Existing upstream regression tests execute separately. This automated single-owner delta review inherits the existing 0.5.0 baseline exemption; it does not claim independent/full-crate audit, complete parser canonicality or release qualification." + +[[audits.nostr]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.45.5" +notes = "Reviewed the native protocol path: relay-message sequence visitor and serializers, event intermediate decoding, exact-width kind, tag/vector admission, event ID computation, key/signature decoding and verification, JSON helpers, feature manifest and the paired registry-to-Git source changes. Original bounded wire fields must pass the Radroots canonical-length guard before the upstream fixed-array hex decoder can normalize them; real malformed-ID/ACK controls prove that composition. Tags remain raw nonempty string lists, not domain-policy or typed-NIP validation. Complete event verification is required through private client admission even when the SDK ID cache hits. Optional encrypted/remote-signer NIPs are disabled in the selected native graph. This focused automated native-use review does not qualify every NIP helper, unrestricted parser inputs, cryptographic algorithms independently or another platform." + +[[audits.nostr]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.45.5 -> 0.45.5@git:de3031aa99986eb29b3f34580355f9a099e4c927" +notes = "Reviewed the complete registry-to-Git Rust delta: stronger CryptoRng key-generation bounds and fixed-array conversions, key/tag conversion helpers, optional NIP47 BIP321 request/response codecs, removed infallible NIP66 accessors and relay-URL length admission. The native composition does not enable NIP47 or treat typed helper conversion as domain validation. The admitted bounded JSON/identity path retains the original-wire guard and per-event verification. This automated delta review does not qualify optional payment behavior, a browser runtime or unrestricted helper inputs." + +[[audits.nostr-database]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.45.2" +notes = "Reviewed all three production modules and the complete original manifest. The unsafe-forbidden crate now contains typed database traits, statuses, owned Arc conversions, errors and a query-to-negentropy default transformation; it owns no wire decoder, storage backend, socket, process or implicit filesystem access. Save assumes an already verified Event and default negentropy collection inherits the supplied backend query's bounds. The native SDK selects its separate 35,000-entry EventId-only LRU tracker; Radroots does not admit this trait as its durable product store. This automated source review is not qualification of arbitrary backend implementations or an unbounded query." + +[[audits.nostr-database]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.45.2 -> 0.45.2@git:de3031aa99986eb29b3f34580355f9a099e4c927" +notes = "Verified that every Rust source and the original package manifest are byte-identical to registry 0.45.2. The Git workspace resolves the reviewed exact private Nostr family and opaquerr dependency under the immutable source policy. No additional database implementation or parser is introduced by this Git delta." + +[[audits.nostr-gossip]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.45.1" +notes = "Reviewed all production modules and the manifest. This unsafe-forbidden crate contains backend traits, typed selection limits/statuses, deterministic ordering, URL category selection, bit flags and typed errors; it opens no network connection and owns no byte decoder or storage backend. A backend must enforce its own query and collection bounds. The native adapter leaves SDK gossip unconfigured and admits destinations only through its explicit hardened connector. The flag remove operation toggles bits and is not an idempotent clear; that optional API is not used by this composition. This automated native-use review does not qualify optional gossip discovery, arbitrary backend behavior or another platform." + +[[audits.nostr-gossip]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.45.1 -> 0.45.1@git:de3031aa99986eb29b3f34580355f9a099e4c927" +notes = "Reviewed the complete registry-to-Git Rust delta: default without_tls becomes false and its two documentation defaults follow that stricter choice. No parser, backend or connection authority is added. Radroots still uses no configured upstream gossip backend." + +[[audits.nostr-sdk]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.45.4" +notes = "Reviewed the selected native client/pool/relay ownership, WebSocket SPI, notification and subscription paths, relay-message admission, exact ACK/authentication flow, configured limits, tracker and verification-cache behavior. Radroots supplies one hardened connector, a finite configured relay inventory, bounded ingress and operation budgets, no gossip backend and no implicit authenticator. An ID-only verification cache cannot authenticate a later changed signature, so private per-event admission verifies the complete event; a real loopback regression failed before this correction. Ordinary notification streams lose receiver gaps and are not admitted: this baseline is paired with the exact Git gap/reconnect delta and with_gaps consumers. This focused automated native-use review does not qualify optional discovery/sync/local-relay APIs, upstream diagnostic redaction, an unbounded browser implementation or another platform." + +[[audits.nostr-sdk]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.45.4 -> 0.45.4@git:de3031aa99986eb29b3f34580355f9a099e4c927" +notes = "Reviewed the affected native connection driver and ownership, notification stream, persistent subscription/queued-REQ and admission changes against the registry source. Connection attempts retain identity and completion notification; stale generations cannot take current frames, queued request counters decrement on drop, and explicit disconnect revokes reconnect authority. The added with_gaps stream preserves each receiver's repeated loss and continuation instead of silently filtering it. Real Radroots socket controls cover repeated local gaps, single REQ after reconnect, configured deadlines, wire identity, explicit AUTH and retained receive limits; native source publication requires their executed results. This focused automated delta review is not an independent full-SDK audit or qualification of unused optional APIs, browser actors or diagnostic redaction." + +[[audits.opaquerr]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.2.0" +notes = "Reviewed both complete production modules and the manifest. The no_std error container and declarative macros construct typed kinds and optional boxed sources; they do not decode network input, invoke processes, access files or use unsafe code. Allocating construction caches the source Display text and Debug exposes the source, so this dependency supplies neither redaction nor an independent allocation limit. The native adapter maps upstream causes into its existing typed public errors, with ingress bounded before parsing. This is automated source review for that use, not a secret-handling or unrestricted-error-formatting claim." + +[[audits.r-efi]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "6.0.0" +notes = "Reviewed target/package boundaries, no_std module ownership, RNG protocol ABI and the getrandom UEFI caller. This reference crate supplies constants, ABI structures and unsafe firmware function pointers, not a network decoder or default startup routine. getrandom admits it only for target_os=uefi with the explicit efi_rng backend; its native caller is inactive in both selected macOS/Linux public graphs. This focused automated review admits that inactive target dependency and does not certify all firmware ABIs, a firmware provider, entropy quality or a UEFI artifact." + +[[audits.rand]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.10.1" +notes = "Reviewed the feature manifest, RNG extension interface and integer uniform sampling used by the selected SDK. Its actual features select std/alloc/sys_rng and the getrandom SysRng reexport; software StdRng/ThreadRng and their optional ChaCha generator are not selected. The SDK uses fixed ping bytes and a constant nonempty bounded reconnect-jitter range, with no network-controlled sampling range or collection allocation. Infallible wrappers can panic on OS entropy failure and invalid caller ranges; this review does not claim those are impossible. This focused automated native-use review is not a full distribution/cryptographic algorithm audit or another platform's runtime qualification." + +[[audits.rand_core]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.10.1" +notes = "Reviewed the TryRng/Rng and crypto-marker trait interfaces, dereference forwarding, complete UnwrapErr adapter and integer/byte conversion helpers used by SysRng. These interfaces own no network decoder or variable-length packet queue; they forward bounded caller buffers and propagate fallible completion. The explicit UnwrapErr conversion panics on provider errors, and a crypto marker alone proves no algorithm properties. The selected native SDK supplies fixed-size arrays and trusted sampling bounds. This focused automated source review does not qualify optional block/seed generators, arbitrary provider implementations or secret generation policy." + +[[audits.secp256k1]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.29.1 -> 0.30.0" +notes = "Reviewed the complete published Rust release delta and manifest. Fixed-array key/signature constructors reject incorrect slice lengths before FFI; recovery IDs become a closed checked enum. Schnorr signing and verification now pass an explicit message slice length through the existing C backend, and the admitted Nostr path supplies a fixed 32-byte event digest. Secret Debug output remains hidden or an explicit feature-gated fingerprint, and removed no-context parsing paths no longer panic unconditionally. The resolved secp256k1-sys 0.10.1 source is unchanged. This automated delta review inherits the existing 0.29.1 network-parser exemption and does not claim a fresh C/cryptographic algorithm audit, arbitrary-message signing policy, or another platform's runtime qualification." + +[[audits.syn]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "3.0.2" +notes = "Reviewed package/features, parser entry/completion checks and token-buffer construction/cursor ownership, tracing the optional defmt procedural-macro route. It parses compiler-owned token streams and rejects unconsumed tokens; recursive token construction and syntax trees do not supply a network-input allocation/depth limit. Version 3.0.2 is absent from both selected native public graphs, including build dependencies. This focused automated review admits that inactive compile-time dependency for the current composition; it does not audit every Rust grammar/unsafe cursor operation or qualify an untrusted runtime source parser." + +[[audits.tokio-happy-eyeballs]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.1.0" +notes = "Reviewed the entire unsafe-free source and manifest, including empty resolution, IPv4/IPv6 partitioning, the 250 ms race, error completion and remaining-address iteration. The helper extracts the prior Async WebSocket dialing algorithm; it owns no protocol decoder, filesystem or credential input. Its caller owns total dialing deadlines and destination admission. The configured Radroots native connector bypasses this default dialer and enforces its own bounded DNS/address/TLS admission and deadline. This automated native-use review does not qualify unrestricted default dialing or another platform." + +[[audits.tokio-stream]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.1.18" +notes = "Reviewed the manifest, module/feature boundaries, complete BroadcastStream and bounded/unbounded MPSC wrappers, and actual SDK call sites. The admitted SDK notification path uses BroadcastStream: each poll preserves Lagged(n), retains receiver ownership across future replacement and returns None on closure. It performs no byte decoding and owns no additional input queue. The SDK ordinary stream discards gaps, so the adapter uses only with_gaps; real socket overflow tests verify two receiver-local losses and continued retained delivery. The SDK local-relay feature is disabled. This focused automated native-use review builds on the existing same-version safe-to-deploy exemption; it does not qualify optional filesystem/signal/local-relay APIs, unbounded channels or every stream combinator." + +[[audits.tokio-tungstenite]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.26.2 -> 0.28.0" +notes = "Reviewed the complete published release delta: into-inner/get-ref accessors, boxed TLS error conversion, the matching Tungstenite parent and example error adaptation. The native connector retains its verified TLS roots, original server name, pinned addresses and bounded frame configuration; real socket, exact-byte, cancellation, authentication and deadline controls execute separately. This automated delta review inherits the existing 0.26.2 exemption and does not claim an independent full-crate audit or browser/runtime-platform qualification." + +[[audits.tokio-util]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.7.19" +notes = "Reviewed the manifest and complete ReusableBoxFuture, PollSender and PollSemaphore implementations, tracing the actual SDK BroadcastStream dependency. The selected helper owns one pinned Send future; replacement checks size and alignment before reuse, drops the old value in place, and restores ownership through its unwind guard. Sync exposes no shared access to the non-Sync future. PollSender retains typed ownership around its lifetime cast and reserves bounded channel permits; it is not the SDK notification reader. These helpers parse no network bytes. Current native receiver-local repeated-gap and closure tests exercise the admitted future replacement path. This focused automated review builds on the existing same-version safe-to-deploy exemption; it is not a full-crate unsafe-code proof or qualification of optional codecs, filesystem, task trackers or another platform." + +[[audits.tungstenite]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +delta = "0.26.2 -> 0.28.0" +notes = "Reviewed the complete published release delta: boxed errors, HTTP-version admission and duplicate-header handling, frame buffer reservation/read bounds, borrowed UTF-8 hashing, fragment/control/close state transitions and TLS error conversion. The native Radroots connector supplies fixed message/frame limits and metered ingress before this decoder. Real malformed, oversized, continuation, exact-byte and deadline cases execute separately. This automated delta review inherits the existing 0.26.2 exemption; it is not an independent full-crate audit, unbounded-input guarantee or 32-bit/browser qualification." + +[[audits.universal-time]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.3.1" +notes = "Reviewed all production source, feature gates and manifest: native std time uses a OnceLock monotonic origin and explicit wall-clock conversion; checked arithmetic returns Option and duration subtraction saturates, while duration operators can panic on overflow/underflow. There is no network parser or variable-sized input allocation. Native SDK connection durations come from validated adapter configuration, and packet timestamps do not supply Instant arithmetic. The no_std/WASM provider is a separately linked Sync static and is not admitted as the Radroots browser clock. This automated native-use source review is not independent audit, arbitrary-duration panic freedom or browser/no_std runtime qualification." + +[[audits.wasip3]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +notes = "Reviewed manifest, generated import/export module boundaries and the complete random-u64 host import selected by getrandom's WASIp3 backend. That backend fills bounded caller buffers with scalar imports and avoids the allocating random-byte-vector import. Generated imports depend on the trusted component host ABI; other host-pointer/vector/HTTP APIs are outside this use. The target_arch=wasm32, target_os=wasi, target_env=p3 dependency is absent from both selected native public graphs and is not the browser wasm32-unknown-unknown route. This automated review admits the inactive target dependency only, not a full generated-bindings audit, WASI host/entropy qualification or browser execution." + +[[audits.windows-sys]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.60.2" +notes = "Reviewed package/feature/dependency boundaries, the complete core types and string-literal helpers, and generated socket-FFI declarations. This crate supplies Windows constants, ABI layouts and unsafe foreign-function declarations rather than a Rust-owned network decoder. Literal conversion is used by compile-time string macros; it is not a Radroots network-text API. Actual macOS aarch64 and Linux x86_64 selected public feature graphs exclude this version. This automated review admits that inactive dependency in the current native composition; it does not audit every generated Windows API, grant raw pointer/buffer authority or qualify a Windows binary." + +[[audits.windows-targets]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.5" +notes = "Reviewed the complete source and target manifest. The no_std link macro emits external declarations and chooses the matching Windows import-library target or raw-dylib helper; it parses no network data and starts no runtime work. The current macOS/Linux public feature graphs exclude this version. This automated source review does not qualify Windows FFI callers, import-library execution or a Windows artifact." + +[[audits.windows_aarch64_gnullvm]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_aarch64_msvc]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_i686_gnu]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_i686_gnullvm]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_i686_msvc]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_x86_64_gnu]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_x86_64_gnullvm]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." + +[[audits.windows_x86_64_msvc]] +who = "Codex (automated single-owner source review)" +criteria = "network-parser-reviewed" +version = "0.53.1" +notes = "Reviewed the complete Rust source and build script, package metadata and target selection. Both Rust bodies are identical to 0.52.6: an empty no_std library and a build script that emits only a package-local lib search path; there are no dependencies or network parsers. Checksum-verified packaged import archives and their actual member lists were inventoried separately. The selected macOS/Linux public graphs exclude this target. This automated review admits the inactive package for that native composition, not a binary-code audit or Windows linking/runtime qualification." diff --git a/supply-chain/config.toml b/supply-chain/config.toml @@ -4,11 +4,20 @@ [cargo-vet] version = "0.10" +[policy."async-utility:0.3.2@git:b323ae0c1c1424b78bf8bb8832b95e86771f1dee"] +audit-as-crates-io = true + +[policy."nostr-database:0.45.2@git:de3031aa99986eb29b3f34580355f9a099e4c927"] +audit-as-crates-io = true + [policy."nostr-gossip:0.44.0"] [policy."nostr-gossip:0.44.0@git:5bba5163eb77107f82c4a8262cf29d7f33a73219"] audit-as-crates-io = true +[policy."nostr-gossip:0.45.1@git:de3031aa99986eb29b3f34580355f9a099e4c927"] +audit-as-crates-io = true + [policy.nostr-relay-builder] audit-as-crates-io = true @@ -17,11 +26,17 @@ audit-as-crates-io = true [policy."nostr-relay-pool:0.44.3"] +[policy."nostr-sdk:0.45.4@git:de3031aa99986eb29b3f34580355f9a099e4c927"] +audit-as-crates-io = true + [policy."nostr:0.44.1@git:5bba5163eb77107f82c4a8262cf29d7f33a73219"] audit-as-crates-io = true [policy."nostr:0.44.7"] +[policy."nostr:0.45.5@git:de3031aa99986eb29b3f34580355f9a099e4c927"] +audit-as-crates-io = true + [policy.radroots_identity] dependency-criteria = { k256 = "crypto-reviewed" } @@ -34,7 +49,6 @@ zeroize = "secret-handling-reviewed" [policy.radroots_transport_nostr.dependency-criteria] async-wsocket = "network-parser-reviewed" -nostr-relay-pool = "network-parser-reviewed" nostr-sdk = "network-parser-reviewed" tokio-tungstenite = "network-parser-reviewed" url = "network-parser-reviewed" diff --git a/tools/xtask/src/supply_chain_qualification.rs b/tools/xtask/src/supply_chain_qualification.rs @@ -127,16 +127,23 @@ fn validate(root: &Path, contract: &Contract, expected_packages: usize) -> Resul return Err("invalid supply-chain qualification contract".to_owned()); } - if contract.git_source.len() != 1 { - return Err("supply-chain contract requires exactly one retained Git source".to_owned()); - } - let source = &contract.git_source[0]; - if source.url != "https://github.com/rust-nostr/nostr.git" - || source.revision != "5bba5163eb77107f82c4a8262cf29d7f33a73219" - || source.packages != ["nostr", "nostr-relay-builder", "nostr-sdk"] - || source.removal_when != "nostr 0.45 stable satisfies Studio compatibility tests" - { - return Err("retained Git source authority drifted".to_owned()); + let mut sources = BTreeSet::new(); + for source in &contract.git_source { + let packages = source.packages.iter().collect::<BTreeSet<_>>(); + if !source.url.starts_with("https://github.com/") + || source.url.contains(['?', '#', '@']) + || source.revision.len() != 40 + || !source.revision.bytes().all(|byte| byte.is_ascii_hexdigit()) + || source.removal_when.is_empty() + || packages.is_empty() + || packages.len() != source.packages.len() + || packages.iter().any(|name| name.is_empty()) + || !sources.insert(&source.url) + { + return Err( + "Git source contract requires unique immutable sources and packages".to_owned(), + ); + } } let names = contract @@ -190,6 +197,12 @@ fn qualify_git_sources(root: &Path, contract: &Contract) -> Result<(), String> { .map(|source| (source.url.clone(), source.revision.clone())) .collect::<BTreeSet<_>>(); let mut seen = BTreeSet::new(); + let manifest = root.join("Cargo.toml"); + let raw = fs::read_to_string(&manifest) + .map_err(|error| format!("failed to read {}: {error}", manifest.display()))?; + let value = toml::from_str(&raw) + .map_err(|error| format!("failed to parse {}: {error}", manifest.display()))?; + inspect_git_dependencies(&value, &manifest, &approved, &mut seen)?; for entry in walkdir::WalkDir::new(root.join("crates")) { let entry = entry.map_err(|error| format!("failed to walk manifests: {error}"))?; if entry.file_name() != "Cargo.toml" { @@ -201,9 +214,6 @@ fn qualify_git_sources(root: &Path, contract: &Contract) -> Result<(), String> { .map_err(|error| format!("failed to parse {}: {error}", entry.path().display()))?; inspect_git_dependencies(&manifest, entry.path(), &approved, &mut seen)?; } - if seen != approved { - return Err("approved Git source set is stale or incomplete".to_owned()); - } let deny_raw = fs::read_to_string(root.join(DENY_PATH)) .map_err(|error| format!("failed to read {DENY_PATH}: {error}"))?; let deny = toml::from_str::<toml::Value>(&deny_raw) @@ -226,14 +236,27 @@ fn qualify_git_sources(root: &Path, contract: &Contract) -> Result<(), String> { "cargo-deny Git source authority differs from the exact-revision policy".to_owned(), ); } - let lock = fs::read_to_string(root.join("Cargo.lock")) + let raw = fs::read_to_string(root.join("Cargo.lock")) .map_err(|error| format!("failed to read Cargo.lock: {error}"))?; - for source in lock.lines().filter_map(|line| { - line.trim() - .strip_prefix("source = \"") - .and_then(|value| value.strip_suffix('"')) + qualify_locked_git_sources(&raw, &contract.git_source) +} + +fn qualify_locked_git_sources(raw: &str, approved: &[GitSource]) -> Result<(), String> { + let lock: toml::Value = + toml::from_str(raw).map_err(|error| format!("failed to parse Cargo.lock: {error}"))?; + let packages = lock + .get("package") + .and_then(toml::Value::as_array) + .ok_or_else(|| "Cargo.lock contains no packages".to_owned())?; + let mut seen = BTreeMap::<&str, BTreeSet<&str>>::new(); + for package in packages { + let Some(source) = package + .get("source") + .and_then(toml::Value::as_str) .filter(|value| value.starts_with("git+")) - }) { + else { + continue; + }; let (url_and_query, commit) = source .rsplit_once('#') .ok_or_else(|| format!("Git lock source has no commit: {source}"))?; @@ -241,11 +264,34 @@ fn qualify_git_sources(root: &Path, contract: &Contract) -> Result<(), String> { .strip_prefix("git+") .and_then(|value| value.split_once("?rev=")) .ok_or_else(|| format!("Git lock source is not exact-rev pinned: {source}"))?; - if commit != revision || !approved.contains(&(url.to_owned(), revision.to_owned())) { + let authority = approved + .iter() + .find(|entry| entry.url == url && entry.revision == revision); + let name = package + .get("name") + .and_then(toml::Value::as_str) + .ok_or_else(|| "locked Git package has no name".to_owned())?; + if commit != revision + || authority.is_none_or(|entry| !entry.packages.iter().any(|p| p == name)) + { return Err(format!( "Git lock source is not approved and immutable: {source}" )); } + seen.entry(url).or_default().insert(name); + } + for source in approved { + let expected = source + .packages + .iter() + .map(String::as_str) + .collect::<BTreeSet<_>>(); + if seen.get(source.url.as_str()) != Some(&expected) { + return Err(format!( + "approved Git package set is stale or incomplete: {}", + source.url + )); + } } Ok(()) } @@ -303,7 +349,6 @@ fn validate_exceptions(root: &Path, contract: &Contract) -> Result<(), String> { "CARGO-YANKED-SPIN-0.9.8".to_owned(), "RUSTSEC-2024-0384".to_owned(), "RUSTSEC-2024-0421".to_owned(), - "RUSTSEC-2026-0243".to_owned(), ]); let actual = contract .advisory_exception @@ -336,12 +381,6 @@ fn validate_exceptions(root: &Path, contract: &Contract) -> Result<(), String> { && exception.classification == "vulnerability" && exception.remove_when == "nostr >=0.45.0 stable" } - "RUSTSEC-2026-0243" => { - exception.package == "nostr-relay-pool" - && exception.affected_version == "0.44.3" - && exception.classification == "unmaintained" - && exception.remove_when == "nostr >=0.45.0 stable" - } "CARGO-YANKED-SPIN-0.9.8" => { exception.package == "spin" && exception.affected_version == "0.9.8" @@ -835,6 +874,56 @@ mod tests { let root = root(); let contract = load(&root).expect("contract"); validate(&root, &contract, 19).expect("valid contract"); + qualify_git_sources(&root, &contract).expect("exact direct and transitive Git sources"); + } + + #[test] + fn locked_git_sources_reject_substitution_unpinned_refs_and_missing_packages() { + let root = root(); + let contract = load(&root).unwrap(); + let raw = fs::read_to_string(root.join("Cargo.lock")).unwrap(); + qualify_locked_git_sources(&raw, &contract.git_source).unwrap(); + for replacement in [ + "git+https://github.com/unapproved/nostr?rev=de3031aa99986eb29b3f34580355f9a099e4c927#de3031aa99986eb29b3f34580355f9a099e4c927", + "git+https://github.com/nostrdevkit/nostr.git?branch=master#de3031aa99986eb29b3f34580355f9a099e4c927", + "git+https://github.com/nostrdevkit/nostr.git?rev=de3031aa99986eb29b3f34580355f9a099e4c927#0000000000000000000000000000000000000000", + ] { + let mut lock: toml::Value = toml::from_str(&raw).unwrap(); + let package = lock["package"] + .as_array_mut() + .unwrap() + .iter_mut() + .find(|p| p["name"].as_str() == Some("nostr-sdk")) + .unwrap(); + package["source"] = replacement.into(); + assert!( + qualify_locked_git_sources(&toml::to_string(&lock).unwrap(), &contract.git_source) + .is_err() + ); + } + for name in ["async-utility", "nostr-sdk"] { + let mut lock: toml::Value = toml::from_str(&raw).unwrap(); + lock["package"] + .as_array_mut() + .unwrap() + .retain(|p| p["name"].as_str() != Some(name)); + assert!( + qualify_locked_git_sources(&toml::to_string(&lock).unwrap(), &contract.git_source) + .is_err() + ); + } + let mut lock: toml::Value = toml::from_str(&raw).unwrap(); + let package = lock["package"] + .as_array_mut() + .unwrap() + .iter_mut() + .find(|p| p["name"].as_str() == Some("async-utility")) + .unwrap(); + package["name"] = "unapproved-package".into(); + assert!( + qualify_locked_git_sources(&toml::to_string(&lock).unwrap(), &contract.git_source) + .is_err() + ); } #[test]