diff --git a/docs/discord-bridge.md b/docs/discord-bridge.md index 86210d3e..7fb3669a 100644 --- a/docs/discord-bridge.md +++ b/docs/discord-bridge.md @@ -135,3 +135,17 @@ The text explains `codeman notify "message" --urgent`. `codeman` is on the PATH | Posts arrive but replies do nothing | Message Content Intent is off, or your user ID is not in the allowlist | | Thread is not deleted | The bot lacks Manage Threads on the parent channel | | Nothing arrives on the phone | Check the Discord app's notification setting for the server | + +## Reply replay protection + +The bridge remembers up to 2,048 recent Discord reply IDs for seven days in the private +`discord-reply-dedupe.json` metadata file. It claims an allowed reply before downloading +attachments or typing into its session, so a gateway replay normally cannot type it twice, +including after a restart. The file contains IDs and timestamps only, with mode 0600. + +This is best-effort replay protection, not an exactly-once guarantee. A full disk, unreadable +file, or invalid metadata logs one content-free warning and falls back to in-memory checks. +Reads and writes retry on later replies; a restart during a metadata outage can lose those +checks. At capacity the oldest entries are evicted, so capacity never blocks new replies. +Notifications retain their existing delivery and retry behavior; this store never suppresses +an outbound alert or replays terminal input. diff --git a/src/discord-bridge.ts b/src/discord-bridge.ts index a8703371..6fe14629 100644 --- a/src/discord-bridge.ts +++ b/src/discord-bridge.ts @@ -27,6 +27,7 @@ import { join } from 'node:path'; import { probePathKind } from './utils/bounded-path-probe.js'; import { DISCORD_RECEIVED_DIR } from './web/paste-image-gc.js'; import WebSocket from 'ws'; +import { DiscordReplyDedupe } from './discord-reply-dedupe.js'; import { WEBHOOK_SCOPES, type WebhookUrgency } from './types/push.js'; import { DISCORD_ON_DELETE, @@ -824,6 +825,7 @@ export interface DiscordBridgeDeps { } export class DiscordBridge { + private readonly replies: DiscordReplyDedupe; private cfg: DiscordConfig = { ...DEFAULT_DISCORD_CONFIG }; private threads: Record = {}; private loaded = false; @@ -857,6 +859,7 @@ export class DiscordBridge { constructor(private readonly deps: DiscordBridgeDeps) { this.now = deps.now ?? Date.now; + this.replies = new DiscordReplyDedupe(deps.configDir, this.now); this.api = new DiscordApi(() => this.cfg.token, deps.fetchImpl, deps.sleep); } @@ -1312,6 +1315,7 @@ export class DiscordBridge { await this.say(m.channel_id, 'The agent in that tab has exited, so nothing was typed.'); return; } + if (!(await this.replies.claim(m.guild_id, m.channel_id, m.id))) return; this.lastReply.set(sessionId, t); const saved = await this.saveAttachments(sessionId, m); const text = sanitiseReply(m.content ?? '', saved.paths); diff --git a/src/discord-reply-dedupe.ts b/src/discord-reply-dedupe.ts new file mode 100644 index 00000000..2c71497d --- /dev/null +++ b/src/discord-reply-dedupe.ts @@ -0,0 +1,74 @@ +/** Best-effort replay protection. A metadata-store failure must never block a person's reply. */ +import fs from 'node:fs/promises'; +import { join } from 'node:path'; +import { randomUUID } from 'node:crypto'; + +const LIMIT = 2048; +const RETENTION_MS = 7 * 24 * 60 * 60_000; +export class DiscordReplyDedupe { + private seen = new Map(); + private loaded = false; + private warned = false; + private chain: Promise = Promise.resolve(); + constructor( + private readonly dir: string, + private readonly now: () => number = Date.now, + private readonly warn: () => void = () => + console.warn('Discord reply metadata unavailable; using in-memory deduplication') + ) {} + claim(guild: string, channel: string, message: string): Promise { + const run = this.chain.then(() => this.take(guild, channel, message)); + this.chain = run.catch(() => undefined); + return run; + } + private async take(guild: string, channel: string, message: string): Promise { + // Restored legacy configurations and malformed gateway IDs must not poison the store. + if (![guild, channel, message].every((id) => /^\d{15,25}$/.test(id))) return true; + const path = join(this.dir, 'discord-reply-dedupe.json'); + const cutoff = this.now() - RETENTION_MS; + try { + if (!this.loaded) { + try { + const st = await fs.lstat(path); + if (!st.isFile() || st.isSymbolicLink() || st.size > 512 * 1024) throw new Error('Unsafe metadata'); + const rows: unknown = JSON.parse(await fs.readFile(path, 'utf8')); + if (!Array.isArray(rows) || rows.length > LIMIT) throw new Error('Invalid metadata'); + const recovered = new Map(); + for (const row of rows) { + if (!Array.isArray(row) || !/^\d{15,25}:\d{15,25}:\d{15,25}$/.test(row[0]) || !Number.isFinite(row[1])) + throw new Error('Invalid metadata'); + if (row[1] >= cutoff) recovered.set(row[0], row[1]); + } + for (const [key, at] of recovered) if (!this.seen.has(key)) this.seen.set(key, at); + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err; + } + this.loaded = true; + } + } catch { + this.report(); // Retry the read on the next reply; never memoize a failed load. + } + for (const [key, at] of this.seen) if (at < cutoff) this.seen.delete(key); + const key = `${guild}:${channel}:${message}`; + if (this.seen.has(key)) return false; + this.seen.set(key, this.now()); // Before awaiting IO or typing, including during an outage. + while (this.seen.size > LIMIT) this.seen.delete(this.seen.keys().next().value!); + if (!this.loaded) return true; // Do not overwrite a journal we could not read. + const tmp = `${path}.${randomUUID()}.tmp`; + try { + await fs.mkdir(this.dir, { recursive: true }); + await fs.writeFile(tmp, JSON.stringify([...this.seen]), { mode: 0o600, flag: 'wx' }); + await fs.rename(tmp, path); + this.warned = false; + } catch { + this.report(); + } finally { + await fs.rm(tmp, { force: true }).catch(() => undefined); + } + return true; + } + private report(): void { + if (!this.warned) this.warn(); + this.warned = true; + } +} diff --git a/test/discord-bridge.test.ts b/test/discord-bridge.test.ts index 32db32d7..1468def2 100644 --- a/test/discord-bridge.test.ts +++ b/test/discord-bridge.test.ts @@ -448,6 +448,38 @@ describe('DiscordBridge', () => { return b; } + it('deduplicates replayed real gateway IDs across restart while new replies still work', async () => { + const b = await ready(); + const m = reply({ id: '555555555555555555' }); + await b.onGatewayMessage(m); + b.stop(); + t += 10_000; + const next = await ready(); + await next.onGatewayMessage(m); + expect(sendInput).toHaveBeenCalledTimes(1); + await next.onGatewayMessage(reply({ id: '555555555555555556', content: 'continue' })); + expect(sendInput).toHaveBeenCalledTimes(2); + next.stop(); + }); + + it('delivers replies and alerts when metadata is corrupt and recovers without restarting', async () => { + const b = await ready(); + writeFileSync(join(dir, 'discord-reply-dedupe.json'), '{'); + const log = vi.spyOn(console, 'warn').mockImplementation(() => undefined); + try { + await b.onGatewayMessage(reply({ id: '555555555555555555' })); + expect(sendInput).toHaveBeenCalledTimes(1); + t += 10_000; + expect((await b.notify(MSG))?.ok).toBe(true); + rmSync(join(dir, 'discord-reply-dedupe.json')); + await b.onGatewayMessage(reply({ id: '555555555555555556' })); + expect(sendInput).toHaveBeenCalledTimes(2); + expect(log).toHaveBeenCalledTimes(1); + } finally { + log.mockRestore(); + b.stop(); + } + }); it('types an allowed reply into the mapped session and ticks it', async () => { const b = await ready(); await b.onGatewayMessage(reply()); @@ -1157,11 +1189,11 @@ describe('DiscordBridge files and startup hint', () => { await b.onGatewayMessage(reply({ content: '2', attachments: [] })); expect(sent.at(-1)?.[1]).toBe('2'); t += 5_000; - await b.onGatewayMessage(reply({ content: 'yes', attachments: [] })); + await b.onGatewayMessage(reply({ id: '555555555555555556', content: 'yes', attachments: [] })); expect(sent.at(-1)?.[1]).toBe('yes\r'); t += 5_000; waiting = false; - await b.onGatewayMessage(reply({ content: '2', attachments: [] })); + await b.onGatewayMessage(reply({ id: '555555555555555557', content: '2', attachments: [] })); expect(sent.at(-1)?.[1]).toBe('2\r'); b.stop(); }); diff --git a/test/discord-reply-dedupe.test.ts b/test/discord-reply-dedupe.test.ts new file mode 100644 index 00000000..fbf6793a --- /dev/null +++ b/test/discord-reply-dedupe.test.ts @@ -0,0 +1,73 @@ +import { mkdtemp, readFile, rm, writeFile, stat } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { afterEach, beforeEach, expect, it, vi } from 'vitest'; +import { DiscordReplyDedupe } from '../src/discord-reply-dedupe.js'; +import fs from 'node:fs/promises'; +let dir: string; +const guild = '111111111111111111', + channel = '222222222222222222', + message = '333333333333333333'; +beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), 'discord-replies-')); +}); +afterEach(async () => { + await rm(dir, { recursive: true, force: true }); +}); +it('claims once across concurrent events and restarts, without storing message content', async () => { + const store = new DiscordReplyDedupe(dir); + expect(await Promise.all([store.claim(guild, channel, message), store.claim(guild, channel, message)])).toEqual([ + true, + false, + ]); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); + expect((await stat(join(dir, 'discord-reply-dedupe.json'))).mode & 0o777).toBe(0o600); + expect(JSON.parse(await readFile(join(dir, 'discord-reply-dedupe.json'), 'utf8'))).toEqual([ + [`${guild}:${channel}:${message}`, expect.any(Number)], + ]); +}); +it('falls back on corrupt metadata, logs once, retries after recovery, and retains memory claims', async () => { + const path = join(dir, 'discord-reply-dedupe.json'); + await writeFile(path, '{'); + const warn = vi.fn(), + store = new DiscordReplyDedupe(dir, Date.now, warn); + expect(await store.claim(guild, channel, message)).toBe(true); + expect(await store.claim(guild, channel, message)).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + await rm(path); + expect(await store.claim(guild, channel, '444444444444444444')).toBe(true); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); +}); +it('does not poison metadata with incomplete restored configuration', async () => { + const store = new DiscordReplyDedupe(dir); + expect(await store.claim('', channel, message)).toBe(true); + expect(await store.claim(guild, channel, message)).toBe(true); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); +}); +it('keeps accepting new replies during ENOSPC and persists memory claims when writes recover', async () => { + const warn = vi.fn(); + const store = new DiscordReplyDedupe(dir, Date.now, warn); + const rename = vi.spyOn(fs, 'rename').mockRejectedValueOnce(Object.assign(new Error('full'), { code: 'ENOSPC' })); + try { + expect(await store.claim(guild, channel, message)).toBe(true); + expect(await store.claim(guild, channel, message)).toBe(false); + expect(await store.claim(guild, channel, '444444444444444444')).toBe(true); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + } finally { + rename.mockRestore(); + } +}); +it('evicts old records rather than refusing replies at capacity', async () => { + await writeFile( + join(dir, 'discord-reply-dedupe.json'), + JSON.stringify( + Array.from({ length: 2048 }, (_, i) => [ + `${guild}:${channel}:${String(100000000000000000n + BigInt(i))}`, + Date.now(), + ]) + ) + ); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(true); + expect(JSON.parse(await readFile(join(dir, 'discord-reply-dedupe.json'), 'utf8'))).toHaveLength(2048); +});