Skip to content

Commit c540ec9

Browse files
jmecomnpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
andauthored
[codex] Prevent actor-tag UI impersonation (#1931)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
1 parent 73b885c commit c540ec9

26 files changed

Lines changed: 591 additions & 188 deletions

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,12 @@ fn count_e_tags(event: &Event) -> usize {
703703
.count()
704704
}
705705

706-
/// Extract the effective author of a stored event (handles relay-signed REST events).
706+
/// Extract the effective author of a stored event (handles workflow-generated and
707+
/// legacy relay-signed attributed events).
707708
pub(crate) fn effective_message_author(event: &Event, relay_pubkey: &nostr::PublicKey) -> Vec<u8> {
708709
if event.pubkey == *relay_pubkey {
709-
// Relay-signed REST event — real author in "actor" or "p" tag.
710+
// Workflow-generated or legacy relay-signed attributed event — real author
711+
// in "actor" or "p" tag.
710712
if let Some(hex) = event.tags.iter().find_map(|t| {
711713
if t.kind().to_string() == "actor" {
712714
t.content().map(|s| s.to_string())

‎desktop/src-tauri/src/commands/messages.rs‎

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
use nostr::{Event, EventId, Keys, PublicKey};
22
use tauri::{AppHandle, State};
33

4+
mod forum;
5+
6+
use forum::{forum_message_from_event, forum_reply_from_event};
7+
48
use crate::{
59
app_state::AppState,
610
events,
711
managed_agents::{find_managed_agent_mut, load_managed_agents, ManagedAgentRecord},
812
models::{
913
FeedItemInfo, FeedMeta, FeedResponse, FeedSections, ForumMessageInfo, ForumPostsResponse,
1014
ForumThreadReplyInfo, ForumThreadResponse, SearchResponse, SendChannelMessageResponse,
11-
ThreadRepliesResponse, ThreadSummary,
15+
ThreadRepliesResponse,
1216
},
1317
nostr_convert,
1418
relay::{query_relay, submit_event, submit_event_with_keys},
@@ -1017,65 +1021,3 @@ fn feed_item_from_event(ev: &nostr::Event, category: &str) -> FeedItemInfo {
10171021
category: category.to_string(),
10181022
}
10191023
}
1020-
1021-
fn forum_message_from_event(ev: &nostr::Event, channel_id: &str) -> ForumMessageInfo {
1022-
ForumMessageInfo {
1023-
event_id: ev.id.to_hex(),
1024-
pubkey: ev.pubkey.to_hex(),
1025-
content: ev.content.clone(),
1026-
kind: ev.kind.as_u16() as u32,
1027-
created_at: ev.created_at.as_secs() as i64,
1028-
channel_id: channel_id.to_string(),
1029-
tags: tags_to_vec(ev),
1030-
thread_summary: Some(ThreadSummary {
1031-
reply_count: 0,
1032-
descendant_count: 0,
1033-
last_reply_at: None,
1034-
participants: Vec::new(),
1035-
}),
1036-
reactions: serde_json::Value::Null,
1037-
}
1038-
}
1039-
1040-
fn forum_reply_from_event(
1041-
ev: &nostr::Event,
1042-
channel_id: &str,
1043-
root_event_id: &str,
1044-
) -> ForumThreadReplyInfo {
1045-
// Walk e-tags for NIP-10 parent/root markers.
1046-
let (mut parent_id, mut explicit_root) = (None, None);
1047-
for t in ev.tags.iter() {
1048-
let s = t.as_slice();
1049-
if s.len() >= 2 && s[0] == "e" {
1050-
match s.get(3).map(|x| x.as_str()) {
1051-
Some("root") => explicit_root = Some(s[1].clone()),
1052-
Some("reply") => parent_id = Some(s[1].clone()),
1053-
_ => {
1054-
if parent_id.is_none() {
1055-
parent_id = Some(s[1].clone());
1056-
}
1057-
}
1058-
}
1059-
}
1060-
}
1061-
let parent = parent_id
1062-
.clone()
1063-
.unwrap_or_else(|| root_event_id.to_string());
1064-
let root = explicit_root.unwrap_or_else(|| root_event_id.to_string());
1065-
let depth = if parent == root { 1 } else { 2 };
1066-
1067-
ForumThreadReplyInfo {
1068-
event_id: ev.id.to_hex(),
1069-
pubkey: ev.pubkey.to_hex(),
1070-
content: ev.content.clone(),
1071-
kind: ev.kind.as_u16() as u32,
1072-
created_at: ev.created_at.as_secs() as i64,
1073-
channel_id: channel_id.to_string(),
1074-
tags: tags_to_vec(ev),
1075-
parent_event_id: Some(parent),
1076-
root_event_id: Some(root),
1077-
depth,
1078-
broadcast: false,
1079-
reactions: serde_json::Value::Null,
1080-
}
1081-
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::models::{ForumMessageInfo, ForumThreadReplyInfo, ThreadSummary};
2+
3+
fn tags_to_vec(event: &nostr::Event) -> Vec<Vec<String>> {
4+
event
5+
.tags
6+
.iter()
7+
.map(|tag| tag.as_slice().to_vec())
8+
.collect()
9+
}
10+
11+
pub(super) fn forum_message_from_event(event: &nostr::Event, channel_id: &str) -> ForumMessageInfo {
12+
ForumMessageInfo {
13+
event_id: event.id.to_hex(),
14+
pubkey: event.pubkey.to_hex(),
15+
sig: event.sig.to_string(),
16+
content: event.content.clone(),
17+
kind: event.kind.as_u16() as u32,
18+
created_at: event.created_at.as_secs() as i64,
19+
channel_id: channel_id.to_string(),
20+
tags: tags_to_vec(event),
21+
thread_summary: Some(ThreadSummary {
22+
reply_count: 0,
23+
descendant_count: 0,
24+
last_reply_at: None,
25+
participants: Vec::new(),
26+
}),
27+
reactions: serde_json::Value::Null,
28+
}
29+
}
30+
31+
pub(super) fn forum_reply_from_event(
32+
event: &nostr::Event,
33+
channel_id: &str,
34+
root_event_id: &str,
35+
) -> ForumThreadReplyInfo {
36+
let (mut parent_id, mut explicit_root) = (None, None);
37+
for tag in event.tags.iter() {
38+
let values = tag.as_slice();
39+
if values.len() >= 2 && values[0] == "e" {
40+
match values.get(3).map(String::as_str) {
41+
Some("root") => explicit_root = Some(values[1].clone()),
42+
Some("reply") => parent_id = Some(values[1].clone()),
43+
_ if parent_id.is_none() => parent_id = Some(values[1].clone()),
44+
_ => {}
45+
}
46+
}
47+
}
48+
49+
let parent = parent_id
50+
.clone()
51+
.unwrap_or_else(|| root_event_id.to_string());
52+
let root = explicit_root.unwrap_or_else(|| root_event_id.to_string());
53+
let depth = if parent == root { 1 } else { 2 };
54+
55+
ForumThreadReplyInfo {
56+
event_id: event.id.to_hex(),
57+
pubkey: event.pubkey.to_hex(),
58+
sig: event.sig.to_string(),
59+
content: event.content.clone(),
60+
kind: event.kind.as_u16() as u32,
61+
created_at: event.created_at.as_secs() as i64,
62+
channel_id: channel_id.to_string(),
63+
tags: tags_to_vec(event),
64+
parent_event_id: Some(parent),
65+
root_event_id: Some(root),
66+
depth,
67+
broadcast: false,
68+
reactions: serde_json::Value::Null,
69+
}
70+
}

‎desktop/src-tauri/src/models.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ pub struct ThreadSummary {
251251
pub struct ForumMessageInfo {
252252
pub event_id: String,
253253
pub pubkey: String,
254+
pub sig: String,
254255
pub content: String,
255256
pub kind: u32,
256257
pub created_at: i64,
@@ -272,6 +273,7 @@ pub struct ForumPostsResponse {
272273
pub struct ForumThreadReplyInfo {
273274
pub event_id: String,
274275
pub pubkey: String,
276+
pub sig: String,
275277
pub content: String,
276278
pub kind: u32,
277279
pub created_at: i64,

‎desktop/src/features/channels/ui/ChannelScreen.tsx‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { cacheSearchHitEvent } from "@/app/navigation/searchHitEventCache";
44
import { useAppNavigation } from "@/app/navigation/useAppNavigation";
55
import { useActiveChannelHeader } from "@/features/channels/useActiveChannelHeader";
66
import { useChannelPaneHandlers } from "@/features/channels/useChannelPaneHandlers";
7+
import { useMessageEventProfilePubkeys } from "@/features/channels/useMessageEventProfilePubkeys";
78
import { useThreadTargetSync } from "@/features/channels/useThreadTargetSync";
89
import {
910
useChannelMembersQuery,
@@ -37,12 +38,7 @@ import {
3738
useSendMessageMutation,
3839
useToggleReactionMutation,
3940
} from "@/features/messages/hooks";
40-
import {
41-
collectMessageAuthorPubkeys,
42-
collectMessageMentionPubkeys,
43-
collectReactionActorPubkeys,
44-
formatTimelineMessages,
45-
} from "@/features/messages/lib/formatTimelineMessages";
41+
import { formatTimelineMessages } from "@/features/messages/lib/formatTimelineMessages";
4642
import {
4743
channelWindowThreadSummaries,
4844
type ChannelWindowThreadSummary,
@@ -59,6 +55,7 @@ import { useThreadReplies } from "@/features/messages/useThreadReplies";
5955
import { useChannelTyping } from "@/features/messages/useChannelTyping";
6056
import type { TimelineMessage } from "@/features/messages/types";
6157
import { useUsersBatchQuery } from "@/features/profile/hooks";
58+
import { useRelaySelfQuery } from "@/features/moderation/hooks";
6259
import type { RelayEvent, RespondToMode, SearchHit } from "@/shared/api/types";
6360
import { useChannelFind } from "@/features/search/useChannelFind";
6461
import { ViewLoadingFallback } from "@/shared/ui/ViewLoadingFallback";
@@ -158,6 +155,7 @@ export function ChannelScreen({
158155
const mainInsetRef = useMainInsetRef();
159156
const currentPubkey = currentIdentity?.pubkey;
160157
const activeChannelId = activeChannel?.id ?? null;
158+
const relaySelfPubkey = useRelaySelfQuery(activeChannel !== null).data;
161159
const effectiveOpenThreadHeadId =
162160
optimisticOpenThreadHeadId === undefined
163161
? openThreadHeadId
@@ -249,16 +247,11 @@ export function ChannelScreen({
249247
return extraEvents.reduce(mergeMessages, currentMessages);
250248
}, [activeChannel, findEvents, messagesQuery.data, targetMessageEvents]);
251249
const threadReplyEvents = threadRepliesQuery.data ?? EMPTY_RELAY_EVENTS;
252-
const messageEventProfilePubkeys = React.useMemo(() => {
253-
const events = [...resolvedMessages, ...threadReplyEvents];
254-
return [
255-
...new Set([
256-
...collectMessageAuthorPubkeys(events),
257-
...collectMessageMentionPubkeys(events),
258-
...collectReactionActorPubkeys(events),
259-
]),
260-
];
261-
}, [resolvedMessages, threadReplyEvents]);
250+
const messageEventProfilePubkeys = useMessageEventProfilePubkeys(
251+
resolvedMessages,
252+
threadReplyEvents,
253+
relaySelfPubkey,
254+
);
262255
const latestMessageEvent = React.useMemo(
263256
() => resolvedMessages[resolvedMessages.length - 1] ?? null,
264257
[resolvedMessages],
@@ -267,6 +260,7 @@ export function ChannelScreen({
267260
activeChannel,
268261
currentPubkey,
269262
latestMessageEvent,
263+
relaySelfPubkey,
270264
);
271265
const activeDmParticipantPubkeys = React.useMemo(
272266
() =>
@@ -396,6 +390,7 @@ export function ChannelScreen({
396390
channelMembers,
397391
personaLookup,
398392
respondToLookup,
393+
relaySelfPubkey,
399394
),
400395
[
401396
activeChannel,
@@ -404,6 +399,7 @@ export function ChannelScreen({
404399
currentPubkey,
405400
messageProfiles,
406401
personaLookup,
402+
relaySelfPubkey,
407403
respondToLookup,
408404
resolvedMessages,
409405
],
@@ -442,6 +438,7 @@ export function ChannelScreen({
442438
members: channelMembers,
443439
personaLookup,
444440
respondToLookup,
441+
relaySelfPubkey,
445442
});
446443
const {
447444
firstUnreadMessageId,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as React from "react";
2+
3+
import {
4+
collectMessageAuthorPubkeys,
5+
collectMessageMentionPubkeys,
6+
collectReactionActorPubkeys,
7+
} from "@/features/messages/lib/formatTimelineMessages";
8+
import type { RelayEvent } from "@/shared/api/types";
9+
10+
export function useMessageEventProfilePubkeys(
11+
messages: RelayEvent[],
12+
threadReplies: RelayEvent[],
13+
relaySelfPubkey: string | null | undefined,
14+
) {
15+
return React.useMemo(() => {
16+
const events = [...messages, ...threadReplies];
17+
return [
18+
...new Set([
19+
...collectMessageAuthorPubkeys(events, relaySelfPubkey),
20+
...collectMessageMentionPubkeys(events),
21+
...collectReactionActorPubkeys(events, relaySelfPubkey),
22+
]),
23+
];
24+
}, [messages, relaySelfPubkey, threadReplies]);
25+
}

‎desktop/src/features/forum/hooks.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
22

33
import { getForumPosts, getForumThread } from "@/shared/api/forum";
4+
import { useRelaySelfQuery } from "@/features/moderation/hooks";
45
import { deleteMessage, sendChannelMessage } from "@/shared/api/tauri";
56
import type {
67
Channel,
@@ -19,11 +20,13 @@ export function forumThreadQueryKey(channelId: string, eventId: string) {
1920

2021
export function useForumPostsQuery(channel: Channel | null) {
2122
const channelId = channel?.id ?? "";
23+
const enabled = channel !== null && channel.channelType === "forum";
24+
const relaySelfPubkey = useRelaySelfQuery(enabled).data;
2225

2326
return useQuery<ForumPostsResponse>({
24-
enabled: channel !== null && channel.channelType === "forum",
25-
queryKey: forumPostsQueryKey(channelId),
26-
queryFn: () => getForumPosts(channelId, 50),
27+
enabled,
28+
queryKey: [...forumPostsQueryKey(channelId), relaySelfPubkey ?? null],
29+
queryFn: () => getForumPosts(channelId, 50, undefined, relaySelfPubkey),
2730
staleTime: 15_000,
2831
refetchInterval: 15_000,
2932
});
@@ -33,10 +36,23 @@ export function useForumThreadQuery(
3336
channelId: string | null,
3437
eventId: string | null,
3538
) {
39+
const enabled = channelId !== null && eventId !== null;
40+
const relaySelfPubkey = useRelaySelfQuery(enabled).data;
41+
3642
return useQuery<ForumThreadResponse>({
37-
enabled: channelId !== null && eventId !== null,
38-
queryKey: forumThreadQueryKey(channelId ?? "", eventId ?? ""),
39-
queryFn: () => getForumThread(channelId ?? "", eventId ?? ""),
43+
enabled,
44+
queryKey: [
45+
...forumThreadQueryKey(channelId ?? "", eventId ?? ""),
46+
relaySelfPubkey ?? null,
47+
],
48+
queryFn: () =>
49+
getForumThread(
50+
channelId ?? "",
51+
eventId ?? "",
52+
undefined,
53+
undefined,
54+
relaySelfPubkey,
55+
),
4056
staleTime: 10_000,
4157
refetchInterval: 10_000,
4258
});

‎desktop/src/features/home/lib/inbox.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export type InboxReply = {
7272
reactions?: TimelineReaction[];
7373
rootId?: string | null;
7474
/**
75-
* Raw event signer, NOT the tag-attributed display author
76-
* (`authorPubkey`). The config-nudge trust gate authenticates against
77-
* this field so `actor`/`p`-tag spoofing can't enable the card.
75+
* Raw event signer, not a relay-delegated display author (`authorPubkey`).
76+
* The config-nudge trust gate authenticates against this field because only
77+
* the signing agent may enable the card.
7878
*/
7979
signerPubkey?: string;
8080
tags?: string[][];

‎desktop/src/features/home/lib/inboxViewHelpers.test.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ test("inbox mappings preserve kind and signerPubkey so the nudge gate can pass",
317317
);
318318
});
319319

320-
test("inbox mappings keep the raw signer distinct from a spoofed actor tag", () => {
320+
test("inbox mappings ignore a spoofed actor tag", () => {
321321
const message = mapThroughInboxPipeline(
322322
makeNudgeEvent({
323323
pubkey: NUDGE_HUMAN_SIGNER,
@@ -328,9 +328,9 @@ test("inbox mappings keep the raw signer distinct from a spoofed actor tag", ()
328328
}),
329329
);
330330

331-
// The attributed display author resolves to the agent (actor tag), but the
332-
// signer stays human — the gate must reject.
333-
assert.equal(message.pubkey?.toLowerCase(), NUDGE_AGENT_SIGNER);
331+
// An untrusted actor tag cannot replace the display author. The signer also
332+
// stays human, so the config-nudge gate rejects the message.
333+
assert.equal(message.pubkey?.toLowerCase(), NUDGE_HUMAN_SIGNER);
334334
assert.equal(message.signerPubkey, NUDGE_HUMAN_SIGNER);
335335
assert.equal(
336336
getConfigNudgeAuthorPubkey(

0 commit comments

Comments
 (0)