Skip to content

Commit 0b5525f

Browse files
matt2eclaude
andcommitted
fix(desktop): remove the dead community API token plumbing
applyCommunity sent a `token` arg that the Rust apply_workspace command stopped declaring when the relay moved to pure Nostr key auth ("Sprout speaks Nostr. Nothing else.", #475). Tauri silently drops invoke args the command doesn't declare, so the Community.token collected by the add/edit dialogs was persisted to localStorage and never applied anywhere — a secret-shaped field with no consumer. Resolve the arg mismatch by deleting the plumbing rather than re-accepting the token: - Stop sending `token` from applyCommunity and align its invoke args with the Rust signature (relayUrl, nsec, reposDir). - Remove the API Token field from the Add/Edit Community dialogs, and drop token handling from useCommunities (update-result matrix, dup-merge on add, reinit trigger) and the sidebar/switcher prop types. - Retire Community.token to a `@deprecated token?: never` marker and strip the stale secret from persisted entries in loadCommunities (extending the existing nsec-strip migration), so it cannot leak from localStorage into future sessions. - Regression tests: applyCommunity must send exactly the arg keys apply_workspace declares (guards the silent-drop IPC contract), and loadCommunities strips token/nsec and persists the cleaned list. Tested: desktop pnpm test (2941 passed, incl. the 2 new regression tests), tsc --noEmit, biome check. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 695dfe0 commit 0b5525f

15 files changed

Lines changed: 148 additions & 80 deletions

‎desktop/src/app/App.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ export function App() {
379379
useNestNotifications();
380380

381381
// Composite key: changes when community ID changes OR when
382-
// the active community's config is updated (relayUrl/token).
382+
// the active community's config is updated (relayUrl/reposDir).
383383
const communityKey = `${activeCommunity?.id ?? "none"}-${reinitKey}`;
384384

385385
// Latch once the community key deviates from its cold-boot value: from then

‎desktop/src/features/communities/communityStorage.test.mjs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from "node:test";
33

44
import {
55
clearCommunityStorage,
6+
loadCommunities,
67
migrateLegacyCommunityStorage,
78
} from "./communityStorage.ts";
89

@@ -59,3 +60,61 @@ test("clearCommunityStorage removes new and legacy state", () => {
5960

6061
assert.equal(storage.length, 0);
6162
});
63+
64+
test("loadCommunities strips legacy nsec and token secrets and persists the cleaned list", () => {
65+
// `token` was a relay API token sent to an `apply_workspace` arg the Rust
66+
// command no longer declares — Tauri silently dropped it, so the secret sat
67+
// unused in localStorage. `nsec` was superseded by the on-disk identity.key.
68+
const storage = createMemoryStorage({
69+
"buzz-communities": JSON.stringify([
70+
{
71+
id: "ws-1",
72+
name: "Community A",
73+
relayUrl: "wss://relay-a.example.com",
74+
token: "buzz_legacy-secret",
75+
addedAt: "2024-01-01",
76+
},
77+
{
78+
id: "ws-2",
79+
name: "Community B",
80+
relayUrl: "wss://relay-b.example.com",
81+
nsec: "nsec1legacysecret",
82+
addedAt: "2024-01-02",
83+
},
84+
{
85+
id: "ws-3",
86+
name: "Community C",
87+
relayUrl: "wss://relay-c.example.com",
88+
addedAt: "2024-01-03",
89+
},
90+
]),
91+
});
92+
// loadCommunities reads the real localStorage global (and writes back via
93+
// window.localStorage), so point both at the in-memory store for the test.
94+
globalThis.localStorage = storage;
95+
const hadWindow = "window" in globalThis;
96+
const previousWindow = globalThis.window;
97+
globalThis.window = { localStorage: storage };
98+
try {
99+
const communities = loadCommunities();
100+
101+
assert.equal(communities.length, 3);
102+
for (const community of communities) {
103+
assert.equal("token" in community, false);
104+
assert.equal("nsec" in community, false);
105+
}
106+
// The cleaned list is persisted back so the secrets cannot leak into
107+
// future sessions.
108+
const persisted = storage.getItem("buzz-communities");
109+
assert.equal(persisted.includes("token"), false);
110+
assert.equal(persisted.includes("nsec"), false);
111+
assert.equal(persisted.includes("relay-b.example.com"), true);
112+
} finally {
113+
delete globalThis.localStorage;
114+
if (hadWindow) {
115+
globalThis.window = previousWindow;
116+
} else {
117+
delete globalThis.window;
118+
}
119+
}
120+
});

‎desktop/src/features/communities/communityStorage.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,24 @@ export function loadCommunities(): Community[] {
5757
if (!Array.isArray(parsed)) {
5858
return [];
5959
}
60-
// Migration: older builds stored the user's `nsec` in localStorage and
61-
// re-applied it to the backend on every reload, which silently overwrote
62-
// any `import_identity` result with the original generated key. The
63-
// on-disk `identity.key` file is the only source of truth now. Strip
64-
// any lingering `nsec` from existing entries on read and persist the
65-
// cleaned list back so it cannot leak into future sessions.
60+
// Migration: strip secrets older builds persisted but nothing reads.
61+
// - `nsec`: older builds stored the user's nsec in localStorage and
62+
// re-applied it to the backend on every reload, which silently overwrote
63+
// any `import_identity` result with the original generated key. The
64+
// on-disk `identity.key` file is the only source of truth now.
65+
// - `token`: a relay API token from before the relay moved to pure Nostr
66+
// key auth (NIP-42/NIP-98). The backend command that once consumed it
67+
// stopped accepting it, so the secret sat unused in localStorage.
68+
// Strip both from existing entries on read and persist the cleaned list
69+
// back so they cannot leak into future sessions.
6670
let didStrip = false;
6771
const cleaned = (parsed as Array<Record<string, unknown>>).map((entry) => {
68-
if (entry && typeof entry === "object" && "nsec" in entry) {
69-
const { nsec: _nsec, ...rest } = entry;
72+
if (
73+
entry &&
74+
typeof entry === "object" &&
75+
("nsec" in entry || "token" in entry)
76+
) {
77+
const { nsec: _nsec, token: _token, ...rest } = entry;
7078
didStrip = true;
7179
return rest;
7280
}

‎desktop/src/features/communities/resolveCommunityUpdateResult.test.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ test("resolveCommunityUpdateResult_relay_edit_on_inactive_community_no_reinit",
8383
assert.deepEqual(result, { kind: "updated", requiresReinit: false });
8484
});
8585

86-
test("resolveCommunityUpdateResult_token_change_on_active_requires_reinit", () => {
86+
test("resolveCommunityUpdateResult_repos_dir_change_on_active_requires_reinit", () => {
8787
const result = resolveCommunityUpdateResult(COMMUNITIES, "ws-1", "ws-1", {
88-
token: "new-token",
88+
reposDir: "/Users/dev/repos",
8989
});
9090
assert.deepEqual(result, { kind: "updated", requiresReinit: true });
9191
});

‎desktop/src/features/communities/types.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export type Community = {
22
id: string;
33
name: string;
44
relayUrl: string;
5-
token?: string;
65
/**
76
* The pubkey associated with the active identity at the time the community
87
* was created. Display-only — auth always uses the persisted `identity.key`
@@ -24,4 +23,11 @@ export type Community = {
2423
* authoritative private key is the on-disk `identity.key` file.
2524
*/
2625
nsec?: never;
26+
/**
27+
* @deprecated Never read. A relay API token from before the relay moved to
28+
* pure Nostr key auth (NIP-42/NIP-98) — the backend stopped accepting it,
29+
* so the secret sat unused in localStorage. New entries never set this
30+
* field, and `loadCommunities()` strips it on read.
31+
*/
32+
token?: never;
2733
};

‎desktop/src/features/communities/ui/AddCommunityDialog.tsx‎

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function AddCommunityDialog({
3535
}: AddCommunityDialogProps) {
3636
const [name, setName] = React.useState("");
3737
const [relayUrl, setRelayUrl] = React.useState("");
38-
const [token, setToken] = React.useState("");
3938
const [inviteCode, setInviteCode] = React.useState("");
4039
const [inviteError, setInviteError] = React.useState<string | null>(null);
4140
const [reposDir, setReposDir] = React.useState("");
@@ -45,7 +44,6 @@ export function AddCommunityDialog({
4544
onOpenChange(false);
4645
setName("");
4746
setRelayUrl("");
48-
setToken("");
4947
setInviteCode("");
5048
setInviteError(null);
5149
setReposDir("");
@@ -92,15 +90,14 @@ export function AddCommunityDialog({
9290
id: crypto.randomUUID(),
9391
name: name.trim() || deriveCommunityName(relayUrl.trim()),
9492
relayUrl: normalizedRelayUrl,
95-
token: token.trim() || undefined,
9693
reposDir: expandedReposDir,
9794
addedAt: new Date().toISOString(),
9895
};
9996

10097
onSubmit(community);
10198
handleClose();
10299
},
103-
[name, relayUrl, token, inviteCode, reposDir, onSubmit, handleClose],
100+
[name, relayUrl, inviteCode, reposDir, onSubmit, handleClose],
104101
);
105102

106103
return (
@@ -151,24 +148,6 @@ export function AddCommunityDialog({
151148
value={name}
152149
/>
153150
</div>
154-
<div className="flex flex-col gap-1.5">
155-
<label
156-
className="text-sm font-medium text-foreground"
157-
htmlFor="ws-token"
158-
>
159-
API Token
160-
<span className="ml-1 text-xs font-normal text-muted-foreground">
161-
(optional)
162-
</span>
163-
</label>
164-
<Input
165-
id="ws-token"
166-
onChange={(e) => setToken(e.target.value)}
167-
placeholder="buzz_..."
168-
type="password"
169-
value={token}
170-
/>
171-
</div>
172151
<div className="flex flex-col gap-1.5">
173152
<label
174153
className="text-sm font-medium text-foreground"

‎desktop/src/features/communities/ui/CommunitySwitcher.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type CommunitySwitcherProps = {
4949
onAddCommunity: () => void;
5050
onUpdateCommunity: (
5151
id: string,
52-
updates: Partial<Pick<Community, "name" | "relayUrl" | "token">>,
52+
updates: Partial<Pick<Community, "name" | "relayUrl">>,
5353
) => void;
5454
onRemoveCommunity: (id: string) => void;
5555
};

‎desktop/src/features/communities/ui/EditCommunityDialog.tsx‎

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ type EditCommunityDialogProps = {
2222
onOpenChange: (open: boolean) => void;
2323
onSave: (
2424
id: string,
25-
updates: Partial<
26-
Pick<Community, "name" | "relayUrl" | "token" | "reposDir">
27-
>,
25+
updates: Partial<Pick<Community, "name" | "relayUrl" | "reposDir">>,
2826
) => void;
2927
onRemove?: (id: string) => void;
3028
canRemove?: boolean;
@@ -40,7 +38,6 @@ export function EditCommunityDialog({
4038
}: EditCommunityDialogProps) {
4139
const [name, setName] = React.useState("");
4240
const [relayUrl, setRelayUrl] = React.useState("");
43-
const [token, setToken] = React.useState("");
4441
const [reposDir, setReposDir] = React.useState("");
4542
const [reposDirError, setReposDirError] = React.useState<string | null>(null);
4643

@@ -49,7 +46,6 @@ export function EditCommunityDialog({
4946
if (community && open) {
5047
setName(community.name);
5148
setRelayUrl(community.relayUrl);
52-
setToken(community.token ?? "");
5349
setReposDir(community.reposDir ?? "");
5450
setReposDirError(null);
5551
}
@@ -67,7 +63,7 @@ export function EditCommunityDialog({
6763
}
6864

6965
const updates: Partial<
70-
Pick<Community, "name" | "relayUrl" | "token" | "reposDir">
66+
Pick<Community, "name" | "relayUrl" | "reposDir">
7167
> = {};
7268

7369
const trimmedName = name.trim();
@@ -80,11 +76,6 @@ export function EditCommunityDialog({
8076
updates.relayUrl = normalizedUrl;
8177
}
8278

83-
const trimmedToken = token.trim() || undefined;
84-
if (trimmedToken !== community.token) {
85-
updates.token = trimmedToken;
86-
}
87-
8879
// Expand `~` to an absolute path before save — the backend rejects
8980
// tilde paths. An empty field clears the override (REPOS reverts to a
9081
// real dir). Validate the expanded value (the bytes the backend
@@ -108,7 +99,7 @@ export function EditCommunityDialog({
10899

109100
handleClose();
110101
},
111-
[community, name, relayUrl, token, reposDir, onSave, handleClose],
102+
[community, name, relayUrl, reposDir, onSave, handleClose],
112103
);
113104

114105
const handleRemove = React.useCallback(() => {
@@ -166,24 +157,6 @@ export function EditCommunityDialog({
166157
value={relayUrl}
167158
/>
168159
</div>
169-
<div className="flex flex-col gap-1.5">
170-
<label
171-
className="text-sm font-medium text-foreground"
172-
htmlFor="edit-ws-token"
173-
>
174-
API Token
175-
<span className="ml-1 text-xs font-normal text-muted-foreground">
176-
(optional)
177-
</span>
178-
</label>
179-
<Input
180-
id="edit-ws-token"
181-
onChange={(e) => setToken(e.target.value)}
182-
placeholder="buzz_..."
183-
type="password"
184-
value={token}
185-
/>
186-
</div>
187160
<div className="flex flex-col gap-1.5">
188161
<label
189162
className="text-sm font-medium text-foreground"

‎desktop/src/features/communities/useCommunities.tsx‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function resolveCommunityUpdateResult(
3838
activeId: string | null,
3939
id: string,
4040
updates: Partial<
41-
Pick<Community, "name" | "relayUrl" | "token" | "pubkey" | "reposDir">
41+
Pick<Community, "name" | "relayUrl" | "pubkey" | "reposDir">
4242
>,
4343
): UpdateCommunityResult {
4444
const current = communities.find((w) => w.id === id);
@@ -55,7 +55,6 @@ export function resolveCommunityUpdateResult(
5555
const hasChange =
5656
(updates.name !== undefined && updates.name !== current.name) ||
5757
(updates.relayUrl !== undefined && updates.relayUrl !== current.relayUrl) ||
58-
(updates.token !== undefined && updates.token !== current.token) ||
5958
(updates.pubkey !== undefined && updates.pubkey !== current.pubkey) ||
6059
(updates.reposDir !== undefined && updates.reposDir !== current.reposDir);
6160

@@ -66,7 +65,6 @@ export function resolveCommunityUpdateResult(
6665
isActive &&
6766
((updates.relayUrl !== undefined &&
6867
updates.relayUrl !== current.relayUrl) ||
69-
(updates.token !== undefined && updates.token !== current.token) ||
7068
(updates.reposDir !== undefined &&
7169
updates.reposDir !== current.reposDir));
7270

@@ -76,7 +74,7 @@ export function resolveCommunityUpdateResult(
7674
export type UseCommunitiesReturn = {
7775
communities: Community[];
7876
activeCommunity: Community | null;
79-
/** Counter bumped when the active community's config changes (relayUrl/token). */
77+
/** Counter bumped when the active community's config changes (relayUrl/reposDir). */
8078
reinitKey: number;
8179
/** Add a community, deduplicating by relayUrl. Returns the final ID in the list. */
8280
addCommunity: (community: Community) => string;
@@ -88,7 +86,7 @@ export type UseCommunitiesReturn = {
8886
updateCommunity: (
8987
id: string,
9088
updates: Partial<
91-
Pick<Community, "name" | "relayUrl" | "token" | "pubkey" | "reposDir">
89+
Pick<Community, "name" | "relayUrl" | "pubkey" | "reposDir">
9290
>,
9391
) => UpdateCommunityResult;
9492
};
@@ -154,7 +152,6 @@ function useCommunitiesInternal(): UseCommunitiesReturn {
154152
? {
155153
...w,
156154
name: community.name || w.name,
157-
token: community.token ?? w.token,
158155
pubkey: community.pubkey ?? w.pubkey,
159156
}
160157
: w,
@@ -227,7 +224,7 @@ function useCommunitiesInternal(): UseCommunitiesReturn {
227224
(
228225
id: string,
229226
updates: Partial<
230-
Pick<Community, "name" | "relayUrl" | "token" | "pubkey" | "reposDir">
227+
Pick<Community, "name" | "relayUrl" | "pubkey" | "reposDir">
231228
>,
232229
): UpdateCommunityResult => {
233230
const result = resolveCommunityUpdateResult(

‎desktop/src/features/communities/useCommunityInit.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function useCommunityInit(
7878
// before resetting when the user switches to a different community.
7979
const prevCommunityIdRef = useRef<string | null>(null);
8080

81-
// biome-ignore lint/correctness/useExhaustiveDependencies: we intentionally depend on specific properties (id/relayUrl/token/reposDir) — depending on the whole object would trigger resets on name-only changes
81+
// biome-ignore lint/correctness/useExhaustiveDependencies: we intentionally depend on specific properties (id/relayUrl/reposDir) — depending on the whole object would trigger resets on name-only changes
8282
useEffect(() => {
8383
let cancelled = false;
8484

@@ -155,7 +155,6 @@ export function useCommunityInit(
155155
await applyCommunity(
156156
activeCommunity.relayUrl,
157157
undefined,
158-
activeCommunity.token,
159158
activeCommunity.reposDir,
160159
);
161160
} catch (error) {
@@ -211,7 +210,6 @@ export function useCommunityInit(
211210
}, [
212211
activeCommunity?.id,
213212
activeCommunity?.relayUrl,
214-
activeCommunity?.token,
215213
activeCommunity?.reposDir,
216214
isSharedIdentity,
217215
communityKey,

0 commit comments

Comments
 (0)