Files
buzz/desktop/src-tauri/src/egress_guard_tests.rs
Salman Mohammed f99807a79a fix(desktop): skip a starter channel the relay will never create (#8911)
## Problem

On first run, the desktop app creates the `general` and
`welcome-everyone` starter channels at fixed ids when it can't find a
live one. Some refusals of that create are permanent. Any refusal
aborted setup **before it joined the new member to the starters that do
exist**:

| Starter state in the community | Relay reply to the create | Before |
|---|---|---|
| Another listed channel holds the name, archived ones included (with
#8782) | 400 `invalid: an open channel named "…" already exists` | setup
fails, not joined to `general` |
| An archived channel holds the fixed id | 400 `invalid: channel is
archived` (the relay checks this before duplicates) | setup fails |
| The channel at the fixed id was renamed or retyped | `duplicate` |
waits, then `starter channels created but metadata not yet available` |
| The channel at the fixed id was deleted | `duplicate`, and no metadata
is ever served | same |

Onboarding itself continues, because public starter failures are already
caught in `initializeStarterChannels`. The new member just never gets
joined to the other starter. With #8782 this happens to every new
desktop user in a community whose `welcome-everyone` or `general` is
archived.

## Fix

`ensure_starter_channels_inner` now skips a starter it can never create
or use, clears its pending-owner mark, and still joins the rest. Before
skipping after a permanent refusal, it reads the open directory again,
from the writer rather than a replica, once for all refused starters: a
live starter created after the first read is joined instead of skipped.
Any other refusal still fails setup. On the TS side,
`ensureStarterChannels` reports a skipped starter as `null` instead of
throwing. No caller reads those fields.

## Testing

- New tests run `ensure_starter_channels_inner` against a stub relay
that sends the relay's real reply shapes, one per row above, plus one
checking that other refusals still fail, and one where a live
`welcome-everyone` is listed only by writer-pinned reads (a lagging
replica) and gets joined. Each test fails when its guard is removed.
- Ran the real starter-setup code as a new user against a local relay
built from #8782, one community per row:
  - On `main`, rows 1, 2 and 4 fail.
- On this branch, all four join `general`, and each community still has
exactly one `welcome-everyone`.
  - A rerun is a no-op.
- `cargo test` (desktop Tauri, full), clippy, fmt, desktop `pnpm test`,
`tsc`, and biome all pass.

Related: #8782 (relay refuses taken names), #8807 (the same fix for
mobile). The archived-id case returns `channel is archived`, not
`duplicate`, so mobile needs to handle that reply too.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: opus <f038cc09b32a776e7d95b65c267e9d851777ee2b6cdbd46a48edba01b1d10ebd@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 12:59:28 -05:00

512 lines
20 KiB
Rust

use super::*;
/// NIP-49 spec vector — a real ncryptsec blob for injection payloads.
const NCRYPTSEC: &str = "ncryptsec1qgg9947rlpvqu76pj5ecreduf9jxhselq2nae2kghhvd5g7dgjtcxfqtd67p9m0w57lspw8gsq6yphnm8623nsl8xn9j4jdzz84zm3frztj3z7s35vpzmqf6ksu8r89qk5z2zxfmu5gv8th8wclt0h4p";
fn assert_guard_error(err: &str) {
assert!(
err.contains("key-backup material"),
"expected the egress-guard error, got: {err}"
);
}
// ── Guard unit behavior ───────────────────────────────────────────────────────
#[test]
fn rejects_ncryptsec_anywhere_in_text() {
assert_guard_error(&assert_no_key_backup(NCRYPTSEC, "test").unwrap_err());
assert_guard_error(
&assert_no_key_backup(
&format!("{{\"content\":\"my backup: {NCRYPTSEC}\"}}"),
"test",
)
.unwrap_err(),
);
}
/// Bech32 permits an all-uppercase encoding of the same payload — an
/// uppercased valid backup must not bypass the guard (text and bytes).
/// Mixed case is invalid bech32 (cannot decode) and is deliberately not
/// blocked.
#[test]
fn rejects_uppercase_ncryptsec() {
let upper = NCRYPTSEC.to_ascii_uppercase();
assert_guard_error(&assert_no_key_backup(&upper, "test").unwrap_err());
assert_guard_error(&assert_no_key_backup_bytes(upper.as_bytes(), "test").unwrap_err());
// Mixed case cannot decode; not blocked.
assert!(assert_no_key_backup("nCrYpTsEc1qgg9947r", "test").is_ok());
}
#[test]
fn passes_clean_payloads_including_raw_nsec() {
assert!(assert_no_key_backup("hello world", "test").is_ok());
assert!(assert_no_key_backup("", "test").is_ok());
// Scope is ncryptsec1 ONLY: raw nsec intentionally transits the encrypted
// pairing session and must NOT be blocked (plan D4 / pairing.rs).
let nsec = nostr::ToBech32::to_bech32(nostr::Keys::generate().secret_key()).unwrap();
assert!(assert_no_key_backup(&nsec, "test").is_ok());
// Near-miss prefixes are not blocked.
assert!(assert_no_key_backup("ncryptsec", "test").is_ok());
}
#[test]
fn byte_variant_matches_text_variant() {
assert_guard_error(&assert_no_key_backup_bytes(NCRYPTSEC.as_bytes(), "test").unwrap_err());
assert!(assert_no_key_backup_bytes(b"clean body", "test").is_ok());
// Invalid UTF-8 around an intact ncryptsec substring must still trip the
// guard (from_utf8_lossy preserves the ASCII run).
let mut body = vec![0xff, 0xfe];
body.extend_from_slice(NCRYPTSEC.as_bytes());
body.push(0xff);
assert_guard_error(&assert_no_key_backup_bytes(&body, "test").unwrap_err());
}
#[test]
fn error_names_the_boundary_context() {
let err = assert_no_key_backup(NCRYPTSEC, "huddle STT publish").unwrap_err();
assert!(err.contains("huddle STT publish"), "{err}");
}
// ── Runtime injection per boundary ────────────────────────────────────────────
//
// Each test drives the real production function with an ncryptsec-bearing
// payload and asserts the guard aborts the operation before any network I/O
// (no listener exists at the target address; a distinctive guard error — not
// a connection error — proves the abort happened first).
//
// Boundaries 6 and 7 (`submit_engram_event` twins) are module-private inside
// `commands`; their injection tests live next to them:
// - commands/team_snapshot/tests.rs::egress_guard_boundary
// - commands/personas/snapshot/import.rs::egress_guard_tests
/// Boundary 1: `relay/submit.rs` `submit_event_at_with_keys` (the funnel for
/// all `submit_event*` variants).
#[tokio::test]
async fn boundary_submit_event_at_with_keys_blocks_ncryptsec() {
let state = crate::app_state::build_app_state();
let keys = nostr::Keys::generate();
let builder = nostr::EventBuilder::new(nostr::Kind::Custom(9), NCRYPTSEC);
let err = crate::relay::submit_event_at_with_keys(
builder,
&state,
"http://127.0.0.1:9", // discard port — must never be reached
&keys,
)
.await
.unwrap_err();
assert_guard_error(&err);
}
/// Boundary 2: `relay.rs` `sync_managed_agent_profile` (agent kind:0 profile).
#[tokio::test]
async fn boundary_sync_managed_agent_profile_blocks_ncryptsec() {
let state = crate::app_state::build_app_state();
let keys = nostr::Keys::generate();
let err = crate::relay::sync_managed_agent_profile(
&state,
"ws://127.0.0.1:9",
&keys,
&format!("agent {NCRYPTSEC}"),
None,
None,
None,
)
.await
.unwrap_err();
assert_guard_error(&err);
}
/// Boundary 3: `relay/submit.rs` `submit_signed_event_at_with_keys` — the
/// pre-signed entry into the boundary-1 funnel (main's submit refactor
/// replaced `relay.rs` `submit_signed_event` with this scoped form).
#[tokio::test]
async fn boundary_submit_signed_event_at_with_keys_blocks_ncryptsec() {
let state = crate::app_state::build_app_state();
let keys = nostr::Keys::generate();
let event = nostr::EventBuilder::new(nostr::Kind::Custom(9), NCRYPTSEC)
.sign_with_keys(&keys)
.unwrap();
let err = crate::relay::submit_signed_event_at_with_keys(
&event,
&state,
"http://127.0.0.1:9", // discard port — must never be reached
&keys,
)
.await
.unwrap_err();
assert_guard_error(&err);
}
/// Boundary 4: `relay.rs` `submit_signed_event_with_keys`.
#[tokio::test]
async fn boundary_submit_signed_event_with_keys_blocks_ncryptsec() {
let state = crate::app_state::build_app_state();
*state.relay_url_override.lock().unwrap() = Some("ws://127.0.0.1:9".to_string());
let keys = nostr::Keys::generate();
let event = nostr::EventBuilder::new(nostr::Kind::Custom(9), NCRYPTSEC)
.sign_with_keys(&keys)
.unwrap();
let err = crate::relay::submit_signed_event_with_keys(&event, &state, &keys, None)
.await
.unwrap_err();
assert_guard_error(&err);
}
/// Boundary 5: huddle STT publisher (`huddle/pipeline.rs`).
#[test]
fn boundary_huddle_stt_blocks_ncryptsec() {
let keys = nostr::Keys::generate();
let channel = uuid::Uuid::new_v4();
let builder = crate::events::build_message(
channel,
NCRYPTSEC,
None,
&[],
&[],
&[],
&[],
&[],
None,
&crate::relay::relay_api_base_url(),
)
.unwrap();
let err = crate::huddle::pipeline::sign_and_guard_stt_body(builder, &keys).unwrap_err();
assert_guard_error(&err);
// Clean transcripts pass through the same seam.
let builder = crate::events::build_message(
channel,
"hello huddle",
None,
&[],
&[],
&[],
&[],
&[],
None,
&crate::relay::relay_api_base_url(),
)
.unwrap();
assert!(crate::huddle::pipeline::sign_and_guard_stt_body(builder, &keys).is_ok());
}
/// Boundary 8: native websocket send loop — the single choke point for all
/// webview-originated relay websocket frames.
#[tokio::test]
async fn boundary_native_websocket_blocks_ncryptsec() {
let manager = crate::native_websocket::WebSocketManager::default();
// Text frame: guard fires before the connection lookup, so no connection
// is needed — and the error must be the guard's, not "not found".
let err = crate::native_websocket::send_message(
&manager,
1,
crate::native_websocket::WebSocketMessage::Text(format!(
"[\"EVENT\",{{\"content\":\"{NCRYPTSEC}\"}}]"
)),
)
.await
.unwrap_err();
assert_guard_error(&err);
// Binary frame variant.
let err = crate::native_websocket::send_message(
&manager,
1,
crate::native_websocket::WebSocketMessage::Binary(NCRYPTSEC.as_bytes().to_vec()),
)
.await
.unwrap_err();
assert_guard_error(&err);
// Clean frames fall through to normal handling ("connection not found"
// here — the guard did not reject them).
let err = crate::native_websocket::send_message(
&manager,
1,
crate::native_websocket::WebSocketMessage::Text("[\"REQ\",\"sub\",{}]".to_string()),
)
.await
.unwrap_err();
assert!(err.contains("not found"), "{err}");
}
// ── Structural tripwires ──────────────────────────────────────────────────────
fn src_rust_files() -> Vec<std::path::PathBuf> {
fn walk(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
walk(&path, out);
} else if path.extension().and_then(|e| e.to_str()) == Some("rs") {
out.push(path);
}
}
}
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut out = Vec::new();
walk(&root, &mut out);
out
}
/// Site-granular `/events` inventory: `(file suffix, expected non-comment
/// `/events` occurrences, expected guard call sites — full-path calls into
/// the egress-guard module)`.
///
/// Every entry pairs the URL-construction count with the guard-call count for
/// that file, so BOTH of these fail the scan (not just a brand-new file):
/// - adding an unguarded ninth `/events` site inside an already-listed file
/// (count goes up without a matching table update), and
/// - removing/refactoring away a guard call while its egress site remains.
///
/// Updating a row here is the deliberate act that must accompany wiring the
/// guard + adding an injection test for the new site.
const EVENTS_INVENTORY: &[(&str, usize, usize)] = &[
// Production egress boundaries (see egress_guard.rs table):
("src/relay.rs", 2, 2), // boundaries 2, 4
("src/relay/submit.rs", 1, 1), // boundaries 1 + 3 (shared funnel)
("src/huddle/pipeline.rs", 1, 1), // boundary 5
("src/commands/team_snapshot.rs", 1, 1), // boundary 6
("src/commands/personas/snapshot/import.rs", 2, 1), // boundary 7 + its in-file injection-test fixture URL
("src/native_websocket.rs", 0, 2), // boundary 8 (WS frames; no events URL)
// Test-only fixtures — no production egress, no guard:
("src/relay_admission.rs", 1, 0),
("src/native_relay_client_transport_tests.rs", 1, 0),
("src/archive/mod_tests.rs", 1, 0),
("src/relay/profile_avatar/tests.rs", 1, 0),
("src/managed_agents/persona_events/tests.rs", 1, 0),
("src/commands/team_snapshot/tests.rs", 1, 0),
// Mock-relay route in its in-file tests; production publish goes through
// the guarded boundary-1 funnel (`submit_signed_event_at_with_keys`).
("src/commands/personas/sharing.rs", 1, 0),
// Loopback submit relay in `identity_archive.rs`'s in-file regen tests;
// production archive/unarchive publish through the guarded boundary-1
// funnel via `submit_event`.
("src/commands/identity_archive.rs", 1, 0),
// Mock-relay routes in team-sharing tests (accept/reject stub +
// recording stub for the delete-then-share gate + gated recording stub for
// the two-flush serialization gate + stalling stub for the per-scope
// isolation and bounded-stall gates); same pattern as persona sharing
// above — production publish goes through the guarded boundary-1 funnel via
// the flush loop.
("src/commands/teams/sharing/tests.rs", 4, 0),
// Stub-relay route in the tombstone-flush gate tests; production flush
// publishes through the guarded boundary-1 funnel.
("src/commands/teams/pending/tests/gate.rs", 1, 0),
// Stub relays in the starter-channel creation tests: one accepts every
// event, the other refuses channel creates.
("src/commands/channels/starter_tests.rs", 2, 0),
// Admin API: every JSON body passes the guard in
// `helpers::send_admin_mutation` (injection tests in admin `direct_action_tests.rs`),
// and every admin API request URL passes it before signing or sending:
// `build_admin_mutation_request` and `fetch_admin_json` (injection test in
// admin `reads_tests.rs`), the connection probe before its first, unsigned
// request, and the feedback-attachment fetch before its sign-and-retry loop
// (injection tests in admin `mod_tests.rs`).
// The `/events/{id}/delete` route + its path test and the native delete
// test only construct URLs that flow into that guarded boundary. The
// bodyless `GET /events/{id}` preview read and its mock-relay test carry
// no key material.
("src/commands/admin/helpers.rs", 0, 3), // boundaries 9, 10, 11
("src/commands/admin/mod.rs", 0, 1), // boundary 12
("src/commands/admin/attachment.rs", 0, 1), // boundary 13
("src/commands/admin/routes.rs", 3, 0),
("src/commands/admin/reads_tests.rs", 1, 0),
("src/commands/admin/direct_action_tests.rs", 1, 0),
];
// Needles are assembled at runtime so this scan file itself contains no
// contiguous match and needs no self-referential inventory row.
fn events_needle() -> String {
["/ev", "ents"].concat()
}
fn guard_needle() -> String {
["egress_guard::", "assert_no_key_backup"].concat()
}
/// Pure scan core over `(relative path, content)` pairs. Returns violations;
/// empty means every file matches its inventory row exactly (files absent
/// from the table are expected to have zero `/events` sites and zero guard
/// calls).
fn events_inventory_violations(files: &[(String, String)]) -> Vec<String> {
let events = events_needle();
let guard = guard_needle();
let mut violations = Vec::new();
for (rel, content) in files {
let expected = EVENTS_INVENTORY
.iter()
.find(|(suffix, _, _)| rel.ends_with(suffix))
.map(|&(_, e, g)| (e, g))
.unwrap_or((0, 0));
let mut event_sites = Vec::new();
for (i, line) in content.lines().enumerate() {
if line.trim_start().starts_with("//") {
continue; // doc/comment mentions
}
if line.contains(&events) {
event_sites.push(format!(" {rel}:{}: {}", i + 1, line.trim()));
}
}
let guard_count = content.matches(&guard).count();
if (event_sites.len(), guard_count) != expected {
violations.push(format!(
"{rel}: found {} events-URL site(s) + {} guard call(s), inventory \
expects {} + {}. Sites found:\n{}",
event_sites.len(),
guard_count,
expected.0,
expected.1,
if event_sites.is_empty() {
" (none)".to_string()
} else {
event_sites.join("\n")
},
));
}
}
violations
}
fn read_src_files() -> Vec<(String, String)> {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
src_rust_files()
.into_iter()
.map(|path| {
let rel = path
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
let content = std::fs::read_to_string(&path).unwrap();
(rel, content)
})
.collect()
}
/// Inventory completeness: every `/events` URL-construction site in
/// `desktop/src-tauri/src` must match the site-granular inventory above. A
/// future ninth submission path — in a NEW file or an ALREADY-LISTED one —
/// fails this test until its guard is wired, its injection test exists, and
/// its inventory row is updated.
#[test]
fn events_url_inventory_is_fully_guarded() {
let violations = events_inventory_violations(&read_src_files());
assert!(
violations.is_empty(),
"events-URL egress inventory drift — wire crate::egress_guard, add an \
injection test, then update EVENTS_INVENTORY:\n{}",
violations.join("\n")
);
}
/// Mutation-style proof of the tripwire's guarantee: an unguarded ninth
/// `/events` site added to an already-inventoried file (relay.rs) is caught.
#[test]
fn inventory_scan_catches_new_site_in_allowlisted_file() {
let mut files = read_src_files();
let relay = files
.iter_mut()
.find(|(rel, _)| rel.ends_with("src/relay.rs"))
.expect("relay.rs must be in the scan set");
relay.1.push_str(&format!(
"\nfn sneaky_ninth_site(base: &str) -> String {{ format!(\"{{base}}{}\") }}\n",
events_needle()
));
let violations = events_inventory_violations(&files);
assert!(
violations.iter().any(|v| v.contains("src/relay.rs")),
"an unguarded ninth events-URL site in relay.rs must trip the scan: {violations:?}"
);
}
/// The pairing also fires in reverse: a guard call deleted while its egress
/// site remains is caught.
#[test]
fn inventory_scan_catches_removed_guard_call() {
let mut files = read_src_files();
let relay = files
.iter_mut()
.find(|(rel, _)| rel.ends_with("src/relay.rs"))
.expect("relay.rs must be in the scan set");
relay.1 = relay.1.replacen(&guard_needle(), "removed_guard", 1);
let violations = events_inventory_violations(&files);
assert!(
violations.iter().any(|v| v.contains("src/relay.rs")),
"a removed guard call in relay.rs must trip the scan: {violations:?}"
);
}
/// A brand-new file with an `/events` site (no inventory row) is caught.
#[test]
fn inventory_scan_catches_new_unlisted_file() {
let mut files = read_src_files();
files.push((
"src/brand_new_egress.rs".to_string(),
format!("let url = format!(\"{{}}{}\", base);", events_needle()),
));
let violations = events_inventory_violations(&files);
assert!(
violations
.iter()
.any(|v| v.contains("src/brand_new_egress.rs")),
"{violations:?}"
);
}
/// Source allowlist: NIP-49 material handling is confined to the identity /
/// backup / import / guard files. Anything else touching ncryptsec or the
/// nip49 codec is structural drift.
#[test]
fn ncryptsec_handling_is_confined_to_allowlisted_files() {
let allowlist: &[&str] = &[
"src/key_backup.rs",
"src/key_backup_tests.rs",
"src/egress_guard.rs",
"src/egress_guard_tests.rs",
"src/commands/identity.rs",
"src/commands/identity_key_backup_tests.rs",
"src/lib.rs", // module registration + invoke handler
// boundary wiring (guard call sites name the module, not the codec):
"src/relay.rs",
"src/relay/submit.rs",
"src/huddle/pipeline.rs",
"src/commands/team_snapshot.rs",
"src/commands/team_snapshot/tests.rs",
"src/commands/personas/snapshot/import.rs",
"src/native_websocket.rs",
"src/commands/admin/helpers.rs",
"src/commands/admin/direct_action_tests.rs",
"src/commands/admin/reads_tests.rs",
"src/commands/admin/mod.rs",
"src/commands/admin/mod_tests.rs",
"src/commands/admin/attachment.rs",
];
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let mut violations = Vec::new();
for path in src_rust_files() {
let rel = path
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
if allowlist.iter().any(|a| rel.ends_with(a)) {
continue;
}
let content = std::fs::read_to_string(&path).unwrap();
for needle in ["ncryptsec", "EncryptedSecretKey", "nip49"] {
if content.contains(needle) {
violations.push(format!("{rel}: contains {needle:?}"));
}
}
}
assert!(
violations.is_empty(),
"NIP-49 material outside allowlisted files:\n{}",
violations.join("\n")
);
}