|
| 1 | +//! Tombstone-aware ownership recovery through the authenticated Nostr bridge. |
| 2 | +//! Run against a local development relay with `--ignored`. |
| 3 | +
|
| 4 | +use nostr::{Event, EventBuilder, Keys, Kind, Tag}; |
| 5 | +use serde_json::{json, Value}; |
| 6 | + |
| 7 | +fn url() -> String { |
| 8 | + std::env::var("RELAY_URL") |
| 9 | + .unwrap_or_else(|_| "ws://localhost:3000".into()) |
| 10 | + .replace("ws://", "http://") |
| 11 | + .replace("wss://", "https://") |
| 12 | +} |
| 13 | + |
| 14 | +async fn post(keys: &Keys, event: &Event) { |
| 15 | + let response = reqwest::Client::new() |
| 16 | + .post(format!("{}/events", url())) |
| 17 | + .header("X-Pubkey", keys.public_key().to_hex()) |
| 18 | + .json(event) |
| 19 | + .send() |
| 20 | + .await |
| 21 | + .expect("event request"); |
| 22 | + assert!( |
| 23 | + response.status().is_success(), |
| 24 | + "event status {}", |
| 25 | + response.status() |
| 26 | + ); |
| 27 | + let body: Value = response.json().await.expect("event response"); |
| 28 | + assert_eq!(body["accepted"], true, "event rejected: {body}"); |
| 29 | +} |
| 30 | + |
| 31 | +async fn query(keys: &Keys, filter: Value) -> Vec<Event> { |
| 32 | + let response = reqwest::Client::new() |
| 33 | + .post(format!("{}/query", url())) |
| 34 | + .header("X-Pubkey", keys.public_key().to_hex()) |
| 35 | + .json(&vec![filter]) |
| 36 | + .send() |
| 37 | + .await |
| 38 | + .expect("query request"); |
| 39 | + assert!( |
| 40 | + response.status().is_success(), |
| 41 | + "query status {}", |
| 42 | + response.status() |
| 43 | + ); |
| 44 | + response.json().await.expect("signed event array") |
| 45 | +} |
| 46 | + |
| 47 | +async fn channel(keys: &Keys, private: bool) -> String { |
| 48 | + let id = uuid::Uuid::new_v4().to_string(); |
| 49 | + let event = EventBuilder::new(Kind::Custom(9007), "") |
| 50 | + .tags([ |
| 51 | + Tag::parse(["h", &id]).unwrap(), |
| 52 | + Tag::parse(["name", &format!("thread-roots-{id}")]).unwrap(), |
| 53 | + Tag::parse(["channel_type", "stream"]).unwrap(), |
| 54 | + Tag::parse(["visibility", if private { "private" } else { "open" }]).unwrap(), |
| 55 | + ]) |
| 56 | + .sign_with_keys(keys) |
| 57 | + .unwrap(); |
| 58 | + post(keys, &event).await; |
| 59 | + id |
| 60 | +} |
| 61 | + |
| 62 | +fn request(channel: &str, target: &Event) -> Value { |
| 63 | + json!({"kinds": [39005], "ids": [target.id.to_hex()], "#h": [channel], "resolve_thread_roots": true}) |
| 64 | +} |
| 65 | + |
| 66 | +#[tokio::test] |
| 67 | +#[ignore] |
| 68 | +async fn deleted_reply_roots_are_recovered_without_content_or_scope_leaks() { |
| 69 | + let owner = Keys::generate(); |
| 70 | + let outsider = Keys::generate(); |
| 71 | + let other_channel = channel(&owner, false).await; |
| 72 | + let bounded_filter = json!({ |
| 73 | + "kinds": [39005], "#h": [&other_channel], "resolve_thread_roots": true, |
| 74 | + "ids": (0..60).map(|id| format!("{id:064x}")).collect::<Vec<_>>() |
| 75 | + }); |
| 76 | + let oversized = reqwest::Client::new() |
| 77 | + .post(format!("{}/query", url())) |
| 78 | + .header("X-Pubkey", owner.public_key().to_hex()) |
| 79 | + .json(&vec![bounded_filter.clone(), bounded_filter]) |
| 80 | + .send() |
| 81 | + .await |
| 82 | + .expect("bounded batch"); |
| 83 | + assert_eq!( |
| 84 | + oversized.status(), |
| 85 | + reqwest::StatusCode::BAD_REQUEST, |
| 86 | + "the target cap applies across filters, not just within each filter" |
| 87 | + ); |
| 88 | + for deletion_kind in [5, 9005] { |
| 89 | + let channel_id = channel(&owner, true).await; |
| 90 | + let root = EventBuilder::new(Kind::Custom(9), "root") |
| 91 | + .tags([Tag::parse(["h", &channel_id]).unwrap()]) |
| 92 | + .sign_with_keys(&owner) |
| 93 | + .unwrap(); |
| 94 | + post(&owner, &root).await; |
| 95 | + let reply = EventBuilder::new(Kind::Custom(9), "deleted secret payload") |
| 96 | + .tags([ |
| 97 | + Tag::parse(["h", &channel_id]).unwrap(), |
| 98 | + Tag::parse(["e", &root.id.to_hex(), "", "reply"]).unwrap(), |
| 99 | + ]) |
| 100 | + .sign_with_keys(&owner) |
| 101 | + .unwrap(); |
| 102 | + post(&owner, &reply).await; |
| 103 | + let deletion = EventBuilder::new(Kind::Custom(deletion_kind), "") |
| 104 | + .tags([ |
| 105 | + Tag::parse(["h", &channel_id]).unwrap(), |
| 106 | + Tag::parse(["e", &reply.id.to_hex()]).unwrap(), |
| 107 | + ]) |
| 108 | + .sign_with_keys(&owner) |
| 109 | + .unwrap(); |
| 110 | + post(&owner, &deletion).await; |
| 111 | + |
| 112 | + let ordinary = query( |
| 113 | + &owner, |
| 114 | + json!({"kinds":[9],"ids":[reply.id.to_hex()],"#h":[channel_id]}), |
| 115 | + ) |
| 116 | + .await; |
| 117 | + assert!( |
| 118 | + ordinary.is_empty(), |
| 119 | + "ordinary history must exclude the deleted payload" |
| 120 | + ); |
| 121 | + let summaries = query(&owner, request(&channel_id, &reply)).await; |
| 122 | + assert_eq!(summaries.len(), 1, "metadata must survive deletion"); |
| 123 | + let summary = &summaries[0]; |
| 124 | + summary.verify().expect("relay signature"); |
| 125 | + assert_eq!(summary.kind, Kind::Custom(39005)); |
| 126 | + assert!(summary |
| 127 | + .tags |
| 128 | + .iter() |
| 129 | + .any(|tag| tag.as_slice() == ["e", &root.id.to_hex()])); |
| 130 | + let counts: Value = serde_json::from_str(&summary.content).unwrap(); |
| 131 | + assert_eq!(counts["reply_count"], 0); |
| 132 | + assert_eq!(counts["descendant_count"], 0); |
| 133 | + assert!(!summary.content.contains("deleted secret payload")); |
| 134 | + |
| 135 | + assert!( |
| 136 | + query(&owner, request(&other_channel, &reply)) |
| 137 | + .await |
| 138 | + .is_empty(), |
| 139 | + "wrong channel must not leak ownership" |
| 140 | + ); |
| 141 | + assert!( |
| 142 | + query(&outsider, request(&channel_id, &reply)) |
| 143 | + .await |
| 144 | + .is_empty(), |
| 145 | + "private channel membership is required" |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments