From 1072ce995e878e43332ef2eec7dfd127b3360b0d Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Mon, 16 Nov 2026 22:47:50 +0100 Subject: [PATCH 1/3] feat(bridge): link a Discord channel to a Fluxer channel A new Bridge module shares one conversation between a Discord channel and a Fluxer channel. Both servers agree: one asks for a link and gets a one-time code (hashed, valid 15 minutes), someone with Edit access in the other server uses it. Either side can pause or unlink at any time. Messages cross through webhooks Sylo makes in each channel, with the author's name and picture. Replies are quoted, files are re-uploaded within limits (otherwise a link), custom emoji map by name, stickers become a picture link, rich embeds cross, polls become text and joins are optional. Edits and deletes follow through a message map that keeps a message for 30 days. Safety: no ping crosses (allowed mentions none, mentions turned into names), Sylo's own bridge messages are never relayed again, bots are off by default, an age-restricted channel only links to an age-restricted channel (checked on every message), a notice shows in both channels, each webhook has its own rate-limited queue with back-off, and a failing link is stopped, shown in Health and not retried. Adds migration 83 (versions 81 and 82 stay empty placeholders until the other 5.3 changes land), the Bridge page in the dashboard, the `bridge` feature string in /api/mobile/info and a docs page. --- README.md | 4 +- docs/dashboard-v2.md | 2 +- docs/fluxer.md | 1 + docs/mobile-app.md | 3 +- docs/modules/README.md | 1 + docs/modules/bridge.md | 85 ++ src/db/bridge.js | 469 +++++++++++ src/db/index.js | 63 ++ src/db/purge.js | 25 + src/db/retention.js | 7 + src/modules/bridge.js | 1087 ++++++++++++++++++++++++++ src/modules/index.js | 1 + src/modules/lib/bridgeQueue.js | 161 ++++ src/modules/lib/bridgeText.js | 285 +++++++ src/modules/registry.js | 11 + src/platform/modulePlatformNotes.js | 2 + src/platform/webhooks.js | 146 ++++ src/web/lib/cardSummary.js | 5 + src/web/lib/configImport.js | 2 + src/web/lib/moduleIcons.js | 1 + src/web/lib/overviewSummary.js | 10 + src/web/lib/permissionHealth.js | 27 +- src/web/lib/routeAccess.js | 10 + src/web/routes/mobile.js | 3 + src/web/routes/v2Api.js | 3 + src/web/routes/v2Bridge.js | 169 ++++ test/bridge.db.test.js | 381 +++++++++ test/bridge.engine.test.js | 1045 +++++++++++++++++++++++++ test/bridge.postgres.test.js | 125 +++ test/bridgeHealth.test.js | 73 ++ test/bridgeQueue.test.js | 208 +++++ test/bridgeText.test.js | 219 ++++++ test/bridgeTransport.test.js | 88 +++ test/helpers/fakeBridge.js | 173 ++++ test/mobileInfo.test.js | 4 + test/v2Bridge.test.js | 381 +++++++++ web-v2/src/__tests__/Bridge.test.jsx | 296 +++++++ web-v2/src/api.js | 13 + web-v2/src/main.jsx | 1 + web-v2/src/moduleForms/Bridge.jsx | 410 ++++++++++ web-v2/src/moduleForms/index.js | 2 + web-v2/src/pages/ModulePage.jsx | 2 +- web-v2/src/styles/bridge.css | 45 ++ 43 files changed, 6043 insertions(+), 6 deletions(-) create mode 100644 docs/modules/bridge.md create mode 100644 src/db/bridge.js create mode 100644 src/modules/bridge.js create mode 100644 src/modules/lib/bridgeQueue.js create mode 100644 src/modules/lib/bridgeText.js create mode 100644 src/platform/webhooks.js create mode 100644 src/web/routes/v2Bridge.js create mode 100644 test/bridge.db.test.js create mode 100644 test/bridge.engine.test.js create mode 100644 test/bridge.postgres.test.js create mode 100644 test/bridgeHealth.test.js create mode 100644 test/bridgeQueue.test.js create mode 100644 test/bridgeText.test.js create mode 100644 test/bridgeTransport.test.js create mode 100644 test/helpers/fakeBridge.js create mode 100644 test/v2Bridge.test.js create mode 100644 web-v2/src/__tests__/Bridge.test.jsx create mode 100644 web-v2/src/moduleForms/Bridge.jsx create mode 100644 web-v2/src/styles/bridge.css diff --git a/README.md b/README.md index b85a430f..8a243c3b 100644 --- a/README.md +++ b/README.md @@ -89,14 +89,14 @@ tier. iOS app with push notifications for new tickets.
-All 43 modules +All 44 modules | Area | Modules | |---|---| | **Moderation & safety** | [Moderation](docs/modules/moderation.md) (case log, warnings, auto-actions) · [Auto-moderation](docs/modules/automod.md) (can sync to Discord's native AutoMod) · [Server logging](docs/modules/logging.md) · [Verification](docs/modules/verification.md) (button or Turnstile captcha) · [Ban appeals](docs/modules/appeals.md) · [Tickets / modmail](docs/modules/tickets.md) · [Applications](docs/modules/applications.md) (forms for staff to accept or deny) · [Channel cleanup](docs/modules/channel-cleanup.md) · Honeypot (beta) | | **Welcome & roles** | [Welcome & leave](docs/modules/welcome.md) · [Welcome channel](docs/modules/welcome-channel.md) · [Introductions](docs/modules/introductions.md) (membership cards) · [Reaction roles & autoroles](docs/modules/roles.md) (emoji, buttons or dropdown) · [Temporary roles](docs/modules/temp-roles.md) (beta) · [Birthdays](docs/modules/birthdays.md) | | **Engagement** | [Leveling](docs/modules/leveling.md) (image rank cards, public leaderboard) · [Economy](docs/modules/economy.md) (currency and shop, beta) · [Invite tracker](docs/modules/invite-tracker.md) · [Starboard](docs/modules/starboard.md) · [Quote board](docs/modules/quote-board.md) (beta, optional public page) · [Polls](docs/modules/polls.md) · [Giveaways](docs/modules/giveaways.md) · [Events](docs/modules/events.md) (beta) · [Suggestions](docs/modules/suggestions.md) (beta) · [Counting](docs/modules/counting.md) · [Game stats](docs/modules/game-stats.md) (Battlefield, OSRS, RS3) | -| **Automation** | [Custom commands](docs/modules/custom-commands.md) · [Autoresponder](docs/modules/autoresponder.md) · [Auto-react](docs/modules/auto-react.md) · [Auto threads](docs/modules/auto-threads.md) (beta, Discord only) · [Reminders](docs/modules/reminders.md) · [Sticky messages](docs/modules/sticky.md) · [AFK](docs/modules/afk.md) · [Temporary voice channels](docs/modules/temp-voice.md) | +| **Automation** | [Custom commands](docs/modules/custom-commands.md) · [Autoresponder](docs/modules/autoresponder.md) · [Auto-react](docs/modules/auto-react.md) · [Auto threads](docs/modules/auto-threads.md) (beta, Discord only) · [Bridge](docs/modules/bridge.md) (Discord ↔ Fluxer, beta) · [Reminders](docs/modules/reminders.md) · [Sticky messages](docs/modules/sticky.md) · [AFK](docs/modules/afk.md) · [Temporary voice channels](docs/modules/temp-voice.md) | | **Alerts** | [Twitch](docs/modules/twitch-alerts.md) · [YouTube](docs/modules/youtube-alerts.md) · [Kick](docs/modules/kick-alerts.md) · [RSS / Atom](docs/modules/rss.md) (also Reddit, Mastodon, Bluesky) · [GitHub](docs/modules/github.md) · [Free games](docs/modules/free-games.md) · [App Store Connect](docs/modules/apple-connect.md) (instance operator only) | | **Insights** | [Server statistics](docs/modules/server-stats.md) (live counter channels) · [Server insights](docs/modules/insights.md) (activity charts, aggregate counts only) | diff --git a/docs/dashboard-v2.md b/docs/dashboard-v2.md index 3c55bf9f..09c6515f 100644 --- a/docs/dashboard-v2.md +++ b/docs/dashboard-v2.md @@ -26,7 +26,7 @@ note. ## What the dashboard shows -- **Sidebar shows only what's turned on.** Instead of listing all 43 modules +- **Sidebar shows only what's turned on.** Instead of listing all 44 modules with an on/off dot, the sidebar lists just the modules enabled for whichever server you're looking at, grouped the same way the Dashboard/Overview page groups them. Turn a module on from Overview and it appears in the sidebar diff --git a/docs/fluxer.md b/docs/fluxer.md index b9625071..bb2fb9ff 100644 --- a/docs/fluxer.md +++ b/docs/fluxer.md @@ -238,6 +238,7 @@ does not register anything with Fluxer for these. | `/intro create` and the **Create my intro** button | **Introductions** uses `!intro create`, then asks the questions in DM, as on Discord. Sylo answers "Check your DMs" in the channel and removes that message after about 15 seconds. The **Create my intro** button of the prompt message, and **Post** and **Start over** under the preview in DM, are shown as reactions. | | `/apply start` and the **Applications** panel | **Applications** uses `!apply start
` (and `!apply withdraw`), then asks the questions in DM, as on Discord. Choice and role questions are numbered options (at most 10), answered with the numbers. The panel message, the choice between DM and web, **Submit** and **Start over** under the preview, and **Accept** and **Deny** on each application are shown as reactions. Staff give a reason with `!application accept ` or `!application deny `. A staff thread is not opened. | | `/balance`, `/daily`, `/pay` and the other **Economy** commands | **Economy** uses chat commands with the same names (`!balance`, `!daily`, `!pay @member 100`, `!eco add @member 500 reason:"Prize"`). The leaderboard is `!economy-leaderboard`; its pages turn with the ⬅️ and ➡️ reactions. The shop is `!shop` (a numbered list that pages the same way), `!buy `, `!inventory` and `!collection` (collectibles, with their pictures as embeds; the pages turn with the same reactions; with collections private it is a private reply, so it comes to your DMs); a giveaway entry fee works with the Enter reaction. A robbery result is posted in the channel as on Discord, and the victim gets the same DM. | +| A Discord channel's conversation in a Fluxer channel | The **Bridge** module shares one channel with a Discord channel through webhooks, with a one-time code both servers agree on. It is set up in the dashboard; no chat command is needed. A Discord poll shows on Fluxer as text. See [Bridge](modules/bridge.md). | | Message menu **Save quote** | Not available. The **Quote board** module saves a quote with `!quote save `, or `!quote save` as a reply to the message. | | Threads and auto-threads | Not available yet. The **Auto threads** module does nothing on Fluxer. Lock and lockdown skip the thread permissions. | | Moving members between voice | Not usable from a bot. **Temporary voice channels** are still created, but the member gets a DM with a link to join, and a channel nobody joins within two minutes is removed. | diff --git a/docs/mobile-app.md b/docs/mobile-app.md index d03cd0c3..da0b26ae 100644 --- a/docs/mobile-app.md +++ b/docs/mobile-app.md @@ -317,7 +317,7 @@ in there: | `login` | Whether sign-in is available (false in open mode) | | `loginProviders` | The configured logins, for example `["discord", "fluxer"]`; empty when `login` is false | | `platforms` | The platforms the bot runs on, for example `["discord"]` | -| `features` | What this version's app API supports, so the app can hide what an older Sylo lacks. Today: `app-login`, `live-access`, `changelog`, `collectibles`, `welcome-trigger`, `member-search`, `access-levels`, `health`, `app-connect`, `setup-wizard`, `config-history`, `inbox-search`, `config-import`, `duplicate`, `message-templates` and `message-move`, plus `push` and `push-pigeon` when this instance can send notifications. See the list below | +| `features` | What this version's app API supports, so the app can hide what an older Sylo lacks. Today: `app-login`, `live-access`, `changelog`, `collectibles`, `welcome-trigger`, `member-search`, `access-levels`, `health`, `app-connect`, `setup-wizard`, `config-history`, `inbox-search`, `config-import`, `duplicate`, `message-templates`, `message-move` and `bridge`, plus `push` and `push-pigeon` when this instance can send notifications. See the list below | The feature strings: @@ -338,6 +338,7 @@ The feature strings: - `duplicate`: the `duplicate` routes for reminders, embed messages, reaction-role sets, custom commands and shop items. - `message-templates`: the built-in message templates and the raw message JSON import and export. - `message-move`: when the channel of a posted **embed message**, the **Welcome channel** message or a **role panel** changes and is published, the server posts a fresh message in the new channel, stores it and then deletes the old one, so the app must not unpublish first. Other bot posts (verification, honeypot traps, applications and so on) are not covered. A publish that loses a race with another publish of the same embed message answers `409` with the current record. +- `bridge`: the [Bridge module](modules/bridge.md): `GET /api/v2/guilds/:id/modules/bridge/config` (the links, waiting requests and channels of the server; View level) and, at Edit level, `POST` to `…/modules/bridge/request` (a code for another server), `…/modules/bridge/accept` (use a code), `…/modules/bridge/requests/:id/cancel`, and `…/modules/bridge/links/:id/options`, `…/pause`, `…/resume` and `…/unlink`. Every answer carries the fresh `overview`. - `app-connect`: [adding the other login](#adding-the-other-login). Only where sign-in is on and both Discord and Fluxer logins are set up. - `push`, `push-pigeon`: this instance can send notifications to signed-in apps. diff --git a/docs/modules/README.md b/docs/modules/README.md index 5fc9c316..d501b7f3 100644 --- a/docs/modules/README.md +++ b/docs/modules/README.md @@ -43,6 +43,7 @@ platform gaps (native AutoMod rules, threads, moving members between voice). | [Quote board](quote-board.md) | Save memorable messages (message menu or `/quote save`), show them with `/quote`, optional public page (beta) | off | — | | [Introductions](introductions.md) | Members answer a few questions in DM and Sylo posts a membership card in the introductions channel (beta) | off | Server Members | | [Applications](applications.md) | Forms members fill in, in DM or on the web, for staff to accept or deny, with requirements, roles and messages (beta) | off | Server Members | +| [Bridge](bridge.md) | Share one conversation between a Discord channel and a Fluxer channel: a one-time code both servers agree on, webhooks, replies, files, edits and deletes, no pings across (beta) | off | Message Content | | [Economy](economy.md) | A currency members earn by chatting and in voice, keep in cash or a bank, pay each other and compete for; optional robbing; staff tools (beta) | off | Message Content | | [AFK](afk.md) | Members mark themselves away; Sylo replies to mentions | off | — | | [Temporary voice channels](temp-voice.md) | Join-to-create voice hubs controlled with `/voice-*` | off | — | diff --git a/docs/modules/bridge.md b/docs/modules/bridge.md new file mode 100644 index 00000000..e32bec9f --- /dev/null +++ b/docs/modules/bridge.md @@ -0,0 +1,85 @@ +# Bridge (beta) + +Share one conversation between a channel on **Discord** and a channel on **Fluxer**. Members on both +sides write as they always do; Sylo shows each message on the other side with the author's name and +picture, and carries replies, files, edits and deletes along. + +This is Sylo's own implementation. A bridge needs a Sylo that is connected to both platforms (the hosted +Sylo is) and that is a member of both servers. + +**Dashboard:** `/v2/guilds//m/bridge` - the linked channels, a form to ask for a link, a form to use a +code, what this server shares, and pause, resume and unlink. + +## Linking: both servers agree + +A link is never made from one side alone. + +1. In the first server, someone with **Edit** access opens the Bridge page, picks a channel and presses + **Get a code**. The code (`XXXX-XXXX-XXXX`) works once and for **15 minutes**. Sylo stores only a hash of + it, so it is shown this one time. +2. They give the code to someone with **Edit** access in the other server. That person opens the Bridge + page there, pastes the code, picks the channel that should join it and presses **Link the channels**. +3. Sylo makes a webhook in each channel and posts a notice in both: "Bridge on. Messages in this channel are + shared with #chat in Fluxer Home on Fluxer ...". + +A link joins a Discord channel to a Fluxer channel, never two on the same platform. A channel is in one link +at a time, and a server has at most 10 links and 5 waiting requests. The Bridge module must be on in both +servers. Wrong codes are rate limited, and a few wrong ones in a row lock the attempts for a while. + +**Either side can unlink at any time** (Edit access). Both webhooks are deleted, the message map goes, and +both channels are told. If Sylo is removed from a server, the server on the other end loses its webhook and +is told too. + +**Pause** stops the bridge from either side. Only the server that paused it can resume it. A link that Sylo +stopped itself (see below) can be resumed by either server once the cause is fixed. + +## What crosses + +| | | +|---|---| +| Messages | Posted by a webhook in the other channel under the author's display name and picture, with the platform in the name ("Alice [Discord]"). | +| Replies | A reply starts with a short quote of the message it answers, with a link to it on the other side when Sylo knows the copy. | +| Files | Re-uploaded up to **8 MB** each and 8 MB per message (at most 10 files). Larger files, files that cannot be fetched, and every file when a server turns uploads off, cross as a link. | +| Emoji | Unicode emoji as they are. A custom emoji crosses as the custom emoji **with the same name** in the other server, otherwise as `:name:`. | +| Stickers | As a picture link. | +| Embeds | Rich embeds (from bots) with their text made harmless. A link preview is left out: the other platform makes its own. | +| Polls | As text: the question and the answers. Fluxer has no polls and a vote cannot count on both sides, so the vote stays where the poll is. | +| Edits and deletes | Mirrored through a message map that keeps a message for **30 days** (the daily retention job removes older rows). An edit or delete of an older message is not followed. Deleting a copy does not delete the original. | +| Joins | Optional, per server: a line "👋 Zed joined Discord Home" on the other side. Needs the Server Members intent on Discord. | + +## Safety rules + +- **No ping crosses.** Every copy is sent with allowed mentions set to none, and the text itself is changed: + `@everyone` and `@here` get an invisible character that breaks them, and users, roles and channels become + plain names ("@Bobby", "@moderators", "#rules"). +- **No loops or echoes.** A message posted by any Sylo bridge webhook, or by Sylo itself, is never relayed. + A channel is in only one link, so a copy cannot find its way back. +- **Bots are off by default.** Each server decides for itself what it sends: **bot messages**, **who + joins** and **copy files** are options of that server's side only, and the other server cannot change them. + System messages (joins, pins, boosts) are never relayed. +- **Age restriction.** An age-restricted channel can only be linked to an age-restricted channel. It is + checked when linking, when resuming, and on **every message**: if one of the channels changes, the link + stops. +- **Visible notice.** Both channels get a notice while a bridge is on, and when it is paused, stopped or + ended. +- **Rate limits.** Each webhook has its own queue: sends are spaced out, a rate limit from the platform is + waited out, a short outage is retried with growing pauses, and a message that waited more than five + minutes is dropped. A queue holds at most 100 messages; a flooded channel drops the newest and counts it. + One server's queue never slows another's. +- **A failing link stops.** No permission, a webhook Sylo cannot make again, a deleted channel, or five + failed deliveries in a row stop the link. Sylo says so in both channels, the **Health** page shows it as a + problem with the reason, and it is not retried until someone presses **Resume**. +- **Webhooks are Sylo's.** Sylo makes and manages the webhook of each channel and makes a new one if it is + deleted. Its token is stored in the database and never sent to the browser. A database backup therefore + holds it, like every other secret; encrypt off-site backups. + +## Needs + +- **Manage Webhooks**, View Channel and Send Messages in each bridged channel (the Health page checks them). +- The **Message Content** intent (`INTENT_MESSAGE_CONTENT`, on by default). +- Edit access to the dashboard in the server that makes the request and in the server that uses the code. + +## API + +The Sylo app can read and change a bridge through `/api/v2` (`bridge` in the app's feature list). A GET at +View level, everything else at Edit level. See [mobile-app.md](../mobile-app.md). diff --git a/src/db/bridge.js b/src/db/bridge.js new file mode 100644 index 00000000..3af0da9f --- /dev/null +++ b/src/db/bridge.js @@ -0,0 +1,469 @@ +// Bridge storage: the links between a Discord channel and a Fluxer channel, the one-time codes that +// start a link, and the map from a message to its copy on the other side. +// +// A link has two sides. Side "a" is the server that asked for the link (it made the code), side "b" +// is the server that accepted it with the code. Each side has its own channel, its own options (what +// leaves THAT server) and its own webhook, so each server controls what it sends and neither side can +// change what the other shares. +// +// bridge_links guild_a/channel_a, guild_b/channel_b server keys (a Discord id, or "fluxer:") +// status active | paused (a server paused it) | failed (stopped by Sylo) +// paused_side 'a' or 'b' when status is paused: only that server may resume it +// options_x JSON text, see normaliseOptions in modules/lib/bridgeText.js +// hook_x_id / hook_x_token the webhook Sylo made in that channel. The token is a +// secret: it never leaves this table (the dashboard only gets "has a webhook") +// requested_by_name / accepted_by_name display names for the audit trail, not user ids +// bridge_codes a pending request: only the SHA-256 of the code is stored, it expires, and it is +// used up by the first server that accepts it +// bridge_messages a_message_id <-> b_message_id for edits, deletes and replies; swept after 30 days +// +// The server keys are in `guild_a` / `guild_b`, not `guild_id`, so purgeBridgeGuild() below deletes by +// either side (the guild-leave purge in purge.js calls it). bridge_codes has a plain guild_id and is in +// GUILD_TABLES. +import { createHash, randomBytes } from 'node:crypto'; +import { prepare, registerPostgresBootstrap } from './driver.js'; + +registerPostgresBootstrap(` + CREATE TABLE IF NOT EXISTS bridge_links ( + id SERIAL PRIMARY KEY, + guild_a TEXT NOT NULL, + channel_a TEXT NOT NULL, + guild_b TEXT NOT NULL, + channel_b TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'active', + paused_side TEXT NOT NULL DEFAULT '', + fail_reason TEXT NOT NULL DEFAULT '', + failure_count INTEGER NOT NULL DEFAULT 0, + options_a TEXT NOT NULL DEFAULT '{}', + options_b TEXT NOT NULL DEFAULT '{}', + hook_a_id TEXT NOT NULL DEFAULT '', + hook_a_token TEXT NOT NULL DEFAULT '', + hook_b_id TEXT NOT NULL DEFAULT '', + hook_b_token TEXT NOT NULL DEFAULT '', + requested_by_name TEXT NOT NULL DEFAULT '', + accepted_by_name TEXT NOT NULL DEFAULT '', + created_at BIGINT NOT NULL, + updated_at BIGINT NOT NULL + ); + CREATE UNIQUE INDEX IF NOT EXISTS idx_bridge_links_a ON bridge_links (guild_a, channel_a); + CREATE UNIQUE INDEX IF NOT EXISTS idx_bridge_links_b ON bridge_links (guild_b, channel_b); + CREATE INDEX IF NOT EXISTS idx_bridge_links_guild_b ON bridge_links (guild_b); + CREATE TABLE IF NOT EXISTS bridge_codes ( + id SERIAL PRIMARY KEY, + guild_id TEXT NOT NULL, + channel_id TEXT NOT NULL, + code_hash TEXT NOT NULL, + options TEXT NOT NULL DEFAULT '{}', + requested_by_name TEXT NOT NULL DEFAULT '', + expires_at BIGINT NOT NULL, + created_at BIGINT NOT NULL + ); + CREATE UNIQUE INDEX IF NOT EXISTS idx_bridge_codes_hash ON bridge_codes (code_hash); + CREATE INDEX IF NOT EXISTS idx_bridge_codes_guild ON bridge_codes (guild_id); + CREATE TABLE IF NOT EXISTS bridge_messages ( + link_id INTEGER NOT NULL, + origin TEXT NOT NULL, + a_message_id TEXT NOT NULL, + b_message_id TEXT NOT NULL, + copy_hook_id TEXT NOT NULL DEFAULT '', + content_hash TEXT NOT NULL DEFAULT '', + extra TEXT NOT NULL DEFAULT '{}', + created_at BIGINT NOT NULL, + PRIMARY KEY (link_id, a_message_id) + ); + CREATE UNIQUE INDEX IF NOT EXISTS idx_bridge_messages_b ON bridge_messages (link_id, b_message_id); + CREATE INDEX IF NOT EXISTS idx_bridge_messages_created ON bridge_messages (created_at); +`); + +/** How long a message map row is kept: the window in which an edit, a delete or a reply still follows. */ +export const MESSAGE_MAP_TTL_MS = 30 * 86_400_000; + +const s = { + insertLink: prepare( + `INSERT INTO bridge_links + (guild_a, channel_a, guild_b, channel_b, status, options_a, options_b, hook_a_id, hook_a_token, + hook_b_id, hook_b_token, requested_by_name, accepted_by_name, created_at, updated_at) + VALUES (@guildA, @channelA, @guildB, @channelB, 'active', @optionsA, @optionsB, @hookAId, @hookAToken, + @hookBId, @hookBToken, @requestedBy, @acceptedBy, @now, @now)`, + { returningId: true } + ), + link: prepare('SELECT * FROM bridge_links WHERE id = ?'), + linksOfGuild: prepare('SELECT * FROM bridge_links WHERE guild_a = ? OR guild_b = ? ORDER BY id'), + linkByChannelA: prepare('SELECT * FROM bridge_links WHERE guild_a = ? AND channel_a = ?'), + linkByChannelB: prepare('SELECT * FROM bridge_links WHERE guild_b = ? AND channel_b = ?'), + linkByHook: prepare( + "SELECT * FROM bridge_links WHERE (hook_a_id = @id AND hook_a_id <> '') OR (hook_b_id = @id AND hook_b_id <> '')" + ), + countLinks: prepare('SELECT COUNT(*) AS n FROM bridge_links WHERE guild_a = ? OR guild_b = ?'), + setStatus: prepare( + `UPDATE bridge_links SET status = @status, paused_side = @pausedSide, fail_reason = @reason, + failure_count = @failures, updated_at = @now WHERE id = @id` + ), + setFailures: prepare('UPDATE bridge_links SET failure_count = @failures, updated_at = @now WHERE id = @id'), + setOptionsA: prepare('UPDATE bridge_links SET options_a = @options, updated_at = @now WHERE id = @id'), + setOptionsB: prepare('UPDATE bridge_links SET options_b = @options, updated_at = @now WHERE id = @id'), + setHookA: prepare( + 'UPDATE bridge_links SET hook_a_id = @id2, hook_a_token = @token, updated_at = @now WHERE id = @id' + ), + setHookB: prepare( + 'UPDATE bridge_links SET hook_b_id = @id2, hook_b_token = @token, updated_at = @now WHERE id = @id' + ), + delLink: prepare('DELETE FROM bridge_links WHERE id = ?'), + delLinkMessages: prepare('DELETE FROM bridge_messages WHERE link_id = ?'), + linkIdsOfGuild: prepare('SELECT id FROM bridge_links WHERE guild_a = ? OR guild_b = ?'), + delLinksOfGuild: prepare('DELETE FROM bridge_links WHERE guild_a = ? OR guild_b = ?'), + + insertCode: prepare( + `INSERT INTO bridge_codes (guild_id, channel_id, code_hash, options, requested_by_name, expires_at, created_at) + VALUES (@guildId, @channelId, @hash, @options, @requestedBy, @expiresAt, @now)`, + { returningId: true } + ), + codeByHash: prepare('SELECT * FROM bridge_codes WHERE code_hash = ? AND expires_at > ?'), + codesOfGuild: prepare( + 'SELECT id, guild_id, channel_id, requested_by_name, expires_at, created_at FROM bridge_codes WHERE guild_id = ? AND expires_at > ? ORDER BY id' + ), + countCodes: prepare('SELECT COUNT(*) AS n FROM bridge_codes WHERE guild_id = ? AND expires_at > ?'), + codeChannel: prepare( + 'SELECT id FROM bridge_codes WHERE guild_id = ? AND channel_id = ? AND expires_at > ?' + ), + delCode: prepare('DELETE FROM bridge_codes WHERE id = ?'), + delCodeOfGuild: prepare('DELETE FROM bridge_codes WHERE id = ? AND guild_id = ?'), + delExpiredCodes: prepare('DELETE FROM bridge_codes WHERE expires_at <= ?'), + + insertMap: prepare( + `INSERT INTO bridge_messages (link_id, origin, a_message_id, b_message_id, copy_hook_id, content_hash, extra, created_at) + VALUES (@linkId, @origin, @aId, @bId, @hookId, @hash, @extra, @now) + ON CONFLICT (link_id, a_message_id) DO UPDATE SET + origin = excluded.origin, b_message_id = excluded.b_message_id, + copy_hook_id = excluded.copy_hook_id, content_hash = excluded.content_hash, + extra = excluded.extra` + ), + mapByA: prepare('SELECT * FROM bridge_messages WHERE link_id = ? AND a_message_id = ?'), + mapByB: prepare('SELECT * FROM bridge_messages WHERE link_id = ? AND b_message_id = ?'), + setHash: prepare( + 'UPDATE bridge_messages SET content_hash = ? WHERE link_id = ? AND a_message_id = ? AND b_message_id = ?' + ), + delMapByA: prepare('DELETE FROM bridge_messages WHERE link_id = ? AND a_message_id = ?'), + delMapByB: prepare('DELETE FROM bridge_messages WHERE link_id = ? AND b_message_id = ?'), + sweepMap: prepare('DELETE FROM bridge_messages WHERE created_at < ?'), + countMap: prepare('SELECT COUNT(*) AS n FROM bridge_messages WHERE link_id = ?'), +}; + +// --- links ----------------------------------------------------------------------------------------- + +const parseJson = (text) => { + try { + const value = JSON.parse(text); + return value && typeof value === 'object' && !Array.isArray(value) ? value : {}; + } catch { + return {}; + } +}; + +/** A stored row as the code uses it (numbers as numbers, options parsed). */ +export function linkFrom(row) { + if (!row) return null; + return { + id: Number(row.id), + guildA: row.guild_a, + channelA: row.channel_a, + guildB: row.guild_b, + channelB: row.channel_b, + status: row.status, + pausedSide: row.paused_side || '', + failReason: row.fail_reason || '', + failureCount: Number(row.failure_count) || 0, + optionsA: parseJson(row.options_a), + optionsB: parseJson(row.options_b), + hookAId: row.hook_a_id || '', + hookAToken: row.hook_a_token || '', + hookBId: row.hook_b_id || '', + hookBToken: row.hook_b_token || '', + requestedBy: row.requested_by_name || '', + acceptedBy: row.accepted_by_name || '', + createdAt: Number(row.created_at), + updatedAt: Number(row.updated_at), + }; +} + +/** + * Create a link. Returns its id, or null when either channel is already in a link (the unique indexes + * decide, so two requests that race cannot both win). + * @param {{ guildA: string, channelA: string, guildB: string, channelB: string, optionsA?: object, + * optionsB?: object, hookA?: { id: string, token: string }, hookB?: { id: string, token: string }, + * requestedBy?: string, acceptedBy?: string }} link + */ +export async function createLink(link) { + try { + const res = await s.insertLink.run({ + guildA: link.guildA, + channelA: link.channelA, + guildB: link.guildB, + channelB: link.channelB, + optionsA: JSON.stringify(link.optionsA ?? {}), + optionsB: JSON.stringify(link.optionsB ?? {}), + hookAId: link.hookA?.id ?? '', + hookAToken: link.hookA?.token ?? '', + hookBId: link.hookB?.id ?? '', + hookBToken: link.hookB?.token ?? '', + requestedBy: String(link.requestedBy ?? '').slice(0, 100), + acceptedBy: String(link.acceptedBy ?? '').slice(0, 100), + now: Date.now(), + }); + return Number(res.lastInsertRowid); + } catch (err) { + if (/unique|duplicate/i.test(String(err?.message ?? err))) return null; + throw err; + } +} + +export const getLink = async (id) => linkFrom(await s.link.get(Number(id))); +export const linksOfGuild = async (key) => (await s.linksOfGuild.all(key, key)).map(linkFrom); +export const linkCount = async (key) => Number((await s.countLinks.get(key, key)).n); + +/** + * The link a channel is in and which side the channel is, or null. + * @returns {Promise<{ link: ReturnType, side: 'a' | 'b' } | null>} + */ +export async function linkForChannel(guildKey, channelId) { + const a = await s.linkByChannelA.get(guildKey, channelId); + if (a) return { link: linkFrom(a), side: 'a' }; + const b = await s.linkByChannelB.get(guildKey, channelId); + if (b) return { link: linkFrom(b), side: 'b' }; + return null; +} + +/** The link that owns a webhook id (one of Sylo's bridge webhooks), or null. */ +export async function linkForHook(hookId) { + if (!hookId) return null; + return linkFrom(await s.linkByHook.get({ id: String(hookId) })); +} + +export const STATUSES = Object.freeze(['active', 'paused', 'failed']); + +/** Change the state of a link. `pausedSide` only means something while paused. */ +export async function setLinkStatus(id, { status, pausedSide = '', reason = '', failures = 0 }) { + await s.setStatus.run({ + id: Number(id), + status, + pausedSide: status === 'paused' ? pausedSide : '', + reason: String(reason).slice(0, 300), + failures, + now: Date.now(), + }); +} + +/** Remember how many sends in a row failed (reset to 0 by a success). */ +export const setLinkFailures = (id, failures) => + s.setFailures.run({ id: Number(id), failures, now: Date.now() }); + +export const setLinkOptions = (id, side, options) => + (side === 'a' ? s.setOptionsA : s.setOptionsB).run({ + id: Number(id), + options: JSON.stringify(options ?? {}), + now: Date.now(), + }); + +/** Store the webhook of one side (after Sylo made a new one). */ +export const setLinkHook = (id, side, hook) => + (side === 'a' ? s.setHookA : s.setHookB).run({ + id: Number(id), + id2: hook?.id ?? '', + token: hook?.token ?? '', + now: Date.now(), + }); + +/** Delete a link and every message-map row of it. */ +export async function deleteLink(id) { + await s.delLinkMessages.run(Number(id)); + return (await s.delLink.run(Number(id))).changes; +} + +/** + * Delete every link a server is in, with the message maps of them. For the guild-leave purge and + * /forget-style erasure. Returns the removed links so the caller can tell the other side. + */ +export async function purgeBridgeGuild(key) { + const ids = (await s.linkIdsOfGuild.all(key, key)).map((r) => Number(r.id)); + for (const id of ids) await s.delLinkMessages.run(id); + await s.delLinksOfGuild.run(key, key); + return ids.length; +} + +// --- codes ----------------------------------------------------------------------------------------- + +/** No 0/O/1/I/L: a code read out loud or copied by hand is not mistaken. */ +const CODE_ALPHABET = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789'; +export const CODE_LENGTH = 12; +export const CODE_TTL_MS = 15 * 60_000; + +/** A new random code, shown as XXXX-XXXX-XXXX (12 characters of a 31-letter alphabet: about 59 bits). */ +export function newCode() { + const bytes = randomBytes(CODE_LENGTH); + let out = ''; + for (let i = 0; i < CODE_LENGTH; i += 1) { + out += CODE_ALPHABET[bytes[i] % CODE_ALPHABET.length]; + if (i % 4 === 3 && i < CODE_LENGTH - 1) out += '-'; + } + return out; +} + +/** The text of a code as typed: upper-case, without dashes or spaces. */ +export function normaliseCode(text) { + return String(text ?? '') + .toUpperCase() + .replace(/[\s-]/g, ''); +} + +// Only the hash is stored; the code itself is shown once, when it is made. +const hashOf = (text) => + createHash('sha256') + .update(`sylo-bridge-code:${normaliseCode(text)}`) + .digest('hex'); + +const CODE_SHAPE = new RegExp(`^[${CODE_ALPHABET}]{${CODE_LENGTH}}$`); + +/** Is this text the right shape for a code (checked before any database lookup)? */ +export const looksLikeCode = (text) => CODE_SHAPE.test(normaliseCode(text)); + +/** + * Store a new request and return the code (shown once: only its hash is kept). + * @returns {Promise<{ id: number, code: string, expiresAt: number }>} + */ +export async function createCode({ + guildId, + channelId, + options = {}, + requestedBy = '', + ttlMs = CODE_TTL_MS, +}) { + const now = Date.now(); + const code = newCode(); + const res = await s.insertCode.run({ + guildId, + channelId, + hash: hashOf(code), + options: JSON.stringify(options), + requestedBy: String(requestedBy).slice(0, 100), + expiresAt: now + ttlMs, + now, + }); + return { id: Number(res.lastInsertRowid), code, expiresAt: now + ttlMs }; +} + +/** + * Use a code up: returns the request it belonged to, or null when it is unknown, expired or already + * used. The DELETE decides who gets it, so two servers accepting at once cannot both succeed. + */ +export async function claimCode(text, now = Date.now()) { + if (!looksLikeCode(text)) return null; + const row = await s.codeByHash.get(hashOf(text), now); + if (!row) return null; + if ((await s.delCode.run(row.id)).changes !== 1) return null; + return { + guildId: row.guild_id, + channelId: row.channel_id, + options: parseJson(row.options), + requestedBy: row.requested_by_name, + expiresAt: Number(row.expires_at), + }; +} + +export async function pendingCodes(guildId, now = Date.now()) { + return (await s.codesOfGuild.all(guildId, now)).map((r) => ({ + id: Number(r.id), + channelId: r.channel_id, + requestedBy: r.requested_by_name, + expiresAt: Number(r.expires_at), + createdAt: Number(r.created_at), + })); +} + +export const pendingCodeCount = async (guildId, now = Date.now()) => + Number((await s.countCodes.get(guildId, now)).n); + +/** Is a request for this channel already waiting? */ +export const channelHasPendingCode = async (guildId, channelId, now = Date.now()) => + Boolean(await s.codeChannel.get(guildId, channelId, now)); + +/** Cancel one of a server's own requests. @returns {Promise} */ +export async function cancelCode(guildId, id) { + return (await s.delCodeOfGuild.run(Number(id), guildId)).changes === 1; +} + +/** Drop requests past their time (the daily retention sweep). */ +export const sweepCodes = async (now = Date.now()) => (await s.delExpiredCodes.run(now)).changes; + +// --- the message map ------------------------------------------------------------------------------- + +/** + * Remember that a message on one side has a copy on the other. + * @param {number} linkId + * @param {'a' | 'b'} origin the side the ORIGINAL message was on + * @param {{ sourceId: string, copyId: string, hookId?: string, hash?: string }} ids + */ +export async function saveMapping(linkId, origin, { sourceId, copyId, hookId = '', hash = '', extra = {} }) { + await s.insertMap.run({ + linkId: Number(linkId), + origin, + aId: origin === 'a' ? sourceId : copyId, + bId: origin === 'a' ? copyId : sourceId, + hookId, + hash, + extra: JSON.stringify(extra ?? {}).slice(0, 4000), + now: Date.now(), + }); +} + +const mapFrom = (row) => + row && { + linkId: Number(row.link_id), + origin: row.origin, + aMessageId: row.a_message_id, + bMessageId: row.b_message_id, + copyHookId: row.copy_hook_id || '', + contentHash: row.content_hash || '', + extra: parseJson(row.extra), + createdAt: Number(row.created_at), + }; + +/** + * The mapping a message on `side` takes part in, whether it is the original or the copy, with the id + * of its counterpart on the other side. Null when it is not mapped (older than the map, or never relayed). + * @returns {Promise<(NonNullable> & { counterpartId: string, isOriginal: boolean }) | null>} + */ +export async function mappingOf(linkId, side, messageId) { + const row = mapFrom(await (side === 'a' ? s.mapByA : s.mapByB).get(Number(linkId), String(messageId))); + if (!row) return null; + return { + ...row, + counterpartId: side === 'a' ? row.bMessageId : row.aMessageId, + isOriginal: row.origin === side, + }; +} + +export const setMappingHash = (linkId, aId, bId, hash) => s.setHash.run(hash, Number(linkId), aId, bId); + +/** Read a request by its code without using it up (the accepting server's checks run before the claim). */ +export async function peekCode(text, now = Date.now()) { + if (!looksLikeCode(text)) return null; + const row = await s.codeByHash.get(hashOf(text), now); + if (!row) return null; + return { + guildId: row.guild_id, + channelId: row.channel_id, + options: parseJson(row.options), + requestedBy: row.requested_by_name, + expiresAt: Number(row.expires_at), + }; +} + +/** Forget a message's mapping (its original was deleted). */ +export const deleteMapping = (linkId, side, messageId) => + (side === 'a' ? s.delMapByA : s.delMapByB).run(Number(linkId), String(messageId)); + +export const mappingCount = async (linkId) => Number((await s.countMap.get(Number(linkId))).n); + +/** Delete map rows older than the retention window (the daily sweep in retention.js). */ +export const sweepMappings = async (now = Date.now()) => + (await s.sweepMap.run(now - MESSAGE_MAP_TTL_MS)).changes; diff --git a/src/db/index.js b/src/db/index.js index 5991616a..29b5002c 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -1594,6 +1594,69 @@ export const MIGRATIONS = [ if (!has.has('decided_by_user_id')) database.exec('ALTER TABLE appeals ADD COLUMN decided_by_user_id TEXT;'); }, + + // Versions 81 and 82 are taken by other 5.3 changes (the look of the public member pages, and the + // tickets index). Their branches add their own steps here; until they are merged these two stay empty, + // so the Bridge below keeps the version it was given (83) and a database never skips a step. + () => {}, + () => {}, + + // Bridge (src/db/bridge.js, which bootstraps the same tables on Postgres): the links between a Discord + // channel and a Fluxer channel, the one-time codes that start one (only the hash is kept) and the map + // from a message to its copy on the other side (swept after 30 days by the retention job). + (database) => { + database.exec(` + CREATE TABLE bridge_links ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + guild_a TEXT NOT NULL, + channel_a TEXT NOT NULL, + guild_b TEXT NOT NULL, + channel_b TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'active', + paused_side TEXT NOT NULL DEFAULT '', + fail_reason TEXT NOT NULL DEFAULT '', + failure_count INTEGER NOT NULL DEFAULT 0, + options_a TEXT NOT NULL DEFAULT '{}', + options_b TEXT NOT NULL DEFAULT '{}', + hook_a_id TEXT NOT NULL DEFAULT '', + hook_a_token TEXT NOT NULL DEFAULT '', + hook_b_id TEXT NOT NULL DEFAULT '', + hook_b_token TEXT NOT NULL DEFAULT '', + requested_by_name TEXT NOT NULL DEFAULT '', + accepted_by_name TEXT NOT NULL DEFAULT '', + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL + ); + CREATE UNIQUE INDEX idx_bridge_links_a ON bridge_links (guild_a, channel_a); + CREATE UNIQUE INDEX idx_bridge_links_b ON bridge_links (guild_b, channel_b); + CREATE INDEX idx_bridge_links_guild_b ON bridge_links (guild_b); + CREATE TABLE bridge_codes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + guild_id TEXT NOT NULL, + channel_id TEXT NOT NULL, + code_hash TEXT NOT NULL, + options TEXT NOT NULL DEFAULT '{}', + requested_by_name TEXT NOT NULL DEFAULT '', + expires_at INTEGER NOT NULL, + created_at INTEGER NOT NULL + ); + CREATE UNIQUE INDEX idx_bridge_codes_hash ON bridge_codes (code_hash); + CREATE INDEX idx_bridge_codes_guild ON bridge_codes (guild_id); + CREATE TABLE bridge_messages ( + link_id INTEGER NOT NULL, + origin TEXT NOT NULL, + a_message_id TEXT NOT NULL, + b_message_id TEXT NOT NULL, + copy_hook_id TEXT NOT NULL DEFAULT '', + content_hash TEXT NOT NULL DEFAULT '', + extra TEXT NOT NULL DEFAULT '{}', + created_at INTEGER NOT NULL, + PRIMARY KEY (link_id, a_message_id) + ); + CREATE UNIQUE INDEX idx_bridge_messages_b ON bridge_messages (link_id, b_message_id); + CREATE INDEX idx_bridge_messages_created ON bridge_messages (created_at); + `); + }, ]; /** Highest schema version this build knows how to run. */ diff --git a/src/db/purge.js b/src/db/purge.js index fcc06f41..bb243dc1 100644 --- a/src/db/purge.js +++ b/src/db/purge.js @@ -10,6 +10,7 @@ // deleted on the next attempt — self-healing, not a race. So both run as a // plain sequence of awaited statements instead of a transaction. import { prepare } from './driver.js'; +import { log } from '../lib/log.js'; import { clearCommandPrefixCache } from './guildSettings.js'; import { eraseMember } from './economy.js'; import { eraseInventory } from './economyShop.js'; @@ -65,6 +66,7 @@ import './welcomeSent.js'; import './applications.js'; import './economy.js'; import './economyShop.js'; +import { purgeBridgeGuild } from './bridge.js'; // Tables keyed directly by guild_id. A test in test/guildTables.test.js checks // this stays in sync with the schema so new guild data can't escape /forget or @@ -121,6 +123,9 @@ export const GUILD_TABLES = [ 'economy_items', 'economy_inventory', 'collectible_images', + // The requests nobody has accepted yet. The links name two servers (guild_a, guild_b), so they go + // through purgeBridgeGuild() in purgeGuild() below. + 'bridge_codes', ]; const simpleStmts = GUILD_TABLES.map((t) => prepare(`DELETE FROM ${t} WHERE guild_id = ?`)); @@ -141,14 +146,34 @@ const suggestionVoteStmt = prepare( 'DELETE FROM guild_suggestion_votes WHERE suggestion_id IN (SELECT id FROM guild_suggestions WHERE guild_id = ?)' ); +/** Work a module must do before the server's rows go (the bridge tells the server on its other end). */ +const beforePurge = []; + +/** + * Register something to run first when a server is purged, while its rows can still be read. A failing + * hook never stops the purge. + * @param {(guildId: string) => Promise} fn + */ +export function onBeforePurge(fn) { + beforePurge.push(fn); +} + /** Remove all of a guild's stored data. */ export async function purgeGuild(guildId) { + for (const fn of beforePurge) { + try { + await fn(guildId); + } catch (err) { + log.warn('purge', `a clean-up before the purge of ${guildId} failed:`, err); + } + } await ticketMsgStmt.run(guildId); // before `tickets`, which the subquery reads await giveawayEntryStmt.run(guildId); // before `giveaways`, which the subquery reads await giveawayPrizeStmt.run(guildId); await eventParticipantStmt.run(guildId); // before `guild_events`, which the subquery reads await suggestionVoteStmt.run(guildId); // before `guild_suggestions`, which the subquery reads for (const stmt of simpleStmts) await stmt.run(guildId); + await purgeBridgeGuild(guildId); // the links this server is in (on either side) and their message maps clearCommandPrefixCache(guildId); } diff --git a/src/db/retention.js b/src/db/retention.js index 13810f6c..5ca0dbc5 100644 --- a/src/db/retention.js +++ b/src/db/retention.js @@ -23,6 +23,7 @@ import { log } from '../lib/log.js'; // Owners of the tables swept below, for their Postgres bootstrap. import './tickets.js'; import './cache.js'; +import { sweepCodes, sweepMappings } from './bridge.js'; const DAY_MS = 86_400_000; const MAX_DAYS = 3650; // 10 years — matches the dashboard input cap @@ -88,6 +89,12 @@ export async function sweepRetention(now = Date.now()) { const orphanedTicketMessages = (await delOrphanedTicketMsgs.run()).changes; const staleStats = (await delStaleStats.run(now - STATS_KEEP_MS)).changes; + // The bridge's message map keeps a message for 30 days (edits, deletes and replies follow it that + // long), and a request nobody accepted is gone after its 15 minutes. Not part of the result: this + // is housekeeping of a table the bridge owns. + await sweepMappings(now); + await sweepCodes(now); + const result = { closedTickets, ticketMessages, inactiveCases, orphanedTicketMessages, staleStats }; if (result.closedTickets || result.inactiveCases) { log.info( diff --git a/src/modules/bridge.js b/src/modules/bridge.js new file mode 100644 index 00000000..cf932f0d --- /dev/null +++ b/src/modules/bridge.js @@ -0,0 +1,1087 @@ +// Bridge: one conversation between a Discord channel and a Fluxer channel. +// +// A link joins two channels, one on each platform. It starts with a request from one server (a one-time +// code that expires in 15 minutes) and is accepted, with that code, by someone with Edit access in the +// other server. Both servers therefore agreed, Sylo must be in both, and either can unlink at any time. +// Each side has its own options (what leaves THAT server), so neither can make the other share more. +// +// What crosses, through a webhook Sylo makes in each channel: messages (with the author's name and picture), +// replies (a quote of the original), files (re-uploaded within limits, otherwise a link), custom emoji with the +// same name, stickers, rich embeds, polls (as text) and, if a server turns it on, join messages. Edits and +// deletes follow through the message map (db/bridge.js), which keeps a message 30 days. +// +// Safety, in the order a message meets it: +// - no loop: a message sent by any Sylo bridge webhook, or by Sylo itself, is never relayed again, and a +// channel is in at most one link, so a copy cannot come back +// - no ping: every copy is sent with allowedMentions none, and the text has its mentions turned into names +// - bots are off by default (per side), and a message that is not a plain chat message is never relayed +// - an age-restricted channel is only ever linked to an age-restricted channel, checked at link time and again +// on every message (a change pauses the link) +// - the webhook is rate limited: one queue per webhook, spaced sends, back-off on 429, a cap on the queue +// - a link that fails (no permission, webhook cannot be remade, delivery fails five times in a row) is +// stopped, shown as a problem in Health, and announced in both channels; it is not retried forever +// - each server is isolated: queues, links and failures are per link, never shared between servers +import { ChannelType } from 'discord.js'; +import { on } from './dispatch.js'; +import { getGuildModule, isModuleEnabled } from '../db/modules.js'; +import { recordAudit } from '../db/audit.js'; +import { onBeforePurge } from '../db/purge.js'; +import * as store from '../db/bridge.js'; +import { runtime } from '../runtime.js'; +import { splitPlatformGuildId } from '../lib/platformId.js'; +import { guildByKey } from '../platform/platformOf.js'; +import { createWebhookTransport } from '../platform/webhooks.js'; +import { jumpUrl } from './quoteBoard.js'; +import { safeFetch } from '../lib/safeFetch.js'; +import { log } from '../lib/log.js'; +import { BridgeQueueError, classifyError, createRelayQueue } from './lib/bridgeQueue.js'; +import { + FILE_BYTES_LIMIT, + fileLinkLines, + hashContent, + ZWSP, + inertAt, + joinParts, + mapEmoji, + neutraliseMentions, + normaliseOptions, + pollText, + planAttachments, + plainEmbeds, + plainTimestamps, + replyHead, + senderName, + stickerLines, +} from './lib/bridgeText.js'; + +// The module has no settings of its own (modules/limits.js lists none); these are its ceilings, sent to the +// page with the overview. +/** The most links a server can have, and the most requests it can have waiting. */ +export const MAX_LINKS = 10; +export const MAX_REQUESTS = 5; + +/** How many failed deliveries in a row stop a link. */ +export const FAIL_THRESHOLD = 5; +const HOOK_NAME = 'Sylo bridge'; +const CACHE_MS = 60_000; + +/** A refusal the dashboard shows as it is. `status` is the HTTP answer. */ +export class BridgeError extends Error { + constructor(message, status = 400) { + super(message); + this.name = 'BridgeError'; + this.status = status; + } +} + +// --- wiring that tests replace ----------------------------------------------------------------------- + +let transport = createWebhookTransport(); +let queue = createRelayQueue(); +let download = (url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 }); + +/** Use another transport, queue or downloader (tests); call with nothing to go back to the real ones. */ +export function useBridgeParts({ transport: t, queue: q, download: d } = {}) { + transport = t ?? createWebhookTransport(); + queue = q ?? createRelayQueue(); + download = d ?? ((url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 })); +} + +// Relays that are still on their way, so a test (and a shutdown) can wait for them. +const inFlight = new Set(); +const track = (promise) => { + inFlight.add(promise); + promise.finally(() => inFlight.delete(promise)).catch(() => {}); + return promise; +}; +/** Resolves when every relay started so far has been delivered or given up. */ +export const bridgeIdle = async () => { + while (inFlight.size) await Promise.allSettled([...inFlight]); +}; + +// --- small helpers ----------------------------------------------------------------------------------- + +const other = (side) => (side === 'a' ? 'b' : 'a'); +const guildOf = (link, side) => (side === 'a' ? link.guildA : link.guildB); +const channelOf = (link, side) => (side === 'a' ? link.channelA : link.channelB); +const optionsOf = (link, side) => normaliseOptions(side === 'a' ? link.optionsA : link.optionsB); +const hookOf = (link, side) => + side === 'a' + ? { id: link.hookAId, token: link.hookAToken, channelId: link.channelA } + : { id: link.hookBId, token: link.hookBToken, channelId: link.channelB }; +const sideOf = (link, key) => (link.guildA === key ? 'a' : link.guildB === key ? 'b' : null); +const platformOf = (key) => splitPlatformGuildId(key)?.platform ?? 'discord'; +const laneKey = (link, side) => `${link.id}|${side}`; +const TEXT_TYPES = new Set([ChannelType.GuildText, ChannelType.GuildAnnouncement]); + +const channelIn = (key, channelId) => guildByKey(key)?.channels?.cache?.get?.(channelId) ?? null; +const isTextChannel = (channel) => Boolean(channel) && TEXT_TYPES.has(channel.type); +const isAgeRestricted = (channel) => channel?.nsfw === true; + +/** Cached lookups of "which link is this channel in", dropped whenever a link changes. */ +const cache = new Map(); +export const dropBridgeCache = () => cache.clear(); +async function findLink(key, channelId) { + const id = `${key}|${channelId}`; + const hit = cache.get(id); + if (hit && Date.now() - hit.at < CACHE_MS) return hit.value; + const value = await store.linkForChannel(key, channelId); + if (cache.size > 5000) cache.clear(); + cache.set(id, { at: Date.now(), value }); + return value; +} + +/** Say something in a channel (the visible notices). Best effort: a channel Sylo cannot write in is skipped. */ +async function say(key, channelId, content) { + try { + await channelIn(key, channelId)?.send?.({ content, allowedMentions: { parse: [] } }); + } catch (err) { + log.debug('bridge', `could not post a notice in ${channelId}: ${err?.message ?? err}`); + } +} + +const platformName = (key) => (platformOf(key) === 'fluxer' ? 'Fluxer' : 'Discord'); +const serverName = (key) => inertAt(guildByKey(key)?.name ?? 'another server'); +const channelName = (key, channelId) => channelIn(key, channelId)?.name ?? 'a channel'; + +/** The notice both channels show while a bridge is on. */ +const onNotice = (mineKey, theirKey, theirChannelId) => + `🌉 **Bridge on.** Messages in this channel are shared with #${channelName(theirKey, theirChannelId)} in ` + + `**${serverName(theirKey)}** on ${platformName(theirKey)}, and theirs appear here. Nobody is pinged across ` + + `the bridge. Server admins can pause or unlink it at any time in the Sylo dashboard.`; + +const REASONS = { + permissions: + 'Sylo is missing a permission it needs here (Manage Webhooks, or to send in the channel). Fix the permission, then resume the bridge in the dashboard.', + webhook: 'Sylo could not make a new webhook after the old one was deleted.', + unreachable: 'Messages could not be delivered several times in a row.', + 'channel-deleted': 'A bridged channel was deleted.', + 'channel-missing': 'Sylo cannot see one of the bridged channels.', + 'age-restriction': + 'One channel changed its age restriction, so the two no longer match. An age-restricted channel can only be bridged with an age-restricted one.', +}; +/** The text a link's stored failure reason stands for. */ +export const reasonText = (code) => REASONS[code] ?? 'The bridge stopped working.'; + +// --- the pieces of a link, looked up on the platforms ------------------------------------------------ + +/** + * Check a channel of a server can be bridged: Sylo sees it, it is a normal text channel, and Sylo may + * view it, send in it and manage webhooks there. + * @returns {{ guild: any, channel: any }} + */ +function checkChannel(key, channelId) { + const guild = guildByKey(key); + if (!guild) throw new BridgeError('Sylo is not in that server.', 404); + const channel = guild.channels?.cache?.get?.(channelId); + if (!isTextChannel(channel)) throw new BridgeError('Pick a normal text channel of this server.'); + const perms = channel.permissionsFor?.(guild.members?.me); + const missing = ['ViewChannel', 'SendMessages', 'ManageWebhooks'].filter((p) => perms?.has?.(p) === false); + if (missing.length) { + throw new BridgeError( + `Sylo needs ${missing.join(', ')} in #${channel.name}. Give Sylo's role those permissions there first.` + ); + } + return { guild, channel }; +} + +async function requireModule(key) { + if (!(await isModuleEnabled(key, 'bridge'))) { + throw new BridgeError('Turn the Bridge module on for this server first.', 409); + } +} + +function requireBothPlatforms() { + if (!runtime.clients.discord || !runtime.clients.fluxer) { + throw new BridgeError( + 'A bridge needs this Sylo to be connected to both Discord and Fluxer, and it is not.', + 409 + ); + } +} + +/** The age rule: an age-restricted channel only with an age-restricted channel. */ +function checkAge(a, b) { + if (isAgeRestricted(a) !== isAgeRestricted(b)) { + throw new BridgeError( + 'An age-restricted channel can only be bridged with another age-restricted channel.' + ); + } +} + +// --- linking: request, accept ------------------------------------------------------------------------ + +/** + * Ask for a link from one of this server's channels. Returns the one-time code to give the other server. + * @param {{ guildKey: string, channelId: string, options?: object, actor: string }} req + */ +export async function requestLink({ guildKey, channelId, options, actor }) { + requireBothPlatforms(); + await requireModule(guildKey); + checkChannel(guildKey, channelId); + if (await store.linkForChannel(guildKey, channelId)) + throw new BridgeError('That channel is already bridged.', 409); + if (await store.channelHasPendingCode(guildKey, channelId)) { + throw new BridgeError('That channel already has a request waiting. Cancel it to make a new one.', 409); + } + if ((await store.linkCount(guildKey)) >= MAX_LINKS) { + throw new BridgeError(`A server can have at most ${MAX_LINKS} bridged channels.`, 409); + } + if ((await store.pendingCodeCount(guildKey)) >= MAX_REQUESTS) { + throw new BridgeError(`At most ${MAX_REQUESTS} requests can wait at once. Cancel one first.`, 409); + } + const made = await store.createCode({ + guildId: guildKey, + channelId, + options: normaliseOptions(options), + requestedBy: actor, + }); + await recordAudit(guildKey, { + actor, + action: 'bridge:request', + detail: `asked to bridge #${channelName(guildKey, channelId)} (valid 15 minutes)`, + change: null, + }); + return made; +} + +export async function cancelRequest({ guildKey, id, actor }) { + if (!(await store.cancelCode(guildKey, id))) throw new BridgeError('That request is gone already.', 404); + await recordAudit(guildKey, { + actor, + action: 'bridge:cancel', + detail: 'cancelled a bridge request', + change: null, + }); +} + +const INVALID_CODE = 'That code is not valid, has expired or was already used.'; + +/** + * Accept a request with its code, linking one of THIS server's channels to the channel the code was made for. + * Everything that can be refused is checked before the code is used up, so a wrong channel does not burn it. + * @param {{ guildKey: string, channelId: string, code: string, options?: object, actor: string }} req + */ +export async function acceptLink({ guildKey, channelId, code, options, actor }) { + requireBothPlatforms(); + await requireModule(guildKey); + const mine = checkChannel(guildKey, channelId); + if (await store.linkForChannel(guildKey, channelId)) + throw new BridgeError('That channel is already bridged.', 409); + if ((await store.linkCount(guildKey)) >= MAX_LINKS) { + throw new BridgeError(`A server can have at most ${MAX_LINKS} bridged channels.`, 409); + } + + const request = await store.peekCode(code); + if (!request) throw new BridgeError(INVALID_CODE, 404); + if (request.guildId === guildKey) throw new BridgeError('That request was made in this server.', 409); + if (platformOf(request.guildId) === platformOf(guildKey)) { + throw new BridgeError( + 'A bridge joins a Discord channel to a Fluxer channel, not two on the same platform.' + ); + } + // The other server must still have Sylo, with the module on and the channel as it was asked for. + if (!(await isModuleEnabled(request.guildId, 'bridge'))) { + throw new BridgeError('The server that made this request turned its Bridge module off.', 409); + } + let theirs; + try { + theirs = checkChannel(request.guildId, request.channelId); + } catch (err) { + if (err instanceof BridgeError) { + throw new BridgeError(`The other server's channel can no longer be bridged: ${err.message}`, 409); + } + throw err; + } + if (await store.linkForChannel(request.guildId, request.channelId)) { + throw new BridgeError('The other channel was bridged in the meantime.', 409); + } + checkAge(mine.channel, theirs.channel); + + // A webhook in each channel, made before the code is claimed (the claim is the atomic step: if two servers + // race, one loses and its webhooks are removed again). + const made = []; + const dropMade = async () => { + for (const [key, hook] of made) await transport.deleteHook(key, hook).catch(() => {}); + }; + let hookA; + let hookB; + try { + hookA = await transport.createHook(request.guildId, request.channelId, HOOK_NAME); + made.push([request.guildId, hookA]); + hookB = await transport.createHook(guildKey, channelId, HOOK_NAME); + made.push([guildKey, hookB]); + } catch (err) { + await dropMade(); + log.warn('bridge', `could not make the webhooks for a link: ${err?.message ?? err}`); + throw new BridgeError( + 'Sylo could not make a webhook in one of the channels. Check that it has Manage Webhooks in both.', + 502 + ); + } + + const claimed = await store.claimCode(code); + if (!claimed) { + await dropMade(); + throw new BridgeError(INVALID_CODE, 404); + } + const id = await store.createLink({ + guildA: request.guildId, + channelA: request.channelId, + guildB: guildKey, + channelB: channelId, + optionsA: normaliseOptions(claimed.options), + optionsB: normaliseOptions(options), + hookA, + hookB, + requestedBy: claimed.requestedBy, + acceptedBy: actor, + }); + if (!id) { + await dropMade(); + throw new BridgeError('One of the channels was bridged in the meantime.', 409); + } + dropBridgeCache(); + const link = await store.getLink(id); + await recordAudit(guildKey, { + actor, + action: 'bridge:link', + detail: `bridged #${channelName(guildKey, channelId)} with #${channelName(request.guildId, request.channelId)} in ${serverName(request.guildId)} (${platformName(request.guildId)})`, + change: null, + }); + await recordAudit(request.guildId, { + actor: claimed.requestedBy ? `${claimed.requestedBy}` : 'Dashboard', + action: 'bridge:link', + detail: `bridged #${channelName(request.guildId, request.channelId)} with #${channelName(guildKey, channelId)} in ${serverName(guildKey)} (${platformName(guildKey)}); accepted there`, + change: null, + }); + await say(guildKey, channelId, onNotice(guildKey, request.guildId, request.channelId)); + await say(request.guildId, request.channelId, onNotice(request.guildId, guildKey, channelId)); + return link; +} + +// --- link lifecycle: options, pause, resume, unlink -------------------------------------------------- + +/** A link of this server (either side), or a 404: a server never sees or changes another server's links. */ +async function ownLink(guildKey, id) { + const link = await store.getLink(id); + const side = link && sideOf(link, guildKey); + if (!link || !side) throw new BridgeError('No such bridged channel in this server.', 404); + return { link, side }; +} + +export async function setSideOptions({ guildKey, id, options, actor }) { + const { link, side } = await ownLink(guildKey, id); + const next = normaliseOptions({ ...optionsOf(link, side), ...options }); + await store.setLinkOptions(link.id, side, next); + dropBridgeCache(); + await recordAudit(guildKey, { + actor, + action: 'bridge:options', + detail: `changed what #${channelName(guildKey, channelOf(link, side))} shares (bots ${next.bots ? 'on' : 'off'}, joins ${next.joinMessages ? 'on' : 'off'}, files ${next.uploadFiles ? 'uploaded' : 'as links'})`, + change: null, + }); + return next; +} + +export async function pauseLink({ guildKey, id, actor }) { + const { link, side } = await ownLink(guildKey, id); + if (link.status !== 'active') throw new BridgeError('That bridge is not running.', 409); + await store.setLinkStatus(link.id, { status: 'paused', pausedSide: side }); + dropBridgeCache(); + queue.clear(laneKey(link, 'a')); + queue.clear(laneKey(link, 'b')); + await recordAudit(guildKey, { + actor, + action: 'bridge:pause', + detail: `paused #${channelName(guildKey, channelOf(link, side))}`, + change: null, + }); + const theirKey = guildOf(link, other(side)); + await say( + guildOf(link, side), + channelOf(link, side), + '🌉 **Bridge paused** by this server. Nothing is shared until it is resumed.' + ); + await say( + theirKey, + channelOf(link, other(side)), + `🌉 **Bridge paused** by ${serverName(guildKey)}. Nothing is shared until they resume it.` + ); +} + +/** + * Resume a link. A link a server paused can only be resumed by that server; one Sylo stopped (a failure) + * can be resumed by either, after the cause is fixed: the channels, permissions, webhooks and the age rule + * are checked again first. + */ +export async function resumeLink({ guildKey, id, actor }) { + const { link, side } = await ownLink(guildKey, id); + if (link.status === 'active') throw new BridgeError('That bridge is already running.', 409); + if (link.status === 'paused' && link.pausedSide !== side) { + throw new BridgeError('The other server paused this bridge, so only they can resume it.', 403); + } + requireBothPlatforms(); + await requireModule(link.guildA); + await requireModule(link.guildB); + const a = checkChannel(link.guildA, link.channelA); + const b = checkChannel(link.guildB, link.channelB); + checkAge(a.channel, b.channel); + for (const s of ['a', 'b']) { + const key = guildOf(link, s); + const hook = hookOf(link, s); + if (!hook.id || !hook.token) { + await remakeHook(link, s); + } else { + try { + // A webhook can be deleted while the link is stopped; the first message would then fail. Make a new one now. + const probe = channelIn(key, channelOf(link, s))?.fetchWebhooks; + if (probe) { + const list = await channelIn(key, channelOf(link, s)).fetchWebhooks(); + const alive = [...(list?.values?.() ?? list ?? [])].some((h) => String(h.id) === hook.id); + if (!alive) await remakeHook(link, s); + } + } catch (err) { + log.debug('bridge', `could not check the webhook of link ${link.id}: ${err?.message ?? err}`); + } + } + } + failures.delete(link.id); + await store.setLinkStatus(link.id, { status: 'active' }); + dropBridgeCache(); + await recordAudit(guildKey, { + actor, + action: 'bridge:resume', + detail: `resumed #${channelName(guildKey, channelOf(link, side))}`, + change: null, + }); + await say(link.guildA, link.channelA, onNotice(link.guildA, link.guildB, link.channelB)); + await say(link.guildB, link.channelB, onNotice(link.guildB, link.guildA, link.channelA)); +} + +/** Unlink: either side may, at any time. The webhooks are deleted and the message map with the link. */ +export async function unlinkLink({ guildKey, id, actor }) { + const { link, side } = await ownLink(guildKey, id); + await dismantle(link, `unlinked by ${serverName(guildKey)}`); + await recordAudit(guildKey, { + actor, + action: 'bridge:unlink', + detail: `unlinked #${channelName(guildKey, channelOf(link, side))} from #${channelName(guildOf(link, other(side)), channelOf(link, other(side)))} in ${serverName(guildOf(link, other(side)))}`, + change: null, + }); + await recordAudit(guildOf(link, other(side)), { + actor: `${serverName(guildKey)}`, + action: 'bridge:unlink', + detail: `${serverName(guildKey)} unlinked #${channelName(guildOf(link, other(side)), channelOf(link, other(side)))}`, + change: null, + }); +} + +/** Remove a link everywhere: queues, webhooks, rows, and tell both channels. */ +async function dismantle(link, why) { + queue.clear(laneKey(link, 'a')); + queue.clear(laneKey(link, 'b')); + for (const s of ['a', 'b']) { + const hook = hookOf(link, s); + if (hook.id) await transport.deleteHook(guildOf(link, s), hook).catch(() => {}); + } + await store.deleteLink(link.id); + failures.delete(link.id); + dropBridgeCache(); + for (const s of ['a', 'b']) { + await say( + guildOf(link, s), + channelOf(link, s), + `🌉 **Bridge ended:** ${why}. This channel is no longer shared with the other platform.` + ); + } +} + +/** + * Sylo was removed from a server: the server on the other end loses its webhook and is told, before the + * rows go (the purge deletes them). Registered with the purge below. + */ +export async function bridgeServerLeft(key) { + const links = await store.linksOfGuild(key); + for (const link of links) { + const side = sideOf(link, key); + const theirs = other(side); + const hook = hookOf(link, theirs); + queue.clear(laneKey(link, 'a')); + queue.clear(laneKey(link, 'b')); + if (hook.id) await transport.deleteHook(guildOf(link, theirs), hook).catch(() => {}); + await say( + guildOf(link, theirs), + channelOf(link, theirs), + '🌉 **Bridge ended:** the other server removed Sylo. This channel is no longer shared with the other platform.' + ); + await recordAudit(guildOf(link, theirs), { + actor: 'Sylo', + action: 'bridge:unlink', + detail: 'the server on the other end removed Sylo, so the bridge ended', + change: null, + }); + } + dropBridgeCache(); +} +onBeforePurge(bridgeServerLeft); + +// --- failures ---------------------------------------------------------------------------------------- + +/** In a row, how many deliveries of a link failed (reset by a success). In memory: it only gates a pause. */ +const failures = new Map(); + +async function remakeHook(link, side) { + const key = guildOf(link, side); + const channelId = channelOf(link, side); + const old = hookOf(link, side); + const made = await transport.createHook(key, channelId, HOOK_NAME); + await store.setLinkHook(link.id, side, made); + dropBridgeCache(); + if (old.id) await transport.deleteHook(key, old).catch(() => {}); + return { ...made, channelId }; +} +// One recreation at a time per webhook, so a burst of failing sends makes one new webhook, not many. +const remaking = new Map(); +function recreateHook(link, side) { + const id = laneKey(link, side); + if (!remaking.has(id)) { + remaking.set( + id, + remakeHook(link, side).finally(() => remaking.delete(id)) + ); + } + return remaking.get(id); +} + +/** Stop a link that cannot work, show why in Health, and say so in both channels. */ +export async function failLink(linkId, reason) { + const link = await store.getLink(linkId); + if (!link || link.status === 'failed') return; + await store.setLinkStatus(link.id, { status: 'failed', reason, failures: FAIL_THRESHOLD }); + dropBridgeCache(); + queue.clear(laneKey(link, 'a')); + queue.clear(laneKey(link, 'b')); + failures.delete(link.id); + log.warn('bridge', `link ${link.id} stopped: ${reason}`); + for (const s of ['a', 'b']) { + await recordAudit(guildOf(link, s), { + actor: 'Sylo', + action: 'bridge:stopped', + detail: + `the bridge of #${channelName(guildOf(link, s), channelOf(link, s))} stopped: ${reasonText(reason)}`.slice( + 0, + 500 + ), + change: null, + }); + await say( + guildOf(link, s), + channelOf(link, s), + `🌉 **Bridge stopped.** Messages are no longer shared. Server admins can see why under Health in the Sylo dashboard and resume it there.` + ); + } +} + +/** A delivery to `side`'s channel failed for good (after the queue's own retries). */ +async function noteFailure(link, side, err) { + if (err instanceof BridgeQueueError) { + log.debug('bridge', `link ${link.id}: a message was dropped (${err.reason})`); + return; + } + const kind = err?.failure?.kind ?? classifyError(err).kind; + if (kind === 'bad-request' || kind === 'message-gone') { + log.debug('bridge', `link ${link.id}: the platform refused one message (${kind})`); + return; + } + if (err?.code === 'NO_GUILD' || err?.code === 'NO_CHANNEL') return failLink(link.id, 'channel-missing'); + if (kind === 'forbidden') return failLink(link.id, 'permissions'); + if (kind === 'hook-gone') return failLink(link.id, 'webhook'); + const n = (failures.get(link.id) ?? 0) + 1; + failures.set(link.id, n); + log.warn('bridge', `link ${link.id}: delivery to ${side} failed (${n} in a row): ${err?.message ?? err}`); + if (n >= FAIL_THRESHOLD) return failLink(link.id, 'unreachable'); + return undefined; +} + +// --- reading a message ------------------------------------------------------------------------------- + +const toList = (collection) => + collection + ? typeof collection.values === 'function' + ? [...collection.values()] + : Array.isArray(collection) + ? collection + : [] + : []; + +const displayNameOf = (message) => + message.member?.displayName ?? + message.author?.globalName ?? + message.author?.displayName ?? + message.author?.username ?? + 'someone'; + +function avatarOf(message) { + try { + const url = + message.member?.displayAvatarURL?.({ size: 128 }) ?? + message.author?.displayAvatarURL?.({ size: 128, extension: 'png' }) ?? + message.author?.avatarURL?.(); + return typeof url === 'string' && /^https?:\/\//.test(url) ? url : undefined; + } catch { + return undefined; + } +} + +/** Names for the mentions in a message, from what the platform sent with it and the server's caches. */ +function nameResolvers(message, guild) { + const m = message.mentions; + const users = Array.isArray(m) ? m : toList(m?.users); + return { + user: (id) => + m?.members?.get?.(id)?.displayName ?? + users.find((u) => u.id === id)?.displayName ?? + users.find((u) => u.id === id)?.username ?? + guild?.members?.cache?.get?.(id)?.displayName ?? + guild?.members?.cache?.get?.(id)?.user?.username, + role: (id) => m?.roles?.get?.(id)?.name ?? guild?.roles?.cache?.get?.(id)?.name, + channel: (id) => guild?.channels?.cache?.get?.(id)?.name, + }; +} + +/** A custom emoji of the same name in the server the copy goes to. */ +function emojiFinder(guild) { + return (name) => { + const wanted = String(name).toLowerCase(); + for (const e of toList(guild?.emojis?.cache)) if (String(e.name).toLowerCase() === wanted) return e; + return undefined; + }; +} + +/** Is this a message a person (or an allowed bot) wrote, not a join notice, pin notice or other system message? */ +function isChatMessage(message) { + if (message.system) return false; + const type = message.type; + return ( + type === undefined || type === null || type === 0 || type === 19 || type === 'Default' || type === 'Reply' + ); +} + +const SUFFIX = /\s\[(?:Discord|Fluxer)\]$/; + +async function replyInfo(message, link, side) { + const refId = message.reference?.messageId ?? message.messageReference?.messageId; + if (!refId) return null; + const mapped = await store.mappingOf(link.id, side, refId); + let ref = message.referencedMessage ?? null; + if (!ref && typeof message.fetchReference === 'function') { + try { + ref = await message.fetchReference(); + } catch { + ref = null; + } + } + const theirs = other(side); + return { + name: ref + ? String(ref.member?.displayName ?? ref.author?.username ?? 'someone').replace(SUFFIX, '') + : 'someone', + raw: ref?.content ?? '', + ref, + url: mapped ? jumpUrl(guildOf(link, theirs), channelOf(link, theirs), mapped.counterpartId) : undefined, + }; +} + +/** + * Everything a copy of `message` needs: the sender's name and picture, the text with its mentions made + * harmless, the files (downloaded within limits, the rest as links), embeds, stickers and a poll as text. + * Null when there is nothing to show. + */ +async function buildCopy(message, link, side, { guildKey }) { + const srcGuild = guildByKey(guildKey); + const dstKey = guildOf(link, other(side)); + const dstGuild = guildByKey(dstKey); + const options = optionsOf(link, side); + const names = nameResolvers(message, srcGuild); + const find = emojiFinder(dstGuild); + const convert = (text) => mapEmoji(neutraliseMentions(text, names), find); + + const body = convert(String(message.content ?? '')); + const embeds = plainEmbeds(message.embeds, (t) => plainTimestamps(convert(t))); + + const reply = await replyInfo(message, link, side); + const head = reply + ? replyHead({ + name: reply.name, + snippet: convert(reply.raw).replace(/\[([^\]]*)\]\([^)]*\)/g, '$1'), + url: reply.url, + }) + : ''; + + const attachments = toList(message.attachments).map((a) => ({ + name: a.name ?? a.filename ?? 'file', + url: a.url, + size: a.size, + })); + const plan = planAttachments(attachments, { upload: options.uploadFiles }); + const files = []; + const links = [...plan.links]; + for (const a of plan.upload) { + try { + const res = await download(a.url, FILE_BYTES_LIMIT); + if (!res.ok || !res.body?.length) throw new Error(`HTTP ${res.status}`); + files.push({ name: a.name, data: res.body }); + } catch (err) { + log.debug('bridge', `file left as a link (${err?.message ?? err})`); + links.push(a); + } + } + + const poll = message.poll + ? pollText({ + question: message.poll.question?.text ?? message.poll.question, + answers: toList(message.poll.answers).map((a) => ({ text: a.text, emoji: a.emoji?.name })), + }) + : ''; + const tail = [...stickerLines(toList(message.stickers)), poll, ...fileLinkLines(links)] + .filter(Boolean) + .join('\n'); + + const content = joinParts([head, body, tail]); + if (!content && !embeds.length && !files.length) return null; + return { + content, + head, + body, + tail, + embeds, + files, + username: senderName(displayNameOf(message), platformOf(guildKey), { + forDiscord: platformOf(dstKey) === 'discord', + }), + avatarUrl: avatarOf(message), + }; +} + +// --- relaying ---------------------------------------------------------------------------------------- + +/** A webhook send that tries once more with a plain name when the platform refuses the sender's name. */ +async function sendCopy(dstKey, hook, payload) { + try { + return await transport.send(dstKey, hook, payload); + } catch (err) { + if (classifyError(err).kind === 'bad-request' && payload.username) { + return transport.send(dstKey, hook, { + ...payload, + username: `Bridge [${platformName(dstKey) === 'Discord' ? 'Fluxer' : 'Discord'}]`, + }); + } + throw err; + } +} + +/** Run a transport call with the link's webhook, making a new webhook once if the platform says it is gone. */ +async function withHook(link, side, call) { + let hook = hookOf(link, side); + try { + return await call(hook); + } catch (err) { + if (classifyError(err).kind !== 'hook-gone') throw err; + hook = await recreateHook(link, side); + return call(hook); + } +} + +/** Can a message in this channel be relayed at all right now? Loop, bot and age rules. */ +async function allowed(message, link, side, key) { + if (!isChatMessage(message)) return false; + // Never again what a bridge webhook posted (any link's), and never Sylo's own messages. + if (message.webhookId && (await store.linkForHook(message.webhookId))) return false; + if (message.author?.id && message.author.id === message.client?.user?.id) return false; + if (message.author?.bot || message.webhookId) { + if (!optionsOf(link, side).bots) return false; + } + const theirKey = guildOf(link, other(side)); + if (!(await isModuleEnabled(theirKey, 'bridge'))) return false; + // The age rule on every message: a channel can change after the link was made. + const mine = channelIn(key, message.channelId); + const theirs = channelIn(theirKey, channelOf(link, other(side))); + if (!mine || !theirs) { + await failLink(link.id, 'channel-missing'); + return false; + } + if (isAgeRestricted(mine) !== isAgeRestricted(theirs)) { + await failLink(link.id, 'age-restriction'); + return false; + } + return true; +} + +/** `messageCreateAny`: relay a new message to the other end of its channel's link. */ +export async function handleMessage(message, _config, key) { + const found = await findLink(key, message.channelId); + if (!found || found.link.status !== 'active') return; + const { link, side } = found; + if (!(await allowed(message, link, side, key))) return; + + const target = other(side); + const dstKey = guildOf(link, target); + let built; + const job = async () => { + const current = await store.getLink(link.id); + if (!current || current.status !== 'active') return null; + built ??= await buildCopy(message, current, side, { guildKey: key }); + if (!built) return null; + const payload = { + content: built.content || undefined, + username: built.username, + avatarUrl: built.avatarUrl, + embeds: built.embeds.length ? built.embeds : undefined, + files: built.files.length ? built.files : undefined, + }; + const copyId = await withHook(current, target, (hook) => sendCopy(dstKey, hook, payload)); + try { + await store.saveMapping(current.id, side, { + sourceId: message.id, + copyId, + hookId: hookOf(current, target).id, + hash: hashContent(`${built.body}\n${JSON.stringify(built.embeds)}`), + extra: { head: built.head.slice(0, 900), tail: built.tail.slice(0, 900) }, + }); + } catch (err) { + log.warn('bridge', `could not remember a copy: ${err?.message ?? err}`); + } + if (failures.has(current.id)) failures.delete(current.id); + return copyId; + }; + track( + queue.enqueue(laneKey(link, target), job, { op: 'send' }).catch((err) => noteFailure(link, target, err)) + ); +} + +/** `messageUpdate`: a message that was relayed is edited on the other side. */ +export async function handleUpdate({ new: updated }, _config, key) { + let message = updated; + if (message?.partial && typeof message.fetch === 'function') { + try { + message = await message.fetch(); + } catch { + return; + } + } + if (!message?.channelId) return; + const found = await findLink(key, message.channelId); + if (!found || found.link.status !== 'active') return; + const { link, side } = found; + const map = await store.mappingOf(link.id, side, message.id); + if (!map?.isOriginal) return; + if (!(await allowed(message, link, side, key))) return; + + const target = other(side); + const dstKey = guildOf(link, target); + const job = async () => { + const current = await store.getLink(link.id); + if (!current || current.status !== 'active') return null; + const names = nameResolvers(message, guildByKey(key)); + const find = emojiFinder(guildByKey(dstKey)); + const convert = (text) => mapEmoji(neutraliseMentions(text, names), find); + const body = convert(String(message.content ?? '')); + const embeds = plainEmbeds(message.embeds, (t) => plainTimestamps(convert(t))); + const hash = hashContent(`${body}\n${JSON.stringify(embeds)}`); + // An edit event also fires when only a link preview appeared; nothing visible changed then. + if (hash === map.contentHash) return null; + const content = joinParts([map.extra?.head, body, map.extra?.tail]); + const hook = hookOf(current, target); + // The copy can only be edited by the webhook that posted it; if that one was replaced, leave it as it was. + if (!hook.id || (map.copyHookId && map.copyHookId !== hook.id)) return null; + await transport.edit(dstKey, hook, map.counterpartId, { content: content || ZWSP, embeds }); + await store.setMappingHash(current.id, map.aMessageId, map.bMessageId, hash); + return map.counterpartId; + }; + track( + queue.enqueue(laneKey(link, target), job, { op: 'edit' }).catch((err) => noteFailure(link, target, err)) + ); +} + +async function deleteCopyOf(link, side, messageId) { + const map = await store.mappingOf(link.id, side, messageId); + if (!map) return; + const target = other(side); + if (!map.isOriginal) { + // Someone removed a copy: forget it, and leave the original alone. + await store.deleteMapping(link.id, side, messageId); + return; + } + const dstKey = guildOf(link, target); + const job = async () => { + const current = await store.getLink(link.id); + if (!current || current.status !== 'active') return null; + const hook = hookOf(current, target); + if (hook.id && (!map.copyHookId || map.copyHookId === hook.id)) { + try { + await transport.remove(dstKey, hook, map.counterpartId); + } catch (err) { + if (classifyError(err, 'delete').kind !== 'message-gone') throw err; + } + } + await store.deleteMapping(current.id, side, messageId); + return map.counterpartId; + }; + track( + queue.enqueue(laneKey(link, target), job, { op: 'delete' }).catch((err) => noteFailure(link, target, err)) + ); +} + +/** `messageDelete`: the copy of a deleted message is deleted too. */ +export async function handleDelete(message, _config, key) { + if (!message?.channelId || !message.id) return; + const found = await findLink(key, message.channelId); + if (!found || found.link.status !== 'active') return; + await deleteCopyOf(found.link, found.side, message.id); +} + +/** `messageDeleteBulk`: the same for a purge of many messages. */ +export async function handleBulkDelete({ messages, channel }, _config, key) { + const ids = typeof messages?.keys === 'function' ? [...messages.keys()] : []; + // Fluxer may not have the channel cached: the deleted messages carry its id as well. + const channelId = channel?.id ?? [...(messages?.values?.() ?? [])][0]?.channelId; + if (!channelId) return; + const found = await findLink(key, channelId); + if (!found || found.link.status !== 'active') return; + for (const id of ids) await deleteCopyOf(found.link, found.side, id); +} + +/** `guildMemberAdd`: a join notice on the other side, for the links whose server turned it on. */ +export async function handleJoin(member, _config, key) { + const links = (await store.linksOfGuild(key)).filter((l) => l.status === 'active'); + for (const link of links) { + const side = sideOf(link, key); + const options = optionsOf(link, side); + if (!options.joinMessages) continue; + if (member.user?.bot && !options.bots) continue; + const target = other(side); + const dstKey = guildOf(link, target); + if (!(await isModuleEnabled(dstKey, 'bridge'))) continue; + const who = inertAt(String(member.displayName ?? member.user?.username ?? 'Someone')).slice(0, 80); + const payload = { + content: `👋 **${who}** joined **${serverName(key)}**`, + username: senderName(guildByKey(key)?.name ?? 'Server', platformOf(key), { + forDiscord: platformOf(dstKey) === 'discord', + }), + }; + track( + queue + .enqueue( + laneKey(link, target), + async () => { + const current = await store.getLink(link.id); + if (!current || current.status !== 'active') return null; + return withHook(current, target, (hook) => sendCopy(dstKey, hook, payload)); + }, + { op: 'send' } + ) + .catch((err) => noteFailure(link, target, err)) + ); + } +} + +/** `channelDelete`: a bridged channel that is gone stops its link. */ +export async function handleChannelDelete(channel, _config, key) { + const found = await findLink(key, channel.id); + if (found) await failLink(found.link.id, 'channel-deleted'); +} + +on('bridge', 'messageCreateAny', handleMessage); +on('bridge', 'messageUpdate', handleUpdate); +on('bridge', 'messageDelete', handleDelete); +on('bridge', 'messageDeleteBulk', handleBulkDelete); +on('bridge', 'guildMemberAdd', handleJoin); +on('bridge', 'channelDelete', handleChannelDelete); + +// --- what the dashboard shows ------------------------------------------------------------------------ + +/** The end of a link on the other server, as much as both servers agreed to show each other. */ +function endView(key, channelId) { + const channel = channelIn(key, channelId); + return { + platform: platformOf(key), + server: guildByKey(key)?.name ?? 'Unknown server', + channel: channel?.name ?? 'unknown channel', + ageRestricted: isAgeRestricted(channel), + }; +} + +/** One link as the server `key` sees it (never a webhook token). */ +export function linkView(link, key) { + const side = sideOf(link, key); + const theirs = other(side); + return { + id: link.id, + status: link.status, + pausedByMe: link.status === 'paused' && link.pausedSide === side, + pausedByThem: link.status === 'paused' && link.pausedSide === theirs, + problem: link.status === 'failed' ? { code: link.failReason, text: reasonText(link.failReason) } : null, + mine: { channelId: channelOf(link, side), ...endView(key, channelOf(link, side)) }, + theirs: endView(guildOf(link, theirs), channelOf(link, theirs)), + options: optionsOf(link, side), + theirOptions: optionsOf(link, theirs), + createdAt: link.createdAt, + requestedBy: link.requestedBy, + acceptedBy: link.acceptedBy, + queued: queue.stats(laneKey(link, theirs)).queued, + dropped: queue.stats(laneKey(link, theirs)).dropped, + }; +} + +/** Everything the Bridge page needs for a server. */ +export async function bridgeOverview(key) { + const guild = guildByKey(key); + const links = await store.linksOfGuild(key); + const requests = await store.pendingCodes(key); + const taken = new Set([ + ...links.map((l) => channelOf(l, sideOf(l, key))), + ...requests.map((r) => r.channelId), + ]); + return { + platform: platformOf(key), + bothPlatforms: Boolean(runtime.clients.discord && runtime.clients.fluxer), + enabled: (await getGuildModule(key, 'bridge')).enabled, + limits: { links: MAX_LINKS, requests: MAX_REQUESTS }, + links: links.map((l) => linkView(l, key)), + requests: requests.map((r) => ({ + id: r.id, + channelId: r.channelId, + channel: channelIn(key, r.channelId)?.name ?? 'unknown channel', + expiresAt: r.expiresAt, + requestedBy: r.requestedBy, + })), + channels: toList(guild?.channels?.cache) + .filter(isTextChannel) + .sort((x, y) => (x.rawPosition ?? 0) - (y.rawPosition ?? 0)) + .map((c) => ({ id: c.id, name: c.name, ageRestricted: isAgeRestricted(c), taken: taken.has(c.id) })), + }; +} + +/** + * The links of a server that need attention, for the Health page: a stopped link, and the permissions + * the bridge needs in its channels are checked by the generic channel check there. + */ +export async function bridgeProblems(key) { + return (await store.linksOfGuild(key)) + .filter((l) => l.status === 'failed') + .map((l) => { + const side = sideOf(l, key); + return { + linkId: l.id, + channelId: channelOf(l, side), + code: l.failReason, + text: reasonText(l.failReason), + }; + }); +} + +/** The bridged channels of a server, with the permissions the bridge needs in each. */ +export async function bridgedChannels(key) { + return (await store.linksOfGuild(key)).map((l) => channelOf(l, sideOf(l, key))); +} diff --git a/src/modules/index.js b/src/modules/index.js index d1e481df..c42ee5e2 100644 --- a/src/modules/index.js +++ b/src/modules/index.js @@ -36,6 +36,7 @@ import './autoThreads.js'; import './quoteBoard.js'; import './introductions.js'; import './applications.js'; +import './bridge.js'; import './economy.js'; import './messageCreator.js'; // registers the role-button / role-select component handlers import './moderation.js'; // no gateway handlers, but runs the temporary-ban expiry loop diff --git a/src/modules/lib/bridgeQueue.js b/src/modules/lib/bridgeQueue.js new file mode 100644 index 00000000..c51f74f3 --- /dev/null +++ b/src/modules/lib/bridgeQueue.js @@ -0,0 +1,161 @@ +// The send queue of the bridge: one queue per target webhook, so a busy or failing channel never slows +// another server's bridge, and messages reach the other side in the order they were written. +// +// A webhook may only be called a few times a second. Each queue spaces its sends (`minGapMs`), waits out +// a rate limit the platform reports (`retry after`), retries a transient failure with a growing pause, and +// gives up on a job after `maxAttempts`. A queue holds at most `maxQueued` jobs: when a channel is flooded +// the newest message is dropped (and counted) instead of the bridge growing without bound, and a job that +// has waited longer than `maxAgeMs` is dropped as stale instead of arriving minutes late. +// +// What a failure MEANS for the link (pause it, recreate the webhook) is the engine's decision, in +// modules/bridge.js; this file only classifies errors and decides whether to try again. + +/** @typedef {'rate' | 'hook-gone' | 'message-gone' | 'forbidden' | 'bad-request' | 'transient'} FailureKind */ + +export class BridgeQueueError extends Error { + /** @param {'overflow' | 'expired' | 'cleared'} reason */ + constructor(reason) { + super(`bridge queue: ${reason}`); + this.name = 'BridgeQueueError'; + this.reason = reason; + } +} + +const statusOf = (err, depth = 0) => { + if (!err || typeof err !== 'object') return undefined; + return err.status ?? err.statusCode ?? (depth < 2 ? statusOf(err.cause, depth + 1) : undefined); +}; +const codeOf = (err, depth = 0) => { + if (!err || typeof err !== 'object') return undefined; + return err.code ?? err.rawError?.code ?? (depth < 2 ? codeOf(err.cause, depth + 1) : undefined); +}; + +/** How long the platform said to wait, in ms (it reports seconds or milliseconds, depending on the SDK). */ +function retryAfterOf(err, depth = 0) { + if (!err || typeof err !== 'object') return undefined; + const n = Number(err.retryAfter ?? err.retry_after ?? err.data?.retry_after ?? err.rawError?.retry_after); + if (Number.isFinite(n) && n > 0) return n <= 120 ? n * 1000 : n; + return depth < 2 ? retryAfterOf(err.cause, depth + 1) : undefined; +} + +const HOOK_GONE_CODES = [10015, 'UNKNOWN_WEBHOOK', 'WEBHOOK_NOT_FOUND', 50027, 'INVALID_WEBHOOK_TOKEN']; +const MESSAGE_GONE_CODES = [10008, 'UNKNOWN_MESSAGE', 'MESSAGE_NOT_FOUND']; +const FORBIDDEN_CODES = [50001, 50013, 'MISSING_ACCESS', 'MISSING_PERMISSIONS']; + +/** + * What a failed webhook call means. `op` matters for a 404: on a send it is the webhook that is gone, on + * an edit or a delete it is the message (someone removed the copy) unless the platform names the webhook. + * @param {unknown} err + * @param {'send' | 'edit' | 'delete'} [op] + * @returns {{ kind: FailureKind, retryAfterMs?: number }} + */ +export function classifyError(err, op = 'send') { + const status = statusOf(err); + const code = codeOf(err); + if (status === 429 || code === 'RATE_LIMITED' || err?.name === 'RateLimitError') { + return { kind: 'rate', retryAfterMs: Math.min(60_000, Math.max(250, retryAfterOf(err) ?? 2000)) }; + } + if (HOOK_GONE_CODES.includes(code) || status === 401) return { kind: 'hook-gone' }; + if (MESSAGE_GONE_CODES.includes(code)) return { kind: 'message-gone' }; + if (status === 404) return { kind: op === 'send' ? 'hook-gone' : 'message-gone' }; + if (status === 403 || FORBIDDEN_CODES.includes(code)) return { kind: 'forbidden' }; + if (status === 400 || status === 413 || status === 422) return { kind: 'bad-request' }; + return { kind: 'transient' }; +} + +const defaultSleep = (ms) => + new Promise((resolve) => { + setTimeout(resolve, ms).unref?.(); + }); + +/** + * @param {{ minGapMs?: number, maxQueued?: number, maxAgeMs?: number, maxAttempts?: number, + * sleep?: (ms: number) => Promise, now?: () => number }} [opts] + */ +export function createRelayQueue({ + minGapMs = 350, + maxQueued = 100, + maxAgeMs = 5 * 60_000, + maxAttempts = 4, + sleep = defaultSleep, + now = Date.now, +} = {}) { + /** @type {Map} */ + const lanes = new Map(); + + async function drain(key, lane) { + lane.running = true; + try { + while (lane.tasks.length) { + const task = lane.tasks[0]; + if (now() - task.at > maxAgeMs) { + lane.tasks.shift(); + lane.dropped += 1; + task.reject(new BridgeQueueError('expired')); + continue; + } + try { + const value = await task.run(); + lane.tasks.shift(); + task.resolve(value); + } catch (err) { + const failure = classifyError(err, task.op); + if ((failure.kind === 'rate' || failure.kind === 'transient') && task.attempts < maxAttempts - 1) { + task.attempts += 1; + await sleep(failure.kind === 'rate' ? failure.retryAfterMs : 1000 * 2 ** (task.attempts - 1)); + continue; + } + lane.tasks.shift(); + task.reject(Object.assign(err instanceof Error ? err : new Error(String(err)), { failure })); + } + if (lane.tasks.length) await sleep(minGapMs); + } + } finally { + lane.running = false; + if (!lane.tasks.length) lanes.delete(key); + } + } + + return { + /** + * Queue a job for a target webhook. The returned promise settles when the job has run (or been dropped). + * @template T + * @param {string} key the target webhook (one lane per webhook) + * @param {() => Promise} run + * @param {{ op?: 'send' | 'edit' | 'delete' }} [opts] + * @returns {Promise} + */ + enqueue(key, run, { op = 'send' } = {}) { + let lane = lanes.get(key); + if (!lane) { + lane = { tasks: [], running: false, dropped: 0 }; + lanes.set(key, lane); + } + if (lane.tasks.length >= maxQueued) { + lane.dropped += 1; + return Promise.reject(new BridgeQueueError('overflow')); + } + const result = new Promise((resolve, reject) => { + lane.tasks.push({ run, op, at: now(), attempts: 0, resolve, reject }); + }); + if (!lane.running) void drain(key, lane); + return result; + }, + /** How many jobs wait for a webhook, and how many were dropped since the lane was last empty. */ + stats(key) { + const lane = lanes.get(key); + return { queued: lane?.tasks.length ?? 0, dropped: lane?.dropped ?? 0 }; + }, + /** Drop everything waiting for a webhook (the link was paused, unlinked or failed). */ + clear(key) { + const lane = lanes.get(key); + if (!lane) return 0; + // The job at the head may be running; leave it, drop the ones behind it. + const waiting = lane.running ? lane.tasks.splice(1) : lane.tasks.splice(0); + for (const t of waiting) t.reject(new BridgeQueueError('cleared')); + return waiting.length; + }, + /** How many webhooks have a queue right now (for tests and the health page). */ + lanes: () => lanes.size, + }; +} diff --git a/src/modules/lib/bridgeText.js b/src/modules/lib/bridgeText.js new file mode 100644 index 00000000..d22c0600 --- /dev/null +++ b/src/modules/lib/bridgeText.js @@ -0,0 +1,285 @@ +// The pure part of the bridge: turning one platform's message into text and attachments the other +// platform can show, with every ping made harmless on the way. Nothing here touches a platform, the +// database or the network, so the safety rules (no ping crosses, nothing breaks the other side's limits) +// are tested directly in test/bridgeText.test.js. +// +// Mentions: the relay posts with allowedMentions set to none, so nothing is pinged whatever the text says. +// This module also changes the text, so a person reads "@Alice" and "@moderators" on the other side +// instead of a raw id that means nothing there, and "@everyone" can never be turned into a ping by a +// later change to how the copy is sent. +import { createHash } from 'node:crypto'; + +/** What is left of the message limit of the platforms: Discord and Fluxer both allow 2000 characters. */ +export const MESSAGE_LIMIT = 2000; +/** The most embeds one message carries, and the most files. */ +export const EMBED_LIMIT = 10; +export const FILE_COUNT_LIMIT = 10; +/** A file is re-uploaded only up to this size, and only this much per message; bigger ones become a link. */ +export const FILE_BYTES_LIMIT = 8 * 1024 * 1024; +export const TOTAL_BYTES_LIMIT = 8 * 1024 * 1024; + +/** U+200B, the zero-width space: invisible, and enough to break a mention. */ +export const ZWSP = String.fromCharCode(0x200b); + +/** What each side of a link shares about itself. `bots` and `joinMessages` are off until a server asks. */ +export const DEFAULT_OPTIONS = Object.freeze({ bots: false, joinMessages: false, uploadFiles: true }); + +/** The options of one side as stored or submitted, always the full shape with booleans. */ +export function normaliseOptions(raw) { + const o = raw && typeof raw === 'object' ? raw : {}; + return { + bots: o.bots === true, + joinMessages: o.joinMessages === true, + uploadFiles: o.uploadFiles === undefined ? DEFAULT_OPTIONS.uploadFiles : o.uploadFiles === true, + }; +} + +/** A short stable hash of what was sent, so an edit event that changed nothing visible is skipped. */ +export const hashContent = (text) => + createHash('sha256') + .update(String(text ?? '')) + .digest('hex') + .slice(0, 24); + +// --- text helpers ------------------------------------------------------------------------------------ + +/** Cut to `max` UTF-16 units without a half emoji at the end, ending in "…". */ +export function clip(value, max) { + const text = String(value ?? ''); + if (text.length <= max) return text; + let out = text.slice(0, Math.max(0, max - 1)); + if (/[\uD800-\uDBFF]$/.test(out)) out = out.slice(0, -1); + return `${out}…`; +} + +/** + * Control characters and invisible format characters (direction overrides and the like) out of a display + * name or a quote. The zero-width joiner stays, because emoji sequences need it, and so does the + * zero-width space: it is what keeps a mention broken once neutraliseMentions has put it there. + */ +const ZWJ = String.fromCharCode(0x200d); +const stripControl = (text) => + String(text ?? '') + .replace(/[\p{Cc}\p{Cf}]/gu, (c) => (c === ZWJ || c === ZWSP ? c : '')) + .trim(); + +/** A name that shows an @ but cannot ping: the zero-width space after the @ breaks the mention. */ +export const inertAt = (text) => String(text ?? '').replace(/@/g, `@${ZWSP}`); + +/** + * Replace the mentions in a message with plain text and break @everyone / @here. `names` looks up what + * the other side should read for a user, a role or a channel; it may return nothing (then a neutral word). + * @param {string} text + * @param {{ user?: (id: string) => string | undefined, role?: (id: string) => string | undefined, + * channel?: (id: string) => string | undefined }} [names] + */ +export function neutraliseMentions(text, names = {}) { + return String(text ?? '') + .replace(/<@!?(\d{5,25})>/g, (_, id) => inertAt(`@${stripControl(names.user?.(id)) || 'user'}`)) + .replace(/<@&(\d{5,25})>/g, (_, id) => inertAt(`@${stripControl(names.role?.(id)) || 'role'}`)) + .replace(/<#(\d{5,25})>/g, (_, id) => `#${stripControl(names.channel?.(id)) || 'channel'}`) + .replace(/<\/([^:>\s]{1,32}(?: [^:>\s]{1,32}){0,2}):\d{5,25}>/g, (_, name) => `/${name}`) + .replace(/@(everyone|here)\b/gi, (_, word) => `@${ZWSP}${word}`); +} + +/** + * Custom emoji: the same name in the other server when it has one, otherwise ":name:". Unicode emoji + * are plain text and cross as they are. + * @param {string} text + * @param {(name: string) => { id: string, name: string, animated?: boolean } | undefined} find + */ +export function mapEmoji(text, find) { + return String(text ?? '').replace(/<(a?):([\w]{2,32}):(\d{5,25})>/g, (_, anim, name) => { + const match = find?.(name); + if (match?.id) return `<${match.animated ? 'a' : ''}:${match.name ?? name}:${match.id}>`; + return `:${name}:`; + }); +} + +/** `` is a Discord timestamp: the other platform may not render it, so say it in UTC. */ +const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; +const pad = (n) => String(n).padStart(2, '0'); +export function plainTimestamps(text) { + return String(text ?? '').replace(//g, (_, seconds) => { + const d = new Date(Number(seconds) * 1000); + if (Number.isNaN(d.getTime())) return ''; + return `${d.getUTCDate()} ${MONTHS[d.getUTCMonth()]} ${d.getUTCFullYear()}, ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`; + }); +} + +// --- the name and the picture of the sender ---------------------------------------------------------- + +export const PLATFORM_TAG = Object.freeze({ discord: 'Discord', fluxer: 'Fluxer' }); + +// Discord refuses a webhook name that contains these words, so a name from the other platform that has +// one gets a zero-width space inside it. +const FORBIDDEN_NAME = /(clyde|discord)/gi; + +/** + * The name a relayed message is posted under: the author's display name and where they are, so the + * conversation says who is on the other platform ("Alice [Fluxer]"). At most 80 characters, never empty. + */ +export function senderName(displayName, platform, { forDiscord = false } = {}) { + const tag = ` [${PLATFORM_TAG[platform] ?? platform}]`; + let name = inertAt(stripControl(displayName)).replace(/\s+/g, ' '); + if (forDiscord) name = name.replace(FORBIDDEN_NAME, (w) => `${w[0]}${ZWSP}${w.slice(1)}`); + name = clip(name, 80 - tag.length); + if (!name || name === ZWSP) name = 'Someone'; + return `${name}${tag}`; +} + +// --- replies ----------------------------------------------------------------------------------------- + +/** The first line of a copy that answers another message: who, a short quote, and a link when it is mapped. */ +export function replyHead({ name, snippet, url }) { + const who = inertAt(clip(stripControl(name) || 'someone', 40)); + const text = clip(stripControl(String(snippet ?? '').replace(/\s+/g, ' ')).replace(/^>+\s*/, ''), 100); + const label = url ? `[**${who}**](<${url}>)` : `**${who}**`; + return text ? `> ↪ ${label}: ${text}` : `> ↪ ${label}`; +} + +// --- stickers, polls, files -------------------------------------------------------------------------- + +/** One line per sticker: the name and its picture link, which both platforms show as an image. */ +export function stickerLines(stickers) { + return [...(stickers ?? [])] + .filter((s) => s && (s.url || s.name)) + .slice(0, 3) + .map((s) => + s.url + ? `*sticker: ${clip(stripControl(s.name) || 'sticker', 40)}* ${s.url}` + : `*sticker: ${clip(s.name, 40)}*` + ); +} + +/** + * A poll as text: Fluxer has no polls, and a bridge cannot make a vote on one side count on the other, + * so the question and the answers are shown and the vote stays where the poll is. + * @param {{ question?: string, answers?: Array, multiselect?: boolean }} poll + */ +export function pollText(poll) { + if (!poll) return ''; + const answers = [...(poll.answers ?? [])] + .map((a) => (typeof a === 'string' ? { text: a } : a)) + .filter((a) => a?.text) + .slice(0, 10); + const lines = [`📊 **Poll:** ${clip(stripControl(poll.question) || 'Poll', 300)}`]; + for (const a of answers) lines.push(`• ${a.emoji ? `${a.emoji} ` : ''}${clip(stripControl(a.text), 100)}`); + lines.push('*Vote on the original server: polls do not cross the bridge.*'); + return lines.join('\n'); +} + +/** + * Decide which attachments are re-uploaded and which become a link. A file is uploaded when uploading is + * on, it is not larger than the per-file limit, and the message stays within the count and total limits; + * everything else is a link line. + * @param {Array<{ name: string, url: string, size?: number }>} attachments + * @param {{ upload?: boolean, perFile?: number, total?: number, count?: number }} [limits] + */ +export function planAttachments( + attachments, + { upload = true, perFile = FILE_BYTES_LIMIT, total = TOTAL_BYTES_LIMIT, count = FILE_COUNT_LIMIT } = {} +) { + const toUpload = []; + const links = []; + let used = 0; + for (const a of attachments ?? []) { + if (!a?.url) continue; + const size = Number(a.size); + const fits = + upload && + Number.isFinite(size) && + size > 0 && + size <= perFile && + used + size <= total && + toUpload.length < count; + if (fits) { + toUpload.push(a); + used += size; + } else { + links.push(a); + } + } + return { upload: toUpload, links }; +} + +/** The link lines for attachments that were not uploaded. */ +export const fileLinkLines = (links) => + links.slice(0, 10).map((a) => `📎 ${clip(stripControl(a.name) || 'file', 60)}: ${a.url}`); + +// --- embeds ------------------------------------------------------------------------------------------ + +/** + * A rich embed from either SDK as the plain API shape both accept, with its text neutralised. Embeds that + * are only a link preview are left out: the other platform makes its own preview of the link. + * @param {any} embed a discord.js Embed or a Fluxer MessageEmbed + * @param {(text: string) => string} text what to do to every piece of text (mentions, emoji) + */ +export function plainEmbed(embed, text = (t) => t) { + const e = embed?.toJSON?.() ?? embed; + if (!e) return null; + const type = e.type ?? 'rich'; + if (type !== 'rich') return null; + const pick = (v) => (typeof v === 'string' && v ? v : undefined); + const clean = (o) => (o ? Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)) : o); + const url = (v) => (typeof v === 'string' && /^https?:\/\//i.test(v) ? v : undefined); + const out = { + title: pick(text(clip(e.title ?? '', 256))), + description: pick(text(clip(e.description ?? '', 4000))), + url: url(e.url), + color: Number.isFinite(e.color) ? e.color : undefined, + timestamp: pick( + typeof e.timestamp === 'string' + ? e.timestamp + : e.timestamp instanceof Date + ? e.timestamp.toISOString() + : undefined + ), + footer: e.footer?.text + ? clean({ + text: text(clip(e.footer.text, 2048)), + icon_url: url(e.footer.icon_url ?? e.footer.iconURL ?? e.footer.iconUrl), + }) + : undefined, + image: url(e.image?.url) ? { url: e.image.url } : undefined, + thumbnail: url(e.thumbnail?.url) ? { url: e.thumbnail.url } : undefined, + author: e.author?.name + ? clean({ + name: text(clip(e.author.name, 256)), + url: url(e.author.url), + icon_url: url(e.author.icon_url ?? e.author.iconURL ?? e.author.iconUrl), + }) + : undefined, + fields: Array.isArray(e.fields) + ? e.fields + .filter((f) => f?.name && f?.value) + .slice(0, 25) + .map((f) => ({ + name: text(clip(f.name, 256)), + value: text(clip(f.value, 1024)), + inline: f.inline === true, + })) + : undefined, + }; + for (const key of Object.keys(out)) if (out[key] === undefined) delete out[key]; + return Object.keys(out).length ? out : null; +} + +/** The embeds of a message as plain API objects, at most ten, link previews left out. */ +export function plainEmbeds(embeds, text) { + return [...(embeds ?? [])] + .map((e) => plainEmbed(e, text)) + .filter(Boolean) + .slice(0, EMBED_LIMIT); +} + +/** Joins the parts of a copy with a blank-free line break and keeps the whole within the message limit. */ +export function joinParts(parts, limit = MESSAGE_LIMIT) { + return clip( + parts + .map((p) => String(p ?? '').trim()) + .filter(Boolean) + .join('\n'), + limit + ); +} diff --git a/src/modules/registry.js b/src/modules/registry.js index 0212448f..670e18cc 100644 --- a/src/modules/registry.js +++ b/src/modules/registry.js @@ -393,6 +393,17 @@ export const MODULES = [ configurable: true, beta: true, }, + { + id: 'bridge', + name: 'Bridge', + description: + 'Share one conversation between a Discord channel and a Fluxer channel. Both servers agree to a link with a one-time code, and either can unlink at any time.', + icon: '🌉', + requiredIntents: ['MessageContent'], + defaultEnabled: false, + configurable: true, + beta: true, + }, { id: 'economy', name: 'Economy', diff --git a/src/platform/modulePlatformNotes.js b/src/platform/modulePlatformNotes.js index 40ffa210..189d487a 100644 --- a/src/platform/modulePlatformNotes.js +++ b/src/platform/modulePlatformNotes.js @@ -31,6 +31,8 @@ const FLUXER_NOTES = { `Fluxer has no slash commands, no private replies, no modals, no buttons and no threads yet. Members start with a chat command: ${prefix}apply start (and ${prefix}apply withdraw). Sylo answers "Check your DMs" in the channel and removes that message after a few seconds, then asks the questions in DM. A choice or role question is asked as numbered options, and the member answers with the numbers (at most 10 options a question). The panel message, the Answer in DMs and Open on the web choice, the Submit and Start over choices under the preview, and the Accept and Deny buttons on each application are shown as reactions. Staff who want to give a reason use ${prefix}application accept or ${prefix}application deny , or the dashboard. No staff thread is opened. The web form and the dashboard work as on Discord.`, economy: ({ prefix }) => `Fluxer has no slash commands or buttons yet: members use chat commands, for example ${prefix}balance, ${prefix}daily, ${prefix}work, ${prefix}pay @member 100, ${prefix}deposit all and ${prefix}economy-leaderboard (turn the pages with the ⬅️ and ➡️ reactions). Staff use ${prefix}eco add @member 500 reason:"Event prize". A rob result is posted in the channel as on Discord, and the victim gets the same DM. The shop is ${prefix}shop (a numbered list that pages with the ⬅️ and ➡️ reactions), ${prefix}buy , ${prefix}inventory and ${prefix}collection (collectibles with their pictures as embeds; its pages turn with the same reactions; Fluxer has no private replies, so with "Members can show their collection" off Sylo sends your own collection to your DMs, and tells you when your DMs are closed). A giveaway with an entry fee or a currency prize works as on Discord: members press the reaction to enter, and a second press leaves and gets the fee back. The dashboard and the web shop work as on Discord.`, + bridge: ({ prefix }) => + `A bridge joins this Fluxer channel to a Discord channel: set it up in the dashboard (no ${prefix}command is needed). Messages cross through webhooks with the author's name and picture; a poll on Discord shows on Fluxer as text, and a sticker as a picture link. Nothing pings across the bridge: mentions are shown as names.`, 'auto-threads': () => 'Fluxer has no threads yet, so this module does nothing on a Fluxer server: posts stay as they are. The settings can be saved here and start working on a Discord server. Fluxer lists threads for 2026; Sylo will support them then.', welcome: () => diff --git a/src/platform/webhooks.js b/src/platform/webhooks.js new file mode 100644 index 00000000..28a53112 --- /dev/null +++ b/src/platform/webhooks.js @@ -0,0 +1,146 @@ +// Webhooks on both platforms behind one small interface, for the bridge (modules/bridge.js). +// +// A server key ("123..." or "fluxer:123...") says which platform a call is for, so the bridge never +// branches on the platform itself: it asks for a webhook in a channel, then sends, edits and deletes +// through it. The two SDKs differ in small ways (discord.js has a WebhookClient and names the avatar +// `avatarURL`; Fluxer builds a Webhook from an id and a token and names it `avatarUrl`; a file is +// `{ attachment, name }` on one and `{ name, data }` on the other), and those differences live here. +// +// Every message is sent with allowedMentions set to none: whatever the text says, a webhook copy never +// pings anyone. Callers pass a platform-neutral payload: +// { content?, username?, avatarUrl?, embeds?, files?: [{ name, data: Buffer }] } +import { WebhookClient } from 'discord.js'; +import { splitPlatformGuildId } from '../lib/platformId.js'; +import { guildByKey } from './platformOf.js'; + +/** No mention of anything: the copy shows names as text and pings nobody. */ +export const NO_MENTIONS = Object.freeze({ parse: [] }); + +/** @typedef {{ id: string, token: string, channelId?: string }} Hook (the channel is only for the Fluxer SDK's own bookkeeping) */ + +/** + * @typedef {object} BridgeTransport + * @property {(guildKey: string, channelId: string, name: string) => Promise} createHook + * @property {(guildKey: string, hook: Hook, payload: object) => Promise} send resolves the new message id + * @property {(guildKey: string, hook: Hook, messageId: string, payload: object) => Promise} edit + * @property {(guildKey: string, hook: Hook, messageId: string) => Promise} remove + * @property {(guildKey: string, hook: Hook) => Promise} deleteHook best effort + */ + +const platformOfKey = (key) => splitPlatformGuildId(key)?.platform ?? 'discord'; + +// A webhook client per webhook, kept so each send does not build one. Bounded: a long-running instance +// with many links still holds only a few hundred. +const MAX_CACHED = 500; +const clients = new Map(); +const remember = (cacheKey, make) => { + let client = clients.get(cacheKey); + if (!client) { + if (clients.size >= MAX_CACHED) clients.delete(clients.keys().next().value); + client = make(); + clients.set(cacheKey, client); + } + return client; +}; + +/** Forget the cached client of a webhook (it was deleted or replaced). */ +export function forgetHook(hookId) { + for (const key of [...clients.keys()]) if (key.endsWith(`|${hookId}`)) clients.delete(key); +} + +async function discordHook(hook) { + return remember(`discord|${hook.id}`, () => new WebhookClient({ id: hook.id, token: hook.token })); +} + +async function fluxerHook(guild, hook) { + const { Webhook } = await import('@fluxerjs/core'); + return remember(`fluxer|${hook.id}`, () => + Webhook.fromToken(guild.client, hook.id, hook.token, { channelId: hook.channelId, guildId: guild.id }) + ); +} + +const guildOf = (key) => { + const guild = guildByKey(key); + if (!guild) throw Object.assign(new Error('Sylo is not in that server'), { code: 'NO_GUILD' }); + return guild; +}; + +/** The payload in the shape one platform's send() takes. Always with allowedMentions none. */ +export function webhookBody(platform, payload, { forEdit = false } = {}) { + const out = { allowedMentions: NO_MENTIONS }; + if (payload.content !== undefined) out.content = payload.content; + if (payload.embeds) out.embeds = payload.embeds; + if (!forEdit) { + if (payload.username) out.username = payload.username; + if (payload.avatarUrl) out[platform === 'fluxer' ? 'avatarUrl' : 'avatarURL'] = payload.avatarUrl; + if (payload.files?.length) { + out.files = payload.files.map((f) => + platform === 'fluxer' ? { name: f.name, data: f.data } : { attachment: f.data, name: f.name } + ); + } + } + return out; +} + +/** @returns {BridgeTransport} */ +export function createWebhookTransport() { + return { + async createHook(guildKey, channelId, name) { + const guild = guildOf(guildKey); + const channel = guild.channels?.cache?.get(channelId); + if (!channel?.createWebhook) + throw Object.assign(new Error('That channel cannot have a webhook'), { code: 'NO_CHANNEL' }); + const created = await channel.createWebhook({ name, reason: 'Sylo bridge' }); + if (!created?.id || !created?.token) throw new Error('The platform did not return a webhook token'); + return { id: String(created.id), token: String(created.token) }; + }, + + async send(guildKey, hook, payload) { + const platform = platformOfKey(guildKey); + if (platform === 'fluxer') { + const client = await fluxerHook(guildOf(guildKey), hook); + const sent = await client.send(webhookBody(platform, payload), true); + if (!sent?.id) throw new Error('Fluxer did not return the sent message'); + return String(sent.id); + } + const client = await discordHook(hook); + const sent = await client.send(webhookBody(platform, payload)); + return String(sent.id); + }, + + async edit(guildKey, hook, messageId, payload) { + const platform = platformOfKey(guildKey); + const body = webhookBody(platform, payload, { forEdit: true }); + if (platform === 'fluxer') { + const client = await fluxerHook(guildOf(guildKey), hook); + await client.editMessage(messageId, body); + return; + } + await (await discordHook(hook)).editMessage(messageId, body); + }, + + async remove(guildKey, hook, messageId) { + const platform = platformOfKey(guildKey); + if (platform === 'fluxer') { + const client = await fluxerHook(guildOf(guildKey), hook); + await client.deleteMessage(messageId); + return; + } + await (await discordHook(hook)).deleteMessage(messageId); + }, + + async deleteHook(guildKey, hook) { + const platform = platformOfKey(guildKey); + try { + if (platform === 'fluxer') { + const client = await fluxerHook(guildOf(guildKey), hook); + await client.delete(); + } else { + await (await discordHook(hook)).delete('Sylo bridge unlinked'); + } + } finally { + forgetHook(hook.id); + } + }, + }; +} diff --git a/src/web/lib/cardSummary.js b/src/web/lib/cardSummary.js index 1da061ed..5cc0a91f 100644 --- a/src/web/lib/cardSummary.js +++ b/src/web/lib/cardSummary.js @@ -86,6 +86,10 @@ const FORMAT = { noun(num(l, 'Saved quotes'), 'saved quote'), ...(isOn(l, 'Public page') ? ['public page'] : []), ], + bridge: (l) => [ + ...count(l, 'Bridged channels', 'bridged channel'), + ...(num(l, 'Stopped') ? [`${num(l, 'Stopped')} stopped`] : []), + ], introductions: (l) => [ ...(isOn(l, 'Channel') ? [value(l, 'Channel')] : ['No channel yet']), ...count(l, 'Introductions', 'introduction'), @@ -187,6 +191,7 @@ const NEEDS_OWNER_KEYS = new Set(['twitch-alerts', 'kick-alerts', 'apple-connect /** What an empty module says it is missing, instead of the general "Not set up yet". */ const EMPTY = { sticky: 'No sticky messages yet', + bridge: 'No bridged channels yet', 'auto-threads': 'No channels yet', 'auto-react': 'No rules yet', 'custom-commands': 'No commands yet', diff --git a/src/web/lib/configImport.js b/src/web/lib/configImport.js index 7411868e..e13056ab 100644 --- a/src/web/lib/configImport.js +++ b/src/web/lib/configImport.js @@ -446,6 +446,8 @@ const SKIPPED = { 'Custom commands are registered with the platform one by one, so they are in the file but not imported; build them in the module.', reminders: 'Reminders are in the file but not imported yet; create them in the module.', 'youtube-alerts': 'YouTube channels are looked up when you add them; add them in the module.', + bridge: + 'A bridge joins two servers and needs both to agree with a code, so it cannot be imported; set it up in the module.', insights: 'Server insights has nothing to configure.', 'game-stats': 'Game stats has nothing to configure.', 'channel-cleanup': diff --git a/src/web/lib/moduleIcons.js b/src/web/lib/moduleIcons.js index 09af8332..75214574 100644 --- a/src/web/lib/moduleIcons.js +++ b/src/web/lib/moduleIcons.js @@ -34,6 +34,7 @@ export const MODULE_ICONS = { 'temp-roles': 'hourglass', 'auto-threads': 'threads', 'quote-board': 'quote', + bridge: 'threads', introductions: 'id-card', applications: 'clipboard', economy: 'coins', diff --git a/src/web/lib/overviewSummary.js b/src/web/lib/overviewSummary.js index 0a7a610d..f6b31d8b 100644 --- a/src/web/lib/overviewSummary.js +++ b/src/web/lib/overviewSummary.js @@ -21,6 +21,7 @@ import { openSuggestionCount, suggestionCount } from '../../db/suggestions.js'; import { nextTempRoleExpiry, tempRoleCount } from '../../db/tempRoles.js'; import { quoteCount } from '../../db/quotes.js'; import { introductionCount } from '../../db/introductions.js'; +import { linksOfGuild } from '../../db/bridge.js'; import { formCount, pendingCount } from '../../db/applications.js'; import { recentLookups } from '../../db/cache.js'; import { dailySeries } from '../../db/insights.js'; @@ -84,6 +85,7 @@ const LAYOUT = [ 'autoresponder', 'auto-react', 'auto-threads', + 'bridge', 'afk', 'server-stats', 'insights', @@ -326,6 +328,14 @@ async function moduleLines(id, guild, cfg) { const n = Array.isArray(cfg.stickies) ? cfg.stickies.length : 0; return [n ? on('Active sticky messages', String(n)) : off('Active sticky messages', 'none')]; } + case 'bridge': { + const links = await linksOfGuild(guild.id); + const stopped = links.filter((l) => l.status === 'failed').length; + return [ + links.length ? on('Bridged channels', String(links.length)) : off('Bridged channels', 'none'), + stopped ? off('Stopped', String(stopped)) : neutral('Stopped', '0'), + ]; + } case 'quote-board': { const saved = await quoteCount(guild.id); return [ diff --git a/src/web/lib/permissionHealth.js b/src/web/lib/permissionHealth.js index 685ed148..6725c7e7 100644 --- a/src/web/lib/permissionHealth.js +++ b/src/web/lib/permissionHealth.js @@ -19,6 +19,7 @@ import { listCleanupSchedules } from '../../db/channelCleanup.js'; import { listGithubWatches } from '../../db/githubWatches.js'; import { listItems } from '../../db/economyShop.js'; import { listForms } from '../../db/applications.js'; +import { bridgeProblems, bridgedChannels } from '../../modules/bridge.js'; import { guildSupports, platformOfGuild, rawGuildId } from '../../platform/platformOf.js'; import { healthFor } from '../../lib/healthCache.js'; import { sourceLabel } from '../../modules/rss.js'; @@ -44,6 +45,7 @@ const LABELS = { ManageRoles: 'Manage Roles', ManageChannels: 'Manage Channels', ManageNicknames: 'Manage Nicknames', + ManageWebhooks: 'Manage Webhooks', ManageGuild: 'Manage Server', ModerateMembers: 'Timeout Members', KickMembers: 'Kick Members', @@ -240,6 +242,9 @@ export const NEEDS = { arr(cfg.boards).map((b) => post(b?.channelId, `The channel of ${b?.name || 'a starboard'}`, { field: 'channelId' }) ), + // The bridge makes and uses a webhook in each bridged channel, and says things in it. + bridge: (_cfg, x) => + arr(x.channels).map((id) => post(id, 'A bridged channel', { embed: false, extra: ['ManageWebhooks'] })), introductions: (cfg) => [ post(cfg.channelId, 'The introductions channel', { extra: ['ReadMessageHistory'], field: 'channelId' }), post(cfg.promptChannelId, 'The prompt channel', { optional: true, field: 'promptChannelId' }), @@ -423,6 +428,10 @@ const EXTRAS = { 'channel-cleanup': async (guildId) => ({ cleanup: await listCleanupSchedules(guildId) }), reminders: async (guildId) => ({ reminders: await listScheduled(guildId) }), github: async (guildId) => ({ githubWatches: await listGithubWatches(guildId) }), + bridge: async (guildId) => ({ + channels: await bridgedChannels(guildId), + problems: await bridgeProblems(guildId), + }), }; // --- comparing a need with what Sylo can do --------------------------------------------------- @@ -629,8 +638,9 @@ export async function buildHealthReport(guild, { modules } = {}) { const mod = getModule(def.id); const cfg = row?.config && typeof row.config === 'object' ? row.config : {}; let needs; + let x; try { - const x = EXTRAS[def.id] ? await EXTRAS[def.id](guild.id) : {}; + x = EXTRAS[def.id] ? await EXTRAS[def.id](guild.id) : {}; needs = needsOf(cfg, x, guild).filter(Boolean); } catch (err) { log.warn('health', `could not read the settings of ${def.id} (${guild.id})`, err); @@ -654,6 +664,21 @@ export async function buildHealthReport(guild, { modules } = {}) { checks.push(check); } } + // A bridged channel Sylo stopped (a lost permission, a webhook it could not remake, ...) is a problem to fix. + for (const problem of def.id === 'bridge' ? arr(x.problems) : []) { + const channel = guild.channels?.cache?.get(problem.channelId); + checks.push({ + ...moduleBase, + severity: ERROR, + kind: 'bridge-stopped', + code: 'bridge-stopped', + targetKind: 'channel', + targetId: problem.channelId, + targetName: channel?.name ?? null, + message: `The bridge of ${channel ? channelName(channel) : 'a channel'} stopped: ${problem.text}`, + fix: 'Fix the cause, then press Resume for that channel on the Bridge page.', + }); + } // A role to give or take away also needs Manage Roles, once per module, not once per role. if (needs.some((n) => n.type === 'role' && n.action !== 'ping')) { const missing = guildCheck(moduleBase, guildPerm('ManageRoles', 'give and take away roles'), me); diff --git a/src/web/lib/routeAccess.js b/src/web/lib/routeAccess.js index fb09e393..bb688a54 100644 --- a/src/web/lib/routeAccess.js +++ b/src/web/lib/routeAccess.js @@ -199,6 +199,16 @@ export const ROUTE_ACCESS = Object.freeze({ 'POST /apply/:guildId/:formId/submit': 'member', 'POST /apply/:guildId/:formId/withdraw': 'member', + // v2Bridge.js + 'GET /guilds/:guildId/modules/bridge/config': 'view', + 'POST /guilds/:guildId/modules/bridge/request': 'edit', + 'POST /guilds/:guildId/modules/bridge/requests/:id/cancel': 'edit', + 'POST /guilds/:guildId/modules/bridge/accept': 'edit', + 'POST /guilds/:guildId/modules/bridge/links/:id/options': 'edit', + 'POST /guilds/:guildId/modules/bridge/links/:id/pause': 'edit', + 'POST /guilds/:guildId/modules/bridge/links/:id/resume': 'edit', + 'POST /guilds/:guildId/modules/bridge/links/:id/unlink': 'edit', + // v2Economy.js 'GET /guilds/:guildId/modules/economy/config': 'view', 'POST /guilds/:guildId/modules/economy/config': 'edit', diff --git a/src/web/routes/mobile.js b/src/web/routes/mobile.js index 1d5575c4..31abac05 100644 --- a/src/web/routes/mobile.js +++ b/src/web/routes/mobile.js @@ -312,6 +312,8 @@ export const mobileApiRouter = Router(); // sign-in is on and both Discord and Fluxer logins are set up. `access-levels`: View / Inbox / Edit // dashboard access, with `access` on each GET /api/v2/guilds entry and GET/POST // /api/v2/guilds/:id/access. `message-move`: a posted message moves to its new channel on publish. +// `bridge`: the Bridge module (GET /api/v2/guilds/:id/modules/bridge/config and the request, accept, options, +// pause, resume and unlink calls under /modules/bridge/), which joins a Discord and a Fluxer channel. const FEATURES = Object.freeze([ 'app-login', 'live-access', @@ -328,6 +330,7 @@ const FEATURES = Object.freeze([ 'duplicate', 'message-templates', 'message-move', + 'bridge', ]); // Public and ahead of the open-mode guard: the app calls it before anyone signs in, and an open-mode diff --git a/src/web/routes/v2Api.js b/src/web/routes/v2Api.js index 1db30f44..679ccb68 100644 --- a/src/web/routes/v2Api.js +++ b/src/web/routes/v2Api.js @@ -64,6 +64,7 @@ import tempRolesRouter from './v2TempRoles.js'; import quotesRouter from './v2Quotes.js'; import searchRouter from './v2Search.js'; import introRouter from './v2Intro.js'; +import bridgeRouter from './v2Bridge.js'; import applicationsRouter from './v2Applications.js'; import setupRouter from './v2Setup.js'; import applyMemberRouter from './v2Apply.js'; @@ -545,6 +546,8 @@ router.use('/guilds/:guildId', tempRolesRouter); router.use('/guilds/:guildId', quotesRouter); // Introductions: settings, publish the prompt message (v2Intro.js). router.use('/guilds/:guildId', introRouter); +// Bridge: links to a channel on the other platform, requests and codes, options (v2Bridge.js). +router.use('/guilds/:guildId', bridgeRouter); // Economy: settings, members and their ledger, staff changes, reset (v2Economy.js). router.use('/guilds/:guildId', economyRouter); // Economy shop: items, a member's inventory, the activity log (v2Shop.js). diff --git a/src/web/routes/v2Bridge.js b/src/web/routes/v2Bridge.js new file mode 100644 index 00000000..822433dd --- /dev/null +++ b/src/web/routes/v2Bridge.js @@ -0,0 +1,169 @@ +// JSON API for the Bridge page of the dashboard: the links of a server, asking for a link, accepting one +// with a code, the options of this server's side, and pause, resume and unlink. The rules live in +// modules/bridge.js. +// +// Mounted at /guilds/:guildId AFTER the access guard in v2Api.js, so req.guild is set and the caller has +// the level routeAccess.js gives each route: View reads the page, Edit changes anything. Every link is +// looked up through the server in the URL (a link names two servers, and a server may only see and change +// its own side), so another server's link id is a 404 here. +import { accessRouter } from '../middleware/access.js'; +import { currentUser } from '../middleware/auth.js'; +import { clientKey, platformActionLimit, rateLimit } from '../middleware/rateLimit.js'; +import { asyncHandler } from '../lib/asyncHandler.js'; +import { sweepMap } from '../../lib/sweepMap.js'; +import { + BridgeError, + acceptLink, + bridgeOverview, + cancelRequest, + pauseLink, + requestLink, + resumeLink, + setSideOptions, + unlinkLink, +} from '../../modules/bridge.js'; + +const router = accessRouter('/guilds/:guildId', { mergeParams: true }); + +const actor = (req) => (currentUser(req)?.open ? 'Dashboard' : `${currentUser(req).name} (dashboard)`); +const isId = (v) => /^\d{17,20}$/.test(String(v ?? '')); +const linkId = (req) => { + const n = Number(req.params.id); + if (!Number.isSafeInteger(n) || n < 1) + throw new BridgeError('No such bridged channel in this server.', 404); + return n; +}; + +/** Run a handler; a refusal of the bridge becomes its own status and message instead of a server error. */ +const guarded = (fn) => + asyncHandler(async (req, res) => { + try { + await fn(req, res); + } catch (err) { + if (err instanceof BridgeError) return res.status(err.status).json({ error: err.message }); + throw err; + } + }); + +// A code is about 59 bits, so guessing one is hopeless; the limits are about not letting the endpoint be a +// way to hammer the platforms (every accept makes webhooks) or to try codes at speed. One budget per +// person, whichever server they are in, and a lock after several wrong codes. +const acceptLimit = rateLimit({ + windowMs: 60_000, + max: 8, + message: 'Too many attempts. Wait a minute and try again.', + keyFn: (req) => `bridge-accept|${req.account?.id ?? clientKey(req)}`, +}); +const WRONG_LIMIT = 6; +const WRONG_WINDOW_MS = 10 * 60_000; +const wrong = new Map(); // `${person}|${server}` -> { n, at } +sweepMap(wrong, (v, now) => now - v.at > WRONG_WINDOW_MS); +const who = (req) => `${req.account?.id ?? clientKey(req)}|${req.params.guildId}`; +const locked = (req) => { + const hit = wrong.get(who(req)); + return Boolean(hit && Date.now() - hit.at < WRONG_WINDOW_MS && hit.n >= WRONG_LIMIT); +}; +const noteWrong = (req) => { + const id = who(req); + const hit = wrong.get(id); + wrong.set(id, { n: (hit && Date.now() - hit.at < WRONG_WINDOW_MS ? hit.n : 0) + 1, at: Date.now() }); +}; + +const overview = (req) => bridgeOverview(req.guild.id); + +router.get( + '/modules/bridge/config', + asyncHandler(async (req, res) => { + res.json(await overview(req)); + }) +); + +router.post( + '/modules/bridge/request', + platformActionLimit, + guarded(async (req, res) => { + if (!isId(req.body?.channelId)) throw new BridgeError('Pick a channel.'); + const made = await requestLink({ + guildKey: req.guild.id, + channelId: String(req.body.channelId), + options: req.body?.options, + actor: actor(req), + }); + // The code is shown once: only its hash is stored. + res.json({ code: made.code, expiresAt: made.expiresAt, overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/requests/:id/cancel', + guarded(async (req, res) => { + await cancelRequest({ guildKey: req.guild.id, id: linkId(req), actor: actor(req) }); + res.json({ overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/accept', + acceptLimit, + platformActionLimit, + guarded(async (req, res) => { + if (locked(req)) { + throw new BridgeError('Too many wrong codes. Wait a few minutes before trying again.', 429); + } + if (!isId(req.body?.channelId)) throw new BridgeError('Pick a channel.'); + try { + await acceptLink({ + guildKey: req.guild.id, + channelId: String(req.body.channelId), + code: String(req.body?.code ?? ''), + options: req.body?.options, + actor: actor(req), + }); + } catch (err) { + // Only a code that did not work counts as wrong; a channel problem is the person's to fix. + if (err instanceof BridgeError && err.status === 404) noteWrong(req); + throw err; + } + res.json({ overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/links/:id/options', + guarded(async (req, res) => { + await setSideOptions({ + guildKey: req.guild.id, + id: linkId(req), + options: req.body?.options ?? {}, + actor: actor(req), + }); + res.json({ overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/links/:id/pause', + guarded(async (req, res) => { + await pauseLink({ guildKey: req.guild.id, id: linkId(req), actor: actor(req) }); + res.json({ overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/links/:id/resume', + platformActionLimit, + guarded(async (req, res) => { + await resumeLink({ guildKey: req.guild.id, id: linkId(req), actor: actor(req) }); + res.json({ overview: await overview(req) }); + }) +); + +router.post( + '/modules/bridge/links/:id/unlink', + guarded(async (req, res) => { + await unlinkLink({ guildKey: req.guild.id, id: linkId(req), actor: actor(req) }); + res.json({ overview: await overview(req) }); + }) +); + +export default router; diff --git a/test/bridge.db.test.js b/test/bridge.db.test.js new file mode 100644 index 00000000..54df53d2 --- /dev/null +++ b/test/bridge.db.test.js @@ -0,0 +1,381 @@ +// Bridge storage: the three tables (migration 83), one-time codes (only the hash is stored, they expire +// and are used up by the first claim), links (a channel is in at most one), the message map (a lookup +// from either side, swept after 30 days by the retention job) and the guild-leave purge. +import './helpers/tmpDb.js'; +import test, { beforeEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { db, MIGRATIONS, SCHEMA_VERSION } from '../src/db/index.js'; +import { + CODE_TTL_MS, + MESSAGE_MAP_TTL_MS, + cancelCode, + claimCode, + createCode, + createLink, + deleteLink, + deleteMapping, + getLink, + linkCount, + linkForChannel, + linkForHook, + linksOfGuild, + looksLikeCode, + mappingOf, + newCode, + normaliseCode, + peekCode, + pendingCodeCount, + pendingCodes, + purgeBridgeGuild, + saveMapping, + setLinkHook, + setLinkOptions, + setLinkStatus, + sweepCodes, + sweepMappings, +} from '../src/db/bridge.js'; +import { GUILD_TABLES, purgeGuild } from '../src/db/purge.js'; +import { sweepRetention } from '../src/db/retention.js'; + +const D = '710000000000000001'; +const F = 'fluxer:710000000000000002'; +const D2 = '710000000000000003'; +const CH = { + d1: '720000000000000001', + f1: '720000000000000002', + d2: '720000000000000003', + f2: '720000000000000004', +}; + +const link = (over = {}) => ({ + guildA: D, + channelA: CH.d1, + guildB: F, + channelB: CH.f1, + hookA: { id: 'hook-a', token: 'secret-a' }, + hookB: { id: 'hook-b', token: 'secret-b' }, + requestedBy: 'Alice (dashboard)', + acceptedBy: 'Bob (dashboard)', + ...over, +}); + +beforeEach(async () => { + for (const g of [D, F, D2]) await purgeGuild(g); + db.prepare('DELETE FROM bridge_codes').run(); +}); + +test('migration 83 creates the three tables, the unique channel indexes and the 30-day index', () => { + assert.equal(SCHEMA_VERSION, MIGRATIONS.length); + assert.ok(MIGRATIONS.length >= 83, 'the Bridge keeps version 83 (81 and 82 belong to other changes)'); + const names = (t) => + db + .prepare(`PRAGMA table_info(${t})`) + .all() + .map((c) => c.name); + assert.deepEqual(names('bridge_links'), [ + 'id', + 'guild_a', + 'channel_a', + 'guild_b', + 'channel_b', + 'status', + 'paused_side', + 'fail_reason', + 'failure_count', + 'options_a', + 'options_b', + 'hook_a_id', + 'hook_a_token', + 'hook_b_id', + 'hook_b_token', + 'requested_by_name', + 'accepted_by_name', + 'created_at', + 'updated_at', + ]); + assert.deepEqual(names('bridge_codes'), [ + 'id', + 'guild_id', + 'channel_id', + 'code_hash', + 'options', + 'requested_by_name', + 'expires_at', + 'created_at', + ]); + assert.deepEqual(names('bridge_messages'), [ + 'link_id', + 'origin', + 'a_message_id', + 'b_message_id', + 'copy_hook_id', + 'content_hash', + 'extra', + 'created_at', + ]); + const indexes = db + .prepare("SELECT name FROM sqlite_master WHERE type = 'index' AND name LIKE 'idx_bridge_%'") + .all() + .map((r) => r.name); + for (const name of [ + 'idx_bridge_links_a', + 'idx_bridge_links_b', + 'idx_bridge_codes_hash', + 'idx_bridge_messages_b', + 'idx_bridge_messages_created', + ]) { + assert.ok(indexes.includes(name), name); + } + // 81 and 82 are placeholders until their own changes land: they must not have made any table of theirs here. + assert.equal(typeof MIGRATIONS[80], 'function'); + assert.equal(typeof MIGRATIONS[81], 'function'); +}); + +test('bridge_codes is in GUILD_TABLES; the links go through purgeBridgeGuild (they name two servers)', () => { + assert.ok(GUILD_TABLES.includes('bridge_codes')); + assert.ok(!GUILD_TABLES.includes('bridge_links')); +}); + +test('the table columns hold names, not user ids (so /forget has nothing to find)', () => { + const columns = db + .prepare('PRAGMA table_info(bridge_links)') + .all() + .map((c) => c.name); + assert.ok(columns.includes('requested_by_name') && columns.includes('accepted_by_name')); + assert.ok( + !columns.some((c) => /(user|author|owner|decided|moderator|actor)_(id|by)$|^created_by$/.test(c)) + ); +}); + +// --- codes --------------------------------------------------------------------------------------------- + +test('a code is 12 characters of an unambiguous alphabet, shown as XXXX-XXXX-XXXX', () => { + for (let i = 0; i < 50; i += 1) { + const code = newCode(); + assert.match(code, /^[A-HJKMNP-Z2-9]{4}-[A-HJKMNP-Z2-9]{4}-[A-HJKMNP-Z2-9]{4}$/); + assert.ok(looksLikeCode(code)); + assert.ok(looksLikeCode(code.toLowerCase().replace(/-/g, ' ')), 'typed in lower case, with spaces'); + } + assert.equal(normaliseCode(' ab-cd ef '), 'ABCDEF'); + assert.ok(!looksLikeCode('0123-4567-89AB'), '0, 1, O, I and L are not in the alphabet'); + assert.ok(!looksLikeCode('')); + assert.ok(!looksLikeCode(null)); +}); + +test('only the hash of a code is stored, and it is used up by the first claim', async () => { + const made = await createCode({ + guildId: D, + channelId: CH.d1, + options: { bots: true }, + requestedBy: 'Alice', + }); + const stored = JSON.stringify(db.prepare('SELECT * FROM bridge_codes').all()); + assert.ok(!stored.includes(made.code), 'the code is not stored'); + assert.ok(!stored.includes(normaliseCode(made.code)), 'nor its plain form'); + assert.equal(db.prepare('SELECT code_hash FROM bridge_codes').get().code_hash.length, 64); + + assert.equal((await peekCode(made.code)).guildId, D, 'peek reads without using it up'); + assert.equal(await pendingCodeCount(D), 1); + const claimed = await claimCode(made.code.toLowerCase()); + assert.deepEqual( + { + guildId: claimed.guildId, + channelId: claimed.channelId, + options: claimed.options, + by: claimed.requestedBy, + }, + { guildId: D, channelId: CH.d1, options: { bots: true }, by: 'Alice' } + ); + assert.equal(await claimCode(made.code), null, 'a second server cannot use it'); + assert.equal(await peekCode(made.code), null); +}); + +test('a code expires after its time, and a wrong or malformed code finds nothing', async () => { + const made = await createCode({ guildId: D, channelId: CH.d1 }); + assert.equal(made.expiresAt - Date.now() <= CODE_TTL_MS, true); + assert.equal(await claimCode('ABCD-EFGH-JKMN'), null); + assert.equal(await claimCode('not a code'), null); + assert.equal(await claimCode(made.code, made.expiresAt + 1), null, 'past its time'); + assert.ok(await claimCode(made.code, made.expiresAt - 1), 'still inside its time'); + + const old = await createCode({ guildId: D, channelId: CH.d2, ttlMs: -1000 }); + assert.equal(await pendingCodeCount(D), 0, 'an expired request is not pending'); + assert.equal(await sweepCodes(), 1); + assert.equal(await claimCode(old.code), null); +}); + +test('pending requests list without their codes, and only the server that made one can cancel it', async () => { + const a = await createCode({ guildId: D, channelId: CH.d1, requestedBy: 'Alice' }); + await createCode({ guildId: D2, channelId: CH.d2 }); + const list = await pendingCodes(D); + assert.equal(list.length, 1); + assert.deepEqual(Object.keys(list[0]).sort(), ['channelId', 'createdAt', 'expiresAt', 'id', 'requestedBy']); + assert.equal(await cancelCode(D2, a.id), false, 'another server cannot cancel it'); + assert.equal(await cancelCode(D, a.id), true); + assert.equal(await pendingCodeCount(D), 0); + assert.equal(await claimCode(a.code), null); +}); + +// --- links --------------------------------------------------------------------------------------------- + +test('create, read and find a link from either side, from the channel or from a webhook', async () => { + const id = await createLink(link()); + assert.ok(id > 0); + const row = await getLink(id); + assert.equal(row.status, 'active'); + assert.equal(row.guildA, D); + assert.equal(row.channelB, CH.f1); + assert.deepEqual(row.optionsA, {}); + assert.equal(row.requestedBy, 'Alice (dashboard)'); + assert.equal((await linkForChannel(D, CH.d1)).side, 'a'); + assert.equal((await linkForChannel(F, CH.f1)).side, 'b'); + assert.equal(await linkForChannel(D, CH.f1), null, 'a channel id of another server is not a match'); + assert.equal((await linkForHook('hook-b')).id, id); + assert.equal(await linkForHook('nope'), null); + assert.equal(await linkForHook(''), null); + assert.deepEqual( + (await linksOfGuild(F)).map((l) => l.id), + [id] + ); + assert.equal(await linkCount(D), 1); + assert.equal(await linkCount(D2), 0); +}); + +test('a channel is in at most one link, from either side', async () => { + assert.ok(await createLink(link())); + assert.equal(await createLink(link({ channelB: CH.f2 })), null, 'the same channel a second time'); + assert.equal(await createLink(link({ channelA: CH.d2 })), null, 'the same channel b second time'); + assert.ok(await createLink(link({ channelA: CH.d2, channelB: CH.f2 })), 'other channels are free'); +}); + +test('status, options and the webhook of a side change independently', async () => { + const id = await createLink(link()); + await setLinkStatus(id, { status: 'paused', pausedSide: 'b' }); + let row = await getLink(id); + assert.deepEqual([row.status, row.pausedSide], ['paused', 'b']); + await setLinkStatus(id, { status: 'failed', pausedSide: 'a', reason: 'permissions', failures: 5 }); + row = await getLink(id); + assert.deepEqual( + [row.status, row.pausedSide, row.failReason, row.failureCount], + ['failed', '', 'permissions', 5] + ); + await setLinkStatus(id, { status: 'active' }); + row = await getLink(id); + assert.deepEqual([row.status, row.failReason, row.failureCount], ['active', '', 0]); + + await setLinkOptions(id, 'b', { bots: true }); + row = await getLink(id); + assert.deepEqual([row.optionsA, row.optionsB], [{}, { bots: true }]); + + await setLinkHook(id, 'a', { id: 'hook-new', token: 'tok-new' }); + row = await getLink(id); + assert.deepEqual([row.hookAId, row.hookAToken, row.hookBId], ['hook-new', 'tok-new', 'hook-b']); + assert.equal(await linkForHook('hook-a'), null, 'the old webhook no longer belongs to the link'); +}); + +// --- the message map ------------------------------------------------------------------------------------- + +test('the map finds a message from either side, with the counterpart and who was the original', async () => { + const id = await createLink(link()); + await saveMapping(id, 'a', { + sourceId: 'src-1', + copyId: 'copy-1', + hookId: 'hook-b', + hash: 'h1', + extra: { head: '> hi' }, + }); + await saveMapping(id, 'b', { sourceId: 'src-2', copyId: 'copy-2', hookId: 'hook-a' }); + + const fromA = await mappingOf(id, 'a', 'src-1'); + assert.deepEqual( + [fromA.counterpartId, fromA.isOriginal, fromA.copyHookId, fromA.contentHash, fromA.extra], + ['copy-1', true, 'hook-b', 'h1', { head: '> hi' }] + ); + const fromB = await mappingOf(id, 'b', 'copy-1'); + assert.deepEqual( + [fromB.counterpartId, fromB.isOriginal], + ['src-1', false], + 'the copy points back to the original' + ); + const second = await mappingOf(id, 'b', 'src-2'); + assert.deepEqual([second.counterpartId, second.isOriginal], ['copy-2', true]); + assert.equal(await mappingOf(id, 'a', 'nope'), null); + assert.equal(await mappingOf(id, 'b', 'src-1'), null, 'an id is looked up on its own side only'); + + await deleteMapping(id, 'a', 'src-1'); + assert.equal(await mappingOf(id, 'a', 'src-1'), null); + assert.equal(await mappingOf(id, 'b', 'copy-1'), null); +}); + +test('saving a mapping again for the same message replaces it, and the map is per link', async () => { + const one = await createLink(link()); + const two = await createLink( + link({ channelA: CH.d2, channelB: CH.f2, hookA: { id: 'x', token: 'x' }, hookB: { id: 'y', token: 'y' } }) + ); + await saveMapping(one, 'a', { sourceId: 'm', copyId: 'c1' }); + await saveMapping(one, 'a', { sourceId: 'm', copyId: 'c2', hash: 'new' }); + assert.equal((await mappingOf(one, 'a', 'm')).counterpartId, 'c2'); + assert.equal(await mappingOf(two, 'a', 'm'), null, 'another link has its own map'); +}); + +test('a mapping expires after 30 days: the sweep, and the daily retention job, remove it', async () => { + const id = await createLink(link()); + const day = 86_400_000; + await saveMapping(id, 'a', { sourceId: 'old', copyId: 'old-copy' }); + await saveMapping(id, 'a', { sourceId: 'new', copyId: 'new-copy' }); + db.prepare('UPDATE bridge_messages SET created_at = ? WHERE a_message_id = ?').run( + Date.now() - 31 * day, + 'old' + ); + assert.equal(MESSAGE_MAP_TTL_MS, 30 * day); + assert.equal(await sweepMappings(), 1); + assert.equal(await mappingOf(id, 'a', 'old'), null); + assert.ok(await mappingOf(id, 'a', 'new')); + + db.prepare('UPDATE bridge_messages SET created_at = ? WHERE a_message_id = ?').run( + Date.now() - 40 * day, + 'new' + ); + await createCode({ guildId: D, channelId: CH.d1, ttlMs: -1 }); + await sweepRetention(); + assert.equal(await mappingOf(id, 'a', 'new'), null, 'the retention job sweeps the map'); + assert.equal(db.prepare('SELECT COUNT(*) AS n FROM bridge_codes').get().n, 0, 'and the expired requests'); +}); + +// --- purge ----------------------------------------------------------------------------------------------- + +test('deleting a link removes its map; a server leaving removes every link it is in, on either side', async () => { + const one = await createLink(link()); + const two = await createLink( + link({ + guildA: D2, + channelA: CH.d2, + channelB: CH.f2, + hookA: { id: 'p', token: 'p' }, + hookB: { id: 'q', token: 'q' }, + }) + ); + await saveMapping(one, 'a', { sourceId: 'm1', copyId: 'c1' }); + await saveMapping(two, 'a', { sourceId: 'm2', copyId: 'c2' }); + await createCode({ guildId: F, channelId: CH.f1 }); + + assert.equal(await deleteLink(one), 1); + assert.equal(await mappingOf(one, 'a', 'm1'), null); + assert.ok(await mappingOf(two, 'a', 'm2'), 'the other link keeps its map'); + + const one2 = await createLink(link()); + await saveMapping(one2, 'a', { sourceId: 'm3', copyId: 'c3' }); + assert.equal(await purgeBridgeGuild(F), 2, 'F is on the b side of both links'); + assert.equal(await getLink(one2), null); + assert.equal(await getLink(two), null); + assert.equal(await mappingOf(one2, 'a', 'm3'), null); + assert.equal(db.prepare('SELECT COUNT(*) AS n FROM bridge_messages').get().n, 0); + + // purgeGuild does the same, and clears the unaccepted requests of the server too + const three = await createLink(link()); + await purgeGuild(D); + assert.equal(await getLink(three), null); + await createCode({ guildId: D, channelId: CH.d1 }); + await purgeGuild(D); + assert.equal(db.prepare('SELECT COUNT(*) AS n FROM bridge_codes WHERE guild_id = ?').get(D).n, 0); +}); diff --git a/test/bridge.engine.test.js b/test/bridge.engine.test.js new file mode 100644 index 00000000..be9947b9 --- /dev/null +++ b/test/bridge.engine.test.js @@ -0,0 +1,1045 @@ +// The Bridge engine (src/modules/bridge.js): the consent flow, and everything a message meets on its way +// across: loops, pings, bots, the age rule, replies, edits and deletes, files, emoji, stickers, embeds, +// polls, join notices, the rate limit and what a failing link does. Platforms and webhooks are fakes +// (helpers/fakeBridge.js); the HTTP routes are in test/v2Bridge.test.js. +import './helpers/tmpDb.js'; +import test, { afterEach, beforeEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { setGuildModule } from '../src/db/modules.js'; +import { purgeGuild } from '../src/db/purge.js'; +import { listAudit } from '../src/db/audit.js'; +import * as store from '../src/db/bridge.js'; +import { + BridgeError, + FAIL_THRESHOLD, + acceptLink, + bridgeIdle, + bridgeProblems, + cancelRequest, + dropBridgeCache, + handleBulkDelete, + handleChannelDelete, + handleDelete, + handleJoin, + handleMessage, + handleUpdate, + linkView, + pauseLink, + requestLink, + resumeLink, + setSideOptions, + unlinkLink, + useBridgeParts, +} from '../src/modules/bridge.js'; +import { createRelayQueue } from '../src/modules/lib/bridgeQueue.js'; +import { jumpUrl } from '../src/modules/quoteBoard.js'; +import { + D, + F, + ME_D, + apiError, + installPlatforms, + message, + removePlatforms, + snowflake, + useFakes, +} from './helpers/fakeBridge.js'; + +let p; // the platforms +let fakes; // transport, sleeps, downloads + +beforeEach(async () => { + p = installPlatforms(); + // Links left by the previous test are purged first (that talks to the webhook transport), then the + // fakes start empty for this test. + useFakes(); + dropBridgeCache(); + for (const key of [D, F]) await purgeGuild(key); + fakes = useFakes(); + await setGuildModule(D, 'bridge', { enabled: true }); + await setGuildModule(F, 'bridge', { enabled: true }); +}); +afterEach(() => { + useBridgeParts(); + removePlatforms(); +}); + +/** A bridged pair of channels, made through the real request and accept calls. */ +async function bridged({ nsfw = false, optionsD, optionsF } = {}) { + const dCh = p.channel(p.dGuild, { name: 'discord-chat', nsfw }); + const fCh = p.channel(p.fGuild, { name: 'fluxer-chat', nsfw }); + const made = await requestLink({ + guildKey: D, + channelId: dCh.id, + options: optionsD, + actor: 'Alice (dashboard)', + }); + const link = await acceptLink({ + guildKey: F, + channelId: fCh.id, + code: made.code, + options: optionsF, + actor: 'Bob (dashboard)', + }); + return { dCh, fCh, link, code: made.code }; +} +const say = (channel) => channel.said.map((s) => s.content); +const fromD = (dCh, over = {}) => message({ channelId: dCh.id, client: p.discord, ...over }); +const fromF = (fCh, over = {}) => message({ channelId: fCh.id, client: p.fluxer, ...over }); +const relay = async (msg, key) => { + await handleMessage(msg, {}, key); + await bridgeIdle(); +}; +const refuses = async (promise, pattern, status) => { + await assert.rejects(promise, (err) => { + assert.ok(err instanceof BridgeError, `a BridgeError, got ${err}`); + assert.match(err.message, pattern); + if (status) assert.equal(err.status, status); + return true; + }); +}; + +// --- the consent flow ------------------------------------------------------------------------------------ + +test('a request gives a one-time code; accepting it in the other server links the two channels', async () => { + const dCh = p.channel(p.dGuild, { name: 'discord-chat' }); + const fCh = p.channel(p.fGuild, { name: 'fluxer-chat' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice (dashboard)' }); + assert.match(made.code, /^[A-Z2-9]{4}-[A-Z2-9]{4}-[A-Z2-9]{4}$/); + assert.equal((await store.pendingCodes(D)).length, 1); + assert.equal( + await store.linkForChannel(D, dCh.id), + null, + 'nothing is linked until the other server accepts' + ); + assert.equal(fakes.transport.hooks.length, 0, 'and no webhook exists yet'); + + const link = await acceptLink({ + guildKey: F, + channelId: fCh.id, + code: made.code, + actor: 'Bob (dashboard)', + }); + assert.deepEqual( + [link.guildA, link.channelA, link.guildB, link.channelB, link.status], + [D, dCh.id, F, fCh.id, 'active'] + ); + assert.deepEqual([link.requestedBy, link.acceptedBy], ['Alice (dashboard)', 'Bob (dashboard)']); + assert.deepEqual( + fakes.transport.hooks.map((h) => [h.guildKey, h.channelId]), + [ + [D, dCh.id], + [F, fCh.id], + ], + 'one webhook in each channel' + ); + assert.ok(link.hookAId && link.hookAToken && link.hookBId && link.hookBToken); + assert.equal((await store.pendingCodes(D)).length, 0, 'the request is used up'); + await refuses( + acceptLink({ guildKey: F, channelId: p.channel(p.fGuild, {}).id, code: made.code, actor: 'Eve' }), + /not valid, has expired or was already used/, + 404 + ); + + // both channels show a visible notice, and both servers' audit logs record it + assert.match(say(dCh)[0], /Bridge on.*#fluxer-chat.*Fluxer Home.*Fluxer.*Nobody is pinged/s); + assert.match(say(fCh)[0], /Bridge on.*#discord-chat.*Discord Home.*Discord.*Nobody is pinged/s); + assert.deepEqual(dCh.said[0].allowedMentions, { parse: [] }, 'the notice pings nobody either'); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:link')); + assert.ok((await listAudit(F)).some((e) => e.action === 'bridge:link')); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:request')); +}); + +test('a server cannot accept its own request, nor link two servers of the same platform', async () => { + const dCh = p.channel(p.dGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }); + await refuses( + acceptLink({ guildKey: D, channelId: p.channel(p.dGuild, {}).id, code: made.code, actor: 'Alice' }), + /made in this server/, + 409 + ); + const { dGuild: d2 } = p.extra('91'); + const d2Key = d2.id; + await setGuildModule(d2Key, 'bridge', { enabled: true }); + await refuses( + acceptLink({ guildKey: d2Key, channelId: p.channel(d2, {}).id, code: made.code, actor: 'Eve' }), + /Discord channel to a Fluxer channel/ + ); + assert.equal((await store.pendingCodes(D)).length, 1, 'neither attempt used the code up'); +}); + +test('a wrong, malformed, expired or cancelled code is refused the same way', async () => { + const fCh = p.channel(p.fGuild, {}); + const dCh = p.channel(p.dGuild, {}); + const accept = (code) => acceptLink({ guildKey: F, channelId: fCh.id, code, actor: 'Bob' }); + await refuses(accept('ABCD-EFGH-JKMN'), /not valid, has expired or was already used/, 404); + await refuses(accept('hello'), /not valid, has expired or was already used/, 404); + await refuses(accept(''), /not valid, has expired or was already used/, 404); + const expired = await store.createCode({ guildId: D, channelId: dCh.id, ttlMs: -1000 }); + await refuses(accept(expired.code), /not valid, has expired or was already used/, 404); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }); + const [pending] = await store.pendingCodes(D); + await cancelRequest({ guildKey: D, id: pending.id, actor: 'Alice' }); + await refuses(accept(made.code), /not valid, has expired or was already used/, 404); + await refuses(cancelRequest({ guildKey: D, id: pending.id, actor: 'Alice' }), /gone already/, 404); + assert.equal(fakes.transport.hooks.length, 0, 'no wrong code made a webhook'); +}); + +test("only the requesting server may cancel a request, and no server sees another's", async () => { + const dCh = p.channel(p.dGuild, {}); + await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }); + const [pending] = await store.pendingCodes(D); + await refuses(cancelRequest({ guildKey: F, id: pending.id, actor: 'Eve' }), /gone already/, 404); + assert.equal((await store.pendingCodes(D)).length, 1); +}); + +test('both servers need the module on, and Sylo needs the permissions in the channel', async () => { + const dCh = p.channel(p.dGuild, {}); + const fCh = p.channel(p.fGuild, {}); + await setGuildModule(D, 'bridge', { enabled: false }); + await refuses( + requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }), + /Turn the Bridge module on/, + 409 + ); + await setGuildModule(D, 'bridge', { enabled: true }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + + await setGuildModule(F, 'bridge', { enabled: false }); + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /Turn the Bridge module on/, + 409 + ); + await setGuildModule(F, 'bridge', { enabled: true }); + + await setGuildModule(D, 'bridge', { enabled: false }); + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /turned its Bridge module off/, + 409 + ); + await setGuildModule(D, 'bridge', { enabled: true }); + + const locked = p.channel(p.fGuild, { name: 'locked', deny: ['ManageWebhooks'] }); + await refuses( + acceptLink({ guildKey: F, channelId: locked.id, code: made.code, actor: 'B' }), + /needs ManageWebhooks in #locked/ + ); + assert.equal(fakes.transport.hooks.length, 0); + assert.ok( + await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + 'the code still worked' + ); +}); + +test('only a normal text channel of the same server can be bridged', async () => { + const fCh = p.channel(p.fGuild, {}); + await refuses( + requestLink({ guildKey: D, channelId: fCh.id, actor: 'A' }), + /normal text channel of this server/ + ); + await refuses( + requestLink({ guildKey: D, channelId: snowflake(), actor: 'A' }), + /normal text channel of this server/ + ); + const voice = p.channel(p.dGuild, { type: 2 }); + await refuses( + requestLink({ guildKey: D, channelId: voice.id, actor: 'A' }), + /normal text channel of this server/ + ); +}); + +test('a channel can be in one link or one request only', async () => { + const { dCh } = await bridged(); + await refuses(requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }), /already bridged/, 409); + const other = p.channel(p.dGuild, {}); + await requestLink({ guildKey: D, channelId: other.id, actor: 'A' }); + await refuses(requestLink({ guildKey: D, channelId: other.id, actor: 'A' }), /request waiting/, 409); + const fSecond = p.channel(p.fGuild, {}); + const made = await requestLink({ guildKey: D, channelId: p.channel(p.dGuild, {}).id, actor: 'A' }); + const fCh = [...p.fGuild.channels.cache.values()][0]; + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /already bridged/, + 409 + ); + assert.ok(await acceptLink({ guildKey: F, channelId: fSecond.id, code: made.code, actor: 'B' })); +}); + +test('an age-restricted channel only links to an age-restricted one, in both directions', async () => { + const mk = async (dNsfw, fNsfw) => { + const dCh = p.channel(p.dGuild, { nsfw: dNsfw }); + const fCh = p.channel(p.fGuild, { nsfw: fNsfw }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + return () => acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }); + }; + await refuses( + (await mk(true, false))(), + /age-restricted channel can only be bridged with another age-restricted/ + ); + await refuses( + (await mk(false, true))(), + /age-restricted channel can only be bridged with another age-restricted/ + ); + assert.equal(fakes.transport.hooks.length, 0, 'refused before any webhook was made'); + assert.ok(await (await mk(true, true))(), 'two age-restricted channels link'); + assert.ok(await (await mk(false, false))(), 'two ordinary channels link'); +}); + +test('a webhook that cannot be made refuses the link without using the code up', async () => { + const dCh = p.channel(p.dGuild, {}); + const fCh = p.channel(p.fGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + fakes.transport.hookFailures.push(null, apiError(403)); + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /could not make a webhook/, + 502 + ); + assert.equal(fakes.transport.deleted.length, 1, 'the webhook that was made is removed again'); + assert.equal((await store.pendingCodes(D)).length, 1); + assert.ok(await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' })); +}); + +test('two servers that accept the same code at once: one wins, the other leaves nothing behind', async () => { + const dCh = p.channel(p.dGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + const f1 = p.channel(p.fGuild, {}); + const f2 = p.channel(p.fGuild, {}); + const results = await Promise.allSettled([ + acceptLink({ guildKey: F, channelId: f1.id, code: made.code, actor: 'B' }), + acceptLink({ guildKey: F, channelId: f2.id, code: made.code, actor: 'C' }), + ]); + assert.equal(results.filter((r) => r.status === 'fulfilled').length, 1); + assert.equal((await store.linksOfGuild(D)).length, 1); + assert.equal( + fakes.transport.hooks.length - fakes.transport.deleted.length, + 2, + 'only the winner keeps its two webhooks' + ); +}); + +// --- relaying a message ---------------------------------------------------------------------------------- + +test('a message is posted on the other side under the author name and platform, never with a ping', async () => { + const { dCh, fCh, link } = await bridged(); + await relay(fromD(dCh, { content: 'Hello from Discord' }), D); + assert.equal(fakes.transport.sends.length, 1); + const sent = fakes.transport.sends[0]; + assert.equal(sent.guildKey, F, 'sent into the Fluxer server'); + assert.deepEqual(sent.hook, { id: link.hookBId, token: link.hookBToken, channelId: fCh.id }); + assert.equal(sent.payload.content, 'Hello from Discord'); + assert.equal(sent.payload.username, 'Alice [Discord]'); + + await relay(fromF(fCh, { content: 'Hello back', member: { displayName: 'Bob' } }), F); + const back = fakes.transport.sends[1]; + assert.equal(back.guildKey, D); + assert.equal(back.hook.id, link.hookAId); + assert.equal(back.payload.username, 'Bob [Fluxer]'); +}); + +test('mentions are shown as names and @everyone and @here cannot ping: users, roles, channels, commands', async () => { + const { dCh } = await bridged(); + const msg = fromD(dCh, { + content: + '@everyone @here <@111111111111111111> <@!111111111111111111> <@&222222222222222222> <#333333333333333333> hi', + mentions: { + users: new Map([ + ['111111111111111111', { id: '111111111111111111', username: 'bob', displayName: 'Bobby' }], + ]), + members: new Map(), + roles: new Map([['222222222222222222', { id: '222222222222222222', name: 'moderators' }]]), + }, + }); + p.dGuild.channels.cache.set('333333333333333333', { id: '333333333333333333', name: 'rules' }); + await relay(msg, D); + const text = fakes.transport.sends[0].payload.content; + assert.ok(!/<[@#&]/.test(text), `no raw mention tokens left: ${text}`); + assert.ok(!/@everyone|@here/.test(text), 'a plain @everyone or @here is broken by an invisible character'); + assert.match(text, /@.everyone @.here @.Bobby @.Bobby @.moderators #rules \/ban hi/); +}); + +test('custom emoji map to the same name on the other side, else become :name:; Unicode crosses as it is', async () => { + const { dCh } = await bridged(); + p.fGuild.emojis.cache.set('555555555555555555', { + id: '555555555555555555', + name: 'Party', + animated: true, + }); + await relay(fromD(dCh, { content: 'hi 🎉 <:party:123456789012345678> <:nope:123456789012345679>' }), D); + assert.equal(fakes.transport.sends[0].payload.content, 'hi 🎉 :nope:'); +}); + +test('a reply carries a short quote of the original and a link to its copy when it is mapped', async () => { + const { dCh, fCh } = await bridged(); + const original = fromF(fCh, { content: 'Who wants pizza?', member: { displayName: 'Bob' } }); + await relay(original, F); + const copyOnD = fakes.transport.sends[0].id; + + const reply = fromD(dCh, { + content: 'Me!', + reference: { messageId: copyOnD }, + referencedMessage: { content: 'Who wants pizza?', author: { username: 'Bob [Fluxer]' } }, + }); + await relay(reply, D); + const content = fakes.transport.sends[1].payload.content; + const url = jumpUrl(F, fCh.id, original.id); + assert.equal(content, `> ↪ [**Bob**](<${url}>): Who wants pizza?\nMe!`); + + const unmapped = fromD(dCh, { + content: 'Hm', + reference: { messageId: snowflake() }, + referencedMessage: { + content: 'old message', + author: { username: 'carol' }, + member: { displayName: 'Carol' }, + }, + }); + await relay(unmapped, D); + assert.equal( + fakes.transport.sends[2].payload.content, + '> ↪ **Carol**: old message\nHm', + 'quoted, with no link' + ); +}); + +test('a quote of a message with pings is neutralised too', async () => { + const { dCh, fCh } = await bridged(); + const reply = fromF(fCh, { + content: 'ok', + messageReference: { messageId: snowflake() }, + referencedMessage: { content: '@everyone look', author: { username: 'x' } }, + }); + await relay(reply, F); + assert.match(fakes.transport.sends[0].payload.content, /^> ↪ \*\*x\*\*: @.everyone look\nok$/); + assert.equal(fakes.transport.sends[0].guildKey, D); + assert.ok(dCh); +}); + +// --- loops, bots and what is not chat ------------------------------------------------------------------------ + +test('what a bridge webhook posted is never relayed again, so a copy cannot come back', async () => { + const { dCh, fCh, link } = await bridged(); + await relay(fromD(dCh, { content: 'ping' }), D); + assert.equal(fakes.transport.sends.length, 1); + // The copy arrives in the Fluxer channel as a message from the webhook Sylo made there. + const copy = fromF(fCh, { + content: 'ping', + webhookId: link.hookBId, + author: { bot: true, username: 'Alice [Discord]' }, + }); + await relay(copy, F); + // Even with bots switched on for that side, and even from the OTHER webhook of the link. + await setSideOptions({ guildKey: F, id: link.id, options: { bots: true }, actor: 'B' }); + await relay(copy, F); + await relay(fromD(dCh, { content: 'x', webhookId: link.hookAId, author: { bot: true } }), D); + assert.equal(fakes.transport.sends.length, 1, 'nothing went back'); +}); + +test("Sylo's own messages and messages from any Sylo bridge webhook are not relayed, even with bots on", async () => { + const { dCh, link } = await bridged({ optionsD: { bots: true } }); + await relay(fromD(dCh, { content: 'welcome!', author: { id: ME_D, bot: true } }), D); + // another link's webhook posting into this channel + const otherD = p.channel(p.dGuild, {}); + const otherF = p.channel(p.fGuild, {}); + const made = await requestLink({ guildKey: D, channelId: otherD.id, actor: 'A' }); + const second = await acceptLink({ guildKey: F, channelId: otherF.id, code: made.code, actor: 'B' }); + await relay(fromD(dCh, { content: 'stray', webhookId: second.hookAId, author: { bot: true } }), D); + assert.equal(fakes.transport.sends.length, 0); + assert.ok(link); +}); + +test('bots are off by default and only cross when that side turns them on', async () => { + const { dCh, link } = await bridged(); + const bot = () => fromD(dCh, { content: 'beep', member: null, author: { bot: true, username: 'RoleBot' } }); + const foreignHook = () => + fromD(dCh, { + content: 'ci done', + member: null, + webhookId: snowflake(), + author: { bot: true, username: 'CI' }, + }); + await relay(bot(), D); + await relay(foreignHook(), D); + assert.equal(fakes.transport.sends.length, 0, 'off by default'); + await setSideOptions({ guildKey: D, id: link.id, options: { bots: true }, actor: 'A' }); + await relay(bot(), D); + await relay(foreignHook(), D); + assert.equal(fakes.transport.sends.length, 2); + assert.equal(fakes.transport.sends[0].payload.username, 'RoleBot [Discord]'); +}); + +test("each side's options only change what that side sends", async () => { + const { dCh, fCh, link } = await bridged({ optionsD: { bots: true } }); + await relay(fromD(dCh, { content: 'a', author: { bot: true } }), D); + await relay(fromF(fCh, { content: 'b', author: { bot: true } }), F); + assert.equal(fakes.transport.sends.length, 1, 'the Fluxer side never turned bots on'); + const view = linkView(await store.getLink(link.id), F); + assert.deepEqual([view.options.bots, view.theirOptions.bots], [false, true]); +}); + +test('system messages, empty messages and threads are not relayed', async () => { + const { dCh } = await bridged(); + await relay(fromD(dCh, { content: 'x joined', type: 7, system: true }), D); + await relay(fromD(dCh, { content: '' }), D); + await relay(message({ channelId: snowflake(), client: p.discord, content: 'in a thread' }), D); + assert.equal(fakes.transport.sends.length, 0); +}); + +test('nothing is relayed while the other server has the module off, or the link is paused', async () => { + const { dCh, link } = await bridged(); + await setGuildModule(F, 'bridge', { enabled: false }); + await relay(fromD(dCh, { content: 'one' }), D); + await setGuildModule(F, 'bridge', { enabled: true }); + await pauseLink({ guildKey: F, id: link.id, actor: 'B' }); + await relay(fromD(dCh, { content: 'two' }), D); + assert.equal(fakes.transport.sends.length, 0); + await resumeLink({ guildKey: F, id: link.id, actor: 'B' }); + await relay(fromD(dCh, { content: 'three' }), D); + assert.equal(fakes.transport.sends.length, 1); +}); + +test('the age rule is checked on every message: a channel that changes pauses the link', async () => { + const { dCh, link } = await bridged({ nsfw: true }); + await relay(fromD(dCh, { content: 'fine' }), D); + assert.equal(fakes.transport.sends.length, 1); + dCh.nsfw = false; // the Discord channel is made ordinary + await relay(fromD(dCh, { content: 'now what' }), D); + assert.equal(fakes.transport.sends.length, 1, 'not relayed'); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'age-restriction']); + assert.ok( + say(dCh).some((s) => /Bridge stopped/.test(s)), + 'and said so in the channel' + ); +}); + +// --- edits and deletes ------------------------------------------------------------------------------------- + +test('an edit is mirrored on the copy; an edit that changes nothing visible is not', async () => { + const { dCh } = await bridged(); + const original = fromD(dCh, { content: 'first' }); + await relay(original, D); + const copyId = fakes.transport.sends[0].id; + + await handleUpdate( + { old: original, new: { ...original, content: 'second <@123456789012345678> @everyone' } }, + {}, + D + ); + await bridgeIdle(); + assert.equal(fakes.transport.edits.length, 1); + const edit = fakes.transport.edits[0]; + assert.equal(edit.messageId, copyId); + assert.match(edit.payload.content, /^second @.user @.everyone$/); + + // a link preview appearing fires an update with the same text: nothing to do + await handleUpdate( + { + old: original, + new: { + ...original, + content: 'second <@123456789012345678> @everyone', + embeds: [{ type: 'link', url: 'https://x' }], + }, + }, + {}, + D + ); + await bridgeIdle(); + assert.equal(fakes.transport.edits.length, 1); +}); + +test('an edit keeps the quote and the file links of the original copy', async () => { + const { dCh, fCh } = await bridged(); + const original = fromF(fCh, { content: 'pizza?', member: { displayName: 'Bob' } }); + await relay(original, F); + const reply = fromD(dCh, { + content: 'yes', + reference: { messageId: fakes.transport.sends[0].id }, + referencedMessage: { content: 'pizza?', author: { username: 'Bob [Fluxer]' } }, + attachments: new Map([['a', { name: 'big.zip', url: 'https://cdn/big.zip', size: 50 * 1024 * 1024 }]]), + }); + await relay(reply, D); + const first = fakes.transport.sends[1].payload.content; + await handleUpdate({ old: reply, new: { ...reply, content: 'yes please' } }, {}, D); + await bridgeIdle(); + const edited = fakes.transport.edits[0].payload.content; + assert.match(first, /^> ↪ .*\nyes\n📎 big.zip: https:\/\/cdn\/big.zip$/s); + assert.match(edited, /^> ↪ .*\nyes please\n📎 big.zip: https:\/\/cdn\/big.zip$/s); +}); + +test('only the original is mirrored: an edit of a copy, or of a message that was never relayed, does nothing', async () => { + const { dCh, fCh } = await bridged(); + await relay(fromD(dCh, { content: 'one' }), D); + const copyOnF = { + id: fakes.transport.sends[0].id, + channelId: fCh.id, + content: 'tampered', + client: p.fluxer, + webhookId: 'x', + }; + await handleUpdate({ old: copyOnF, new: copyOnF }, {}, F); + await handleUpdate({ old: null, new: fromD(dCh, { content: 'never relayed' }) }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.edits.length, 0); +}); + +test('a delete is mirrored through the map, also for a purge of many; deleting a copy leaves the original', async () => { + const { dCh, fCh } = await bridged(); + const m1 = fromD(dCh, { content: 'one' }); + const m2 = fromD(dCh, { content: 'two' }); + const m3 = fromD(dCh, { content: 'three' }); + for (const m of [m1, m2, m3]) await relay(m, D); + const [c1, c2, c3] = fakes.transport.sends.map((s) => s.id); + + await handleDelete({ id: m1.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.removes.map((r) => r.messageId), + [c1] + ); + assert.equal(fakes.transport.removes[0].guildKey, F); + assert.equal( + await store.mappingOf((await store.linksOfGuild(D))[0].id, 'a', m1.id), + null, + 'the map row is gone' + ); + + await handleBulkDelete( + { + messages: new Map([ + [m2.id, {}], + [m3.id, {}], + ]), + channel: dCh, + }, + {}, + D + ); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.removes.map((r) => r.messageId), + [c1, c2, c3] + ); + + // someone deletes a copy on the Fluxer side: the original on Discord stays + const m4 = fromD(dCh, { content: 'four' }); + await relay(m4, D); + await handleDelete({ id: fakes.transport.sends[3].id, channelId: fCh.id }, {}, F); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length, 3, 'nothing else was deleted'); + await handleDelete({ id: snowflake(), channelId: dCh.id }, {}, D); + assert.equal(fakes.transport.removes.length, 3, 'an unknown message is ignored'); +}); + +test('a message older than the map (30 days) is no longer followed', async () => { + const { dCh, link } = await bridged(); + const m = fromD(dCh, { content: 'old' }); + await relay(m, D); + await store.sweepMappings(Date.now() + 31 * 86_400_000); + assert.equal(await store.mappingOf(link.id, 'a', m.id), null); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + await handleUpdate({ old: m, new: { ...m, content: 'edited' } }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length + fakes.transport.edits.length, 0); +}); + +// --- files, stickers, embeds, polls, joins ----------------------------------------------------------------- + +test('a small file is re-uploaded, a big one or one that cannot be fetched becomes a link', async () => { + const MB = 1024 * 1024; + fakes = useFakes({ files: { 'https://cdn/cat.png': 'PNGDATA' } }); + const { dCh } = await bridged(); + const att = (name, size, url = `https://cdn/${name}`) => [name, { name, url, size }]; + await relay( + fromD(dCh, { + content: 'files', + attachments: new Map([att('cat.png', 7), att('movie.mp4', 20 * MB), att('gone.png', 100)]), + }), + D + ); + const { payload } = fakes.transport.sends[0]; + assert.deepEqual( + payload.files.map((f) => [f.name, f.data.toString()]), + [['cat.png', 'PNGDATA']] + ); + assert.equal( + payload.content, + 'files\n📎 movie.mp4: https://cdn/movie.mp4\n📎 gone.png: https://cdn/gone.png' + ); + assert.deepEqual( + fakes.downloads.map((d) => d.url), + ['https://cdn/cat.png', 'https://cdn/gone.png'], + 'the big one was never fetched' + ); +}); + +test('a server can send every file as a link instead of copying it', async () => { + fakes = useFakes({ files: { 'https://cdn/cat.png': 'PNGDATA' } }); + const { dCh } = await bridged({ optionsD: { uploadFiles: false } }); + await relay( + fromD(dCh, { + content: '', + attachments: new Map([['a', { name: 'cat.png', url: 'https://cdn/cat.png', size: 7 }]]), + }), + D + ); + assert.equal(fakes.downloads.length, 0); + assert.equal(fakes.transport.sends[0].payload.files, undefined); + assert.equal(fakes.transport.sends[0].payload.content, '📎 cat.png: https://cdn/cat.png'); +}); + +test('stickers become a picture link, a poll becomes readable text, and link previews are left out', async () => { + const { dCh } = await bridged({ optionsD: { bots: true } }); + await relay( + fromD(dCh, { + content: 'look', + stickers: new Map([['s', { name: 'wave', url: 'https://cdn/stickers/1.png' }]]), + poll: { + question: { text: 'Pizza or pasta?' }, + answers: new Map([ + [1, { text: 'Pizza', emoji: { name: '🍕' } }], + [2, { text: 'Pasta' }], + ]), + }, + embeds: [{ type: 'link', url: 'https://example.com', title: 'A preview' }], + }), + D + ); + const { content, embeds } = fakes.transport.sends[0].payload; + assert.equal( + content, + 'look\n*sticker: wave* https://cdn/stickers/1.png\n📊 **Poll:** Pizza or pasta?\n• 🍕 Pizza\n• Pasta\n*Vote on the original server: polls do not cross the bridge.*' + ); + assert.equal(embeds, undefined, 'a link preview is the other platform’s own job'); +}); + +test('a rich embed from a bot crosses with its text made harmless', async () => { + const { dCh } = await bridged({ optionsD: { bots: true } }); + await relay( + fromD(dCh, { + content: '', + author: { bot: true, username: 'Deploy' }, + embeds: [ + { + type: 'rich', + title: 'Build done @everyone', + description: 'by <@123456789012345678>', + color: 0x00ff00, + fields: [{ name: 'Env', value: 'prod', inline: true }], + footer: { text: 'at ' }, + }, + ], + }), + D + ); + const [embed] = fakes.transport.sends[0].payload.embeds; + assert.match(embed.title, /^Build done @.everyone$/); + assert.match(embed.description, /^by @.user$/); + assert.equal(embed.color, 0x00ff00); + assert.deepEqual(embed.fields, [{ name: 'Env', value: 'prod', inline: true }]); + assert.match(embed.footer.text, /^at 14 Nov 2023, 22:13 UTC$/); +}); + +test('join notices are off until a server turns them on, and go to the other side only', async () => { + const { dCh, fCh, link } = await bridged(); + const member = { displayName: 'Zed', user: { bot: false } }; + await handleJoin(member, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.sends.length, 0); + await setSideOptions({ guildKey: D, id: link.id, options: { joinMessages: true }, actor: 'A' }); + await handleJoin(member, {}, D); + await handleJoin({ displayName: 'AutoBot', user: { bot: true } }, {}, D); + await handleJoin(member, {}, F); + await bridgeIdle(); + assert.equal( + fakes.transport.sends.length, + 1, + 'the Fluxer side never turned it on, and bots need their option' + ); + const sent = fakes.transport.sends[0]; + assert.equal(sent.guildKey, F); + assert.equal(sent.payload.content, '👋 **Zed** joined **Discord Home**'); + assert.equal(sent.payload.username, 'Discord Home [Discord]'); + assert.ok(dCh && fCh); +}); + +// --- the rate limit, back-off and failing links -------------------------------------------------------------- + +test('a rate limit is waited out and the message still arrives, in order', async () => { + const { dCh } = await bridged(); + fakes.transport.sendFailures.push(apiError(429, { retryAfter: 1.5 })); + await handleMessage(fromD(dCh, { content: 'first' }), {}, D); + await handleMessage(fromD(dCh, { content: 'second' }), {}, D); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.sends.map((s) => s.payload.content), + ['first', 'second'] + ); + assert.deepEqual(fakes.sleeps, [1500], 'it waited exactly what the platform asked'); + assert.equal(fakes.transport.attempts, 3); +}); + +test('a short outage backs off and recovers, and the failure count starts over', async () => { + const { dCh, link } = await bridged(); + fakes.transport.sendFailures.push(apiError(500), apiError(502)); + await relay(fromD(dCh, { content: 'try' }), D); + assert.equal(fakes.transport.sends.length, 1); + assert.deepEqual(fakes.sleeps, [1000, 2000], 'a growing pause'); + assert.equal((await store.getLink(link.id)).status, 'active'); +}); + +test(`a link whose deliveries fail ${FAIL_THRESHOLD} times in a row is stopped, announced and not retried`, async () => { + const { dCh, fCh, link } = await bridged(); + for (let i = 0; i < 4 * FAIL_THRESHOLD; i += 1) fakes.transport.sendFailures.push(apiError(500)); + for (let i = 0; i < FAIL_THRESHOLD; i += 1) await relay(fromD(dCh, { content: `m${i}` }), D); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'unreachable']); + assert.ok( + say(dCh).some((s) => /Bridge stopped/.test(s)) && say(fCh).some((s) => /Bridge stopped/.test(s)), + 'both channels say so' + ); + const before = fakes.transport.attempts; + await relay(fromD(dCh, { content: 'later' }), D); + assert.equal(fakes.transport.attempts, before, 'a stopped link is not tried again'); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:stopped')); + assert.deepEqual( + (await bridgeProblems(D)).map((x) => [x.linkId, x.channelId, x.code]), + [[link.id, dCh.id, 'unreachable']], + 'and it is listed for the Health page' + ); + assert.equal(linkView(row, D).problem.code, 'unreachable'); +}); + +test('a success between failures starts the count again, so a flaky link keeps running', async () => { + const { dCh, link } = await bridged(); + for (let round = 0; round < 3; round += 1) { + for (let i = 0; i < 4 * (FAIL_THRESHOLD - 1); i += 1) fakes.transport.sendFailures.push(apiError(500)); + for (let i = 0; i < FAIL_THRESHOLD - 1; i += 1) await relay(fromD(dCh, { content: 'x' }), D); + await relay(fromD(dCh, { content: 'ok' }), D); + } + assert.equal((await store.getLink(link.id)).status, 'active', 'never five in a row'); + assert.equal(fakes.transport.sends.filter((s) => s.payload.content === 'ok').length, 3); +}); + +test('a refused webhook (permissions) stops the link at once', async () => { + const { dCh, link } = await bridged(); + fakes.transport.sendFailures.push(apiError(403)); + await relay(fromD(dCh, { content: 'x' }), D); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'permissions']); +}); + +test('a deleted webhook is made again, once, and the message still arrives', async () => { + const { dCh, link } = await bridged(); + fakes.transport.sendFailures.push(apiError(404, { code: 10015 })); + await relay(fromD(dCh, { content: 'after the webhook was deleted' }), D); + assert.equal(fakes.transport.sends.length, 1); + const row = await store.getLink(link.id); + assert.equal(row.status, 'active'); + assert.notEqual(row.hookBId, link.hookBId, 'a new webhook for the Fluxer channel'); + assert.equal(fakes.transport.sends[0].hook.id, row.hookBId); + assert.equal(fakes.transport.hooks.length, 3); + assert.deepEqual( + fakes.transport.deleted.map((d) => d.hook.id), + [link.hookBId], + 'the old one is cleaned up' + ); +}); + +test('a webhook that cannot be made again stops the link', async () => { + const { dCh, link } = await bridged(); + fakes.transport.sendFailures.push(apiError(404, { code: 10015 })); + fakes.transport.hookFailures.push(apiError(403)); + await relay(fromD(dCh, { content: 'x' }), D); + const row = await store.getLink(link.id); + assert.equal(row.status, 'failed'); + assert.equal(fakes.transport.sends.length, 0); +}); + +test('one flooded channel drops its newest messages instead of growing; other links are unaffected', async () => { + const gate = {}; + gate.open = new Promise((resolve) => { + gate.release = resolve; + }); + fakes = useFakes({ queue: createRelayQueue({ minGapMs: 0, maxQueued: 2, sleep: async () => {} }) }); + const realSend = fakes.transport.send; + let sendCalls = 0; + fakes.transport.send = async (...args) => { + sendCalls += 1; + await gate.open; + return realSend(...args); + }; + const { dCh, link } = await bridged(); + const other = p.extra('92'); + await setGuildModule(other.dGuild.id, 'bridge', { enabled: true }); + await setGuildModule(`fluxer:${other.fGuild.id}`, 'bridge', { enabled: true }); + const dCh2 = p.channel(other.dGuild, {}); + const fCh2 = p.channel(other.fGuild, {}); + const made = await requestLink({ guildKey: other.dGuild.id, channelId: dCh2.id, actor: 'A' }); + await acceptLink({ + guildKey: `fluxer:${other.fGuild.id}`, + channelId: fCh2.id, + code: made.code, + actor: 'B', + }); + + for (let i = 0; i < 6; i += 1) await handleMessage(fromD(dCh, { content: `flood ${i}` }), {}, D); + await handleMessage(fromD(dCh2, { content: 'quiet server' }), {}, other.dGuild.id); + const view = linkView(await store.getLink(link.id), D); + assert.equal(view.queued, 2, 'two wait'); + assert.equal(view.dropped, 4, 'four were dropped'); + gate.release(); + await bridgeIdle(); + const contents = fakes.transport.sends.map((s) => s.payload.content); + assert.deepEqual( + contents.filter((c) => c.startsWith('flood')), + ['flood 0', 'flood 1'] + ); + assert.ok(contents.includes('quiet server'), 'the other server was not held up by the flood'); + assert.ok(sendCalls >= 3); +}); + +test('a failing link does not touch the links of other servers', async () => { + const one = await bridged(); + const other = p.extra('93'); + const otherF = `fluxer:${other.fGuild.id}`; + await setGuildModule(other.dGuild.id, 'bridge', { enabled: true }); + await setGuildModule(otherF, 'bridge', { enabled: true }); + const dCh2 = p.channel(other.dGuild, {}); + const fCh2 = p.channel(other.fGuild, {}); + const made = await requestLink({ guildKey: other.dGuild.id, channelId: dCh2.id, actor: 'A' }); + const second = await acceptLink({ guildKey: otherF, channelId: fCh2.id, code: made.code, actor: 'B' }); + + fakes.transport.sendFailures.push(apiError(403)); + await relay(fromD(one.dCh, { content: 'breaks the first link' }), D); + assert.equal((await store.getLink(one.link.id)).status, 'failed'); + assert.equal((await store.getLink(second.id)).status, 'active'); + await relay(fromD(dCh2, { content: 'still fine' }), other.dGuild.id); + assert.equal(fakes.transport.sends.at(-1).payload.content, 'still fine'); + assert.ok(!say(dCh2).some((s) => /Bridge stopped/.test(s)), 'the other server was not told anything'); +}); + +test('a deleted bridged channel stops its link', async () => { + const { dCh, link } = await bridged(); + await handleChannelDelete(dCh, {}, D); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'channel-deleted']); +}); + +// --- pause, resume, unlink, leaving ----------------------------------------------------------------------------- + +test('a server that paused a link is the only one that can resume it; a stopped link can be resumed by either', async () => { + const { dCh, fCh, link } = await bridged(); + await pauseLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.ok( + say(fCh).some((s) => /Bridge paused.*Discord Home/s.test(s)), + 'the other side is told who paused it' + ); + await refuses(resumeLink({ guildKey: F, id: link.id, actor: 'B' }), /only they can resume/, 403); + await refuses(pauseLink({ guildKey: D, id: link.id, actor: 'A' }), /not running/, 409); + const view = linkView(await store.getLink(link.id), F); + assert.deepEqual([view.status, view.pausedByMe, view.pausedByThem], ['paused', false, true]); + await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.equal((await store.getLink(link.id)).status, 'active'); + assert.match(say(dCh).at(-1), /Bridge on/); + + await store.setLinkStatus(link.id, { status: 'failed', reason: 'permissions' }); + dropBridgeCache(); + await resumeLink({ guildKey: F, id: link.id, actor: 'B' }); + assert.equal((await store.getLink(link.id)).status, 'active'); + await refuses(resumeLink({ guildKey: F, id: link.id, actor: 'B' }), /already running/, 409); + assert.ok(fCh); +}); + +test('a stopped link is only resumed when its channels can be bridged again', async () => { + const { dCh, fCh, link } = await bridged(); + await store.setLinkStatus(link.id, { status: 'failed', reason: 'permissions' }); + dCh.permissionsFor = () => ({ has: (perm) => perm !== 'ManageWebhooks' }); + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /needs ManageWebhooks/); + dCh.permissionsFor = () => ({ has: () => true }); + fCh.nsfw = true; + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /age-restricted/); + fCh.nsfw = false; + await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.equal((await store.getLink(link.id)).status, 'active'); +}); + +test('either side can unlink: both webhooks go, the rows go, both channels are told', async () => { + const { dCh, fCh, link } = await bridged(); + const m = fromD(dCh, { content: 'hi' }); + await relay(m, D); + await unlinkLink({ guildKey: F, id: link.id, actor: 'B' }); + assert.equal(await store.getLink(link.id), null); + assert.equal(await store.linkForChannel(D, dCh.id), null); + assert.deepEqual(fakes.transport.deleted.map((d) => d.hook.id).sort(), [link.hookAId, link.hookBId].sort()); + assert.match(say(dCh).at(-1), /Bridge ended.*Fluxer Home/s); + assert.match(say(fCh).at(-1), /Bridge ended/); + assert.equal(await store.mappingCount(link.id), 0, 'the message map of the link went with it'); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:unlink')); + assert.ok((await listAudit(F)).some((e) => e.action === 'bridge:unlink')); + await relay(fromD(dCh, { content: 'after' }), D); + assert.equal(fakes.transport.sends.length, 1, 'nothing crosses any more'); + // the channels can be bridged again + const again = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + assert.ok(await acceptLink({ guildKey: F, channelId: fCh.id, code: again.code, actor: 'B' })); +}); + +test('a server never sees or changes a link it is not in', async () => { + const { link } = await bridged(); + const { dGuild: stranger } = p.extra('94'); + for (const act of [ + () => pauseLink({ guildKey: stranger.id, id: link.id, actor: 'X' }), + () => resumeLink({ guildKey: stranger.id, id: link.id, actor: 'X' }), + () => unlinkLink({ guildKey: stranger.id, id: link.id, actor: 'X' }), + () => setSideOptions({ guildKey: stranger.id, id: link.id, options: { bots: true }, actor: 'X' }), + ]) { + await refuses(act(), /No such bridged channel/, 404); + } + assert.equal((await store.getLink(link.id)).status, 'active'); + assert.deepEqual((await store.getLink(link.id)).optionsA, { + bots: false, + joinMessages: false, + uploadFiles: true, + }); +}); + +test('when Sylo is removed from a server, the server on the other end loses its webhook and is told', async () => { + const { dCh, fCh, link } = await bridged(); + await relay(fromD(dCh, { content: 'hi' }), D); + await purgeGuild(F); + assert.equal(await store.getLink(link.id), null); + assert.deepEqual( + fakes.transport.deleted.map((d) => [d.guildKey, d.hook.id]), + [[D, link.hookAId]] + ); + assert.match(say(dCh).at(-1), /the other server removed Sylo/); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:unlink')); + assert.ok(fCh); +}); + +test('the dashboard view never carries a webhook token', async () => { + const { link } = await bridged(); + const text = JSON.stringify(linkView(await store.getLink(link.id), D)); + assert.ok(!text.includes(link.hookAToken) && !text.includes(link.hookBToken)); + assert.ok(!/hook/i.test(text)); +}); + +test('a bulk delete finds the channel from the messages when the platform did not cache it (Fluxer)', async () => { + const { dCh, fCh } = await bridged(); + const m = fromF(fCh, { content: 'gone soon' }); + await relay(m, F); + await handleBulkDelete( + { messages: new Map([[m.id, { id: m.id, channelId: fCh.id }]]), channel: null }, + {}, + F + ); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.removes.map((r) => r.guildKey), + [D] + ); + assert.ok(dCh); +}); diff --git a/test/bridge.postgres.test.js b/test/bridge.postgres.test.js new file mode 100644 index 00000000..36c894bd --- /dev/null +++ b/test/bridge.postgres.test.js @@ -0,0 +1,125 @@ +// Proves the Bridge storage against a real Postgres connection: the tables come from the bootstrap, ids and +// BIGINT times come back as numbers, a code is used up once, a channel is in one link, the message map is +// found from both sides and swept, and the guild purge removes the links of either side. +import './helpers/isolateSqlite.js'; +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { closePostgres } from '../src/db/driver.js'; + +const url = process.env.DATABASE_URL; + +test( + 'bridge against a real Postgres connection', + { skip: !url && 'DATABASE_URL not set (sqlite-only run)' }, + async (t) => { + const store = await import('../src/db/bridge.js'); + const purge = await import('../src/db/purge.js'); + const retention = await import('../src/db/retention.js'); + t.after(async () => { + await closePostgres(); + }); + + const run = Date.now(); + const G1 = `pgtest-bridge-${run}-a`; + const G2 = `fluxer:pgtest-bridge-${run}-b`; + const link = (over = {}) => ({ + guildA: G1, + channelA: `ca-${run}`, + guildB: G2, + channelB: `cb-${run}`, + hookA: { id: `ha-${run}`, token: 'ta' }, + hookB: { id: `hb-${run}`, token: 'tb' }, + requestedBy: 'Alice', + acceptedBy: 'Bob', + ...over, + }); + + await t.test( + 'a link is created, found from the channel and the webhook, and a channel is in one link only', + async () => { + const id = await store.createLink(link()); + assert.equal(typeof id, 'number'); + const found = await store.linkForChannel(G2, `cb-${run}`); + assert.equal(found.side, 'b'); + assert.equal(typeof found.link.createdAt, 'number'); + assert.equal((await store.linkForHook(`ha-${run}`)).id, id); + assert.equal( + await store.createLink(link({ channelB: `other-${run}` })), + null, + 'the same a-channel again' + ); + assert.equal( + await store.createLink(link({ channelA: `other-${run}` })), + null, + 'the same b-channel again' + ); + assert.equal(await store.linkCount(G1), 1); + await store.setLinkStatus(id, { status: 'paused', pausedSide: 'a' }); + assert.deepEqual( + [(await store.getLink(id)).status, (await store.getLink(id)).pausedSide], + ['paused', 'a'] + ); + await store.setLinkOptions(id, 'a', { bots: true }); + assert.deepEqual((await store.getLink(id)).optionsA, { bots: true }); + await store.setLinkHook(id, 'b', { id: `hb2-${run}`, token: 'tb2' }); + assert.equal((await store.getLink(id)).hookBId, `hb2-${run}`); + await store.deleteLink(id); + assert.equal(await store.getLink(id), null); + } + ); + + await t.test('a code is stored as a hash, expires, and is used up once', async () => { + const made = await store.createCode({ + guildId: G1, + channelId: 'c1', + options: { bots: true }, + requestedBy: 'Alice', + }); + assert.equal(await store.pendingCodeCount(G1), 1); + assert.equal(typeof (await store.pendingCodes(G1))[0].expiresAt, 'number'); + assert.equal((await store.peekCode(made.code)).guildId, G1); + const [a, b] = await Promise.all([store.claimCode(made.code), store.claimCode(made.code)]); + assert.equal([a, b].filter(Boolean).length, 1, 'exactly one claim wins'); + const old = await store.createCode({ guildId: G1, channelId: 'c2', ttlMs: -1000 }); + assert.equal(await store.claimCode(old.code), null); + assert.ok((await store.sweepCodes()) >= 1); + assert.equal(await store.cancelCode(G2, 1), false); + }); + + await t.test('the message map is found from both sides, replaced, and swept after 30 days', async () => { + const id = await store.createLink(link()); + await store.saveMapping(id, 'a', { + sourceId: 'm1', + copyId: 'c1', + hookId: 'h', + hash: 'x', + extra: { head: '> q' }, + }); + const fromCopy = await store.mappingOf(id, 'b', 'c1'); + assert.deepEqual([fromCopy.counterpartId, fromCopy.isOriginal], ['m1', false]); + assert.deepEqual((await store.mappingOf(id, 'a', 'm1')).extra, { head: '> q' }); + await store.saveMapping(id, 'a', { sourceId: 'm1', copyId: 'c2' }); + assert.equal((await store.mappingOf(id, 'a', 'm1')).counterpartId, 'c2'); + await store.saveMapping(id, 'b', { sourceId: 'old', copyId: 'old-copy' }); + const { prepare } = await import('../src/db/driver.js'); + await prepare('UPDATE bridge_messages SET created_at = ? WHERE a_message_id = ?').run( + Date.now() - 40 * 86_400_000, + 'old-copy' + ); + await retention.sweepRetention(); + assert.equal(await store.mappingOf(id, 'a', 'old-copy'), null); + assert.ok(await store.mappingOf(id, 'a', 'm1')); + assert.equal(await store.mappingCount(id), 1); + }); + + await t.test( + 'purging a server removes the links it is in, on either side, and its requests', + async () => { + await store.createCode({ guildId: G2, channelId: 'c9' }); + await purge.purgeGuild(G2); + assert.equal((await store.linksOfGuild(G1)).length, 0); + assert.equal(await store.pendingCodeCount(G2), 0); + } + ); + } +); diff --git a/test/bridgeHealth.test.js b/test/bridgeHealth.test.js new file mode 100644 index 00000000..5c324468 --- /dev/null +++ b/test/bridgeHealth.test.js @@ -0,0 +1,73 @@ +// The Health page for the bridge: the permissions it needs in a bridged channel, and a stopped link shown +// as a problem with its reason. +import './helpers/tmpDb.js'; +import test, { afterEach, beforeEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { setGuildModule } from '../src/db/modules.js'; +import { purgeGuild } from '../src/db/purge.js'; +import { buildHealthReport } from '../src/web/lib/permissionHealth.js'; +import { keyedGuild } from '../src/platform/platformOf.js'; +import { acceptLink, failLink, requestLink, useBridgeParts, dropBridgeCache } from '../src/modules/bridge.js'; +import { D, F, installPlatforms, removePlatforms, useFakes } from './helpers/fakeBridge.js'; + +let p; +beforeEach(async () => { + p = installPlatforms(); + useFakes(); + dropBridgeCache(); + for (const key of [D, F]) await purgeGuild(key); + useFakes(); + await setGuildModule(D, 'bridge', { enabled: true }); + await setGuildModule(F, 'bridge', { enabled: true }); +}); +afterEach(() => { + useBridgeParts(); + removePlatforms(); +}); + +const forBridge = (guild) => + buildHealthReport(guild, { modules: [{ id: 'bridge', enabled: true, config: {} }] }); + +test('the bridge needs Manage Webhooks in a bridged channel, and Health says so', async () => { + const dCh = p.channel(p.dGuild, { name: 'discord-chat' }); + const fCh = p.channel(p.fGuild, { name: 'fluxer-chat' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }); + assert.deepEqual((await forBridge(p.dGuild)).checks, [], 'nothing to report while Sylo has what it needs'); + + dCh.permissionsFor = () => ({ has: (perm) => perm !== 'ManageWebhooks' }); + const report = await forBridge(p.dGuild); + assert.equal(report.checks.length, 1); + const [check] = report.checks; + assert.equal(check.module, 'bridge'); + assert.equal(check.severity, 'error'); + assert.deepEqual(check.permissions, ['ManageWebhooks']); + assert.match(check.message, /A bridged channel \(#discord-chat\): Sylo is missing Manage Webhooks/); +}); + +test('a link Sylo stopped is a problem with the reason and what to do, on both servers', async () => { + const dCh = p.channel(p.dGuild, { name: 'discord-chat' }); + const fCh = p.channel(p.fGuild, { name: 'fluxer-chat' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + const link = await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }); + await failLink(link.id, 'permissions'); + // The dashboard hands the health check the guild with its key as `.id` (keyedGuild). + for (const guild of [keyedGuild(p.dGuild), keyedGuild(p.fGuild)]) { + const { checks } = await forBridge(guild); + const stopped = checks.find((c) => c.code === 'bridge-stopped'); + assert.ok(stopped, 'a stopped bridge is listed'); + assert.equal(stopped.severity, 'error'); + assert.match( + stopped.message, + /The bridge of #(discord|fluxer)-chat stopped: Sylo is missing a permission/ + ); + assert.match(stopped.fix, /press Resume/); + } +}); + +test('a server with the module off is not checked', async () => { + const report = await buildHealthReport(p.dGuild, { + modules: [{ id: 'bridge', enabled: false, config: {} }], + }); + assert.deepEqual(report.checks, []); +}); diff --git a/test/bridgeQueue.test.js b/test/bridgeQueue.test.js new file mode 100644 index 00000000..75e46b6b --- /dev/null +++ b/test/bridgeQueue.test.js @@ -0,0 +1,208 @@ +// The bridge's send queue: one lane per webhook, in order, spaced, with back-off on a rate limit, a cap on +// what waits and a limit on how long a job may wait; and how a failed webhook call is classified. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { BridgeQueueError, classifyError, createRelayQueue } from '../src/modules/lib/bridgeQueue.js'; + +const err = (props) => Object.assign(new Error('x'), props); + +test('a failed webhook call is classified by what it means for the link', () => { + assert.deepEqual(classifyError(err({ status: 429, retryAfter: 2 })), { kind: 'rate', retryAfterMs: 2000 }); + assert.deepEqual(classifyError(err({ statusCode: 429, retry_after: 0.5 })), { + kind: 'rate', + retryAfterMs: 500, + }); + assert.deepEqual( + classifyError(err({ status: 429, retryAfter: 3500 })), + { kind: 'rate', retryAfterMs: 3500 }, + 'milliseconds' + ); + assert.deepEqual( + classifyError(err({ status: 429 })), + { kind: 'rate', retryAfterMs: 2000 }, + 'no hint: two seconds' + ); + assert.equal(classifyError(err({ status: 429, retryAfter: 9999 })).retryAfterMs, 9999); + assert.equal( + classifyError(err({ status: 429, retryAfter: 600000 })).retryAfterMs, + 60000, + 'never longer than a minute' + ); + assert.equal( + classifyError(err({ status: 429, retryAfter: 0.001 })).retryAfterMs, + 250, + 'never shorter than 250 ms' + ); + assert.deepEqual( + classifyError(err({ cause: err({ statusCode: 429, retry_after: 1 }) })), + { kind: 'rate', retryAfterMs: 1000 }, + 'the SDKs wrap the HTTP error in a cause' + ); + + assert.equal(classifyError(err({ code: 10015, status: 404 })).kind, 'hook-gone', 'Unknown Webhook'); + assert.equal(classifyError(err({ code: 'UNKNOWN_WEBHOOK' })).kind, 'hook-gone'); + assert.equal(classifyError(err({ status: 401 })).kind, 'hook-gone', 'a bad webhook token'); + assert.equal(classifyError(err({ status: 404 }), 'send').kind, 'hook-gone'); + assert.equal(classifyError(err({ status: 404 }), 'edit').kind, 'message-gone'); + assert.equal(classifyError(err({ status: 404 }), 'delete').kind, 'message-gone'); + assert.equal(classifyError(err({ code: 10008, status: 404 }), 'edit').kind, 'message-gone'); + assert.equal(classifyError(err({ status: 403 })).kind, 'forbidden'); + assert.equal(classifyError(err({ code: 50013 })).kind, 'forbidden'); + assert.equal(classifyError(err({ status: 400 })).kind, 'bad-request'); + assert.equal(classifyError(err({ status: 413 })).kind, 'bad-request'); + assert.equal(classifyError(err({ status: 500 })).kind, 'transient'); + assert.equal(classifyError(new Error('ECONNRESET')).kind, 'transient'); + assert.equal(classifyError('a string').kind, 'transient'); + assert.equal(classifyError(null).kind, 'transient'); +}); + +function harness(opts = {}) { + const sleeps = []; + let clock = 1_000_000; + const queue = createRelayQueue({ + minGapMs: 100, + sleep: async (ms) => { + sleeps.push(ms); + clock += ms; + }, + now: () => clock, + ...opts, + }); + return { queue, sleeps, tick: (ms) => (clock += ms) }; +} + +test('jobs of one webhook run one at a time, in the order they came, spaced apart', async () => { + const { queue, sleeps } = harness(); + const order = []; + let running = 0; + let peak = 0; + const job = (name) => async () => { + running += 1; + peak = Math.max(peak, running); + await new Promise((r) => setImmediate(r)); + order.push(name); + running -= 1; + return name; + }; + const results = await Promise.all(['a', 'b', 'c'].map((n) => queue.enqueue('hook1', job(n)))); + assert.deepEqual(results, ['a', 'b', 'c']); + assert.deepEqual(order, ['a', 'b', 'c']); + assert.equal(peak, 1); + assert.deepEqual(sleeps, [100, 100], 'a gap after each job that has another behind it'); + assert.equal(queue.lanes(), 0, 'an idle lane is forgotten'); +}); + +test('webhooks do not wait for each other', async () => { + const { queue } = harness(); + const gate = {}; + gate.open = new Promise((r) => (gate.release = r)); + const slow = queue.enqueue('slow', () => gate.open.then(() => 'slow done')); + const fast = await queue.enqueue('fast', async () => 'fast done'); + assert.equal(fast, 'fast done', 'the second webhook finished while the first was stuck'); + gate.release(); + assert.equal(await slow, 'slow done'); +}); + +test('a rate limit is waited out as long as the platform said, then the same job runs again', async () => { + const { queue, sleeps } = harness(); + let calls = 0; + const out = await queue.enqueue('h', async () => { + calls += 1; + if (calls === 1) throw err({ status: 429, retryAfter: 1.25 }); + return 'ok'; + }); + assert.equal(out, 'ok'); + assert.equal(calls, 2); + assert.deepEqual(sleeps, [1250]); +}); + +test('a transient failure backs off 1 s, 2 s, 4 s and gives up after four tries', async () => { + const { queue, sleeps } = harness(); + let calls = 0; + await assert.rejects( + queue.enqueue('h', async () => { + calls += 1; + throw err({ status: 503 }); + }), + (e) => e.failure.kind === 'transient' && e.status === 503 + ); + assert.equal(calls, 4); + assert.deepEqual(sleeps, [1000, 2000, 4000]); + const again = harness(); + let n = 0; + assert.equal( + await again.queue.enqueue('h', async () => (++n < 3 ? Promise.reject(err({ status: 500 })) : 'fine')), + 'fine' + ); + assert.deepEqual(again.sleeps, [1000, 2000]); +}); + +test('a refusal is not retried: forbidden, webhook gone and bad request fail at once, with their kind', async () => { + for (const [props, kind] of [ + [{ status: 403 }, 'forbidden'], + [{ status: 404, code: 10015 }, 'hook-gone'], + [{ status: 400 }, 'bad-request'], + ]) { + const { queue, sleeps } = harness(); + let calls = 0; + await assert.rejects( + queue.enqueue('h', async () => { + calls += 1; + throw err(props); + }), + (e) => e.failure.kind === kind + ); + assert.equal(calls, 1, kind); + assert.deepEqual(sleeps, []); + } +}); + +test('a failed job does not stop the ones behind it', async () => { + const { queue } = harness(); + const bad = queue.enqueue('h', async () => { + throw err({ status: 403 }); + }); + const good = queue.enqueue('h', async () => 'next'); + await assert.rejects(bad); + assert.equal(await good, 'next'); +}); + +test('a full lane drops the newest job and counts it; the jobs already in keep their place', async () => { + const { queue } = harness({ maxQueued: 2 }); + const gate = {}; + gate.open = new Promise((r) => (gate.release = r)); + const first = queue.enqueue('h', () => gate.open.then(() => 1)); + const second = queue.enqueue('h', async () => 2); + const third = queue.enqueue('h', async () => 3); + await assert.rejects(third, (e) => e instanceof BridgeQueueError && e.reason === 'overflow'); + assert.deepEqual(queue.stats('h'), { queued: 2, dropped: 1 }); + assert.deepEqual(queue.stats('other'), { queued: 0, dropped: 0 }, 'another webhook has its own lane'); + gate.release(); + assert.deepEqual(await Promise.all([first, second]), [1, 2]); +}); + +test('a job that waited too long is dropped instead of arriving late', async () => { + const { queue, tick } = harness({ maxAgeMs: 60_000 }); + const gate = {}; + gate.open = new Promise((r) => (gate.release = r)); + const first = queue.enqueue('h', () => gate.open.then(() => 'first')); + const stale = queue.enqueue('h', async () => 'stale'); + const expired = assert.rejects(stale, (e) => e instanceof BridgeQueueError && e.reason === 'expired'); + tick(61_000); + gate.release(); + assert.equal(await first, 'first'); + await expired; +}); + +test('clearing a lane drops what waits (a paused or unlinked bridge) but lets the running job finish', async () => { + const { queue } = harness(); + const gate = {}; + gate.open = new Promise((r) => (gate.release = r)); + const running = queue.enqueue('h', () => gate.open.then(() => 'ran')); + const waiting = queue.enqueue('h', async () => 'never'); + assert.equal(queue.clear('h'), 1); + await assert.rejects(waiting, (e) => e.reason === 'cleared'); + gate.release(); + assert.equal(await running, 'ran'); + assert.equal(queue.clear('nothing'), 0); +}); diff --git a/test/bridgeText.test.js b/test/bridgeText.test.js new file mode 100644 index 00000000..c029f1fa --- /dev/null +++ b/test/bridgeText.test.js @@ -0,0 +1,219 @@ +// The pure rules of the bridge: how text, names, replies, files, embeds and options are made safe and +// fit for the other platform. No database and no platform needed. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { + EMBED_LIMIT, + FILE_BYTES_LIMIT, + FILE_COUNT_LIMIT, + MESSAGE_LIMIT, + TOTAL_BYTES_LIMIT, + ZWSP, + clip, + fileLinkLines, + hashContent, + inertAt, + joinParts, + mapEmoji, + neutraliseMentions, + normaliseOptions, + planAttachments, + plainEmbed, + plainEmbeds, + plainTimestamps, + pollText, + replyHead, + senderName, + stickerLines, +} from '../src/modules/lib/bridgeText.js'; + +const plain = (s) => s.split(ZWSP).join(''); + +test('every kind of mention is replaced by a name, and @everyone and @here are broken', () => { + const names = { user: () => 'Bob', role: () => 'mods', channel: () => 'rules' }; + assert.equal( + plain(neutraliseMentions('<@111111> <@!111111> <@&222222> <#333333> @everyone @here @EVERYONE', names)), + '@Bob @Bob @mods #rules @everyone @here @EVERYONE' + ); + assert.equal(plain(neutraliseMentions('<@111111> <@&222222> <#333333>')), '@user @role #channel'); + assert.equal(neutraliseMentions(' and '), '/ban and /mod warn'); + assert.equal( + neutraliseMentions('plain text, an email a@b.c and 5 > 3'), + 'plain text, an email a@b.c and 5 > 3' + ); +}); + +test('a name cannot smuggle a mention: control characters go and an @ is broken', () => { + const out = neutraliseMentions('<@111111>', { user: () => '@everyone‮\u0000x' }); + assert.ok(!/@everyone/.test(out), out); + assert.equal(plain(inertAt('@a@b')), '@a@b'); +}); + +test('custom emoji map by name to the other server, else become :name:; Unicode is untouched', () => { + const find = (name) => + name === 'wave' + ? { id: '999', name: 'wave', animated: false } + : name === 'dance' + ? { id: '888', name: 'dance', animated: true } + : undefined; + assert.equal( + mapEmoji('a <:wave:12345> b c <:gone:11111> 🎉', find), + 'a <:wave:999> b c :gone: 🎉' + ); + assert.equal(mapEmoji('<:wave:12345>'), ':wave:', 'no lookup at all: names only'); +}); + +test('a Discord timestamp is written out in UTC', () => { + assert.equal( + plainTimestamps('at or '), + 'at 14 Nov 2023, 22:13 UTC or 1 Jan 1970, 00:00 UTC' + ); +}); + +test('the sender name carries the platform, fits 80 characters, is never empty, and cannot ping', () => { + assert.equal(senderName('Alice', 'discord'), 'Alice [Discord]'); + assert.equal(senderName('Bob', 'fluxer'), 'Bob [Fluxer]'); + assert.equal(plain(senderName('@everyone', 'fluxer')), '@everyone [Fluxer]'); + assert.equal(senderName('', 'fluxer'), 'Someone [Fluxer]'); + assert.equal(senderName('‮\u0000', 'fluxer'), 'Someone [Fluxer]'); + const long = senderName('x'.repeat(200), 'discord'); + assert.equal(long.length, 80); + assert.ok(long.endsWith('… [Discord]')); + // Discord refuses a webhook name with "discord" or "clyde" in it + const forDiscord = senderName('ClydeBot and Discordian', 'fluxer', { forDiscord: true }); + assert.ok(!/clyde|discord/i.test(forDiscord), forDiscord); + assert.ok(/Clyde/.test(forDiscord.split(ZWSP).join('')), 'the name is still readable'); + assert.ok(senderName('Zoë 🧑‍🚀', 'discord').includes('🧑‍🚀'), 'a joiner in an emoji sequence stays'); +}); + +test('a reply head is a short one-line quote with a link when the copy is known', () => { + assert.equal( + replyHead({ name: 'Bob', snippet: 'hello\nthere world', url: 'https://x/y' }), + '> ↪ [**Bob**](): hello there world' + ); + assert.equal(replyHead({ name: 'Bob', snippet: '' }), '> ↪ **Bob**'); + assert.equal(replyHead({ name: '', snippet: '> nested quote' }), '> ↪ **someone**: nested quote'); + const head = replyHead({ name: 'x'.repeat(100), snippet: 'y'.repeat(500) }); + assert.ok(head.length < 190); + assert.ok(head.endsWith('…')); +}); + +test('stickers become a picture link, and a poll becomes plain text', () => { + assert.deepEqual(stickerLines([{ name: 'wave', url: 'https://cdn/1.png' }, { name: 'nolink' }]), [ + '*sticker: wave* https://cdn/1.png', + '*sticker: nolink*', + ]); + assert.deepEqual(stickerLines(null), []); + assert.equal( + pollText({ question: 'Pizza?', answers: ['Yes', { text: 'No', emoji: '👎' }, { text: '' }] }), + '📊 **Poll:** Pizza?\n• Yes\n• 👎 No\n*Vote on the original server: polls do not cross the bridge.*' + ); + assert.equal(pollText(null), ''); +}); + +test('files are uploaded only within the per-file, total and count limits, the rest are links', () => { + const MB = 1024 * 1024; + const att = (name, size) => ({ name, url: `https://cdn/${name}`, size }); + const plan = planAttachments([ + att('a', 1 * MB), + att('b', 9 * MB), + att('c', 7 * MB), + att('d', 2 * MB), + att('e', 0), + ]); + assert.deepEqual( + plan.upload.map((a) => a.name), + ['a', 'c'], + 'a (1) + c (7) = 8 MB; d would pass the total' + ); + assert.deepEqual( + plan.links.map((a) => a.name), + ['b', 'd', 'e'] + ); + assert.deepEqual( + planAttachments([att('a', 5)], { upload: false }).links.map((a) => a.name), + ['a'] + ); + const many = planAttachments(Array.from({ length: 14 }, (_, i) => att(`f${i}`, 10))); + assert.equal(many.upload.length, FILE_COUNT_LIMIT); + assert.equal(many.links.length, 4); + assert.equal(FILE_BYTES_LIMIT, 8 * MB); + assert.equal(TOTAL_BYTES_LIMIT, 8 * MB); + assert.deepEqual(fileLinkLines([att('a b.png', 3)]), ['📎 a b.png: https://cdn/a b.png']); +}); + +test('only rich embeds cross, as plain API objects, with their text made harmless and their limits kept', () => { + assert.equal(plainEmbed({ type: 'link', url: 'https://x', title: 'preview' }), null); + assert.equal(plainEmbed({ type: 'image', url: 'https://x/a.png' }), null); + assert.equal(plainEmbed({ type: 'rich' }), null, 'an empty embed is nothing'); + const up = (t) => t.toUpperCase(); + const embed = plainEmbed( + { + type: 'rich', + title: 'Hi', + description: 'there', + url: 'javascript:alert(1)', + color: 255, + timestamp: '2030-01-01T00:00:00.000Z', + author: { name: 'me', iconURL: 'https://x/a.png' }, + footer: { text: 'f', iconUrl: 'https://x/f.png' }, + image: { url: 'https://x/i.png' }, + thumbnail: { url: 'ftp://nope' }, + fields: [ + { name: 'n', value: 'v' }, + { name: '', value: 'x' }, + ], + }, + up + ); + assert.deepEqual(embed, { + title: 'HI', + description: 'THERE', + color: 255, + timestamp: '2030-01-01T00:00:00.000Z', + footer: { text: 'F', icon_url: 'https://x/f.png' }, + image: { url: 'https://x/i.png' }, + author: { name: 'ME', icon_url: 'https://x/a.png' }, + fields: [{ name: 'N', value: 'V', inline: false }], + }); + assert.equal( + plainEmbed({ toJSON: () => ({ type: 'rich', title: 't' }) }).title, + 't', + 'a discord.js embed class' + ); + assert.equal( + plainEmbeds(Array.from({ length: 14 }, () => ({ type: 'rich', title: 't' }))).length, + EMBED_LIMIT + ); + assert.deepEqual(plainEmbeds(null), []); +}); + +test('text is cut to the limit without half an emoji, and parts join without blank lines', () => { + assert.equal(clip('abcdef', 4), 'abc…'); + assert.equal(clip('ab😀cd', 4), 'ab…', 'a surrogate pair is not cut in two'); + assert.equal(clip('short', 10), 'short'); + assert.equal(joinParts(['a', '', null, ' b ', undefined, 'c']), 'a\nb\nc'); + assert.equal(joinParts(['x'.repeat(5000)]).length, MESSAGE_LIMIT); +}); + +test('options are always booleans, bots and joins are off by default, files are copied by default', () => { + assert.deepEqual(normaliseOptions(undefined), { bots: false, joinMessages: false, uploadFiles: true }); + assert.deepEqual(normaliseOptions({}), { bots: false, joinMessages: false, uploadFiles: true }); + assert.deepEqual(normaliseOptions({ bots: 'yes', joinMessages: 1, uploadFiles: 'no' }), { + bots: false, + joinMessages: false, + uploadFiles: false, + }); + assert.deepEqual(normaliseOptions({ bots: true, joinMessages: true, uploadFiles: true, extra: 1 }), { + bots: true, + joinMessages: true, + uploadFiles: true, + }); + assert.deepEqual(normaliseOptions('junk'), { bots: false, joinMessages: false, uploadFiles: true }); +}); + +test('the content hash tells a changed text from an unchanged one', () => { + assert.equal(hashContent('a'), hashContent('a')); + assert.notEqual(hashContent('a'), hashContent('b')); + assert.equal(hashContent(null), hashContent('')); +}); diff --git a/test/bridgeTransport.test.js b/test/bridgeTransport.test.js new file mode 100644 index 00000000..0b173bd8 --- /dev/null +++ b/test/bridgeTransport.test.js @@ -0,0 +1,88 @@ +// The webhook transport (src/platform/webhooks.js): what both platforms are sent. The one rule that matters +// most is here: every body carries allowedMentions none, whatever the text says. +import './helpers/tmpDb.js'; +import test, { afterEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { runtime } from '../src/runtime.js'; +import { NO_MENTIONS, createWebhookTransport, webhookBody } from '../src/platform/webhooks.js'; + +afterEach(() => { + runtime.clients.discord = null; + runtime.clients.fluxer = null; +}); + +const FILE = { name: 'a.png', data: Buffer.from('x') }; + +test('a body never allows a mention, and has the platform’s own names for the avatar and files', () => { + const payload = { + content: '@everyone <@&123> hi', + username: 'Alice [Fluxer]', + avatarUrl: 'https://cdn/a.png', + embeds: [{ title: 't' }], + files: [FILE], + }; + const d = webhookBody('discord', payload); + assert.deepEqual(d.allowedMentions, { parse: [] }); + assert.deepEqual(NO_MENTIONS, { parse: [] }); + assert.equal(d.avatarURL, 'https://cdn/a.png'); + assert.equal(d.avatarUrl, undefined); + assert.deepEqual(d.files, [{ attachment: FILE.data, name: 'a.png' }]); + assert.equal(d.username, 'Alice [Fluxer]'); + assert.equal(d.content, '@everyone <@&123> hi'); + + const f = webhookBody('fluxer', payload); + assert.deepEqual(f.allowedMentions, { parse: [] }); + assert.equal(f.avatarUrl, 'https://cdn/a.png'); + assert.equal(f.avatarURL, undefined); + assert.deepEqual(f.files, [{ name: 'a.png', data: FILE.data }]); +}); + +test('an edit body has the text and embeds only, still with no mentions', () => { + for (const platform of ['discord', 'fluxer']) { + const body = webhookBody( + platform, + { content: 'new', username: 'x', avatarUrl: 'https://a/b', files: [FILE], embeds: [] }, + { forEdit: true } + ); + assert.deepEqual(body, { allowedMentions: { parse: [] }, content: 'new', embeds: [] }); + } + assert.deepEqual(webhookBody('discord', {}), { allowedMentions: { parse: [] } }); +}); + +test('a webhook is made through the channel of the right platform, and its token is returned', async () => { + const made = []; + const channel = { createWebhook: async (opts) => (made.push(opts), { id: '77', token: 'tok' }) }; + const guild = (client) => ({ + id: '710000000000000001', + client, + channels: { cache: new Map([['720000000000000001', channel]]) }, + }); + runtime.clients.discord = { guilds: { cache: new Map() } }; + runtime.clients.discord.guilds.cache.set('710000000000000001', guild(runtime.clients.discord)); + const transport = createWebhookTransport(); + assert.deepEqual(await transport.createHook('710000000000000001', '720000000000000001', 'Sylo bridge'), { + id: '77', + token: 'tok', + }); + assert.deepEqual(made, [{ name: 'Sylo bridge', reason: 'Sylo bridge' }]); +}); + +test('a server Sylo is not in, a channel without webhooks and a webhook without a token are refused', async () => { + const transport = createWebhookTransport(); + await assert.rejects( + transport.createHook('710000000000000009', '720000000000000001', 'x'), + (e) => e.code === 'NO_GUILD' + ); + runtime.clients.discord = { guilds: { cache: new Map() } }; + const noHooks = { id: '710000000000000001', channels: { cache: new Map([['720000000000000001', {}]]) } }; + runtime.clients.discord.guilds.cache.set(noHooks.id, noHooks); + await assert.rejects( + transport.createHook(noHooks.id, '720000000000000001', 'x'), + (e) => e.code === 'NO_CHANNEL' + ); + noHooks.channels.cache.set('720000000000000002', { createWebhook: async () => ({ id: '1' }) }); + await assert.rejects( + transport.createHook(noHooks.id, '720000000000000002', 'x'), + /did not return a webhook token/ + ); +}); diff --git a/test/helpers/fakeBridge.js b/test/helpers/fakeBridge.js new file mode 100644 index 00000000..14799b8b --- /dev/null +++ b/test/helpers/fakeBridge.js @@ -0,0 +1,173 @@ +// A Discord and a Fluxer side for the bridge tests: two fake clients with one server each, text channels that +// record what Sylo says in them, and a fake webhook transport that records every send, edit and delete (and +// can be told to fail). The real transport (src/platform/webhooks.js) is covered separately. +import { ChannelType } from 'discord.js'; +import { runtime } from '../../src/runtime.js'; +import { createRelayQueue } from '../../src/modules/lib/bridgeQueue.js'; +import { useBridgeParts } from '../../src/modules/bridge.js'; + +export const D_RAW = '710000000000000001'; +export const F_RAW = '710000000000000002'; +export const D = D_RAW; +export const F = `fluxer:${F_RAW}`; +export const ME_D = '999000000000000001'; +export const ME_F = '999000000000000002'; + +let nextId = 80_000_000_000_000_000n; +export const snowflake = () => String((nextId += 1n)); + +function makeChannel(guild, { id, name, nsfw = false, type = ChannelType.GuildText, deny = [] }) { + const channel = { + id, + name, + type, + nsfw, + rawPosition: 0, + guild, + said: [], + permissionsFor: () => ({ has: (perm) => !deny.includes(perm) }), + send: async (payload) => { + channel.said.push(payload); + return { id: snowflake() }; + }, + }; + guild.channels.cache.set(id, channel); + return channel; +} + +function makeGuild(client, { id, name }) { + const guild = { + id, + name, + client, + ownerId: 'owner', + channels: { cache: new Map() }, + emojis: { cache: new Map() }, + roles: { cache: new Map() }, + members: { me: { id: client.user.id }, cache: new Map() }, + }; + client.guilds.cache.set(id, guild); + return guild; +} + +/** Install the two fake clients; returns both servers and a way to add channels. */ +export function installPlatforms() { + const discord = { user: { id: ME_D }, guilds: { cache: new Map() }, isReady: () => true }; + const fluxer = { user: { id: ME_F }, guilds: { cache: new Map() }, isReady: () => true }; + runtime.clients.discord = discord; + runtime.clients.fluxer = fluxer; + const dGuild = makeGuild(discord, { id: D_RAW, name: 'Discord Home' }); + const fGuild = makeGuild(fluxer, { id: F_RAW, name: 'Fluxer Home' }); + return { + discord, + fluxer, + dGuild, + fGuild, + channel: (guild, opts) => makeChannel(guild, { id: snowflake(), name: 'chat', ...opts }), + /** Another pair of servers (for isolation tests). */ + extra: (suffix) => ({ + dGuild: makeGuild(discord, { id: `71000000000000${suffix}1`, name: `Discord ${suffix}` }), + fGuild: makeGuild(fluxer, { id: `71000000000000${suffix}2`, name: `Fluxer ${suffix}` }), + }), + }; +} + +export function removePlatforms() { + runtime.clients.discord = null; + runtime.clients.fluxer = null; +} + +/** An HTTP-ish error as the SDKs throw it. */ +export const apiError = (status, { code, retryAfter } = {}) => + Object.assign(new Error(`HTTP ${status}`), { + status, + ...(code && { code }), + ...(retryAfter && { retryAfter }), + }); + +/** A webhook transport that records what the bridge does, and fails on request. */ +export function fakeTransport() { + const t = { + hooks: [], + sends: [], + edits: [], + removes: [], + deleted: [], + sendFailures: [], // errors thrown by the next sends, in order + hookFailures: [], // errors thrown by the next createHook calls + attempts: 0, + async createHook(guildKey, channelId, name) { + const next = t.hookFailures.shift(); + if (next) throw next; + const hook = { id: snowflake(), token: `tok-${t.hooks.length + 1}` }; + t.hooks.push({ guildKey, channelId, name, ...hook }); + return hook; + }, + async send(guildKey, hook, payload) { + t.attempts += 1; + const next = t.sendFailures.shift(); + if (next) throw next; + const id = snowflake(); + t.sends.push({ guildKey, hook, payload, id }); + return id; + }, + async edit(guildKey, hook, messageId, payload) { + t.edits.push({ guildKey, hook, messageId, payload }); + }, + async remove(guildKey, hook, messageId) { + t.removes.push({ guildKey, hook, messageId }); + }, + async deleteHook(guildKey, hook) { + t.deleted.push({ guildKey, hook }); + }, + }; + return t; +} + +/** Use a fake transport, an instant queue and a downloader that serves `files` (url -> bytes). */ +export function useFakes({ files = {}, queue } = {}) { + const transport = fakeTransport(); + const sleeps = []; + const q = + queue ?? + createRelayQueue({ + minGapMs: 0, + sleep: async (ms) => { + if (ms > 0) sleeps.push(ms); // the spacing between sends is 0 here + }, + }); + const downloads = []; + useBridgeParts({ + transport, + queue: q, + download: async (url, maxBytes) => { + downloads.push({ url, maxBytes }); + const body = files[url]; + if (!body) return { ok: false, status: 404, body: Buffer.alloc(0) }; + return { ok: true, status: 200, body: Buffer.isBuffer(body) ? body : Buffer.from(body) }; + }, + }); + return { transport, sleeps, downloads, queue: q }; +} + +/** A chat message as the SDKs deliver it, with what the bridge reads. */ +export function message({ channelId, content = '', author = {}, ...rest }) { + const platformClient = rest.client; + return { + id: snowflake(), + channelId, + content, + type: 0, + system: false, + webhookId: null, + author: { id: snowflake(), username: 'alice', bot: false, ...author }, + member: { displayName: 'Alice' }, + attachments: new Map(), + embeds: [], + stickers: new Map(), + mentions: { users: new Map(), members: new Map(), roles: new Map() }, + reference: null, + client: platformClient, + ...rest, + }; +} diff --git a/test/mobileInfo.test.js b/test/mobileInfo.test.js index efba5685..d2ea2da9 100644 --- a/test/mobileInfo.test.js +++ b/test/mobileInfo.test.js @@ -51,6 +51,10 @@ test('GET /api/mobile/info describes a server with login on, without a cookie or assert.ok(info.features.includes(f), f); } assert.ok(info.features.includes('message-move'), 'a posted message moves when its channel changes'); + assert.ok( + info.features.includes('bridge'), + 'the Bridge module: GET /api/v2/guilds/:id/modules/bridge/config' + ); assert.ok(info.features.every((f) => typeof f === 'string')); }); diff --git a/test/v2Bridge.test.js b/test/v2Bridge.test.js new file mode 100644 index 00000000..829f2450 --- /dev/null +++ b/test/v2Bridge.test.js @@ -0,0 +1,381 @@ +// The Bridge routes of the dashboard API, through the real app with both logins: who may see and change a +// server's bridge, the consent flow across two servers on two platforms, and that one server can never +// reach into another's links. The engine itself is in bridge.engine.test.js. +import './helpers/trustLoopbackProxy.js'; +import test, { beforeEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { + D_GUILD, + F_GUILD, + OTHER_D_GUILD, + browser, + profiles, + setProfiles, + signIn, + startOAuthApp, +} from './helpers/oauthApp.js'; + +await startOAuthApp(); + +const { runtime } = await import('../src/runtime.js'); +const { setGuildModule } = await import('../src/db/modules.js'); +const store = await import('../src/db/bridge.js'); +const { listAudit } = await import('../src/db/audit.js'); +const { purgeGuild } = await import('../src/db/purge.js'); +const { ROUTE_ACCESS } = await import('../src/web/lib/routeAccess.js'); +const { dropBridgeCache } = await import('../src/modules/bridge.js'); +const { useFakes } = await import('./helpers/fakeBridge.js'); + +const F_KEY = `fluxer:${F_GUILD}`; +const CH = { + d1: '810000000000000001', + d2: '810000000000000002', + f1: '810000000000000003', + f2: '810000000000000004', + dNsfw: '810000000000000005', +}; +const dGuild = runtime.clients.discord.guilds.cache.get(D_GUILD); +const fGuild = runtime.clients.fluxer.guilds.cache.get(F_GUILD); +const addChannel = (guild, id, name, nsfw = false) => + guild.channels.cache.set(id, { + id, + name, + type: 0, + nsfw, + rawPosition: Number(id.slice(-2)), + guild, + said: [], + permissionsFor: () => ({ has: () => true }), + async send(payload) { + this.said.push(payload); + return { id: '1' }; + }, + }); + +let fakes; +beforeEach(async () => { + useFakes(); // the purge below talks to the webhook transport for links the previous test left + dropBridgeCache(); + for (const key of [D_GUILD, F_KEY, OTHER_D_GUILD]) await purgeGuild(key); + fakes = useFakes(); + dGuild.channels.cache.clear(); + fGuild.channels.cache.clear(); + addChannel(dGuild, CH.d1, 'discord-main'); + addChannel(dGuild, CH.d2, 'discord-two'); + addChannel(dGuild, CH.dNsfw, 'discord-nsfw', true); + addChannel(fGuild, CH.f1, 'fluxer-main'); + addChannel(fGuild, CH.f2, 'fluxer-two'); + await setGuildModule(D_GUILD, 'bridge', { enabled: true }); + await setGuildModule(F_KEY, 'bridge', { enabled: true }); +}); + +/** A browser that sends its requests from an address of its own, so the sign-in limit is not shared. */ +let clients = 0; +function ownBrowser() { + const b = browser(); + const ip = `10.8.${Math.floor(++clients / 250)}.${clients % 250}`; + return { + ...b, + get: (path, init = {}) => b.get(path, { ...init, headers: { ...init.headers, 'x-forwarded-for': ip } }), + }; +} + +/** Someone signed in on a platform, with JSON helpers that carry the CSRF token. */ +async function person(platform, { guilds } = {}) { + setProfiles(); + if (guilds) profiles[platform].guilds = guilds; + const b = ownBrowser(); + await signIn(b, platform); + const token = (await (await b.get('/api/v2/csrf')).json()).token; + const headers = { accept: 'application/json', 'content-type': 'application/json', 'x-csrf-token': token }; + return { + get: (path) => b.get(`/api/v2${path}`, { headers: { accept: 'application/json' } }), + post: (path, body = {}) => + b.get(`/api/v2${path}`, { method: 'POST', headers, body: JSON.stringify(body) }), + }; +} +const dAdmin = () => person('discord'); +const fAdmin = () => person('fluxer'); +const base = (key) => `/guilds/${key}/modules/bridge`; +const json = (res) => res.json(); +const DEFAULTS = { bots: false, joinMessages: false, uploadFiles: true }; + +test('the routes are listed with the access they need: View reads, Edit changes', () => { + assert.equal(ROUTE_ACCESS['GET /guilds/:guildId/modules/bridge/config'], 'view'); + for (const [key, level] of Object.entries(ROUTE_ACCESS)) { + if (key.includes('/modules/bridge/') && key.startsWith('POST ')) assert.equal(level, 'edit', key); + } + assert.equal(Object.keys(ROUTE_ACCESS).filter((k) => k.includes('/modules/bridge/')).length, 8); +}); + +test('nobody signed out, and nobody without Manage Server, can read or change a bridge', async () => { + const b = ownBrowser(); + const read = await b.get(`/api/v2${base(D_GUILD)}/config`, { headers: { accept: 'application/json' } }); + assert.ok([401, 403].includes(read.status)); + const member = await person('discord', { guilds: [{ id: D_GUILD, owner: false, permissions: '0' }] }); + assert.equal((await member.get(`${base(D_GUILD)}/config`)).status, 403); + for (const [path, body] of [ + ['/request', { channelId: CH.d1 }], + ['/accept', { code: 'ABCD-EFGH-JKMN', channelId: CH.d1 }], + ['/links/1/pause', {}], + ['/links/1/resume', {}], + ['/links/1/unlink', {}], + ['/links/1/options', { options: { bots: true } }], + ['/requests/1/cancel', {}], + ]) { + assert.equal((await member.post(`${base(D_GUILD)}${path}`, body)).status, 403, path); + } + assert.equal((await store.pendingCodes(D_GUILD)).length, 0); + // an admin of one server has nothing in another + const stranger = await person('discord', { guilds: [{ id: OTHER_D_GUILD, owner: true }] }); + assert.equal((await stranger.get(`${base(D_GUILD)}/config`)).status, 403); + assert.equal((await stranger.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })).status, 403); +}); + +test('a state-changing call without the CSRF token is refused', async () => { + setProfiles(); + const b = ownBrowser(); + await signIn(b, 'discord'); + const res = await b.get(`/api/v2${base(D_GUILD)}/request`, { + method: 'POST', + headers: { 'content-type': 'application/json', accept: 'application/json' }, + body: JSON.stringify({ channelId: CH.d1 }), + }); + assert.equal(res.status, 403); + assert.equal((await store.pendingCodes(D_GUILD)).length, 0); +}); + +test('the overview lists channels, shows what is set up, and says when the module is off', async () => { + const d = await dAdmin(); + const res = await d.get(`${base(D_GUILD)}/config`); + assert.equal(res.status, 200); + const data = await json(res); + assert.equal(data.platform, 'discord'); + assert.equal(data.bothPlatforms, true); + assert.equal(data.enabled, true); + assert.deepEqual(data.limits, { links: 10, requests: 5 }); + assert.deepEqual(data.links, []); + assert.deepEqual( + data.channels.map((c) => [c.name, c.ageRestricted, c.taken]), + [ + ['discord-main', false, false], + ['discord-two', false, false], + ['discord-nsfw', true, false], + ] + ); + await setGuildModule(D_GUILD, 'bridge', { enabled: false }); + assert.equal((await json(await d.get(`${base(D_GUILD)}/config`))).enabled, false); + const refused = await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 }); + assert.equal(refused.status, 409); + assert.match((await json(refused)).error, /Turn the Bridge module on/); +}); + +test('asking, then accepting in the other server on the other platform, links the two channels', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + + const asked = await d.post(`${base(D_GUILD)}/request`, { + channelId: CH.d1, + options: { joinMessages: true }, + }); + assert.equal(asked.status, 200); + const { code, expiresAt, overview } = await json(asked); + assert.match(code, /^[A-Z2-9]{4}-[A-Z2-9]{4}-[A-Z2-9]{4}$/); + assert.ok(expiresAt > Date.now()); + assert.equal(overview.requests.length, 1); + assert.ok(!JSON.stringify(overview).includes(code), 'the code is shown once and never listed'); + assert.equal(overview.channels.find((c) => c.id === CH.d1).taken, true); + + // asking again for the same channel is refused, and the Fluxer server sees none of it + assert.equal((await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })).status, 409); + assert.deepEqual((await json(await f.get(`${base(F_KEY)}/config`))).requests, []); + + const accepted = await f.post(`${base(F_KEY)}/accept`, { code: code.toLowerCase(), channelId: CH.f1 }); + assert.equal(accepted.status, 200); + const mine = (await json(accepted)).overview; + assert.equal(mine.platform, 'fluxer'); + assert.equal(mine.links.length, 1); + const view = mine.links[0]; + assert.equal(view.status, 'active'); + assert.deepEqual( + [view.mine.channel, view.theirs.channel, view.theirs.server, view.theirs.platform], + ['fluxer-main', 'discord-main', 'D-main', 'discord'] + ); + assert.deepEqual( + view.theirOptions, + { bots: false, joinMessages: true, uploadFiles: true }, + 'what the other side chose to share' + ); + assert.deepEqual(view.options, { bots: false, joinMessages: false, uploadFiles: true }); + + // Discord sees the same link from its side, and the request is gone + const theirs = await json(await d.get(`${base(D_GUILD)}/config`)); + assert.equal(theirs.requests.length, 0); + assert.equal(theirs.links.length, 1); + assert.deepEqual( + [theirs.links[0].mine.channel, theirs.links[0].theirs.channel], + ['discord-main', 'fluxer-main'] + ); + + // never a webhook or its token to the browser + const everything = JSON.stringify([mine, theirs]); + for (const hook of fakes.transport.hooks) assert.ok(!everything.includes(hook.token)); + assert.ok(!/token|webhook/i.test(everything)); + + assert.ok((await listAudit(D_GUILD)).some((e) => e.action === 'bridge:link')); + assert.ok((await listAudit(F_KEY)).some((e) => e.action === 'bridge:link')); + assert.ok(dGuild.channels.cache.get(CH.d1).said[0].content.includes('Bridge on')); + assert.ok(fGuild.channels.cache.get(CH.f1).said[0].content.includes('Bridge on')); +}); + +test('someone without Edit in the accepting server cannot use a code there, whatever they hold in the other', async () => { + const d = await dAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + // the Discord admin has no right in the Fluxer server + const refused = await d.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + assert.equal(refused.status, 403); + // and a Fluxer member without Manage Server cannot either + const member = await person('fluxer', { guilds: [{ id: F_GUILD, owner: false, permissions: '0' }] }); + assert.equal((await member.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 403); + assert.equal((await store.pendingCodes(D_GUILD)).length, 1, 'the code is still unused'); + assert.equal(await store.linkForChannel(F_KEY, CH.f1), null); +}); + +test('a wrong code, a used code, your own code and the wrong platform are all refused', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + + const wrong = await f.post(`${base(F_KEY)}/accept`, { code: 'ABCD-EFGH-JKMN', channelId: CH.f1 }); + assert.equal(wrong.status, 404); + assert.match((await json(wrong)).error, /not valid, has expired or was already used/); + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code: '', channelId: CH.f1 })).status, 404); + assert.equal( + (await f.post(`${base(F_KEY)}/accept`, { code, channelId: 'junk' })).status, + 400, + 'no channel' + ); + assert.equal( + (await d.post(`${base(D_GUILD)}/accept`, { code, channelId: CH.d2 })).status, + 409, + 'your own code' + ); + + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 200); + assert.equal( + (await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f2 })).status, + 404, + 'a used code' + ); +}); + +test('too many wrong codes lock the attempts for a while, for that person in that server', async () => { + const f = await fAdmin(); + for (let i = 0; i < 6; i += 1) { + assert.equal( + (await f.post(`${base(F_KEY)}/accept`, { code: 'ABCD-EFGH-JKM' + i, channelId: CH.f1 })).status, + 404 + ); + } + const locked = await f.post(`${base(F_KEY)}/accept`, { code: 'ABCD-EFGH-JKMN', channelId: CH.f1 }); + assert.equal(locked.status, 429); + assert.match((await json(locked)).error, /Wait a few minutes/); + // a valid code does not get around it + const d = await dAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 429); + assert.equal((await store.pendingCodes(D_GUILD)).length, 1); +}); + +test('an age-restricted channel is refused for an ordinary one when asking and when accepting', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.dNsfw })); + const refused = await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + assert.equal(refused.status, 400); + assert.match( + (await json(refused)).error, + /age-restricted channel can only be bridged with another age-restricted/ + ); + assert.equal(fakes.transport.hooks.length, 0); + addChannel(fGuild, '810000000000000006', 'fluxer-nsfw', true); + assert.equal( + (await f.post(`${base(F_KEY)}/accept`, { code, channelId: '810000000000000006' })).status, + 200 + ); +}); + +test('pause, resume and unlink from either side, with the rules about who may resume', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + const id = (await store.linksOfGuild(D_GUILD))[0].id; + + const paused = await d.post(`${base(D_GUILD)}/links/${id}/pause`); + assert.equal(paused.status, 200); + assert.equal((await json(paused)).overview.links[0].pausedByMe, true); + assert.equal((await json(await f.get(`${base(F_KEY)}/config`))).links[0].pausedByThem, true); + const blocked = await f.post(`${base(F_KEY)}/links/${id}/resume`); + assert.equal(blocked.status, 403); + assert.match((await json(blocked)).error, /only they can resume/); + assert.equal((await d.post(`${base(D_GUILD)}/links/${id}/resume`)).status, 200); + assert.equal((await store.getLink(id)).status, 'active'); + + const opts = await f.post(`${base(F_KEY)}/links/${id}/options`, { options: { bots: true, junk: 1 } }); + assert.deepEqual((await json(opts)).overview.links[0].options, { + bots: true, + joinMessages: false, + uploadFiles: true, + }); + assert.deepEqual( + (await store.getLink(id)).optionsA, + DEFAULTS, + "the other server's options are not touched" + ); + + const unlinked = await f.post(`${base(F_KEY)}/links/${id}/unlink`); + assert.equal(unlinked.status, 200); + assert.deepEqual((await json(unlinked)).overview.links, []); + assert.equal(await store.getLink(id), null); + assert.equal(fakes.transport.deleted.length, 2, 'both webhooks were deleted'); + assert.ok((await listAudit(D_GUILD)).some((e) => e.action === 'bridge:unlink')); +}); + +test("another server's link id is a 404 in every route, and nothing of it changes", async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + const id = (await store.linksOfGuild(D_GUILD))[0].id; + + const stranger = await person('discord', { guilds: [{ id: OTHER_D_GUILD, owner: true }] }); + for (const path of ['pause', 'resume', 'unlink']) { + const res = await stranger.post(`${base(OTHER_D_GUILD)}/links/${id}/${path}`); + assert.equal(res.status, 404, path); + } + assert.equal( + (await stranger.post(`${base(OTHER_D_GUILD)}/links/${id}/options`, { options: { bots: true } })).status, + 404 + ); + assert.equal((await stranger.post(`${base(OTHER_D_GUILD)}/requests/1/cancel`)).status, 404); + assert.equal((await stranger.post(`${base(OTHER_D_GUILD)}/links/abc/pause`)).status, 404); + const link = await store.getLink(id); + assert.deepEqual([link.status, link.optionsA, link.optionsB], ['active', DEFAULTS, DEFAULTS]); +}); + +test('a request can be cancelled by the server that made it, which kills its code', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code, overview } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + const requestId = overview.requests[0].id; + assert.equal( + (await f.post(`${base(F_KEY)}/requests/${requestId}/cancel`)).status, + 404, + 'not by the other server' + ); + const cancelled = await d.post(`${base(D_GUILD)}/requests/${requestId}/cancel`); + assert.equal(cancelled.status, 200); + assert.deepEqual((await json(cancelled)).overview.requests, []); + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 404); +}); diff --git a/web-v2/src/__tests__/Bridge.test.jsx b/web-v2/src/__tests__/Bridge.test.jsx new file mode 100644 index 00000000..dca0e724 --- /dev/null +++ b/web-v2/src/__tests__/Bridge.test.jsx @@ -0,0 +1,296 @@ +import { fireEvent, screen, waitFor } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import Bridge from '../moduleForms/Bridge.jsx'; +import { renderAtGuild } from './routerHarness.jsx'; + +vi.mock('../api.js', () => ({ + ApiError: class ApiError extends Error {}, + getBridge: vi.fn(), + requestBridgeLink: vi.fn(), + cancelBridgeRequest: vi.fn(), + acceptBridgeLink: vi.fn(), + setBridgeOptions: vi.fn(), + bridgeLinkAction: vi.fn(), +})); +vi.mock('../notify.js', () => ({ notify: vi.fn() })); +vi.mock('../components/ConfirmDialog.jsx', () => ({ confirmDialog: vi.fn().mockResolvedValue(true) })); +const api = await import('../api.js'); +const { confirmDialog } = await import('../components/ConfirmDialog.jsx'); +const { notify } = await import('../notify.js'); + +const GUILD = '123456789012345678'; +const MAIN = '666666666666666661'; +const TWO = '666666666666666662'; + +const link = (over = {}) => ({ + id: 7, + status: 'active', + pausedByMe: false, + pausedByThem: false, + problem: null, + mine: { channelId: MAIN, platform: 'discord', server: 'Home', channel: 'general', ageRestricted: false }, + theirs: { platform: 'fluxer', server: 'Fluxer Home', channel: 'chat', ageRestricted: false }, + options: { bots: false, joinMessages: false, uploadFiles: true }, + theirOptions: { bots: true, joinMessages: false, uploadFiles: true }, + createdAt: Date.UTC(2030, 0, 2), + requestedBy: 'Alice (dashboard)', + acceptedBy: 'Bob (dashboard)', + queued: 0, + dropped: 0, + ...over, +}); +const overview = (over = {}) => ({ + platform: 'discord', + bothPlatforms: true, + enabled: true, + limits: { links: 10, requests: 5 }, + links: [], + requests: [], + channels: [ + { id: MAIN, name: 'general', ageRestricted: false, taken: false }, + { id: TWO, name: 'off-topic', ageRestricted: true, taken: false }, + ], + ...over, +}); + +beforeEach(() => { + vi.resetAllMocks(); + confirmDialog.mockResolvedValue(true); + api.getBridge.mockResolvedValue(overview()); +}); + +describe('Bridge page', () => { + it('says there is nothing bridged yet and offers the two ways to start', async () => { + renderAtGuild(, GUILD); + expect(await screen.findByRole('heading', { name: 'Bridge' })).toBeTruthy(); + expect(screen.getByRole('heading', { name: 'Bridged channels (0 of 10)' })).toBeTruthy(); + expect(screen.getByText(/No channel is bridged yet/)).toBeTruthy(); + expect(screen.getByRole('heading', { name: 'Ask the other server' })).toBeTruthy(); + expect(screen.getByRole('heading', { name: 'Use a code' })).toBeTruthy(); + expect(api.getBridge).toHaveBeenCalledWith(GUILD); + }); + + it('names the other platform, and explains that nothing pings and bots are off', async () => { + renderAtGuild(, GUILD); + await screen.findByRole('heading', { name: 'Bridge' }); + expect(screen.getAllByText(/Fluxer/).length).toBeGreaterThan(0); + expect(screen.getAllByText(/pings/).length).toBeGreaterThan(0); + }); + + it('does not offer anything when this Sylo is not on both platforms, or the module is off', async () => { + api.getBridge.mockResolvedValue(overview({ bothPlatforms: false })); + const { unmount } = renderAtGuild(, GUILD); + expect(await screen.findByText(/not connected to both Discord and Fluxer/)).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Get a code' }).disabled).toBe(true); + unmount(); + api.getBridge.mockResolvedValue(overview({ enabled: false })); + renderAtGuild(, GUILD); + expect(await screen.findByText(/Turn the module on above/)).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Link the channels' }).disabled).toBe(true); + }); + + it('lists a link with the other side, its status, and what each side shares', async () => { + api.getBridge.mockResolvedValue(overview({ links: [link({ queued: 3 })] })); + renderAtGuild(, GUILD); + expect(await screen.findByText(/#general ↔ #chat/)).toBeTruthy(); + expect(screen.getByText('Running')).toBeTruthy(); + expect(screen.getByText(/Fluxer Home on Fluxer/)).toBeTruthy(); + expect(screen.getByText(/3 waiting to send/)).toBeTruthy(); + expect(screen.getByText(/Bot messages: shared. Who joins: not shared./)).toBeTruthy(); + expect( + screen + .getByLabelText('Share messages from bots', { selector: '#opt-7-bots' }) + .getAttribute('aria-checked') + ).toBe('false'); + }); + + it('shows why a stopped link stopped, and who paused a paused one', async () => { + api.getBridge.mockResolvedValue( + overview({ + links: [ + link({ + id: 1, + status: 'failed', + problem: { code: 'permissions', text: 'Sylo is missing a permission here.' }, + }), + link({ + id: 2, + status: 'paused', + pausedByThem: true, + mine: { + channelId: TWO, + platform: 'discord', + server: 'Home', + channel: 'off-topic', + ageRestricted: true, + }, + }), + ], + }) + ); + renderAtGuild(, GUILD); + expect(await screen.findByText('Stopped')).toBeTruthy(); + expect(screen.getByText('Sylo is missing a permission here.')).toBeTruthy(); + expect(screen.getByText('Paused by the other server')).toBeTruthy(); + expect(screen.getByText(/Fluxer Home paused this bridge. Only they can resume it./)).toBeTruthy(); + // a stopped link can be resumed; one the other server paused cannot + expect(screen.getAllByRole('button', { name: 'Resume' })).toHaveLength(1); + }); + + it('pauses a link and shows the fresh state the server answers with', async () => { + api.getBridge.mockResolvedValue(overview({ links: [link()] })); + api.bridgeLinkAction.mockResolvedValue({ + overview: overview({ links: [link({ status: 'paused', pausedByMe: true })] }), + }); + renderAtGuild(, GUILD); + fireEvent.click(await screen.findByRole('button', { name: 'Pause' })); + await waitFor(() => expect(api.bridgeLinkAction).toHaveBeenCalledWith(GUILD, 7, 'pause')); + expect(await screen.findByText('Paused by this server')).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Resume' })).toBeTruthy(); + }); + + it('changes a switch of this side through the server', async () => { + api.getBridge.mockResolvedValue(overview({ links: [link()] })); + api.setBridgeOptions.mockResolvedValue({ + overview: overview({ + links: [link({ options: { bots: true, joinMessages: false, uploadFiles: true } })], + }), + }); + renderAtGuild(, GUILD); + fireEvent.click(await screen.findByLabelText('Share messages from bots', { selector: '#opt-7-bots' })); + await waitFor(() => expect(api.setBridgeOptions).toHaveBeenCalledWith(GUILD, 7, { bots: true })); + await waitFor(() => + expect( + screen + .getByLabelText('Share messages from bots', { selector: '#opt-7-bots' }) + .getAttribute('aria-checked') + ).toBe('true') + ); + }); + + it('asks before unlinking, and only unlinks when confirmed', async () => { + api.getBridge.mockResolvedValue(overview({ links: [link()] })); + api.bridgeLinkAction.mockResolvedValue({ overview: overview() }); + confirmDialog.mockResolvedValueOnce(false); + renderAtGuild(, GUILD); + const open = async () => { + fireEvent.click(await screen.findByRole('button', { name: 'More actions for #general' })); + fireEvent.click(screen.getByRole('menuitem', { name: 'Unlink' })); + }; + await open(); + await waitFor(() => expect(confirmDialog).toHaveBeenCalledTimes(1)); + expect(confirmDialog.mock.calls[0][0].title).toBe('Unlink #general?'); + expect(api.bridgeLinkAction).not.toHaveBeenCalled(); + await open(); + await waitFor(() => expect(api.bridgeLinkAction).toHaveBeenCalledWith(GUILD, 7, 'unlink')); + expect(await screen.findByText(/No channel is bridged yet/)).toBeTruthy(); + }); + + it('asks for a code, shows it once with its expiry and a copy button', async () => { + api.requestBridgeLink.mockResolvedValue({ + code: 'ABCD-EFGH-JKMN', + expiresAt: Date.now() + 900_000, + overview: overview({ + requests: [ + { + id: 3, + channelId: MAIN, + channel: 'general', + expiresAt: Date.now() + 900_000, + requestedBy: 'Alice', + }, + ], + channels: [ + { id: MAIN, name: 'general', ageRestricted: false, taken: true }, + { id: TWO, name: 'off-topic', ageRestricted: true, taken: false }, + ], + }), + }); + renderAtGuild(, GUILD); + const button = await screen.findByRole('button', { name: 'Get a code' }); + expect(button.disabled).toBe(true); + fireEvent.change(screen.getByLabelText('Channel here', { selector: '#bridge-ask-channel' }), { + target: { value: MAIN }, + }); + fireEvent.click(screen.getByLabelText('Share who joins', { selector: '#ask-joinMessages' })); + fireEvent.click(screen.getByRole('button', { name: 'Get a code' })); + await waitFor(() => + expect(api.requestBridgeLink).toHaveBeenCalledWith(GUILD, { + channelId: MAIN, + options: { bots: false, joinMessages: true, uploadFiles: true }, + }) + ); + expect(await screen.findByText('ABCD-EFGH-JKMN')).toBeTruthy(); + expect(screen.getByText(/works once/)).toBeTruthy(); + expect(screen.getByRole('heading', { name: 'Waiting for the other server' })).toBeTruthy(); + expect(screen.getByText(/code valid until/)).toBeTruthy(); + // the channel that has a request waiting can no longer be picked + const options = [ + ...screen + .getByLabelText('Channel here', { selector: '#bridge-ask-channel' }) + .querySelectorAll('option'), + ].map((o) => o.textContent); + expect(options).toEqual(['Pick a channel', '#off-topic (age-restricted)']); + }); + + it('uses a code with a channel and says so', async () => { + api.acceptBridgeLink.mockResolvedValue({ overview: overview({ links: [link()] }) }); + renderAtGuild(, GUILD); + await screen.findByRole('button', { name: 'Link the channels' }); + fireEvent.change(screen.getByLabelText('Code'), { target: { value: ' abcd-efgh-jkmn ' } }); + fireEvent.change(screen.getByLabelText('Channel here', { selector: '#bridge-use-channel' }), { + target: { value: TWO }, + }); + fireEvent.click(screen.getByRole('button', { name: 'Link the channels' })); + await waitFor(() => + expect(api.acceptBridgeLink).toHaveBeenCalledWith(GUILD, { + code: 'abcd-efgh-jkmn', + channelId: TWO, + options: { bots: false, joinMessages: false, uploadFiles: true }, + }) + ); + expect(await screen.findByText(/#general ↔ #chat/)).toBeTruthy(); + expect(notify).toHaveBeenCalledWith('The bridge is on.'); + }); + + it('shows the refusal of the server as it is', async () => { + const { ApiError } = await import('../api.js'); + api.acceptBridgeLink.mockRejectedValue( + new ApiError('That code is not valid, has expired or was already used.') + ); + renderAtGuild(, GUILD); + await screen.findByRole('button', { name: 'Link the channels' }); + fireEvent.change(screen.getByLabelText('Code'), { target: { value: 'WRONG' } }); + fireEvent.change(screen.getByLabelText('Channel here', { selector: '#bridge-use-channel' }), { + target: { value: MAIN }, + }); + fireEvent.click(screen.getByRole('button', { name: 'Link the channels' })); + await waitFor(() => + expect(notify).toHaveBeenCalledWith('That code is not valid, has expired or was already used.') + ); + }); + + it('cancels a waiting request after asking', async () => { + api.getBridge.mockResolvedValue( + overview({ + requests: [ + { + id: 3, + channelId: MAIN, + channel: 'general', + expiresAt: Date.now() + 900_000, + requestedBy: 'Alice', + }, + ], + }) + ); + api.cancelBridgeRequest.mockResolvedValue({ overview: overview() }); + renderAtGuild(, GUILD); + fireEvent.click(await screen.findByRole('button', { name: 'More actions for the request of #general' })); + fireEvent.click(screen.getByRole('menuitem', { name: 'Cancel request' })); + await waitFor(() => expect(api.cancelBridgeRequest).toHaveBeenCalledWith(GUILD, 3)); + await waitFor(() => + expect(screen.queryByRole('heading', { name: 'Waiting for the other server' })).toBeNull() + ); + }); +}); diff --git a/web-v2/src/api.js b/web-v2/src/api.js index e93b45e1..6ae6a523 100644 --- a/web-v2/src/api.js +++ b/web-v2/src/api.js @@ -157,6 +157,19 @@ export const moveSuggestionBack = (guildId, number) => // { moved, remaining, failed, next, error? }; ask again with after = next while remaining > 0. export const moveAllSuggestionsBack = (guildId, after = 0) => postJson(`/api/v2/guilds/${gid(guildId)}/modules/suggestions/move-all-back`, { after }); +// Bridge: the links of a server and what the page needs (GET), then the calls that change them. Every +// answer to a change carries the fresh `overview`. +export const getBridge = (guildId) => apiFetch(`/api/v2/guilds/${gid(guildId)}/modules/bridge/config`); +export const requestBridgeLink = (guildId, body) => + postJson(`/api/v2/guilds/${gid(guildId)}/modules/bridge/request`, body); +export const cancelBridgeRequest = (guildId, id) => + postJson(`/api/v2/guilds/${gid(guildId)}/modules/bridge/requests/${Number(id)}/cancel`, {}); +export const acceptBridgeLink = (guildId, body) => + postJson(`/api/v2/guilds/${gid(guildId)}/modules/bridge/accept`, body); +export const setBridgeOptions = (guildId, id, options) => + postJson(`/api/v2/guilds/${gid(guildId)}/modules/bridge/links/${Number(id)}/options`, { options }); +export const bridgeLinkAction = (guildId, id, action) => + postJson(`/api/v2/guilds/${gid(guildId)}/modules/bridge/links/${Number(id)}/${action}`, {}); export const getQuotes = (guildId, { q = '' } = {}) => apiFetch(`/api/v2/guilds/${gid(guildId)}/modules/quote-board/list?q=${encodeURIComponent(q)}`); export const deleteQuote = (guildId, number) => diff --git a/web-v2/src/main.jsx b/web-v2/src/main.jsx index e97b7ce7..f3bb9585 100644 --- a/web-v2/src/main.jsx +++ b/web-v2/src/main.jsx @@ -22,6 +22,7 @@ import './styles/setup-wizard.css'; import './styles/access.css'; import './styles/history.css'; import './styles/health.css'; +import './styles/bridge.css'; createRoot(document.getElementById('root')).render( diff --git a/web-v2/src/moduleForms/Bridge.jsx b/web-v2/src/moduleForms/Bridge.jsx new file mode 100644 index 00000000..40b540af --- /dev/null +++ b/web-v2/src/moduleForms/Bridge.jsx @@ -0,0 +1,410 @@ +import { useState } from 'react'; +import { useParams } from 'react-router-dom'; +import { + acceptBridgeLink, + bridgeLinkAction, + cancelBridgeRequest, + getBridge, + requestBridgeLink, + setBridgeOptions, +} from '../api.js'; +import { notify } from '../notify.js'; +import { useApiData } from '../useApiData.js'; +import { AccessLock } from '../access.jsx'; +import LoadError from '../components/LoadError.jsx'; +import HowItWorks from '../components/HowItWorks.jsx'; +import CopyButton from '../components/CopyButton.jsx'; +import EmptyList from '../components/EmptyList.jsx'; +import RowActions from '../components/RowActions.jsx'; +import Switch from '../components/Switch.jsx'; +import Meta from '../components/Meta.jsx'; +import { confirmDialog } from '../components/ConfirmDialog.jsx'; +import { formatDateTime } from '../formatTime.js'; + +const PLATFORM = { discord: 'Discord', fluxer: 'Fluxer' }; +// The code is XXXX-XXXX-XXXX; a pasted one may carry spaces. +const CODE_FIELD_LENGTH = 24; + +const OPTION_ROWS = [ + { + key: 'bots', + label: 'Share messages from bots', + hint: 'Off by default. Messages from other bots and webhooks of this server cross; Sylo is never relayed.', + }, + { + key: 'joinMessages', + label: 'Share who joins', + hint: 'Off by default. A short "joined" line appears on the other side when someone joins this server.', + }, + { + key: 'uploadFiles', + label: 'Copy files across', + hint: 'Files up to 8 MB are re-uploaded to the other side; bigger ones, or all of them when this is off, cross as a link.', + }, +]; + +function statusOf(link) { + if (link.status === 'active') return { tone: 'success', text: 'Running' }; + if (link.status === 'paused') { + return { + tone: 'warning', + text: link.pausedByMe ? 'Paused by this server' : 'Paused by the other server', + }; + } + return { tone: 'danger', text: 'Stopped' }; +} + +/** One channel picker: the text channels of this server that are not in a link or a request yet. */ +function ChannelSelect({ id, channels, value, onChange }) { + const free = channels.filter((c) => !c.taken); + return ( + + ); +} + +/** The two switches of a side that are off until asked for, as a small group. */ +function OptionSwitches({ options, onChange, idPrefix, disabled }) { + return ( +
+ {OPTION_ROWS.map((o) => ( +
+ onChange({ [o.key]: on })} + label={o.label} + disabled={disabled} + /> + + + {o.hint} + +
+ ))} +
+ ); +} + +const DEFAULT_OPTIONS = { bots: false, joinMessages: false, uploadFiles: true }; + +export default function Bridge() { + const { guildId } = useParams(); + const { data, dataKey, loading, error, setData } = useApiData(() => getBridge(guildId), [guildId], guildId); + const [busy, setBusy] = useState(false); + // Ask the other server + const [askChannel, setAskChannel] = useState(''); + const [askOptions, setAskOptions] = useState(DEFAULT_OPTIONS); + const [issued, setIssued] = useState(null); // { code, expiresAt } + // Use a code + const [code, setCode] = useState(''); + const [useChannel, setUseChannel] = useState(''); + const [useOptions, setUseOptions] = useState(DEFAULT_OPTIONS); + + if (error) return ; + // The previous server's page stays in `data` while the next one loads: never show or change it as this one. + if (!data || dataKey !== guildId) return loading ?

Loading…

: null; + + const other = data.platform === 'fluxer' ? 'Discord' : 'Fluxer'; + const here = PLATFORM[data.platform]; + const ready = data.bothPlatforms && data.enabled; + + async function run(fn, done) { + setBusy(true); + try { + const out = await fn(); + if (out?.overview) setData(out.overview); + done?.(out); + } catch (err) { + notify(err.message); + } finally { + setBusy(false); + } + } + + const onAsk = (e) => { + e.preventDefault(); + if (!askChannel) return notify('Pick a channel.'); + return run( + () => requestBridgeLink(guildId, { channelId: askChannel, options: askOptions }), + (out) => { + setIssued({ code: out.code, expiresAt: out.expiresAt }); + setAskChannel(''); + } + ); + }; + + const onUse = (e) => { + e.preventDefault(); + if (!code.trim()) return notify('Paste the code from the other server.'); + if (!useChannel) return notify('Pick a channel.'); + return run( + () => acceptBridgeLink(guildId, { code: code.trim(), channelId: useChannel, options: useOptions }), + () => { + setCode(''); + setUseChannel(''); + notify('The bridge is on.'); + } + ); + }; + + const onOption = (link, patch) => run(() => setBridgeOptions(guildId, link.id, patch)); + + async function onUnlink(link) { + const ok = await confirmDialog({ + title: `Unlink #${link.mine.channel}?`, + body: `Messages stop crossing to #${link.theirs.channel} in ${link.theirs.server}. Sylo deletes both webhooks and tells both channels. Messages already posted stay where they are.`, + confirmLabel: 'Unlink', + }); + if (ok) await run(() => bridgeLinkAction(guildId, link.id, 'unlink')); + } + + async function onCancelRequest(request) { + const ok = await confirmDialog({ + title: 'Cancel this request?', + body: 'The code stops working. You can ask again.', + confirmLabel: 'Cancel request', + cancelLabel: 'Keep it', + }); + if (ok) await run(() => cancelBridgeRequest(guildId, request.id)); + } + + return ( + <> +

Bridge

+

+ One conversation between a channel here on {here} and a channel on {other}. Members on both sides + write as they always do, and Sylo shows each message on the other side with the author's name and + picture. +

+ +

+ Both servers agree. One asks for a link and gets a code that works for 15 minutes; someone with Edit + access in the other server uses it and picks a channel. Sylo has to be in both servers. Either side + can unlink at any time. +

+

+ What crosses: messages, replies (as a short quote), files up to 8 MB (otherwise a link), custom + emoji with the same name, stickers (as a picture link), embeds, polls (as text: the vote stays where + the poll is) and, if you turn it on, who joins. Edits and deletes follow for 30 days. +

+

+ What never crosses: pings. @everyone, @here, roles and people are shown as plain text on the other + side, and nothing the bridge posts notifies anyone. Sylo's own bridge messages are never + relayed again, so there are no loops. Other bots are off until you turn them on. An age-restricted + channel can only be bridged with another age-restricted channel. Both channels show a notice while a + bridge is on. +

+
+ + {!data.bothPlatforms ? ( +

+ This Sylo is not connected to both Discord and Fluxer, so there is nothing to bridge to. +

+ ) : null} + {data.bothPlatforms && !data.enabled ? ( +

Turn the module on above to ask for a link or use a code.

+ ) : null} + +
+

+ Bridged channels ({data.links.length} of {data.limits.links}) +

+ {data.links.length === 0 ? ( + + No channel is bridged yet. Ask for a link below, or use a code from the other server. + + ) : null} +
+ {data.links.map((link) => { + const status = statusOf(link); + const canResume = link.status === 'failed' || link.pausedByMe; + return ( +
+
+

+ #{link.mine.channel} ↔ #{link.theirs.channel} + {status.text} +

+

+ 0 && `${link.queued} waiting to send`, + link.dropped > 0 && `${link.dropped} dropped while busy`, + ]} + /> +

+ {link.problem ?

{link.problem.text}

: null} + {link.pausedByThem ? ( +

+ {link.theirs.server} paused this bridge. Only they can resume it. +

+ ) : null} +
+ What this server shares + + onOption(link, patch)} + /> + +

+ The other server decides for itself what it shares. Bot messages:{' '} + {link.theirOptions.bots ? 'shared' : 'not shared'}. Who joins:{' '} + {link.theirOptions.joinMessages ? 'shared' : 'not shared'}. +

+
+
+ run(() => bridgeLinkAction(guildId, link.id, 'pause')), + } + : canResume + ? { + label: 'Resume', + disabled: busy, + onClick: () => run(() => bridgeLinkAction(guildId, link.id, 'resume')), + } + : null + } + items={[{ label: 'Unlink', danger: true, disabled: busy, onClick: () => onUnlink(link) }]} + /> +
+ ); + })} +
+
+ + {data.requests.length ? ( +
+

Waiting for the other server

+
+ {data.requests.map((r) => ( +
+
+

#{r.channel}

+

+ +

+
+ onCancelRequest(r), + }, + ]} + /> +
+ ))} +
+
+ ) : null} + + +
+

Ask the other server

+

+ Pick the channel here that should be bridged. You get a one-time code to give to someone with Edit + access in the {other} server. +

+ +
+ + +
+ setAskOptions((o) => ({ ...o, ...patch }))} + /> + + + {issued ? ( +
+

+ Give this code to the {other} server. It works once, until {formatDateTime(issued.expiresAt)}, + and is not shown again. +

+

+ {issued.code}{' '} + +

+
+ ) : null} +
+ +
+

Use a code

+

+ Someone on {other} asked for a link and gave you a code. Pick the channel here that joins it. +

+
+
+ + setCode(e.target.value)} + /> +
+
+ + +
+ setUseOptions((o) => ({ ...o, ...patch }))} + /> + + +
+
+ + ); +} diff --git a/web-v2/src/moduleForms/index.js b/web-v2/src/moduleForms/index.js index 1001f944..c47ebd7d 100644 --- a/web-v2/src/moduleForms/index.js +++ b/web-v2/src/moduleForms/index.js @@ -19,6 +19,7 @@ const Suggestions = lazy(() => import('./Suggestions.jsx')); const TempRoles = lazy(() => import('./TempRoles.jsx')); const QuoteBoard = lazy(() => import('./QuoteBoard.jsx')); const Introductions = lazy(() => import('./Introductions.jsx')); +const Bridge = lazy(() => import('./Bridge.jsx')); const Applications = lazy(() => import('./Applications.jsx')); const Economy = lazy(() => import('./Economy.jsx')); const AutoThreads = lazy(() => import('./AutoThreads.jsx')); @@ -86,6 +87,7 @@ export const MODULE_FORMS = { 'temp-roles': TempRoles, 'quote-board': QuoteBoard, introductions: Introductions, + bridge: Bridge, applications: Applications, economy: Economy, 'auto-threads': AutoThreads, diff --git a/web-v2/src/pages/ModulePage.jsx b/web-v2/src/pages/ModulePage.jsx index 5e1605b3..c02f5ccc 100644 --- a/web-v2/src/pages/ModulePage.jsx +++ b/web-v2/src/pages/ModulePage.jsx @@ -22,7 +22,7 @@ const INBOX_PATH = { tickets: '/tickets', appeals: '/appeals' }; // A module's settings need Edit access (access.jsx), so below it the whole form is disabled. Not for // pages with tabs or a search a reader still uses: there the save bar, the switches, the row actions // and the queue's own buttons (Applications, Suggestions: Inbox) check the level themselves. -const NOT_LOCKED_AS_A_WHOLE = new Set(['applications', 'suggestions', 'economy', 'quote-board']); +const NOT_LOCKED_AS_A_WHOLE = new Set(['applications', 'suggestions', 'economy', 'quote-board', 'bridge']); // The module's on/off state with a switch, so a module can be turned on from // its own settings page. Reads and patches the same shared overview state the diff --git a/web-v2/src/styles/bridge.css b/web-v2/src/styles/bridge.css new file mode 100644 index 00000000..ccd0aa2c --- /dev/null +++ b/web-v2/src/styles/bridge.css @@ -0,0 +1,45 @@ +/* The Bridge page (moduleForms/Bridge.jsx): the switches of one side, the one-time code and the forms. */ +.v2-bridge-options { + display: grid; + gap: 12px; + margin: 12px 0; +} +.v2-bridge-option { + display: flex; + align-items: flex-start; + gap: 12px; +} +.v2-bridge-option > span { + display: grid; + gap: 2px; + min-width: 0; +} +.v2-bridge-option label { + font-weight: 500; +} +.v2-bridge-details { + margin-top: 8px; +} +.v2-bridge-details > summary { + cursor: pointer; + color: var(--text-muted); + font-size: 0.875rem; +} +.v2-bridge-form { + display: grid; + gap: 12px; + max-width: 480px; +} +.v2-bridge-code { + margin-top: 16px; + padding: 12px 16px; + border: 1px solid var(--border-strong); + border-radius: var(--radius-md, 10px); + background: color-mix(in srgb, var(--success) 8%, transparent); +} +.v2-bridge-code-text { + font-size: 1.25rem; + letter-spacing: 0.08em; + font-weight: 600; + overflow-wrap: anywhere; +} From 385a84e029fbbb981e5180884ce777df7c61bab8 Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Mon, 16 Nov 2026 23:18:29 +0100 Subject: [PATCH 2/3] fix(bridge): age restriction on Fluxer, actor access, deletes, loops, sealed tokens - Age restriction uses the platform's real rule: on Fluxer the channel's own setting, its category's or the server's (fail closed when unreadable), and a Discord server marked age-restricted counts for its channels. Checked when linking, resuming and on every message. - The person linking (request and accept) must be able to view, read and send in the channel themselves; Sylo's own rights that cannot be read count as missing. - Deletes always reach the copy: looked up inside the queued job, run while the link is paused or stopped, not dropped for a full or slow lane, one job for a purge, the webhook that really posted a copy is remembered, the bot deletes the copy when the webhook cannot, and a delete that wins the race against the relay is honoured. Edits are looked up inside the job too. - Webhook messages never cross, even with bots on, and bot accounts that wear a platform tag are skipped, so another bridge cannot make a loop. - Webhook tokens are sealed at rest with the AES-GCM helper now shared in lib/seal.js (the Pigeon credential keeps its format); an unreadable token leads to a new webhook. - One process-wide budget for file downloads (4 at a time, 32 MB); a file that does not fit crosses as a link. - Names in Sylo's own text are escaped, poll, sticker and file names get the mention rules, a late failure no longer overrides a pause, the leaving server's own webhook is deleted, a refused message falls back to a plain name and then to text only, pause is rate limited, and the dashboard shows only the own side's name. --- docs/modules/bridge.md | 24 +- src/db/bridge.js | 30 +- src/lib/pigeon.js | 38 +- src/lib/seal.js | 44 +++ src/modules/bridge.js | 281 ++++++++++---- src/modules/lib/bridgeQueue.js | 28 +- src/modules/lib/bridgeText.js | 8 +- src/platform/webhooks.js | 10 + src/web/lib/permissionHealth.js | 9 +- src/web/routes/v2Bridge.js | 19 +- test/bridge.db.test.js | 50 +++ test/bridge.engine.test.js | 534 ++++++++++++++++++++++++++- test/bridgeQueue.test.js | 26 ++ test/bridgeText.test.js | 13 + test/helpers/fakeBridge.js | 22 +- test/v2.accessLevels.test.js | 3 + test/v2Bridge.test.js | 73 +++- web-v2/src/__tests__/Bridge.test.jsx | 3 +- web-v2/src/moduleForms/Bridge.jsx | 4 +- 19 files changed, 1080 insertions(+), 139 deletions(-) create mode 100644 src/lib/seal.js diff --git a/docs/modules/bridge.md b/docs/modules/bridge.md index e32bec9f..a8b13ad6 100644 --- a/docs/modules/bridge.md +++ b/docs/modules/bridge.md @@ -54,12 +54,16 @@ stopped itself (see below) can be resumed by either server once the cause is fix plain names ("@Bobby", "@moderators", "#rules"). - **No loops or echoes.** A message posted by any Sylo bridge webhook, or by Sylo itself, is never relayed. A channel is in only one link, so a copy cannot find its way back. -- **Bots are off by default.** Each server decides for itself what it sends: **bot messages**, **who +- **Webhook messages never cross**, whatever the options say: they are what bridges post (this one and any other + bridge on the same channels), and relaying them would make two bridges feed each other forever. Bot accounts + whose name already carries a platform tag are skipped too. +- **Bots are off by default.** Each server decides for itself what it sends: **bot messages** (bot accounts), **who joins** and **copy files** are options of that server's side only, and the other server cannot change them. System messages (joins, pins, boosts) are never relayed. -- **Age restriction.** An age-restricted channel can only be linked to an age-restricted channel. It is - checked when linking, when resuming, and on **every message**: if one of the channels changes, the link - stops. +- **Age restriction.** An age-restricted channel can only be linked to an age-restricted channel. On Fluxer a + channel counts as age-restricted when its own setting, its category's or the server's says so (what Sylo cannot + read counts as restricted); a Discord server marked age-restricted counts for all its channels. It is checked + when linking, when resuming, and on **every message**: if one of the channels changes, the link stops. - **Visible notice.** Both channels get a notice while a bridge is on, and when it is paused, stopped or ended. - **Rate limits.** Each webhook has its own queue: sends are spaced out, a rate limit from the platform is @@ -70,8 +74,16 @@ stopped itself (see below) can be resumed by either server once the cause is fix failed deliveries in a row stop the link. Sylo says so in both channels, the **Health** page shows it as a problem with the reason, and it is not retried until someone presses **Resume**. - **Webhooks are Sylo's.** Sylo makes and manages the webhook of each channel and makes a new one if it is - deleted. Its token is stored in the database and never sent to the browser. A database backup therefore - holds it, like every other secret; encrypt off-site backups. + deleted. Its token is stored sealed (AES-256-GCM, keyed from the session secret) and never sent to the + browser. If the session secret changes, the tokens cannot be read and Sylo makes new webhooks. +- **You can only bridge what you can see.** The person who asks for a link, and the one who uses a code, must + be able to view, read and send in their channel themselves; Edit access to the dashboard alone is not enough. +- **Deletes always follow.** A copy is removed even if the link is paused or stopped, when the delete comes + before the copy was posted, after a webhook was replaced (Sylo then deletes it as the bot, which needs Manage + Messages), and for a purge of many messages. After an unlink the message map is gone, so copies already + posted stay and a later delete of the original no longer reaches them. +- **File downloads share one budget** (4 at a time and 32 MB in flight for the whole Sylo): a file that does not + fit crosses as a link. ## Needs diff --git a/src/db/bridge.js b/src/db/bridge.js index 3af0da9f..de0afe8d 100644 --- a/src/db/bridge.js +++ b/src/db/bridge.js @@ -11,7 +11,9 @@ // paused_side 'a' or 'b' when status is paused: only that server may resume it // options_x JSON text, see normaliseOptions in modules/lib/bridgeText.js // hook_x_id / hook_x_token the webhook Sylo made in that channel. The token is a -// secret: it never leaves this table (the dashboard only gets "has a webhook") +// secret: it is stored sealed (AES-256-GCM, lib/seal.js) and never leaves +// this table (the dashboard only gets "has a webhook"). A token that cannot +// be opened reads as empty, and Sylo makes a new webhook. // requested_by_name / accepted_by_name display names for the audit trail, not user ids // bridge_codes a pending request: only the SHA-256 of the code is stored, it expires, and it is // used up by the first server that accepts it @@ -22,6 +24,8 @@ // GUILD_TABLES. import { createHash, randomBytes } from 'node:crypto'; import { prepare, registerPostgresBootstrap } from './driver.js'; +import { config } from '../config.js'; +import { isSealed, open, seal } from '../lib/seal.js'; registerPostgresBootstrap(` CREATE TABLE IF NOT EXISTS bridge_links ( @@ -99,7 +103,6 @@ const s = { `UPDATE bridge_links SET status = @status, paused_side = @pausedSide, fail_reason = @reason, failure_count = @failures, updated_at = @now WHERE id = @id` ), - setFailures: prepare('UPDATE bridge_links SET failure_count = @failures, updated_at = @now WHERE id = @id'), setOptionsA: prepare('UPDATE bridge_links SET options_a = @options, updated_at = @now WHERE id = @id'), setOptionsB: prepare('UPDATE bridge_links SET options_b = @options, updated_at = @now WHERE id = @id'), setHookA: prepare( @@ -151,6 +154,15 @@ const s = { // --- links ----------------------------------------------------------------------------------------- +// A webhook token is a bearer credential for the channel, so it is sealed in the table. A value stored +// before sealing (it has no v1. prefix) is read as it is. +const TOKEN_PURPOSE = 'bridge-webhook'; +const sealToken = (token) => (token ? seal(config.sessionSecret, TOKEN_PURPOSE, token) : ''); +const openToken = (stored) => { + if (!stored) return ''; + return isSealed(stored) ? (open(config.sessionSecret, TOKEN_PURPOSE, stored) ?? '') : stored; +}; + const parseJson = (text) => { try { const value = JSON.parse(text); @@ -176,9 +188,9 @@ export function linkFrom(row) { optionsA: parseJson(row.options_a), optionsB: parseJson(row.options_b), hookAId: row.hook_a_id || '', - hookAToken: row.hook_a_token || '', + hookAToken: openToken(row.hook_a_token), hookBId: row.hook_b_id || '', - hookBToken: row.hook_b_token || '', + hookBToken: openToken(row.hook_b_token), requestedBy: row.requested_by_name || '', acceptedBy: row.accepted_by_name || '', createdAt: Number(row.created_at), @@ -203,9 +215,9 @@ export async function createLink(link) { optionsA: JSON.stringify(link.optionsA ?? {}), optionsB: JSON.stringify(link.optionsB ?? {}), hookAId: link.hookA?.id ?? '', - hookAToken: link.hookA?.token ?? '', + hookAToken: sealToken(link.hookA?.token), hookBId: link.hookB?.id ?? '', - hookBToken: link.hookB?.token ?? '', + hookBToken: sealToken(link.hookB?.token), requestedBy: String(link.requestedBy ?? '').slice(0, 100), acceptedBy: String(link.acceptedBy ?? '').slice(0, 100), now: Date.now(), @@ -253,10 +265,6 @@ export async function setLinkStatus(id, { status, pausedSide = '', reason = '', }); } -/** Remember how many sends in a row failed (reset to 0 by a success). */ -export const setLinkFailures = (id, failures) => - s.setFailures.run({ id: Number(id), failures, now: Date.now() }); - export const setLinkOptions = (id, side, options) => (side === 'a' ? s.setOptionsA : s.setOptionsB).run({ id: Number(id), @@ -269,7 +277,7 @@ export const setLinkHook = (id, side, hook) => (side === 'a' ? s.setHookA : s.setHookB).run({ id: Number(id), id2: hook?.id ?? '', - token: hook?.token ?? '', + token: sealToken(hook?.token), now: Date.now(), }); diff --git a/src/lib/pigeon.js b/src/lib/pigeon.js index 100938a7..2b3a30c4 100644 --- a/src/lib/pigeon.js +++ b/src/lib/pigeon.js @@ -20,7 +20,8 @@ // dev-log notifications (src/lib/push.js notifyDevLogEvent), which are a dev-log sink, and a // warning or error from there would become a new dev-log entry and so another notification. // Only ensureRegistered(), which runs at start-up outside the sink, logs warnings and errors. -import { createCipheriv, createDecipheriv, hkdfSync, randomBytes, randomUUID } from 'node:crypto'; +import { randomUUID } from 'node:crypto'; +import { open, seal } from './seal.js'; import { log } from './log.js'; import { solvePowInWorker } from './pigeonPow.js'; @@ -88,35 +89,8 @@ async function readJson(res, cap) { } // --- sealing the credential ---------------------------------------------------------------- - -const sealKey = (secret) => Buffer.from(hkdfSync('sha256', String(secret), 'sylo', 'pigeon-credential', 32)); - -function seal(secret, plain) { - const iv = randomBytes(12); - const cipher = createCipheriv('aes-256-gcm', sealKey(secret), iv); - const data = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]); - return [ - 'v1', - iv.toString('base64url'), - cipher.getAuthTag().toString('base64url'), - data.toString('base64url'), - ].join('.'); -} - -/** The text a sealed value holds, or null when it cannot be opened (another secret, damage). */ -function open(secret, sealed) { - try { - const [version, iv, tag, data] = String(sealed).split('.'); - if (version !== 'v1') return null; - const decipher = createDecipheriv('aes-256-gcm', sealKey(secret), Buffer.from(iv, 'base64url')); - decipher.setAuthTag(Buffer.from(tag, 'base64url')); - return Buffer.concat([decipher.update(Buffer.from(data, 'base64url')), decipher.final()]).toString( - 'utf8' - ); - } catch { - return null; - } -} +// (the shared helper in lib/seal.js; the purpose keeps the key the one this credential has always had) +const PURPOSE = 'pigeon-credential'; // --- the client ------------------------------------------------------------------------------ @@ -181,7 +155,7 @@ export function createPigeonClient({ try { const [sealed, forUrl] = await Promise.all([store.get(SETTING_CREDENTIAL), store.get(SETTING_URL)]); if (!sealed || forUrl !== base) return null; - const credential = open(secret, sealed); + const credential = open(secret, PURPOSE, sealed); if (!credential) report(loud, 'warn', 'The stored Pigeon credential cannot be opened; registering again.'); return credential; @@ -224,7 +198,7 @@ export function createPigeonClient({ throw new PigeonFailure('rejected', 'malformed_credential'); } try { - await store.set(SETTING_CREDENTIAL, seal(secret, credential)); + await store.set(SETTING_CREDENTIAL, seal(secret, PURPOSE, credential)); await store.set(SETTING_URL, base); } catch { throw new PigeonFailure('rejected', 'storage_error'); diff --git a/src/lib/seal.js b/src/lib/seal.js new file mode 100644 index 00000000..e261645c --- /dev/null +++ b/src/lib/seal.js @@ -0,0 +1,44 @@ +// Seal a stored credential with AES-256-GCM, keyed from the instance's session secret (HKDF-SHA256, the +// `purpose` as the key's context, so one purpose's key never opens another's value). A sealed value reads +// `v1...`. A value that cannot be opened (the secret changed, the text was damaged) reads as +// null, and the caller makes a new credential instead of failing. +// +// Used by the Postal Pigeon credential (lib/pigeon.js) and the bridge's webhook tokens (db/bridge.js). +import { createCipheriv, createDecipheriv, hkdfSync, randomBytes } from 'node:crypto'; + +const keyFor = (secret, purpose) => Buffer.from(hkdfSync('sha256', String(secret), 'sylo', purpose, 32)); + +/** Is this text a value this module sealed? */ +export const isSealed = (value) => typeof value === 'string' && value.startsWith('v1.'); + +/** + * @param {string} secret the session secret + * @param {string} purpose what the value is for ('pigeon-credential', 'bridge-webhook') + * @param {string} plain + */ +export function seal(secret, purpose, plain) { + const iv = randomBytes(12); + const cipher = createCipheriv('aes-256-gcm', keyFor(secret, purpose), iv); + const data = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]); + return [ + 'v1', + iv.toString('base64url'), + cipher.getAuthTag().toString('base64url'), + data.toString('base64url'), + ].join('.'); +} + +/** The text a sealed value holds, or null when it cannot be opened (another secret or purpose, damage). */ +export function open(secret, purpose, sealed) { + try { + const [version, iv, tag, data] = String(sealed).split('.'); + if (version !== 'v1') return null; + const decipher = createDecipheriv('aes-256-gcm', keyFor(secret, purpose), Buffer.from(iv, 'base64url')); + decipher.setAuthTag(Buffer.from(tag, 'base64url')); + return Buffer.concat([decipher.update(Buffer.from(data, 'base64url')), decipher.final()]).toString( + 'utf8' + ); + } catch { + return null; + } +} diff --git a/src/modules/bridge.js b/src/modules/bridge.js index cf932f0d..c0e2d9cb 100644 --- a/src/modules/bridge.js +++ b/src/modules/bridge.js @@ -31,12 +31,14 @@ import { runtime } from '../runtime.js'; import { splitPlatformGuildId } from '../lib/platformId.js'; import { guildByKey } from '../platform/platformOf.js'; import { createWebhookTransport } from '../platform/webhooks.js'; -import { jumpUrl } from './quoteBoard.js'; +import { isAgeRestricted as ageOf, jumpUrl } from './quoteBoard.js'; import { safeFetch } from '../lib/safeFetch.js'; import { log } from '../lib/log.js'; +import { sweepMap } from '../lib/sweepMap.js'; import { BridgeQueueError, classifyError, createRelayQueue } from './lib/bridgeQueue.js'; import { FILE_BYTES_LIMIT, + escapeMarkdown, fileLinkLines, hashContent, ZWSP, @@ -80,11 +82,29 @@ let transport = createWebhookTransport(); let queue = createRelayQueue(); let download = (url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 }); -/** Use another transport, queue or downloader (tests); call with nothing to go back to the real ones. */ -export function useBridgeParts({ transport: t, queue: q, download: d } = {}) { +// One budget for every file download of the process, across all servers: files are held in memory until +// they are sent, so a few busy bridges must not add up to hundreds of megabytes. A file that does not fit in +// the budget crosses as a link instead of waiting for room. +const DEFAULT_BUDGET = Object.freeze({ concurrent: 4, bytes: 32 * 1024 * 1024 }); +let budget = { ...DEFAULT_BUDGET }; +const inUse = { concurrent: 0, bytes: 0 }; +function reserveDownload(size) { + if (inUse.concurrent >= budget.concurrent || inUse.bytes + size > budget.bytes) return false; + inUse.concurrent += 1; + inUse.bytes += size; + return true; +} +function releaseDownload(size) { + inUse.concurrent -= 1; + inUse.bytes -= size; +} + +/** Use other parts (tests): a transport, a queue, a downloader, a download budget. No arguments: the real ones. */ +export function useBridgeParts({ transport: t, queue: q, download: d, budget: b } = {}) { transport = t ?? createWebhookTransport(); queue = q ?? createRelayQueue(); download = d ?? ((url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 })); + budget = { ...DEFAULT_BUDGET, ...b }; } // Relays that are still on their way, so a test (and a shutdown) can wait for them. @@ -116,7 +136,33 @@ const TEXT_TYPES = new Set([ChannelType.GuildText, ChannelType.GuildAnnouncement const channelIn = (key, channelId) => guildByKey(key)?.channels?.cache?.get?.(channelId) ?? null; const isTextChannel = (channel) => Boolean(channel) && TEXT_TYPES.has(channel.type); -const isAgeRestricted = (channel) => channel?.nsfw === true; +/** + * Is this channel age-restricted, the way the platform decides it? On Fluxer that is the channel's own + * setting, else its category's, else the server's (and what cannot be read counts as restricted: + * quoteBoard.js, fluxerAgeRestricted). A Discord server that is itself marked age-restricted counts for + * every channel in it. + */ +function ageRestricted(key, channel) { + const guild = guildByKey(key); + if (platformOf(key) === 'discord' && guild?.nsfwLevel === 3) return true; + return ageOf(guild, channel); +} + +// Names of servers and channels in Sylo's own notices: no markdown, so a name cannot make a link. +const serverLabel = (key) => escapeMarkdown(inertAt(guildByKey(key)?.name ?? 'another server')); +const channelLabel = (key, channelId) => escapeMarkdown(channelIn(key, channelId)?.name ?? 'a channel'); + +// Messages that were deleted: a delete can win the race against the relay of the same message (moderation +// runs right after the relay starts), so the relay asks before it posts and again after. +const deletedSoon = new Map(); +const TOMBSTONE_MS = 2 * 60_000; +const markDeleted = (key, messageId) => { + if (deletedSoon.size > 20_000) deletedSoon.clear(); + deletedSoon.set(`${key}|${messageId}`, Date.now()); +}; +const wasDeleted = (key, messageId) => + Date.now() - (deletedSoon.get(`${key}|${messageId}`) ?? 0) < TOMBSTONE_MS; +sweepMap(deletedSoon, (at, now) => now - at > TOMBSTONE_MS); /** Cached lookups of "which link is this channel in", dropped whenever a link changes. */ const cache = new Map(); @@ -146,8 +192,8 @@ const channelName = (key, channelId) => channelIn(key, channelId)?.name ?? 'a ch /** The notice both channels show while a bridge is on. */ const onNotice = (mineKey, theirKey, theirChannelId) => - `🌉 **Bridge on.** Messages in this channel are shared with #${channelName(theirKey, theirChannelId)} in ` + - `**${serverName(theirKey)}** on ${platformName(theirKey)}, and theirs appear here. Nobody is pinged across ` + + `🌉 **Bridge on.** Messages in this channel are shared with #${channelLabel(theirKey, theirChannelId)} in ` + + `**${serverLabel(theirKey)}** on ${platformName(theirKey)}, and theirs appear here. Nobody is pinged across ` + `the bridge. Server admins can pause or unlink it at any time in the Sylo dashboard.`; const REASONS = { @@ -165,6 +211,34 @@ export const reasonText = (code) => REASONS[code] ?? 'The bridge stopped working // --- the pieces of a link, looked up on the platforms ------------------------------------------------ +/** Does `perms` lack `name`? No answer at all counts as lacking; a permission name the platform does not know does not. */ +function lacks(perms, name) { + if (!perms || typeof perms.has !== 'function') return true; + try { + return perms.has(name) !== true; + } catch { + return false; + } +} + +/** + * The person linking must be able to view, read and send in the channel themselves: Sylo does not let + * anyone make it do what they could not do (the bridge would carry a hidden channel to a server they run). + * `member` is their member in this server. Open mode (no logins) passes `open: true`. + */ +export function assertActorMayUse(key, channelId, member, { open = false } = {}) { + if (open) return; + const channel = channelIn(key, channelId); + const perms = member && channel?.permissionsFor?.(member); + const missing = ['ViewChannel', 'ReadMessageHistory', 'SendMessages'].filter((p) => lacks(perms, p)); + if (!perms || missing.length) { + throw new BridgeError( + `You need to be able to view, read and send in #${channel?.name ?? 'that channel'} yourself to bridge it.`, + 403 + ); + } +} + /** * Check a channel of a server can be bridged: Sylo sees it, it is a normal text channel, and Sylo may * view it, send in it and manage webhooks there. @@ -176,7 +250,8 @@ function checkChannel(key, channelId) { const channel = guild.channels?.cache?.get?.(channelId); if (!isTextChannel(channel)) throw new BridgeError('Pick a normal text channel of this server.'); const perms = channel.permissionsFor?.(guild.members?.me); - const missing = ['ViewChannel', 'SendMessages', 'ManageWebhooks'].filter((p) => perms?.has?.(p) === false); + // Sylo's own rights: when they cannot be read, they count as missing. + const missing = ['ViewChannel', 'SendMessages', 'ManageWebhooks'].filter((p) => lacks(perms, p)); if (missing.length) { throw new BridgeError( `Sylo needs ${missing.join(', ')} in #${channel.name}. Give Sylo's role those permissions there first.` @@ -201,8 +276,8 @@ function requireBothPlatforms() { } /** The age rule: an age-restricted channel only with an age-restricted channel. */ -function checkAge(a, b) { - if (isAgeRestricted(a) !== isAgeRestricted(b)) { +function checkAge(keyA, a, keyB, b) { + if (ageRestricted(keyA, a) !== ageRestricted(keyB, b)) { throw new BridgeError( 'An age-restricted channel can only be bridged with another age-restricted channel.' ); @@ -296,7 +371,7 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) if (await store.linkForChannel(request.guildId, request.channelId)) { throw new BridgeError('The other channel was bridged in the meantime.', 409); } - checkAge(mine.channel, theirs.channel); + checkAge(guildKey, mine.channel, request.guildId, theirs.channel); // A webhook in each channel, made before the code is claimed (the claim is the atomic step: if two servers // race, one loses and its webhooks are removed again). @@ -406,7 +481,7 @@ export async function pauseLink({ guildKey, id, actor }) { await say( theirKey, channelOf(link, other(side)), - `🌉 **Bridge paused** by ${serverName(guildKey)}. Nothing is shared until they resume it.` + `🌉 **Bridge paused** by ${serverLabel(guildKey)}. Nothing is shared until they resume it.` ); } @@ -426,7 +501,7 @@ export async function resumeLink({ guildKey, id, actor }) { await requireModule(link.guildB); const a = checkChannel(link.guildA, link.channelA); const b = checkChannel(link.guildB, link.channelB); - checkAge(a.channel, b.channel); + checkAge(link.guildA, a.channel, link.guildB, b.channel); for (const s of ['a', 'b']) { const key = guildOf(link, s); const hook = hookOf(link, s); @@ -462,7 +537,7 @@ export async function resumeLink({ guildKey, id, actor }) { /** Unlink: either side may, at any time. The webhooks are deleted and the message map with the link. */ export async function unlinkLink({ guildKey, id, actor }) { const { link, side } = await ownLink(guildKey, id); - await dismantle(link, `unlinked by ${serverName(guildKey)}`); + await dismantle(link, `unlinked by ${serverLabel(guildKey)}`); await recordAudit(guildKey, { actor, action: 'bridge:unlink', @@ -510,6 +585,9 @@ export async function bridgeServerLeft(key) { queue.clear(laneKey(link, 'a')); queue.clear(laneKey(link, 'b')); if (hook.id) await transport.deleteHook(guildOf(link, theirs), hook).catch(() => {}); + // The leaving server's own webhook goes too (a webhook can be deleted by its token without the bot). + const own = hookOf(link, side); + if (own.id) await transport.deleteHook(key, own).catch(() => {}); await say( guildOf(link, theirs), channelOf(link, theirs), @@ -557,7 +635,8 @@ function recreateHook(link, side) { /** Stop a link that cannot work, show why in Health, and say so in both channels. */ export async function failLink(linkId, reason) { const link = await store.getLink(linkId); - if (!link || link.status === 'failed') return; + // Only a running link is stopped: a link a server paused stays paused (and only that server resumes it). + if (!link || link.status !== 'active') return; await store.setLinkStatus(link.id, { status: 'failed', reason, failures: FAIL_THRESHOLD }); dropBridgeCache(); queue.clear(laneKey(link, 'a')); @@ -728,6 +807,11 @@ async function buildCopy(message, link, side, { guildKey }) { const files = []; const links = [...plan.links]; for (const a of plan.upload) { + // No room in the process-wide budget: a link, never a wait. + if (!reserveDownload(a.size)) { + links.push(a); + continue; + } try { const res = await download(a.url, FILE_BYTES_LIMIT); if (!res.ok || !res.body?.length) throw new Error(`HTTP ${res.status}`); @@ -735,6 +819,8 @@ async function buildCopy(message, link, side, { guildKey }) { } catch (err) { log.debug('bridge', `file left as a link (${err?.message ?? err})`); links.push(a); + } finally { + releaseDownload(a.size); } } @@ -744,9 +830,10 @@ async function buildCopy(message, link, side, { guildKey }) { answers: toList(message.poll.answers).map((a) => ({ text: a.text, emoji: a.emoji?.name })), }) : ''; - const tail = [...stickerLines(toList(message.stickers)), poll, ...fileLinkLines(links)] - .filter(Boolean) - .join('\n'); + // The sticker, poll and file names are text from the other side too: the same mention rules apply. + const tail = convert( + [...stickerLines(toList(message.stickers)), poll, ...fileLinkLines(links)].filter(Boolean).join('\n') + ); const content = joinParts([head, body, tail]); if (!content && !embeds.length && !files.length) return null; @@ -766,41 +853,85 @@ async function buildCopy(message, link, side, { guildKey }) { // --- relaying ---------------------------------------------------------------------------------------- -/** A webhook send that tries once more with a plain name when the platform refuses the sender's name. */ +/** + * A webhook send that gives way when the platform refuses it (a 400): first with a plain sender name (some + * names are refused), then without the files and embeds (the text still crosses). + */ async function sendCopy(dstKey, hook, payload) { try { return await transport.send(dstKey, hook, payload); } catch (err) { - if (classifyError(err).kind === 'bad-request' && payload.username) { + if (classifyError(err).kind !== 'bad-request') throw err; + const plain = { + ...payload, + username: `Bridge [${platformName(dstKey) === 'Discord' ? 'Fluxer' : 'Discord'}]`, + }; + try { + return await transport.send(dstKey, hook, plain); + } catch (err2) { + if (classifyError(err2).kind !== 'bad-request' || (!payload.files && !payload.embeds)) throw err2; + log.info('bridge', 'a message crossed without its files and embeds: the platform refused them'); return transport.send(dstKey, hook, { - ...payload, - username: `Bridge [${platformName(dstKey) === 'Discord' ? 'Fluxer' : 'Discord'}]`, + content: payload.content || '(a file or embed could not be copied)', + username: plain.username, + avatarUrl: payload.avatarUrl, }); } - throw err; } } -/** Run a transport call with the link's webhook, making a new webhook once if the platform says it is gone. */ +/** + * Run a transport call with the link's webhook, making a new webhook once if the platform says it is gone + * (or if the stored token cannot be read any more). Resolves `{ value, hook }`: the webhook that was used. + */ async function withHook(link, side, call) { let hook = hookOf(link, side); + if (!hook.id || !hook.token) hook = await recreateHook(link, side); try { - return await call(hook); + return { value: await call(hook), hook }; } catch (err) { if (classifyError(err).kind !== 'hook-gone') throw err; hook = await recreateHook(link, side); - return call(hook); + return { value: await call(hook), hook }; + } +} + +/** + * Remove a copy from the target channel: through the webhook that posted it, and when that cannot (the + * webhook was replaced or is gone) as the bot, which needs Manage Messages there. True when the copy is gone. + */ +async function removeCopy(link, target, copyId, copyHookId) { + const dstKey = guildOf(link, target); + const hook = hookOf(link, target); + if (hook.id && hook.token && (!copyHookId || copyHookId === hook.id)) { + try { + await transport.remove(dstKey, hook, copyId); + return true; + } catch (err) { + if (classifyError(err, 'delete').kind === 'message-gone') return true; + } + } + try { + await transport.botDelete(dstKey, channelOf(link, target), copyId); + return true; + } catch (err) { + if (classifyError(err, 'delete').kind === 'message-gone') return true; + log.debug('bridge', `a copy could not be removed (link ${link.id}): ${err?.message ?? err}`); + return false; } } /** Can a message in this channel be relayed at all right now? Loop, bot and age rules. */ async function allowed(message, link, side, key) { if (!isChatMessage(message)) return false; - // Never again what a bridge webhook posted (any link's), and never Sylo's own messages. - if (message.webhookId && (await store.linkForHook(message.webhookId))) return false; + // Webhook messages never cross, whatever the bots option says: they are what bridges (this one and any + // other) post, and relaying them is how two bridges on the same channels would feed each other forever. + if (message.webhookId) return false; if (message.author?.id && message.author.id === message.client?.user?.id) return false; - if (message.author?.bot || message.webhookId) { + if (message.author?.bot) { if (!optionsOf(link, side).bots) return false; + // A bot that already carries a platform tag in its name is a bridge's relay account. + if (SUFFIX.test(String(message.author.username ?? ''))) return false; } const theirKey = guildOf(link, other(side)); if (!(await isModuleEnabled(theirKey, 'bridge'))) return false; @@ -811,7 +942,7 @@ async function allowed(message, link, side, key) { await failLink(link.id, 'channel-missing'); return false; } - if (isAgeRestricted(mine) !== isAgeRestricted(theirs)) { + if (ageRestricted(key, mine) !== ageRestricted(theirKey, theirs)) { await failLink(link.id, 'age-restriction'); return false; } @@ -823,6 +954,7 @@ export async function handleMessage(message, _config, key) { const found = await findLink(key, message.channelId); if (!found || found.link.status !== 'active') return; const { link, side } = found; + if (wasDeleted(key, message.id)) return; if (!(await allowed(message, link, side, key))) return; const target = other(side); @@ -831,8 +963,9 @@ export async function handleMessage(message, _config, key) { const job = async () => { const current = await store.getLink(link.id); if (!current || current.status !== 'active') return null; + if (wasDeleted(key, message.id)) return null; built ??= await buildCopy(message, current, side, { guildKey: key }); - if (!built) return null; + if (!built || wasDeleted(key, message.id)) return null; const payload = { content: built.content || undefined, username: built.username, @@ -840,19 +973,24 @@ export async function handleMessage(message, _config, key) { embeds: built.embeds.length ? built.embeds : undefined, files: built.files.length ? built.files : undefined, }; - const copyId = await withHook(current, target, (hook) => sendCopy(dstKey, hook, payload)); + const { value: copyId, hook } = await withHook(current, target, (h) => sendCopy(dstKey, h, payload)); + if (failures.has(current.id)) failures.delete(current.id); + // Deleted while it was being sent (moderation can be quicker than the relay): take the copy down again. + if (wasDeleted(key, message.id)) { + await removeCopy(current, target, copyId, hook.id); + return copyId; + } try { await store.saveMapping(current.id, side, { sourceId: message.id, copyId, - hookId: hookOf(current, target).id, + hookId: hook.id, hash: hashContent(`${built.body}\n${JSON.stringify(built.embeds)}`), extra: { head: built.head.slice(0, 900), tail: built.tail.slice(0, 900) }, }); } catch (err) { log.warn('bridge', `could not remember a copy: ${err?.message ?? err}`); } - if (failures.has(current.id)) failures.delete(current.id); return copyId; }; track( @@ -874,15 +1012,17 @@ export async function handleUpdate({ new: updated }, _config, key) { const found = await findLink(key, message.channelId); if (!found || found.link.status !== 'active') return; const { link, side } = found; - const map = await store.mappingOf(link.id, side, message.id); - if (!map?.isOriginal) return; if (!(await allowed(message, link, side, key))) return; const target = other(side); const dstKey = guildOf(link, target); + // The map is read inside the job: the job waits behind the send of the same message in the same lane, so + // an edit that comes quickly still finds the copy. const job = async () => { const current = await store.getLink(link.id); if (!current || current.status !== 'active') return null; + const map = await store.mappingOf(current.id, side, message.id); + if (!map?.isOriginal) return null; const names = nameResolvers(message, guildByKey(key)); const find = emojiFinder(guildByKey(dstKey)); const convert = (text) => mapEmoji(neutraliseMentions(text, names), find); @@ -904,52 +1044,55 @@ export async function handleUpdate({ new: updated }, _config, key) { ); } -async function deleteCopyOf(link, side, messageId) { - const map = await store.mappingOf(link.id, side, messageId); - if (!map) return; +/** + * Take the copies of deleted messages down. A removal by a moderator must reach the other side, so this + * never depends on the state the link has right now: it runs while the link is paused or stopped, it looks + * the map up inside the job (behind the send of the same message in the same lane), it is not dropped for a + * full or slow lane, and when the webhook cannot do it the bot does. A purge of many messages is one job. + */ +async function deleteCopies(link, side, ids) { const target = other(side); - if (!map.isOriginal) { - // Someone removed a copy: forget it, and leave the original alone. - await store.deleteMapping(link.id, side, messageId); - return; - } - const dstKey = guildOf(link, target); + const sourceKey = guildOf(link, side); + for (const id of ids) markDeleted(sourceKey, id); const job = async () => { const current = await store.getLink(link.id); - if (!current || current.status !== 'active') return null; - const hook = hookOf(current, target); - if (hook.id && (!map.copyHookId || map.copyHookId === hook.id)) { - try { - await transport.remove(dstKey, hook, map.counterpartId); - } catch (err) { - if (classifyError(err, 'delete').kind !== 'message-gone') throw err; - } + if (!current) return null; + for (const id of ids) { + const map = await store.mappingOf(current.id, side, id); + if (!map) continue; + // Someone removed a copy: forget it, and leave the original alone. + if (map.isOriginal) await removeCopy(current, target, map.counterpartId, map.copyHookId); + await store.deleteMapping(current.id, side, id); } - await store.deleteMapping(current.id, side, messageId); - return map.counterpartId; + return ids.length; }; track( - queue.enqueue(laneKey(link, target), job, { op: 'delete' }).catch((err) => noteFailure(link, target, err)) + queue + .enqueue(laneKey(link, target), job, { op: 'delete' }) + .catch((err) => + log.debug('bridge', `a delete was not carried across (link ${link.id}): ${err?.message ?? err}`) + ) ); } /** `messageDelete`: the copy of a deleted message is deleted too. */ export async function handleDelete(message, _config, key) { if (!message?.channelId || !message.id) return; + // Not only on a running link: a removal while the link is paused or stopped still has to reach the copy. const found = await findLink(key, message.channelId); - if (!found || found.link.status !== 'active') return; - await deleteCopyOf(found.link, found.side, message.id); + if (!found) return; + await deleteCopies(found.link, found.side, [String(message.id)]); } /** `messageDeleteBulk`: the same for a purge of many messages. */ export async function handleBulkDelete({ messages, channel }, _config, key) { - const ids = typeof messages?.keys === 'function' ? [...messages.keys()] : []; + const ids = typeof messages?.keys === 'function' ? [...messages.keys()].map(String) : []; // Fluxer may not have the channel cached: the deleted messages carry its id as well. const channelId = channel?.id ?? [...(messages?.values?.() ?? [])][0]?.channelId; - if (!channelId) return; + if (!channelId || !ids.length) return; const found = await findLink(key, channelId); - if (!found || found.link.status !== 'active') return; - for (const id of ids) await deleteCopyOf(found.link, found.side, id); + if (!found) return; + await deleteCopies(found.link, found.side, ids); } /** `guildMemberAdd`: a join notice on the other side, for the links whose server turned it on. */ @@ -963,9 +1106,11 @@ export async function handleJoin(member, _config, key) { const target = other(side); const dstKey = guildOf(link, target); if (!(await isModuleEnabled(dstKey, 'bridge'))) continue; - const who = inertAt(String(member.displayName ?? member.user?.username ?? 'Someone')).slice(0, 80); + const who = escapeMarkdown( + inertAt(String(member.displayName ?? member.user?.username ?? 'Someone')).slice(0, 80) + ); const payload = { - content: `👋 **${who}** joined **${serverName(key)}**`, + content: `👋 **${who}** joined **${serverLabel(key)}**`, username: senderName(guildByKey(key)?.name ?? 'Server', platformOf(key), { forDiscord: platformOf(dstKey) === 'discord', }), @@ -977,7 +1122,7 @@ export async function handleJoin(member, _config, key) { async () => { const current = await store.getLink(link.id); if (!current || current.status !== 'active') return null; - return withHook(current, target, (hook) => sendCopy(dstKey, hook, payload)); + return (await withHook(current, target, (hook) => sendCopy(dstKey, hook, payload))).value; }, { op: 'send' } ) @@ -1008,7 +1153,7 @@ function endView(key, channelId) { platform: platformOf(key), server: guildByKey(key)?.name ?? 'Unknown server', channel: channel?.name ?? 'unknown channel', - ageRestricted: isAgeRestricted(channel), + ageRestricted: ageRestricted(key, channel), }; } @@ -1027,8 +1172,8 @@ export function linkView(link, key) { options: optionsOf(link, side), theirOptions: optionsOf(link, theirs), createdAt: link.createdAt, - requestedBy: link.requestedBy, - acceptedBy: link.acceptedBy, + // Only this server's own dashboard name: the other server's staff are not shown to it. + by: side === 'a' ? link.requestedBy : link.acceptedBy, queued: queue.stats(laneKey(link, theirs)).queued, dropped: queue.stats(laneKey(link, theirs)).dropped, }; @@ -1059,7 +1204,7 @@ export async function bridgeOverview(key) { channels: toList(guild?.channels?.cache) .filter(isTextChannel) .sort((x, y) => (x.rawPosition ?? 0) - (y.rawPosition ?? 0)) - .map((c) => ({ id: c.id, name: c.name, ageRestricted: isAgeRestricted(c), taken: taken.has(c.id) })), + .map((c) => ({ id: c.id, name: c.name, ageRestricted: ageRestricted(key, c), taken: taken.has(c.id) })), }; } diff --git a/src/modules/lib/bridgeQueue.js b/src/modules/lib/bridgeQueue.js index c51f74f3..3a8c76da 100644 --- a/src/modules/lib/bridgeQueue.js +++ b/src/modules/lib/bridgeQueue.js @@ -7,6 +7,9 @@ // the newest message is dropped (and counted) instead of the bridge growing without bound, and a job that // has waited longer than `maxAgeMs` is dropped as stale instead of arriving minutes late. // +// A delete is the exception to all three limits: a moderator's removal must reach the copy however busy the +// lane is, however long it waited, and even if the link was paused or stopped meanwhile (clear() keeps it). +// // What a failure MEANS for the link (pause it, recreate the webhook) is the engine's decision, in // modules/bridge.js; this file only classifies errors and decides whether to try again. @@ -30,10 +33,16 @@ const codeOf = (err, depth = 0) => { return err.code ?? err.rawError?.code ?? (depth < 2 ? codeOf(err.cause, depth + 1) : undefined); }; -/** How long the platform said to wait, in ms (it reports seconds or milliseconds, depending on the SDK). */ +/** + * How long the platform said to wait, in ms. The API's own `retry_after` field is always seconds. The SDKs' + * `retryAfter` property is not the same unit everywhere (discord.js reports milliseconds), so a small number + * there is read as seconds and a large one as milliseconds. + */ function retryAfterOf(err, depth = 0) { if (!err || typeof err !== 'object') return undefined; - const n = Number(err.retryAfter ?? err.retry_after ?? err.data?.retry_after ?? err.rawError?.retry_after); + const seconds = Number(err.retry_after ?? err.data?.retry_after ?? err.rawError?.retry_after); + if (Number.isFinite(seconds) && seconds > 0) return seconds * 1000; + const n = Number(err.retryAfter); if (Number.isFinite(n) && n > 0) return n <= 120 ? n * 1000 : n; return depth < 2 ? retryAfterOf(err.cause, depth + 1) : undefined; } @@ -88,7 +97,7 @@ export function createRelayQueue({ try { while (lane.tasks.length) { const task = lane.tasks[0]; - if (now() - task.at > maxAgeMs) { + if (!task.keep && now() - task.at > maxAgeMs) { lane.tasks.shift(); lane.dropped += 1; task.reject(new BridgeQueueError('expired')); @@ -131,12 +140,12 @@ export function createRelayQueue({ lane = { tasks: [], running: false, dropped: 0 }; lanes.set(key, lane); } - if (lane.tasks.length >= maxQueued) { + if (op !== 'delete' && lane.tasks.length >= maxQueued) { lane.dropped += 1; return Promise.reject(new BridgeQueueError('overflow')); } const result = new Promise((resolve, reject) => { - lane.tasks.push({ run, op, at: now(), attempts: 0, resolve, reject }); + lane.tasks.push({ run, op, keep: op === 'delete', at: now(), attempts: 0, resolve, reject }); }); if (!lane.running) void drain(key, lane); return result; @@ -150,10 +159,11 @@ export function createRelayQueue({ clear(key) { const lane = lanes.get(key); if (!lane) return 0; - // The job at the head may be running; leave it, drop the ones behind it. - const waiting = lane.running ? lane.tasks.splice(1) : lane.tasks.splice(0); - for (const t of waiting) t.reject(new BridgeQueueError('cleared')); - return waiting.length; + // The job at the head may be running, and a delete is never dropped: leave those, drop the rest. + const dropped = lane.tasks.filter((t, i) => !t.keep && !(lane.running && i === 0)); + lane.tasks = lane.tasks.filter((t) => !dropped.includes(t)); + for (const t of dropped) t.reject(new BridgeQueueError('cleared')); + return dropped.length; }, /** How many webhooks have a queue right now (for tests and the health page). */ lanes: () => lanes.size, diff --git a/src/modules/lib/bridgeText.js b/src/modules/lib/bridgeText.js index d22c0600..14e292a1 100644 --- a/src/modules/lib/bridgeText.js +++ b/src/modules/lib/bridgeText.js @@ -66,6 +66,12 @@ const stripControl = (text) => /** A name that shows an @ but cannot ping: the zero-width space after the @ breaks the mention. */ export const inertAt = (text) => String(text ?? '').replace(/@/g, `@${ZWSP}`); +/** + * Markdown out of a name that Sylo puts into its own text (a server, a channel, a member in a notice or a + * quote): a name like "x](https://evil.example)[y" must not turn into a link under Sylo's identity. + */ +export const escapeMarkdown = (text) => String(text ?? '').replace(/([\\*_~`|[\]()>#<])/g, '\\$1'); + /** * Replace the mentions in a message with plain text and break @everyone / @here. `names` looks up what * the other side should read for a user, a role or a channel; it may return nothing (then a neutral word). @@ -132,7 +138,7 @@ export function senderName(displayName, platform, { forDiscord = false } = {}) { /** The first line of a copy that answers another message: who, a short quote, and a link when it is mapped. */ export function replyHead({ name, snippet, url }) { - const who = inertAt(clip(stripControl(name) || 'someone', 40)); + const who = escapeMarkdown(inertAt(clip(stripControl(name) || 'someone', 40))); const text = clip(stripControl(String(snippet ?? '').replace(/\s+/g, ' ')).replace(/^>+\s*/, ''), 100); const label = url ? `[**${who}**](<${url}>)` : `**${who}**`; return text ? `> ↪ ${label}: ${text}` : `> ↪ ${label}`; diff --git a/src/platform/webhooks.js b/src/platform/webhooks.js index 28a53112..21185619 100644 --- a/src/platform/webhooks.js +++ b/src/platform/webhooks.js @@ -25,6 +25,8 @@ export const NO_MENTIONS = Object.freeze({ parse: [] }); * @property {(guildKey: string, hook: Hook, messageId: string, payload: object) => Promise} edit * @property {(guildKey: string, hook: Hook, messageId: string) => Promise} remove * @property {(guildKey: string, hook: Hook) => Promise} deleteHook best effort + * @property {(guildKey: string, channelId: string, messageId: string) => Promise} botDelete delete a message as the + * bot (needs Manage Messages): the way to remove a copy whose webhook is gone */ const platformOfKey = (key) => splitPlatformGuildId(key)?.platform ?? 'discord'; @@ -129,6 +131,14 @@ export function createWebhookTransport() { await (await discordHook(hook)).deleteMessage(messageId); }, + async botDelete(guildKey, channelId, messageId) { + const channel = guildOf(guildKey).channels?.cache?.get(channelId); + if (!channel?.messages?.fetch) + throw Object.assign(new Error('That channel cannot be read'), { code: 'NO_CHANNEL' }); + const message = await channel.messages.fetch(messageId); + await message.delete(); + }, + async deleteHook(guildKey, hook) { const platform = platformOfKey(guildKey); try { diff --git a/src/web/lib/permissionHealth.js b/src/web/lib/permissionHealth.js index 6725c7e7..dea04515 100644 --- a/src/web/lib/permissionHealth.js +++ b/src/web/lib/permissionHealth.js @@ -243,8 +243,15 @@ export const NEEDS = { post(b?.channelId, `The channel of ${b?.name || 'a starboard'}`, { field: 'channelId' }) ), // The bridge makes and uses a webhook in each bridged channel, and says things in it. + // Manage Messages is optional: it lets Sylo remove a copy as the bot when the webhook that posted it is gone. bridge: (_cfg, x) => - arr(x.channels).map((id) => post(id, 'A bridged channel', { embed: false, extra: ['ManageWebhooks'] })), + arr(x.channels).flatMap((id) => [ + post(id, 'A bridged channel', { embed: false, extra: ['ManageWebhooks'] }), + inChannel(id, 'A bridged channel (to remove copies when their webhook is gone)', ['ManageMessages'], { + severity: WARNING, + part: 'delete', + }), + ]), introductions: (cfg) => [ post(cfg.channelId, 'The introductions channel', { extra: ['ReadMessageHistory'], field: 'channelId' }), post(cfg.promptChannelId, 'The prompt channel', { optional: true, field: 'promptChannelId' }), diff --git a/src/web/routes/v2Bridge.js b/src/web/routes/v2Bridge.js index 822433dd..fe7f1198 100644 --- a/src/web/routes/v2Bridge.js +++ b/src/web/routes/v2Bridge.js @@ -7,13 +7,14 @@ // looked up through the server in the URL (a link names two servers, and a server may only see and change // its own side), so another server's link id is a 404 here. import { accessRouter } from '../middleware/access.js'; -import { currentUser } from '../middleware/auth.js'; +import { currentUser, userIdForGuild } from '../middleware/auth.js'; import { clientKey, platformActionLimit, rateLimit } from '../middleware/rateLimit.js'; import { asyncHandler } from '../lib/asyncHandler.js'; import { sweepMap } from '../../lib/sweepMap.js'; import { BridgeError, acceptLink, + assertActorMayUse, bridgeOverview, cancelRequest, pauseLink, @@ -71,6 +72,19 @@ const noteWrong = (req) => { const overview = (req) => bridgeOverview(req.guild.id); +// The person must be able to view, read and send in the channel themselves (Edit access alone is not enough: +// a bot-master may not see every channel, and the bridge would carry it to a server they run). +async function requireActorAccess(req, channelId) { + const user = currentUser(req); + if (user?.open) return; + const userId = userIdForGuild(req, req.guild.id); + const guild = req.guild; + const member = userId + ? (guild.members.cache.get(userId) ?? (await guild.members.fetch(userId).catch(() => null))) + : null; + assertActorMayUse(guild.id, channelId, member); +} + router.get( '/modules/bridge/config', asyncHandler(async (req, res) => { @@ -83,6 +97,7 @@ router.post( platformActionLimit, guarded(async (req, res) => { if (!isId(req.body?.channelId)) throw new BridgeError('Pick a channel.'); + await requireActorAccess(req, String(req.body.channelId)); const made = await requestLink({ guildKey: req.guild.id, channelId: String(req.body.channelId), @@ -111,6 +126,7 @@ router.post( throw new BridgeError('Too many wrong codes. Wait a few minutes before trying again.', 429); } if (!isId(req.body?.channelId)) throw new BridgeError('Pick a channel.'); + await requireActorAccess(req, String(req.body.channelId)); try { await acceptLink({ guildKey: req.guild.id, @@ -143,6 +159,7 @@ router.post( router.post( '/modules/bridge/links/:id/pause', + platformActionLimit, guarded(async (req, res) => { await pauseLink({ guildKey: req.guild.id, id: linkId(req), actor: actor(req) }); res.json({ overview: await overview(req) }); diff --git a/test/bridge.db.test.js b/test/bridge.db.test.js index 54df53d2..efc5d170 100644 --- a/test/bridge.db.test.js +++ b/test/bridge.db.test.js @@ -379,3 +379,53 @@ test('deleting a link removes its map; a server leaving removes every link it is await purgeGuild(D); assert.equal(db.prepare('SELECT COUNT(*) AS n FROM bridge_codes WHERE guild_id = ?').get(D).n, 0); }); + +test('a webhook token is stored sealed and read back whole; a legacy plain value and an unreadable one are handled', async () => { + const { seal } = await import('../src/lib/seal.js'); + const id = await createLink(link()); + const row = db.prepare('SELECT hook_a_token, hook_b_token FROM bridge_links WHERE id = ?').get(id); + for (const [stored, token] of [ + [row.hook_a_token, 'secret-a'], + [row.hook_b_token, 'secret-b'], + ]) { + assert.ok(stored.startsWith('v1.')); + assert.ok(!stored.includes(token), 'the plain token is not in the column'); + } + const read = await getLink(id); + assert.deepEqual([read.hookAToken, read.hookBToken], ['secret-a', 'secret-b']); + + await setLinkHook(id, 'a', { id: 'hook-new', token: 'rotated' }); + assert.ok( + db.prepare('SELECT hook_a_token FROM bridge_links WHERE id = ?').get(id).hook_a_token.startsWith('v1.') + ); + assert.equal((await getLink(id)).hookAToken, 'rotated'); + + db.prepare('UPDATE bridge_links SET hook_a_token = ? WHERE id = ?').run('legacy-plain-token', id); + assert.equal( + (await getLink(id)).hookAToken, + 'legacy-plain-token', + 'a value from before sealing reads as it is' + ); + db.prepare('UPDATE bridge_links SET hook_a_token = ? WHERE id = ?').run( + seal('some other secret', 'bridge-webhook', 'x'), + id + ); + assert.equal((await getLink(id)).hookAToken, '', 'sealed with another secret: empty, not a crash'); + db.prepare('UPDATE bridge_links SET hook_a_token = ? WHERE id = ?').run( + seal(process.env.SESSION_SECRET ?? 'x', 'pigeon-credential', 'x'), + id + ); + assert.equal((await getLink(id)).hookAToken, '', 'another purpose never opens it'); +}); + +test('the shared seal helper keeps the Pigeon credential format (v1.iv.tag.data) and rejects damage', async () => { + const { seal, open, isSealed } = await import('../src/lib/seal.js'); + const sealed = seal('secret', 'pigeon-credential', 'credential'); + assert.match(sealed, /^v1\.[\w-]+\.[\w-]+\.[\w-]+$/); + assert.ok(isSealed(sealed) && !isSealed('plain') && !isSealed(null)); + assert.equal(open('secret', 'pigeon-credential', sealed), 'credential'); + assert.equal(open('other', 'pigeon-credential', sealed), null); + assert.equal(open('secret', 'bridge-webhook', sealed), null); + assert.equal(open('secret', 'pigeon-credential', `${sealed}x`), null); + assert.equal(open('secret', 'pigeon-credential', 'v1.a.b'), null); +}); diff --git a/test/bridge.engine.test.js b/test/bridge.engine.test.js index be9947b9..24ba7ff1 100644 --- a/test/bridge.engine.test.js +++ b/test/bridge.engine.test.js @@ -9,10 +9,15 @@ import { setGuildModule } from '../src/db/modules.js'; import { purgeGuild } from '../src/db/purge.js'; import { listAudit } from '../src/db/audit.js'; import * as store from '../src/db/bridge.js'; +import { db } from '../src/db/index.js'; +import { config } from '../src/config.js'; +import { seal } from '../src/lib/seal.js'; import { BridgeError, FAIL_THRESHOLD, acceptLink, + assertActorMayUse, + failLink, bridgeIdle, bridgeProblems, cancelRequest, @@ -466,7 +471,7 @@ test('bots are off by default and only cross when that side turns them on', asyn await setSideOptions({ guildKey: D, id: link.id, options: { bots: true }, actor: 'A' }); await relay(bot(), D); await relay(foreignHook(), D); - assert.equal(fakes.transport.sends.length, 2); + assert.equal(fakes.transport.sends.length, 1, 'a bot account crosses; a webhook message never does'); assert.equal(fakes.transport.sends[0].payload.username, 'RoleBot [Discord]'); }); @@ -1012,8 +1017,12 @@ test('when Sylo is removed from a server, the server on the other end loses its await purgeGuild(F); assert.equal(await store.getLink(link.id), null); assert.deepEqual( - fakes.transport.deleted.map((d) => [d.guildKey, d.hook.id]), - [[D, link.hookAId]] + fakes.transport.deleted.map((d) => [d.guildKey, d.hook.id]).sort(), + [ + [D, link.hookAId], + [F, link.hookBId], + ].sort(), + "the other side's webhook, and the leaving server's own" ); assert.match(say(dCh).at(-1), /the other server removed Sylo/); assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:unlink')); @@ -1043,3 +1052,522 @@ test('a bulk delete finds the channel from the messages when the platform did no ); assert.ok(dCh); }); + +// --- round 2: age restriction on Fluxer, the person linking, deletes, loops, sealed tokens, budgets --------- + +const waitFor = () => { + const gate = {}; + gate.open = new Promise((resolve) => { + gate.release = resolve; + }); + return gate; +}; + +test('Fluxer age restriction counts the category, the server and what cannot be read, not only the own flag', async () => { + const dCh = p.channel(p.dGuild, { name: 'ordinary-discord' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + const accept = (channel) => () => + acceptLink({ guildKey: F, channelId: channel.id, code: made.code, actor: 'B' }); + const tooStrict = /age-restricted channel can only be bridged with another age-restricted/; + + const category = p.channel(p.fGuild, { type: 4, nsfwOverride: true }); + const inheriting = p.channel(p.fGuild, { parentId: category.id }); // nsfw false, nsfwOverride null: inherits + await refuses(accept(inheriting)(), tooStrict); + + p.fGuild.nsfw = true; // the whole Fluxer server is age-restricted + const inServer = p.channel(p.fGuild, {}); + await refuses(accept(inServer)(), tooStrict); + p.fGuild.nsfw = false; + + const unreadable = p.channel(p.fGuild, {}); + unreadable.nsfwOverride = undefined; // the field is missing: cannot tell, so restricted + await refuses(accept(unreadable)(), tooStrict); + const orphan = p.channel(p.fGuild, { parentId: snowflake() }); // its category is unknown + await refuses(accept(orphan)(), tooStrict); + assert.equal(fakes.transport.hooks.length, 0, 'refused before any webhook was made'); + + const plain = p.channel(p.fGuild, {}); + assert.ok(await accept(plain)(), 'a channel that is clearly ordinary links'); +}); + +test('an age-restricted Fluxer category is refused for an ordinary Discord channel, and the same the other way round', async () => { + const category = p.channel(p.fGuild, { type: 4, nsfwOverride: true }); + const fCh = p.channel(p.fGuild, { parentId: category.id }); + const dNsfw = p.channel(p.dGuild, { nsfw: true }); + const dPlain = p.channel(p.dGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dPlain.id, actor: 'A' }); + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /age-restricted/ + ); + const made2 = await requestLink({ guildKey: D, channelId: dNsfw.id, actor: 'A' }); + assert.ok( + await acceptLink({ guildKey: F, channelId: fCh.id, code: made2.code, actor: 'B' }), + 'restricted with restricted' + ); +}); + +test('a Discord server that is itself age-restricted counts for all its channels', async () => { + p.dGuild.nsfwLevel = 3; + const dCh = p.channel(p.dGuild, {}); + const fCh = p.channel(p.fGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /age-restricted/ + ); +}); + +test('a category that turns age-restricted later stops the link on the next message, and resume re-checks', async () => { + const category = p.channel(p.fGuild, { type: 4, nsfwOverride: false }); + const dCh = p.channel(p.dGuild, {}); + const fCh = p.channel(p.fGuild, { parentId: category.id }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + const link = await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }); + await relay(fromD(dCh, { content: 'fine' }), D); + assert.equal(fakes.transport.sends.length, 1); + + category.nsfwOverride = true; + await relay(fromD(dCh, { content: 'now what' }), D); + await relay(fromF(fCh, { content: 'and back' }), F); + assert.equal(fakes.transport.sends.length, 1, 'nothing crossed after the change'); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'age-restriction']); + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /age-restricted/); + category.nsfwOverride = false; + await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.equal((await store.getLink(link.id)).status, 'active'); +}); + +test('the person linking must be able to view, read and send in the channel; open mode needs no check', async () => { + const hidden = p.channel(p.dGuild, { name: 'staff' }); + const lacking = (denied) => ({ id: 'someone', denied }); + hidden.permissionsFor = (member) => ({ has: (perm) => !(member?.denied ?? []).includes(perm) }); + assert.doesNotThrow(() => assertActorMayUse(D, hidden.id, lacking([]))); + for (const perm of ['ViewChannel', 'ReadMessageHistory', 'SendMessages']) { + assert.throws( + () => assertActorMayUse(D, hidden.id, lacking([perm])), + (e) => e instanceof BridgeError && e.status === 403 && /#staff/.test(e.message), + perm + ); + } + assert.throws(() => assertActorMayUse(D, hidden.id, null), BridgeError, 'no member found: refused'); + assert.throws(() => assertActorMayUse(D, snowflake(), lacking([])), BridgeError, 'no such channel'); + assert.doesNotThrow( + () => assertActorMayUse(D, hidden.id, null, { open: true }), + 'open mode: everyone is the admin' + ); +}); + +test("Sylo's own rights that cannot be read count as missing", async () => { + const dCh = p.channel(p.dGuild, {}); + dCh.permissionsFor = undefined; + await refuses( + requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }), + /Sylo needs ViewChannel, SendMessages, ManageWebhooks/ + ); + dCh.permissionsFor = () => null; + await refuses(requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }), /Sylo needs/); +}); + +// deletes + +/** A webhook send that waits at a gate, so a delete or an edit can arrive while the relay is in progress. */ +function slowDownloads(gate) { + useBridgeParts({ + transport: fakes.transport, + queue: fakes.queue, + download: async () => { + await gate.open; + return { ok: true, status: 200, body: Buffer.from('FILE') }; + }, + }); +} +const withFile = (over = {}) => ({ + attachments: new Map([['a', { name: 'cat.png', url: 'https://cdn/cat.png', size: 4 }]]), + ...over, +}); + +test('a delete that comes while the copy is still being made leaves no copy behind', async () => { + const { dCh } = await bridged(); + const gate = waitFor(); + slowDownloads(gate); + const m = fromD(dCh, withFile({ content: 'about to be removed by automod' })); + await handleMessage(m, {}, D); // the relay is queued and waits for the download + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + gate.release(); + await bridgeIdle(); + const standing = + fakes.transport.sends.length - fakes.transport.removes.length - fakes.transport.botDeletes.length; + assert.equal(standing, 0, 'no copy of the deleted message is left on the other side'); +}); + +test('a delete that comes before the relay has even started stops the relay', async () => { + const { dCh } = await bridged(); + const m = fromD(dCh, { content: 'gone first' }); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + await relay(m, D); + assert.equal(fakes.transport.sends.length, 0); +}); + +test('a copy posted while its original is deleted is taken down again', async () => { + const { dCh } = await bridged(); + const gate = waitFor(); + const realSend = fakes.transport.send; + fakes.transport.send = async (...args) => { + await gate.open; // the delete lands while the send is in flight + return realSend(...args); + }; + const m = fromD(dCh, { content: 'raced' }); + await handleMessage(m, {}, D); + await new Promise((r) => setImmediate(r)); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + gate.release(); + await bridgeIdle(); + assert.equal(fakes.transport.sends.length, 1); + assert.equal(fakes.transport.removes.length, 1, 'removed as soon as it was posted'); + assert.equal(fakes.transport.removes[0].messageId, fakes.transport.sends[0].id); +}); + +test('a delete reaches the copy while the link is paused or stopped', async () => { + const { dCh, link } = await bridged(); + const one = fromD(dCh, { content: 'one' }); + const two = fromD(dCh, { content: 'two' }); + await relay(one, D); + await relay(two, D); + await pauseLink({ guildKey: D, id: link.id, actor: 'A' }); + await handleDelete({ id: one.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.removes.map((r) => r.messageId), + [fakes.transport.sends[0].id], + 'paused' + ); + await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + await failLink(link.id, 'permissions'); + await handleDelete({ id: two.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length, 2, 'stopped'); + assert.equal(await store.mappingOf(link.id, 'a', two.id), null); +}); + +test('the webhook that really posted a copy is the one remembered, so a delete after a recreation works', async () => { + const { dCh, link } = await bridged(); + fakes.transport.sendFailures.push(apiError(404, { code: 10015 })); // the webhook was deleted: remade + const m = fromD(dCh, { content: 'posted by the new webhook' }); + await relay(m, D); + const row = await store.getLink(link.id); + assert.notEqual(row.hookBId, link.hookBId); + assert.equal( + (await store.mappingOf(link.id, 'a', m.id)).copyHookId, + row.hookBId, + 'the id of the webhook that sent it' + ); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length, 1); + assert.equal(fakes.transport.botDeletes.length, 0, 'the webhook did it'); +}); + +test('when the webhook cannot delete a copy (it was replaced, or is gone) the bot does', async () => { + const { dCh, fCh, link } = await bridged(); + const m1 = fromD(dCh, { content: 'one' }); + const m2 = fromD(dCh, { content: 'two' }); + const m3 = fromD(dCh, { content: 'three' }); + for (const m of [m1, m2, m3]) await relay(m, D); + const copies = fakes.transport.sends.map((s) => s.id); + // m1: its copy was posted by a webhook that has since been replaced + db.prepare('UPDATE bridge_messages SET copy_hook_id = ? WHERE a_message_id = ?').run( + 'an-older-webhook', + m1.id + ); + // m2: the webhook is gone when the delete is tried + fakes.transport.removeFailures.push(apiError(404, { code: 10015 })); + await handleDelete({ id: m1.id, channelId: dCh.id }, {}, D); + await handleDelete({ id: m2.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.botDeletes.map((d) => [d.guildKey, d.channelId, d.messageId]), + [ + [F, fCh.id, copies[0]], + [F, fCh.id, copies[1]], + ] + ); + // m3: nothing can delete it (no Manage Messages): the failure is quiet and the link keeps running + fakes.transport.removeFailures.push(apiError(404, { code: 10015 })); + fakes.transport.botDeleteFailures.push(apiError(403)); + await handleDelete({ id: m3.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.equal((await store.getLink(link.id)).status, 'active', 'a delete that fails never stops the link'); +}); + +test('a purge of 100 messages on a busy lane removes every copy; deletes are not dropped for a full lane', async () => { + const gate = waitFor(); + fakes = useFakes({ queue: createRelayQueue({ minGapMs: 0, maxQueued: 2, sleep: async () => {} }) }); + const realSend = fakes.transport.send; + fakes.transport.send = async (...args) => { + await gate.open; + return realSend(...args); + }; + const { dCh, link } = await bridged(); + const ids = Array.from({ length: 100 }, (_, i) => `9000000000000${String(1000 + i)}`); + for (const [i, id] of ids.entries()) + await store.saveMapping(link.id, 'a', { sourceId: id, copyId: `c${i}`, hookId: link.hookBId }); + for (let i = 0; i < 4; i += 1) await handleMessage(fromD(dCh, { content: `busy ${i}` }), {}, D); // the lane is full (cap 2) + await handleBulkDelete({ messages: new Map(ids.map((id) => [id, { id }])), channel: dCh }, {}, D); + gate.release(); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length, 100); + assert.equal( + await store.mappingCount(link.id), + 2, + 'only the two busy messages that fit are left in the map' + ); +}); + +test('a delete is not dropped for waiting too long', async () => { + let clock = 1_000_000; + fakes = useFakes({ + queue: createRelayQueue({ + minGapMs: 0, + maxAgeMs: 1000, + now: () => clock, + sleep: async () => { + clock += 5000; + }, + }), + }); + const gate = waitFor(); + const realSend = fakes.transport.send; + fakes.transport.send = async (...args) => { + await gate.open; + return realSend(...args); + }; + const { dCh, link } = await bridged(); + await store.saveMapping(link.id, 'a', { + sourceId: '900000000000009001', + copyId: 'old-copy', + hookId: link.hookBId, + }); + await handleMessage(fromD(dCh, { content: 'blocker' }), {}, D); + await handleDelete({ id: '900000000000009001', channelId: dCh.id }, {}, D); + clock += 60_000; // far past the age at which an ordinary job is dropped + gate.release(); + await bridgeIdle(); + assert.deepEqual( + fakes.transport.removes.map((r) => r.messageId), + ['old-copy'] + ); +}); + +test('an edit that comes while the copy is still being made reaches it', async () => { + const { dCh } = await bridged(); + const gate = waitFor(); + slowDownloads(gate); + const m = fromD(dCh, withFile({ content: 'first' })); + await handleMessage(m, {}, D); + await handleUpdate({ old: m, new: { ...m, content: 'second' } }, {}, D); + gate.release(); + await bridgeIdle(); + assert.equal(fakes.transport.sends.length, 1); + assert.equal(fakes.transport.edits.length, 1); + assert.equal(fakes.transport.edits[0].payload.content, 'second'); +}); + +// loops with other bridges + +test('a webhook message is never relayed, even with bots on, so another bridge on the same channels cannot make a loop', async () => { + const { dCh, fCh } = await bridged({ optionsD: { bots: true }, optionsF: { bots: true } }); + // Another bridge relays Alice's message into the Discord channel through its own webhook + await relay( + fromD(dCh, { + content: 'echo', + webhookId: snowflake(), + author: { bot: true, username: 'Alice [Fluxer]' }, + member: null, + }), + D + ); + await relay( + fromF(fCh, { + content: 'echo', + webhookId: snowflake(), + author: { bot: true, username: 'Bob [Discord]' }, + member: null, + }), + F + ); + // and a bot account that already wears a platform tag (a bridge's relay account) + await relay( + fromD(dCh, { content: 'echo', author: { bot: true, username: 'Alice [Fluxer]' }, member: null }), + D + ); + assert.equal(fakes.transport.sends.length, 0); + // an ordinary bot account still crosses + await relay(fromD(dCh, { content: 'real', author: { bot: true, username: 'Deploy' }, member: null }), D); + assert.equal(fakes.transport.sends.length, 1); +}); + +// sealed tokens + +test('webhook tokens are stored sealed; a token that cannot be opened leads to a new webhook, not a failure', async () => { + const { dCh, link } = await bridged(); + const stored = db.prepare('SELECT hook_a_token, hook_b_token FROM bridge_links WHERE id = ?').get(link.id); + for (const [column, token] of [ + [stored.hook_a_token, link.hookAToken], + [stored.hook_b_token, link.hookBToken], + ]) { + assert.ok(token.startsWith('tok-'), 'the code sees the real token'); + assert.ok(column.startsWith('v1.') && !column.includes(token), 'the table holds a sealed value'); + } + await relay(fromD(dCh, { content: 'works' }), D); + assert.equal(fakes.transport.sends.length, 1); + + // the session secret changed since: the stored value cannot be opened + db.prepare('UPDATE bridge_links SET hook_b_token = ? WHERE id = ?').run( + seal('another secret', 'bridge-webhook', 'tok-x'), + link.id + ); + dropBridgeCache(); + assert.equal((await store.getLink(link.id)).hookBToken, ''); + const before = fakes.transport.hooks.length; + await relay(fromD(dCh, { content: 'after the secret changed' }), D); + assert.equal(fakes.transport.hooks.length, before + 1, 'a new webhook was made'); + assert.equal(fakes.transport.sends.at(-1).payload.content, 'after the secret changed'); + assert.ok(fakes.transport.sends.at(-1).hook.token, 'sent with the new token'); + assert.equal((await store.getLink(link.id)).status, 'active'); + assert.ok(config.sessionSecret); +}); + +// download budget + +test('files that do not fit the process-wide download budget cross as links instead of waiting', async () => { + const gate = waitFor(); + fakes = useFakes({ files: {} }); + useBridgeParts({ + transport: fakes.transport, + queue: fakes.queue, + budget: { concurrent: 1, bytes: 1024 }, + download: async (url) => { + fakes.downloads.push({ url }); + await gate.open; + return { ok: true, status: 200, body: Buffer.from('FILE') }; + }, + }); + const one = await bridged(); + const other = p.extra('95'); + const otherF = `fluxer:${other.fGuild.id}`; + await setGuildModule(other.dGuild.id, 'bridge', { enabled: true }); + await setGuildModule(otherF, 'bridge', { enabled: true }); + const dCh2 = p.channel(other.dGuild, {}); + const fCh2 = p.channel(other.fGuild, {}); + const made = await requestLink({ guildKey: other.dGuild.id, channelId: dCh2.id, actor: 'A' }); + await acceptLink({ guildKey: otherF, channelId: fCh2.id, code: made.code, actor: 'B' }); + + const file = (name, size = 4) => ({ + attachments: new Map([['a', { name, url: `https://cdn/${name}`, size }]]), + }); + await handleMessage(fromD(one.dCh, file('first.png')), {}, D); // takes the only slot and waits + await new Promise((r) => setImmediate(r)); + await handleMessage(fromD(dCh2, file('second.png')), {}, other.dGuild.id); // another server: no room + await new Promise((r) => setImmediate(r)); + assert.deepEqual( + fakes.downloads.map((d) => d.url), + ['https://cdn/first.png'], + 'the second never started a download' + ); + const second = fakes.transport.sends.find((s) => s.payload.content?.includes('second.png')); + assert.equal(second.payload.content, '📎 second.png: https://cdn/second.png', 'it went as a link at once'); + assert.equal(second.payload.files, undefined); + gate.release(); + await bridgeIdle(); + const first = fakes.transport.sends.find((s) => s.payload.files?.length); + assert.equal(first.payload.files[0].name, 'first.png'); + // and a file larger than the whole byte budget is a link too + gate.release(); + await relay(fromD(one.dCh, file('huge.png', 2048)), D); + assert.ok(fakes.transport.sends.at(-1).payload.content.includes('huge.png: https://cdn/huge.png')); +}); + +// minor findings + +test('names that Sylo puts in its own text cannot carry markdown links', async () => { + p.fGuild.name = 'x](https://evil.example)[y'; + const { dCh } = await bridged(); + const notice = say(dCh)[0]; + assert.ok(!notice.includes('](https://evil.example)'), notice); + assert.ok(notice.includes('x\\]\\(https://evil.example\\)\\[y')); + await handleJoin({ displayName: 'a](https://evil.example)[b', user: { bot: false } }, {}, F); + const { link } = { link: (await store.linksOfGuild(F))[0] }; + await setSideOptions({ guildKey: F, id: link.id, options: { joinMessages: true }, actor: 'B' }); + await handleJoin({ displayName: 'a](https://evil.example)[b', user: { bot: false } }, {}, F); + await bridgeIdle(); + const join = fakes.transport.sends.at(-1).payload.content; + assert.ok(!join.includes('](https://evil.example)'), join); +}); + +test('sticker, poll and file names get the same mention rules as the text', async () => { + const { dCh } = await bridged(); + await relay( + fromD(dCh, { + content: 'x', + stickers: new Map([['s', { name: '@everyone', url: 'https://cdn/s.png' }]]), + poll: { + question: { text: '@here vote <@123456789012345678>' }, + answers: new Map([[1, { text: '@everyone' }]]), + }, + attachments: new Map([ + ['a', { name: '@everyone.zip', url: 'https://cdn/a.zip', size: 99 * 1024 * 1024 }], + ]), + }), + D + ); + const text = fakes.transport.sends[0].payload.content; + assert.ok(!/@everyone|@here|<@\d/.test(text), text); +}); + +test('a link a server paused is not turned into a stopped one by a late failure', async () => { + const { link } = await bridged(); + await pauseLink({ guildKey: D, id: link.id, actor: 'A' }); + await failLink(link.id, 'unreachable'); + const row = await store.getLink(link.id); + assert.deepEqual( + [row.status, row.pausedSide], + ['paused', 'a'], + 'still paused by Discord, so only Discord can resume it' + ); + await refuses(resumeLink({ guildKey: F, id: link.id, actor: 'B' }), /only they can resume/, 403); +}); + +test('the dashboard names only your own side, not the other server staff', async () => { + const { link } = await bridged(); + assert.equal(linkView(await store.getLink(link.id), D).by, 'Alice (dashboard)'); + assert.equal(linkView(await store.getLink(link.id), F).by, 'Bob (dashboard)'); + const text = JSON.stringify(linkView(await store.getLink(link.id), D)); + assert.ok(!text.includes('Bob (dashboard)')); +}); + +test('a platform that refuses a message gets a plain name, then the text without files and embeds', async () => { + const { dCh } = await bridged({ optionsD: { bots: true } }); + fakes.transport.sendFailures.push(apiError(400)); + await relay( + fromD(dCh, { content: 'name refused', member: null, author: { bot: true, username: 'Weird' } }), + D + ); + assert.equal(fakes.transport.sends[0].payload.username, 'Bridge [Discord]'); + + fakes.transport.sendFailures.push(apiError(400), apiError(400)); + await relay( + fromD(dCh, { + content: 'text survives', + member: null, + author: { bot: true, username: 'Deploy' }, + embeds: [{ type: 'rich', title: 'big embed' }], + }), + D + ); + const last = fakes.transport.sends.at(-1).payload; + assert.equal(last.content, 'text survives'); + assert.equal(last.embeds, undefined); + assert.equal(last.files, undefined); +}); diff --git a/test/bridgeQueue.test.js b/test/bridgeQueue.test.js index 75e46b6b..c6b9fc53 100644 --- a/test/bridgeQueue.test.js +++ b/test/bridgeQueue.test.js @@ -23,6 +23,12 @@ test('a failed webhook call is classified by what it means for the link', () => 'no hint: two seconds' ); assert.equal(classifyError(err({ status: 429, retryAfter: 9999 })).retryAfterMs, 9999); + assert.equal( + classifyError(err({ status: 429, retry_after: 100 })).retryAfterMs, + 60000, + 'the API field is always seconds' + ); + assert.equal(classifyError(err({ status: 429, rawError: { retry_after: 0.1 } })).retryAfterMs, 250); assert.equal( classifyError(err({ status: 429, retryAfter: 600000 })).retryAfterMs, 60000, @@ -194,6 +200,26 @@ test('a job that waited too long is dropped instead of arriving late', async () await expired; }); +test('a delete is never dropped: not for a full lane, not for waiting too long, not by clear()', async () => { + const { queue, tick } = harness({ maxQueued: 1, maxAgeMs: 1000 }); + const gate = {}; + gate.open = new Promise((r) => (gate.release = r)); + const running = queue.enqueue('h', () => gate.open.then(() => 'ran')); + const del1 = queue.enqueue('h', async () => 'del1', { op: 'delete' }); + const del2 = queue.enqueue('h', async () => 'del2', { op: 'delete' }); + const edit = queue.enqueue('h', async () => 'edit', { op: 'edit' }); + await assert.rejects( + edit, + (e) => e.reason === 'overflow', + 'an ordinary job is turned away from a full lane' + ); + assert.equal(queue.stats('h').queued, 3, 'the deletes were accepted'); + assert.equal(queue.clear('h'), 0, 'clear() leaves the deletes'); + tick(60_000); + gate.release(); + assert.deepEqual(await Promise.all([running, del1, del2]), ['ran', 'del1', 'del2']); +}); + test('clearing a lane drops what waits (a paused or unlinked bridge) but lets the running job finish', async () => { const { queue } = harness(); const gate = {}; diff --git a/test/bridgeText.test.js b/test/bridgeText.test.js index c029f1fa..949ae3c0 100644 --- a/test/bridgeText.test.js +++ b/test/bridgeText.test.js @@ -10,6 +10,7 @@ import { TOTAL_BYTES_LIMIT, ZWSP, clip, + escapeMarkdown, fileLinkLines, hashContent, inertAt, @@ -217,3 +218,15 @@ test('the content hash tells a changed text from an unchanged one', () => { assert.notEqual(hashContent('a'), hashContent('b')); assert.equal(hashContent(null), hashContent('')); }); + +test('a name cannot turn into a markdown link in Sylo’s own text', () => { + const evil = 'x](https://evil.example)[y'; + assert.ok(!escapeMarkdown(evil).includes('](https')); + assert.equal( + escapeMarkdown('a_b *c* ~d~ `e` |f| >g #h '), + String.raw`a\_b \*c\* \~d\~ \`e\` \|f\| \>g \#h \` + ); + assert.equal(escapeMarkdown('plain name 123'), 'plain name 123'); + assert.ok(!replyHead({ name: evil, snippet: 'hi', url: 'https://x/y' }).includes('](https://evil')); + assert.equal(plain(replyHead({ name: '@everyone', snippet: '' })), '> ↪ **@everyone**'); +}); diff --git a/test/helpers/fakeBridge.js b/test/helpers/fakeBridge.js index 14799b8b..3b1b5d8f 100644 --- a/test/helpers/fakeBridge.js +++ b/test/helpers/fakeBridge.js @@ -16,12 +16,21 @@ export const ME_F = '999000000000000002'; let nextId = 80_000_000_000_000_000n; export const snowflake = () => String((nextId += 1n)); -function makeChannel(guild, { id, name, nsfw = false, type = ChannelType.GuildText, deny = [] }) { +/** + * `nsfwOverride` and `parentId` are what Fluxer's age restriction reads (own override, else the category's, + * else the server's); the plain `nsfw` is the channel's own mark. + */ +function makeChannel( + guild, + { id, name, nsfw = false, nsfwOverride = null, parentId = null, type = ChannelType.GuildText, deny = [] } +) { const channel = { id, name, type, nsfw, + nsfwOverride, + parentId, rawPosition: 0, guild, said: [], @@ -41,6 +50,7 @@ function makeGuild(client, { id, name }) { name, client, ownerId: 'owner', + nsfw: false, // the server's own age restriction (Fluxer) channels: { cache: new Map() }, emojis: { cache: new Map() }, roles: { cache: new Map() }, @@ -93,6 +103,9 @@ export function fakeTransport() { edits: [], removes: [], deleted: [], + botDeletes: [], + botDeleteFailures: [], + removeFailures: [], // errors thrown by the next webhook deletes sendFailures: [], // errors thrown by the next sends, in order hookFailures: [], // errors thrown by the next createHook calls attempts: 0, @@ -115,8 +128,15 @@ export function fakeTransport() { t.edits.push({ guildKey, hook, messageId, payload }); }, async remove(guildKey, hook, messageId) { + const next = t.removeFailures.shift(); + if (next) throw next; t.removes.push({ guildKey, hook, messageId }); }, + async botDelete(guildKey, channelId, messageId) { + const next = t.botDeleteFailures.shift(); + if (next) throw next; + t.botDeletes.push({ guildKey, channelId, messageId }); + }, async deleteHook(guildKey, hook) { t.deleted.push({ guildKey, hook }); }, diff --git a/test/v2.accessLevels.test.js b/test/v2.accessLevels.test.js index 8bfd002a..68a2c4f7 100644 --- a/test/v2.accessLevels.test.js +++ b/test/v2.accessLevels.test.js @@ -126,6 +126,7 @@ const READS = [ '/settings', '/access', '/modules/afk/config', + '/modules/bridge/config', '/audit', '/tickets', '/appeals', @@ -144,6 +145,8 @@ const WRITES = [ ['/modules/afk', { enabled: true }, 200], ['/settings', { botMasterRoleIds: [MASTER], modlogChannelId: '' }, 200], ['/modules/afk/config', {}, 200], + ['/modules/bridge/links/999999/pause', {}, 404], + ['/modules/bridge/links/999999/options', { options: { bots: true } }, 404], ['/moderation/warnings/clear', { userId: '810000000000000001' }, 404], ['/access', { viewRoleIds: [VIEW, OTHER_ROLE] }, 200], ['/setup/apply', { items: [] }, 400], diff --git a/test/v2Bridge.test.js b/test/v2Bridge.test.js index 829f2450..945561c2 100644 --- a/test/v2Bridge.test.js +++ b/test/v2Bridge.test.js @@ -42,10 +42,12 @@ const addChannel = (guild, id, name, nsfw = false) => name, type: 0, nsfw, + nsfwOverride: null, rawPosition: Number(id.slice(-2)), guild, said: [], - permissionsFor: () => ({ has: () => true }), + // Sylo (no member) may do everything; a person may not use a channel that is hidden from them. + permissionsFor: (member) => ({ has: () => !member?.hidden?.has(id) }), async send(payload) { this.said.push(payload); return { id: '1' }; @@ -60,6 +62,9 @@ beforeEach(async () => { fakes = useFakes(); dGuild.channels.cache.clear(); fGuild.channels.cache.clear(); + fGuild.nsfw = false; + dGuild.members.cache.clear(); + fGuild.members.cache.clear(); addChannel(dGuild, CH.d1, 'discord-main'); addChannel(dGuild, CH.d2, 'discord-two'); addChannel(dGuild, CH.dNsfw, 'discord-nsfw', true); @@ -81,11 +86,16 @@ function ownBrowser() { } /** Someone signed in on a platform, with JSON helpers that carry the CSRF token. */ -async function person(platform, { guilds } = {}) { +async function person(platform, { guilds, hidden = [], member = true } = {}) { setProfiles(); if (guilds) profiles[platform].guilds = guilds; const b = ownBrowser(); await signIn(b, platform); + // The person's member in the server (the channel rights are read from it); `hidden` channels they cannot see. + if (member) { + const id = profiles[platform].id; + (platform === 'discord' ? dGuild : fGuild).members.cache.set(id, { id, hidden: new Set(hidden) }); + } const token = (await (await b.get('/api/v2/csrf')).json()).token; const headers = { accept: 'application/json', 'content-type': 'application/json', 'x-csrf-token': token }; return { @@ -379,3 +389,62 @@ test('a request can be cancelled by the server that made it, which kills its cod assert.deepEqual((await json(cancelled)).overview.requests, []); assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 404); }); + +test('a person must be able to view, read and send in the channel themselves: a hidden staff channel is refused', async () => { + const d = await person('discord', { hidden: [CH.d2] }); + const hiddenAsk = await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d2 }); + assert.equal(hiddenAsk.status, 403); + assert.match((await json(hiddenAsk)).error, /view, read and send in #discord-two yourself/); + assert.equal((await store.pendingCodes(D_GUILD)).length, 0, 'no request was made'); + + const ok = await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 }); + assert.equal(ok.status, 200); + const { code } = await json(ok); + + const f = await person('fluxer', { hidden: [CH.f2] }); + const hiddenAccept = await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f2 }); + assert.equal(hiddenAccept.status, 403); + assert.equal((await store.pendingCodes(D_GUILD)).length, 1, 'the code is still unused'); + assert.equal(fakes.transport.hooks.length, 0, 'and no webhook was made'); + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 200); +}); + +test('a person Sylo cannot look up in the server (no member) is refused: it fails closed', async () => { + const d = await person('discord', { member: false }); + const res = await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 }); + assert.equal(res.status, 403); + assert.match((await json(res)).error, /yourself/); +}); + +test('a Fluxer channel in an age-restricted category or server is refused for an ordinary Discord channel', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + // a category with its own override, the channel itself inheriting + fGuild.channels.cache.set('810000000000000007', { + id: '810000000000000007', + name: 'adult', + type: 4, + nsfwOverride: true, + nsfw: false, + }); + fGuild.channels.cache.get(CH.f1).parentId = '810000000000000007'; + const category = await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + assert.equal(category.status, 400); + assert.match((await json(category)).error, /age-restricted/); + // the server itself is age-restricted + fGuild.channels.cache.get(CH.f1).parentId = null; + fGuild.nsfw = true; + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 400); + // what cannot be read counts as age-restricted + fGuild.nsfw = false; + fGuild.channels.cache.get(CH.f1).nsfwOverride = undefined; + assert.equal((await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, 400); + assert.equal(fakes.transport.hooks.length, 0); + fGuild.channels.cache.get(CH.f1).nsfwOverride = null; + assert.equal( + (await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 })).status, + 200, + 'clear: it links' + ); +}); diff --git a/web-v2/src/__tests__/Bridge.test.jsx b/web-v2/src/__tests__/Bridge.test.jsx index dca0e724..a00c489a 100644 --- a/web-v2/src/__tests__/Bridge.test.jsx +++ b/web-v2/src/__tests__/Bridge.test.jsx @@ -33,8 +33,7 @@ const link = (over = {}) => ({ options: { bots: false, joinMessages: false, uploadFiles: true }, theirOptions: { bots: true, joinMessages: false, uploadFiles: true }, createdAt: Date.UTC(2030, 0, 2), - requestedBy: 'Alice (dashboard)', - acceptedBy: 'Bob (dashboard)', + by: 'Alice (dashboard)', queued: 0, dropped: 0, ...over, diff --git a/web-v2/src/moduleForms/Bridge.jsx b/web-v2/src/moduleForms/Bridge.jsx index 40b540af..62177344 100644 --- a/web-v2/src/moduleForms/Bridge.jsx +++ b/web-v2/src/moduleForms/Bridge.jsx @@ -29,7 +29,7 @@ const OPTION_ROWS = [ { key: 'bots', label: 'Share messages from bots', - hint: 'Off by default. Messages from other bots and webhooks of this server cross; Sylo is never relayed.', + hint: 'Off by default. Messages from other bot accounts of this server cross. Webhook messages (what bridges post) and Sylo itself never cross.', }, { key: 'joinMessages', @@ -160,7 +160,7 @@ export default function Bridge() { async function onUnlink(link) { const ok = await confirmDialog({ title: `Unlink #${link.mine.channel}?`, - body: `Messages stop crossing to #${link.theirs.channel} in ${link.theirs.server}. Sylo deletes both webhooks and tells both channels. Messages already posted stay where they are.`, + body: `Messages stop crossing to #${link.theirs.channel} in ${link.theirs.server}. Sylo deletes both webhooks and tells both channels. Messages already posted stay where they are, and deleting an original later no longer removes its copy.`, confirmLabel: 'Unlink', }); if (ok) await run(() => bridgeLinkAction(guildId, link.id, 'unlink')); From 3c697240c742fff2ce3ad2f593fcd754e871266b Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Mon, 16 Nov 2026 23:40:45 +0100 Subject: [PATCH 3/3] feat(bridge): only the server owner can use the bridge, and smaller fixes - Only the owner of a server (Discord owner, Fluxer owner) can ask for a link, use a code, change options, pause, resume or unlink; Administrators, Edit users and bot masters are refused. Everyone with View access sees the status, read-only. requireGuildOwner(req, guildKey) in web/lib/guildOwner.js, checked on every action from the owner the platform reports. - The owner of each server is recorded with the link. If either owner changes, the link pauses (on the next message or join, and by a five-minute sweep) until the new owner confirms with Resume; a code made by a previous owner is refused. - A delete that hits a temporary error is retried and keeps its map entry until the copy is gone; the download budget is held until the copy is sent; an edit with an unreadable webhook token makes a new webhook instead of counting as a failed delivery; plain-text webhook tokens are sealed on first read; a paused or stopped link whose channel was deleted shows a Health item. --- docs/mobile-app.md | 2 +- docs/modules/bridge.md | 36 +++- src/db/bridge.js | 71 ++++++-- src/db/index.js | 3 + src/modules/bridge.js | 184 +++++++++++++++++-- src/web/lib/guildOwner.js | 43 +++++ src/web/routes/v2Bridge.js | 19 +- test/bridge.db.test.js | 39 ++++ test/bridge.engine.test.js | 260 +++++++++++++++++++++++++++ test/bridgeHealth.test.js | 31 +++- test/helpers/fakeBridge.js | 2 +- test/v2.accessLevels.test.js | 2 - test/v2Bridge.test.js | 85 ++++++++- web-v2/src/__tests__/Bridge.test.jsx | 32 ++++ web-v2/src/moduleForms/Bridge.jsx | 58 +++--- 15 files changed, 801 insertions(+), 66 deletions(-) create mode 100644 src/web/lib/guildOwner.js diff --git a/docs/mobile-app.md b/docs/mobile-app.md index da0b26ae..67fdbfe4 100644 --- a/docs/mobile-app.md +++ b/docs/mobile-app.md @@ -338,7 +338,7 @@ The feature strings: - `duplicate`: the `duplicate` routes for reminders, embed messages, reaction-role sets, custom commands and shop items. - `message-templates`: the built-in message templates and the raw message JSON import and export. - `message-move`: when the channel of a posted **embed message**, the **Welcome channel** message or a **role panel** changes and is published, the server posts a fresh message in the new channel, stores it and then deletes the old one, so the app must not unpublish first. Other bot posts (verification, honeypot traps, applications and so on) are not covered. A publish that loses a race with another publish of the same embed message answers `409` with the current record. -- `bridge`: the [Bridge module](modules/bridge.md): `GET /api/v2/guilds/:id/modules/bridge/config` (the links, waiting requests and channels of the server; View level) and, at Edit level, `POST` to `…/modules/bridge/request` (a code for another server), `…/modules/bridge/accept` (use a code), `…/modules/bridge/requests/:id/cancel`, and `…/modules/bridge/links/:id/options`, `…/pause`, `…/resume` and `…/unlink`. Every answer carries the fresh `overview`. +- `bridge`: the [Bridge module](modules/bridge.md): `GET /api/v2/guilds/:id/modules/bridge/config` (the links, waiting requests and channels of the server; View level, with `owner` saying whether the caller may change anything) and, for the server owner only (Edit access is not enough: a `403` otherwise), `POST` to `…/modules/bridge/request` (a code for another server), `…/modules/bridge/accept` (use a code), `…/modules/bridge/requests/:id/cancel`, and `…/modules/bridge/links/:id/options`, `…/pause`, `…/resume` and `…/unlink`. Every answer carries the fresh `overview`. - `app-connect`: [adding the other login](#adding-the-other-login). Only where sign-in is on and both Discord and Fluxer logins are set up. - `push`, `push-pigeon`: this instance can send notifications to signed-in apps. diff --git a/docs/modules/bridge.md b/docs/modules/bridge.md index a8b13ad6..eef40076 100644 --- a/docs/modules/bridge.md +++ b/docs/modules/bridge.md @@ -10,14 +10,28 @@ Sylo is) and that is a member of both servers. **Dashboard:** `/v2/guilds//m/bridge` - the linked channels, a form to ask for a link, a form to use a code, what this server shares, and pause, resume and unlink. +## Who can use it: the server owner + +**Only the owner of a server can use the bridge** (the Discord server owner, the Fluxer server owner), on +both sides. Administrators, people with **Edit** access to the dashboard and the bot-master roles cannot ask +for a link, use a code, change what a server shares, or pause, resume or unlink a bridge. People with **View** +access see the status of the links on the page, read-only. Ownership is checked again on every action, from +the owner the platform reports for the server. + +Sylo records who owns each server when the link is made. **If the owner of either server changes, the link +pauses** (on both sides, with a notice and an audit entry) **until the new owner confirms it**: the new +owner opens the Bridge page and presses **Resume**. If both servers changed owner, both owners have to confirm. +A code that was made by a previous owner is refused. Messages check this as they come, and a check every five +minutes covers a quiet link. + ## Linking: both servers agree A link is never made from one side alone. -1. In the first server, someone with **Edit** access opens the Bridge page, picks a channel and presses +1. In the first server, the owner opens the Bridge page, picks a channel and presses **Get a code**. The code (`XXXX-XXXX-XXXX`) works once and for **15 minutes**. Sylo stores only a hash of it, so it is shown this one time. -2. They give the code to someone with **Edit** access in the other server. That person opens the Bridge +2. They give the code to the owner of the other server. That owner opens the Bridge page there, pastes the code, picks the channel that should join it and presses **Link the channels**. 3. Sylo makes a webhook in each channel and posts a notice in both: "Bridge on. Messages in this channel are shared with #chat in Fluxer Home on Fluxer ...". @@ -26,7 +40,7 @@ A link joins a Discord channel to a Fluxer channel, never two on the same platfo at a time, and a server has at most 10 links and 5 waiting requests. The Bridge module must be on in both servers. Wrong codes are rate limited, and a few wrong ones in a row lock the attempts for a while. -**Either side can unlink at any time** (Edit access). Both webhooks are deleted, the message map goes, and +**Either side can unlink at any time** (the owner). Both webhooks are deleted, the message map goes, and both channels are told. If Sylo is removed from a server, the server on the other end loses its webhook and is told too. @@ -75,23 +89,27 @@ stopped itself (see below) can be resumed by either server once the cause is fix problem with the reason, and it is not retried until someone presses **Resume**. - **Webhooks are Sylo's.** Sylo makes and manages the webhook of each channel and makes a new one if it is deleted. Its token is stored sealed (AES-256-GCM, keyed from the session secret) and never sent to the - browser. If the session secret changes, the tokens cannot be read and Sylo makes new webhooks. + browser. If the session secret changes, the tokens cannot be read and Sylo makes new webhooks (also when it + only wanted to edit a copy). A plain-text token from a development database is sealed the first time its link + is read; accepting plain text at all is removed in a later version. - **You can only bridge what you can see.** The person who asks for a link, and the one who uses a code, must - be able to view, read and send in their channel themselves; Edit access to the dashboard alone is not enough. + be able to view, read and send in their channel themselves (an owner normally can). - **Deletes always follow.** A copy is removed even if the link is paused or stopped, when the delete comes before the copy was posted, after a webhook was replaced (Sylo then deletes it as the bot, which needs Manage Messages), and for a purge of many messages. After an unlink the message map is gone, so copies already posted stay and a later delete of the original no longer reaches them. -- **File downloads share one budget** (4 at a time and 32 MB in flight for the whole Sylo): a file that does not - fit crosses as a link. +- **File downloads share one budget** (4 files held at a time and 32 MB for the whole Sylo): a file is held from + its download until its copy is sent, and a file that does not fit crosses as a link. +- **A delete that hits a temporary error is tried again** (with growing pauses), and the message map entry stays + until the copy is really gone. ## Needs - **Manage Webhooks**, View Channel and Send Messages in each bridged channel (the Health page checks them). - The **Message Content** intent (`INTENT_MESSAGE_CONTENT`, on by default). -- Edit access to the dashboard in the server that makes the request and in the server that uses the code. +- Being the owner of the server that makes the request and of the server that uses the code. ## API The Sylo app can read and change a bridge through `/api/v2` (`bridge` in the app's feature list). A GET at -View level, everything else at Edit level. See [mobile-app.md](../mobile-app.md). +View level, everything else for the server owner only (Edit access is not enough). See [mobile-app.md](../mobile-app.md). diff --git a/src/db/bridge.js b/src/db/bridge.js index de0afe8d..5a820d01 100644 --- a/src/db/bridge.js +++ b/src/db/bridge.js @@ -46,6 +46,8 @@ registerPostgresBootstrap(` hook_b_token TEXT NOT NULL DEFAULT '', requested_by_name TEXT NOT NULL DEFAULT '', accepted_by_name TEXT NOT NULL DEFAULT '', + owner_a TEXT NOT NULL DEFAULT '', + owner_b TEXT NOT NULL DEFAULT '', created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL ); @@ -59,6 +61,7 @@ registerPostgresBootstrap(` code_hash TEXT NOT NULL, options TEXT NOT NULL DEFAULT '{}', requested_by_name TEXT NOT NULL DEFAULT '', + guild_owner TEXT NOT NULL DEFAULT '', expires_at BIGINT NOT NULL, created_at BIGINT NOT NULL ); @@ -86,9 +89,9 @@ const s = { insertLink: prepare( `INSERT INTO bridge_links (guild_a, channel_a, guild_b, channel_b, status, options_a, options_b, hook_a_id, hook_a_token, - hook_b_id, hook_b_token, requested_by_name, accepted_by_name, created_at, updated_at) + hook_b_id, hook_b_token, requested_by_name, accepted_by_name, owner_a, owner_b, created_at, updated_at) VALUES (@guildA, @channelA, @guildB, @channelB, 'active', @optionsA, @optionsB, @hookAId, @hookAToken, - @hookBId, @hookBToken, @requestedBy, @acceptedBy, @now, @now)`, + @hookBId, @hookBToken, @requestedBy, @acceptedBy, @ownerA, @ownerB, @now, @now)`, { returningId: true } ), link: prepare('SELECT * FROM bridge_links WHERE id = ?'), @@ -105,6 +108,10 @@ const s = { ), setOptionsA: prepare('UPDATE bridge_links SET options_a = @options, updated_at = @now WHERE id = @id'), setOptionsB: prepare('UPDATE bridge_links SET options_b = @options, updated_at = @now WHERE id = @id'), + setOwnerA: prepare('UPDATE bridge_links SET owner_a = @owner, updated_at = @now WHERE id = @id'), + setOwnerB: prepare('UPDATE bridge_links SET owner_b = @owner, updated_at = @now WHERE id = @id'), + resealA: prepare('UPDATE bridge_links SET hook_a_token = @token WHERE id = @id AND hook_a_token = @plain'), + resealB: prepare('UPDATE bridge_links SET hook_b_token = @token WHERE id = @id AND hook_b_token = @plain'), setHookA: prepare( 'UPDATE bridge_links SET hook_a_id = @id2, hook_a_token = @token, updated_at = @now WHERE id = @id' ), @@ -113,12 +120,13 @@ const s = { ), delLink: prepare('DELETE FROM bridge_links WHERE id = ?'), delLinkMessages: prepare('DELETE FROM bridge_messages WHERE link_id = ?'), + activeLinks: prepare("SELECT * FROM bridge_links WHERE status = 'active'"), linkIdsOfGuild: prepare('SELECT id FROM bridge_links WHERE guild_a = ? OR guild_b = ?'), delLinksOfGuild: prepare('DELETE FROM bridge_links WHERE guild_a = ? OR guild_b = ?'), insertCode: prepare( - `INSERT INTO bridge_codes (guild_id, channel_id, code_hash, options, requested_by_name, expires_at, created_at) - VALUES (@guildId, @channelId, @hash, @options, @requestedBy, @expiresAt, @now)`, + `INSERT INTO bridge_codes (guild_id, channel_id, code_hash, options, requested_by_name, guild_owner, expires_at, created_at) + VALUES (@guildId, @channelId, @hash, @options, @requestedBy, @guildOwner, @expiresAt, @now)`, { returningId: true } ), codeByHash: prepare('SELECT * FROM bridge_codes WHERE code_hash = ? AND expires_at > ?'), @@ -154,8 +162,10 @@ const s = { // --- links ----------------------------------------------------------------------------------------- -// A webhook token is a bearer credential for the channel, so it is sealed in the table. A value stored -// before sealing (it has no v1. prefix) is read as it is. +// A webhook token is a bearer credential for the channel, so it is sealed in the table. A plain value (no +// v1. prefix) is sealed in place the first time its row is read (readLink below), and until then read as it +// is. No release ever wrote plain tokens, so this only covers development and test databases. +// TODO(5.4): drop the plain branch of openToken and the resealing, so a plain value can never persist. const TOKEN_PURPOSE = 'bridge-webhook'; const sealToken = (token) => (token ? seal(config.sessionSecret, TOKEN_PURPOSE, token) : ''); const openToken = (stored) => { @@ -193,17 +203,37 @@ export function linkFrom(row) { hookBToken: openToken(row.hook_b_token), requestedBy: row.requested_by_name || '', acceptedBy: row.accepted_by_name || '', + // The platform user id of each server's owner when the link was made (or last confirmed). The bridge is + // for the server owners only, so a change of owner pauses the link until the new owner confirms. + ownerA: row.owner_a || '', + ownerB: row.owner_b || '', createdAt: Number(row.created_at), updatedAt: Number(row.updated_at), }; } +/** A link from its row, sealing a plain webhook token in place on the way (see openToken). */ +async function readLink(row) { + if (!row) return null; + for (const side of ['a', 'b']) { + const stored = row[`hook_${side}_token`]; + if (stored && !isSealed(stored)) { + await (side === 'a' ? s.resealA : s.resealB).run({ + id: row.id, + token: sealToken(stored), + plain: stored, + }); + } + } + return linkFrom(row); +} + /** * Create a link. Returns its id, or null when either channel is already in a link (the unique indexes * decide, so two requests that race cannot both win). * @param {{ guildA: string, channelA: string, guildB: string, channelB: string, optionsA?: object, * optionsB?: object, hookA?: { id: string, token: string }, hookB?: { id: string, token: string }, - * requestedBy?: string, acceptedBy?: string }} link + * requestedBy?: string, acceptedBy?: string, ownerA?: string, ownerB?: string }} link */ export async function createLink(link) { try { @@ -220,6 +250,8 @@ export async function createLink(link) { hookBToken: sealToken(link.hookB?.token), requestedBy: String(link.requestedBy ?? '').slice(0, 100), acceptedBy: String(link.acceptedBy ?? '').slice(0, 100), + ownerA: String(link.ownerA ?? ''), + ownerB: String(link.ownerB ?? ''), now: Date.now(), }); return Number(res.lastInsertRowid); @@ -229,8 +261,11 @@ export async function createLink(link) { } } -export const getLink = async (id) => linkFrom(await s.link.get(Number(id))); -export const linksOfGuild = async (key) => (await s.linksOfGuild.all(key, key)).map(linkFrom); +export const getLink = async (id) => readLink(await s.link.get(Number(id))); +export const linksOfGuild = async (key) => + Promise.all((await s.linksOfGuild.all(key, key)).map((row) => readLink(row))); +/** Every running link (the owner sweep). */ +export const activeLinks = async () => Promise.all((await s.activeLinks.all()).map((row) => readLink(row))); export const linkCount = async (key) => Number((await s.countLinks.get(key, key)).n); /** @@ -239,16 +274,16 @@ export const linkCount = async (key) => Number((await s.countLinks.get(key, key) */ export async function linkForChannel(guildKey, channelId) { const a = await s.linkByChannelA.get(guildKey, channelId); - if (a) return { link: linkFrom(a), side: 'a' }; + if (a) return { link: await readLink(a), side: 'a' }; const b = await s.linkByChannelB.get(guildKey, channelId); - if (b) return { link: linkFrom(b), side: 'b' }; + if (b) return { link: await readLink(b), side: 'b' }; return null; } /** The link that owns a webhook id (one of Sylo's bridge webhooks), or null. */ export async function linkForHook(hookId) { if (!hookId) return null; - return linkFrom(await s.linkByHook.get({ id: String(hookId) })); + return readLink(await s.linkByHook.get({ id: String(hookId) })); } export const STATUSES = Object.freeze(['active', 'paused', 'failed']); @@ -272,6 +307,14 @@ export const setLinkOptions = (id, side, options) => now: Date.now(), }); +/** Record who owns a server now (the new owner confirmed the bridge). */ +export const setLinkOwner = (id, side, owner) => + (side === 'a' ? s.setOwnerA : s.setOwnerB).run({ + id: Number(id), + owner: String(owner ?? ''), + now: Date.now(), + }); + /** Store the webhook of one side (after Sylo made a new one). */ export const setLinkHook = (id, side, hook) => (side === 'a' ? s.setHookA : s.setHookB).run({ @@ -343,6 +386,7 @@ export async function createCode({ channelId, options = {}, requestedBy = '', + guildOwner = '', ttlMs = CODE_TTL_MS, }) { const now = Date.now(); @@ -353,6 +397,7 @@ export async function createCode({ hash: hashOf(code), options: JSON.stringify(options), requestedBy: String(requestedBy).slice(0, 100), + guildOwner: String(guildOwner ?? ''), expiresAt: now + ttlMs, now, }); @@ -373,6 +418,7 @@ export async function claimCode(text, now = Date.now()) { channelId: row.channel_id, options: parseJson(row.options), requestedBy: row.requested_by_name, + guildOwner: row.guild_owner || '', expiresAt: Number(row.expires_at), }; } @@ -462,6 +508,7 @@ export async function peekCode(text, now = Date.now()) { channelId: row.channel_id, options: parseJson(row.options), requestedBy: row.requested_by_name, + guildOwner: row.guild_owner || '', expiresAt: Number(row.expires_at), }; } diff --git a/src/db/index.js b/src/db/index.js index 29b5002c..1a6cad66 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -1624,6 +1624,8 @@ export const MIGRATIONS = [ hook_b_token TEXT NOT NULL DEFAULT '', requested_by_name TEXT NOT NULL DEFAULT '', accepted_by_name TEXT NOT NULL DEFAULT '', + owner_a TEXT NOT NULL DEFAULT '', + owner_b TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ); @@ -1637,6 +1639,7 @@ export const MIGRATIONS = [ code_hash TEXT NOT NULL, options TEXT NOT NULL DEFAULT '{}', requested_by_name TEXT NOT NULL DEFAULT '', + guild_owner TEXT NOT NULL DEFAULT '', expires_at INTEGER NOT NULL, created_at INTEGER NOT NULL ); diff --git a/src/modules/bridge.js b/src/modules/bridge.js index c0e2d9cb..a3998e2b 100644 --- a/src/modules/bridge.js +++ b/src/modules/bridge.js @@ -23,6 +23,7 @@ // - each server is isolated: queues, links and failures are per link, never shared between servers import { ChannelType } from 'discord.js'; import { on } from './dispatch.js'; +import { every } from './schedule.js'; import { getGuildModule, isModuleEnabled } from '../db/modules.js'; import { recordAudit } from '../db/audit.js'; import { onBeforePurge } from '../db/purge.js'; @@ -164,6 +165,48 @@ const wasDeleted = (key, messageId) => Date.now() - (deletedSoon.get(`${key}|${messageId}`) ?? 0) < TOMBSTONE_MS; sweepMap(deletedSoon, (at, now) => now - at > TOMBSTONE_MS); +// The bridge is for the owners of the two servers only (the routes check who is calling). The owner of each +// server is recorded when the link is made and when a new owner confirms it; if the platform reports another +// owner later, the link pauses until that owner presses Resume. +const ownerOf = (key) => String(guildByKey(key)?.ownerId ?? ''); + +/** The side whose server has another owner than the one recorded, or null. An unknown owner is not a change. */ +function ownerChangedSide(link) { + for (const s of ['a', 'b']) { + const recorded = s === 'a' ? link.ownerA : link.ownerB; + const now = ownerOf(guildOf(link, s)); + if (recorded && now && recorded !== now) return s; + } + return null; +} + +/** Pause a running link because the owner of `side`'s server changed, and say so on both sides. */ +async function pauseForOwner(link, side) { + if (link.status !== 'active') return; + await store.setLinkStatus(link.id, { status: 'paused', pausedSide: side, reason: 'owner-changed' }); + dropBridgeCache(); + queue.clear(laneKey(link, 'a')); + queue.clear(laneKey(link, 'b')); + log.info('bridge', `link ${link.id} paused: the owner of a server changed`); + for (const s of ['a', 'b']) { + await recordAudit(guildOf(link, s), { + actor: 'Sylo', + action: 'bridge:pause', + detail: + `the bridge of #${channelName(guildOf(link, s), channelOf(link, s))} paused: the owner of ${serverName(guildOf(link, side))} changed`.slice( + 0, + 500 + ), + change: null, + }); + await say( + guildOf(link, s), + channelOf(link, s), + `🌉 **Bridge paused:** the owner of ${serverLabel(guildOf(link, side))} changed. Nothing is shared until the new owner confirms it.` + ); + } +} + /** Cached lookups of "which link is this channel in", dropped whenever a link changes. */ const cache = new Map(); export const dropBridgeCache = () => cache.clear(); @@ -203,6 +246,8 @@ const REASONS = { unreachable: 'Messages could not be delivered several times in a row.', 'channel-deleted': 'A bridged channel was deleted.', 'channel-missing': 'Sylo cannot see one of the bridged channels.', + 'owner-changed': + 'The owner of one of the servers changed. The bridge is paused until the new owner confirms it: as the owner, open the Bridge page and press Resume.', 'age-restriction': 'One channel changed its age restriction, so the two no longer match. An age-restricted channel can only be bridged with an age-restricted one.', }; @@ -310,6 +355,7 @@ export async function requestLink({ guildKey, channelId, options, actor }) { channelId, options: normaliseOptions(options), requestedBy: actor, + guildOwner: ownerOf(guildKey), }); await recordAudit(guildKey, { actor, @@ -350,6 +396,10 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) const request = await store.peekCode(code); if (!request) throw new BridgeError(INVALID_CODE, 404); if (request.guildId === guildKey) throw new BridgeError('That request was made in this server.', 409); + const ownerA = ownerOf(request.guildId); + if (request.guildOwner && ownerA && request.guildOwner !== ownerA) { + throw new BridgeError('The server that made this request has a new owner. Ask for a new code.', 409); + } if (platformOf(request.guildId) === platformOf(guildKey)) { throw new BridgeError( 'A bridge joins a Discord channel to a Fluxer channel, not two on the same platform.' @@ -411,6 +461,8 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) hookB, requestedBy: claimed.requestedBy, acceptedBy: actor, + ownerA, + ownerB: ownerOf(guildKey), }); if (!id) { await dropMade(); @@ -496,6 +548,9 @@ export async function resumeLink({ guildKey, id, actor }) { if (link.status === 'paused' && link.pausedSide !== side) { throw new BridgeError('The other server paused this bridge, so only they can resume it.', 403); } + // The one pressing Resume is the owner of this server (the route checks): this is the confirmation a new owner gives. + await store.setLinkOwner(link.id, side, ownerOf(guildKey)); + link[side === 'a' ? 'ownerA' : 'ownerB'] = ownerOf(guildKey); requireBothPlatforms(); await requireModule(link.guildA); await requireModule(link.guildB); @@ -522,6 +577,23 @@ export async function resumeLink({ guildKey, id, actor }) { } } failures.delete(link.id); + // The other server's owner may have changed too: it has to confirm as well before anything is shared. + const stillChanged = ownerChangedSide(link); + if (stillChanged) { + await store.setLinkStatus(link.id, { + status: 'paused', + pausedSide: stillChanged, + reason: 'owner-changed', + }); + dropBridgeCache(); + await recordAudit(guildKey, { + actor, + action: 'bridge:resume', + detail: `confirmed #${channelName(guildKey, channelOf(link, side))}; waiting for the other server's new owner`, + change: null, + }); + return { active: false }; + } await store.setLinkStatus(link.id, { status: 'active' }); dropBridgeCache(); await recordAudit(guildKey, { @@ -532,6 +604,7 @@ export async function resumeLink({ guildKey, id, actor }) { }); await say(link.guildA, link.channelA, onNotice(link.guildA, link.guildB, link.channelB)); await say(link.guildB, link.channelB, onNotice(link.guildB, link.guildA, link.channelA)); + return { active: true }; } /** Unlink: either side may, at any time. The webhooks are deleted and the message map with the link. */ @@ -806,6 +879,15 @@ async function buildCopy(message, link, side, { guildKey }) { const plan = planAttachments(attachments, { upload: options.uploadFiles }); const files = []; const links = [...plan.links]; + // The files are held in memory until the copy is sent (and its retries are over), so the process-wide budget + // is held that long too: `release` gives it back, once. + const held = []; + let released = false; + const release = () => { + if (released) return; + released = true; + for (const n of held) releaseDownload(n); + }; for (const a of plan.upload) { // No room in the process-wide budget: a link, never a wait. if (!reserveDownload(a.size)) { @@ -816,11 +898,11 @@ async function buildCopy(message, link, side, { guildKey }) { const res = await download(a.url, FILE_BYTES_LIMIT); if (!res.ok || !res.body?.length) throw new Error(`HTTP ${res.status}`); files.push({ name: a.name, data: res.body }); + held.push(a.size); } catch (err) { + releaseDownload(a.size); log.debug('bridge', `file left as a link (${err?.message ?? err})`); links.push(a); - } finally { - releaseDownload(a.size); } } @@ -836,8 +918,12 @@ async function buildCopy(message, link, side, { guildKey }) { ); const content = joinParts([head, body, tail]); - if (!content && !embeds.length && !files.length) return null; + if (!content && !embeds.length && !files.length) { + release(); + return null; + } return { + release, content, head, body, @@ -908,14 +994,19 @@ async function removeCopy(link, target, copyId, copyHookId) { await transport.remove(dstKey, hook, copyId); return true; } catch (err) { - if (classifyError(err, 'delete').kind === 'message-gone') return true; + const kind = classifyError(err, 'delete').kind; + if (kind === 'message-gone') return true; + // A temporary error: the delete job is tried again (the queue backs off), the map entry stays. + if (kind === 'rate' || kind === 'transient') throw err; } } try { await transport.botDelete(dstKey, channelOf(link, target), copyId); return true; } catch (err) { - if (classifyError(err, 'delete').kind === 'message-gone') return true; + const kind = classifyError(err, 'delete').kind; + if (kind === 'message-gone') return true; + if (kind === 'rate' || kind === 'transient') throw err; log.debug('bridge', `a copy could not be removed (link ${link.id}): ${err?.message ?? err}`); return false; } @@ -933,6 +1024,11 @@ async function allowed(message, link, side, key) { // A bot that already carries a platform tag in its name is a bridge's relay account. if (SUFFIX.test(String(message.author.username ?? ''))) return false; } + const changed = ownerChangedSide(link); + if (changed) { + await pauseForOwner(link, changed); + return false; + } const theirKey = guildOf(link, other(side)); if (!(await isModuleEnabled(theirKey, 'bridge'))) return false; // The age rule on every message: a channel can change after the link was made. @@ -976,10 +1072,6 @@ export async function handleMessage(message, _config, key) { const { value: copyId, hook } = await withHook(current, target, (h) => sendCopy(dstKey, h, payload)); if (failures.has(current.id)) failures.delete(current.id); // Deleted while it was being sent (moderation can be quicker than the relay): take the copy down again. - if (wasDeleted(key, message.id)) { - await removeCopy(current, target, copyId, hook.id); - return copyId; - } try { await store.saveMapping(current.id, side, { sourceId: message.id, @@ -991,10 +1083,20 @@ export async function handleMessage(message, _config, key) { } catch (err) { log.warn('bridge', `could not remember a copy: ${err?.message ?? err}`); } + if (wasDeleted(key, message.id)) { + // Deleted while it was being sent: take the copy down again. If that cannot be done right now, the delete + // job (which keeps trying, and keeps the map entry) does it. + const gone = await removeCopy(current, target, copyId, hook.id).catch(() => false); + if (gone) await store.deleteMapping(current.id, side, message.id).catch(() => {}); + else await deleteCopies(current, side, [message.id]); + } return copyId; }; track( - queue.enqueue(laneKey(link, target), job, { op: 'send' }).catch((err) => noteFailure(link, target, err)) + queue + .enqueue(laneKey(link, target), job, { op: 'send' }) + .catch((err) => noteFailure(link, target, err)) + .finally(() => built?.release?.()) ); } @@ -1033,6 +1135,12 @@ export async function handleUpdate({ new: updated }, _config, key) { if (hash === map.contentHash) return null; const content = joinParts([map.extra?.head, body, map.extra?.tail]); const hook = hookOf(current, target); + // A token that cannot be read any more (the session secret changed): make the webhook again for what comes + // next. The copy was posted by the old one and cannot be edited, which is not a failure of the link. + if (hook.id && !hook.token) { + await recreateHook(current, target); + return null; + } // The copy can only be edited by the webhook that posted it; if that one was replaced, leave it as it was. if (!hook.id || (map.copyHookId && map.copyHookId !== hook.id)) return null; await transport.edit(dstKey, hook, map.counterpartId, { content: content || ZWSP, embeds }); @@ -1060,9 +1168,12 @@ async function deleteCopies(link, side, ids) { for (const id of ids) { const map = await store.mappingOf(current.id, side, id); if (!map) continue; - // Someone removed a copy: forget it, and leave the original alone. - if (map.isOriginal) await removeCopy(current, target, map.counterpartId, map.copyHookId); - await store.deleteMapping(current.id, side, id); + // Someone removed a copy: forget it, and leave the original alone. The map entry stays until the copy + // is really gone, so a delete that failed for good can still be tried again later. + const gone = map.isOriginal + ? await removeCopy(current, target, map.counterpartId, map.copyHookId) + : true; + if (gone) await store.deleteMapping(current.id, side, id); } return ids.length; }; @@ -1103,6 +1214,11 @@ export async function handleJoin(member, _config, key) { const options = optionsOf(link, side); if (!options.joinMessages) continue; if (member.user?.bot && !options.bots) continue; + const changed = ownerChangedSide(link); + if (changed) { + await pauseForOwner(link, changed); + continue; + } const target = other(side); const dstKey = guildOf(link, target); if (!(await isModuleEnabled(dstKey, 'bridge'))) continue; @@ -1134,8 +1250,44 @@ export async function handleJoin(member, _config, key) { /** `channelDelete`: a bridged channel that is gone stops its link. */ export async function handleChannelDelete(channel, _config, key) { const found = await findLink(key, channel.id); - if (found) await failLink(found.link.id, 'channel-deleted'); + if (!found) return; + const { link } = found; + if (link.status === 'active') { + await failLink(link.id, 'channel-deleted'); + return; + } + // Paused or already stopped: it stays as it is, but Health must say the channel is gone (resume would refuse). + await store.setLinkStatus(link.id, { + status: link.status, + pausedSide: link.pausedSide, + reason: 'channel-deleted', + failures: link.failureCount, + }); + dropBridgeCache(); + await recordAudit(key, { + actor: 'Sylo', + action: 'bridge:stopped', + detail: `a bridged channel was deleted while the bridge of #${channel.name ?? 'it'} was not running`, + change: null, + }); +} + +/** + * Check the owners of every running link (a change of owner pauses it). Messages check as they come; this + * covers a quiet link, so a new owner is asked to confirm without waiting for traffic. + */ +export async function sweepOwners() { + let paused = 0; + for (const link of await store.activeLinks()) { + const changed = ownerChangedSide(link); + if (changed) { + await pauseForOwner(link, changed); + paused += 1; + } + } + return paused; } +every('bridge', 5 * 60_000, () => sweepOwners()); on('bridge', 'messageCreateAny', handleMessage); on('bridge', 'messageUpdate', handleUpdate); @@ -1167,6 +1319,8 @@ export function linkView(link, key) { pausedByMe: link.status === 'paused' && link.pausedSide === side, pausedByThem: link.status === 'paused' && link.pausedSide === theirs, problem: link.status === 'failed' ? { code: link.failReason, text: reasonText(link.failReason) } : null, + // Why a paused link is paused beyond "a server paused it": its owner changed, or its channel is gone. + pauseNote: link.status === 'paused' && link.failReason ? reasonText(link.failReason) : null, mine: { channelId: channelOf(link, side), ...endView(key, channelOf(link, side)) }, theirs: endView(guildOf(link, theirs), channelOf(link, theirs)), options: optionsOf(link, side), @@ -1214,7 +1368,7 @@ export async function bridgeOverview(key) { */ export async function bridgeProblems(key) { return (await store.linksOfGuild(key)) - .filter((l) => l.status === 'failed') + .filter((l) => l.status === 'failed' || (l.status === 'paused' && l.failReason)) .map((l) => { const side = sideOf(l, key); return { diff --git a/src/web/lib/guildOwner.js b/src/web/lib/guildOwner.js new file mode 100644 index 00000000..36042325 --- /dev/null +++ b/src/web/lib/guildOwner.js @@ -0,0 +1,43 @@ +// "Only the server owner": the check some actions of the dashboard need, beyond the dashboard access levels +// (src/web/middleware/access.js). Edit access, Administrator and the bot-master roles do not make anyone the +// owner. The owner is read from the platform's own guild object (`ownerId`, kept current by the gateway, see +// bot/lib/adminSnapshot.js), and the answer is worked out on every call, never remembered. +// +// In open mode (no logins: the dashboard is a LAN tool and everyone is "local admin") there is no one to +// compare, so the check passes, as every other access check does there. +import { config } from '../../config.js'; +import { guildByKey } from '../../platform/platformOf.js'; +import { userIdForGuild } from '../middleware/auth.js'; + +/** The refusal: an HTTP 403 with a message the dashboard shows as it is. */ +export class GuildOwnerError extends Error { + constructor(message = 'Only the server owner can do this.') { + super(message); + this.name = 'GuildOwnerError'; + this.status = 403; + } +} + +/** + * Is the signed-in person the owner of this server? Their id on the server's own platform is compared with + * the owner the platform reports; no identity there, an unknown server or an owner that is not known all mean no. + * @param {import('express').Request} req + * @param {string} guildKey the server key + * @returns {boolean} + */ +export function isGuildOwner(req, guildKey) { + if (!config.authEnabled) return true; + const userId = userIdForGuild(req, guildKey); + const ownerId = guildByKey(guildKey)?.ownerId; + return Boolean(userId && ownerId && String(userId) === String(ownerId)); +} + +/** + * Refuse (GuildOwnerError, 403) unless the signed-in person owns this server. + * @param {import('express').Request} req + * @param {string} guildKey the server key + * @returns {void} + */ +export function requireGuildOwner(req, guildKey) { + if (!isGuildOwner(req, guildKey)) throw new GuildOwnerError(); +} diff --git a/src/web/routes/v2Bridge.js b/src/web/routes/v2Bridge.js index fe7f1198..5a145364 100644 --- a/src/web/routes/v2Bridge.js +++ b/src/web/routes/v2Bridge.js @@ -10,6 +10,7 @@ import { accessRouter } from '../middleware/access.js'; import { currentUser, userIdForGuild } from '../middleware/auth.js'; import { clientKey, platformActionLimit, rateLimit } from '../middleware/rateLimit.js'; import { asyncHandler } from '../lib/asyncHandler.js'; +import { GuildOwnerError, isGuildOwner, requireGuildOwner } from '../lib/guildOwner.js'; import { sweepMap } from '../../lib/sweepMap.js'; import { BridgeError, @@ -35,13 +36,20 @@ const linkId = (req) => { return n; }; -/** Run a handler; a refusal of the bridge becomes its own status and message instead of a server error. */ +/** + * Run a changing handler. Every change to a bridge is the SERVER OWNER's alone (the owner the platform reports + * for this server, worked out again on every call): Edit access, Administrator and the bot-master roles do + * not reach it. View access reads the page (the GET below). A refusal becomes its own status and message. + */ const guarded = (fn) => asyncHandler(async (req, res) => { try { + requireGuildOwner(req, req.guild.id); await fn(req, res); } catch (err) { - if (err instanceof BridgeError) return res.status(err.status).json({ error: err.message }); + if (err instanceof BridgeError || err instanceof GuildOwnerError) { + return res.status(err.status).json({ error: err.message }); + } throw err; } }); @@ -70,7 +78,12 @@ const noteWrong = (req) => { wrong.set(id, { n: (hit && Date.now() - hit.at < WRONG_WINDOW_MS ? hit.n : 0) + 1, at: Date.now() }); }; -const overview = (req) => bridgeOverview(req.guild.id); +// `owner`: may this person change the bridge? The page shows the status to everyone with View access and +// enables the controls only for the owner. +const overview = async (req) => ({ + ...(await bridgeOverview(req.guild.id)), + owner: isGuildOwner(req, req.guild.id), +}); // The person must be able to view, read and send in the channel themselves (Edit access alone is not enough: // a bot-master may not see every channel, and the bridge would carry it to a server they run). diff --git a/test/bridge.db.test.js b/test/bridge.db.test.js index efc5d170..12d6bb66 100644 --- a/test/bridge.db.test.js +++ b/test/bridge.db.test.js @@ -90,6 +90,8 @@ test('migration 83 creates the three tables, the unique channel indexes and the 'hook_b_token', 'requested_by_name', 'accepted_by_name', + 'owner_a', + 'owner_b', 'created_at', 'updated_at', ]); @@ -100,6 +102,7 @@ test('migration 83 creates the three tables, the unique channel indexes and the 'code_hash', 'options', 'requested_by_name', + 'guild_owner', 'expires_at', 'created_at', ]); @@ -406,6 +409,11 @@ test('a webhook token is stored sealed and read back whole; a legacy plain value 'legacy-plain-token', 'a value from before sealing reads as it is' ); + assert.ok( + db.prepare('SELECT hook_a_token FROM bridge_links WHERE id = ?').get(id).hook_a_token.startsWith('v1.'), + 'and is sealed in place by that first read' + ); + assert.equal((await getLink(id)).hookAToken, 'legacy-plain-token', 'and still reads whole once sealed'); db.prepare('UPDATE bridge_links SET hook_a_token = ? WHERE id = ?').run( seal('some other secret', 'bridge-webhook', 'x'), id @@ -429,3 +437,34 @@ test('the shared seal helper keeps the Pigeon credential format (v1.iv.tag.data) assert.equal(open('secret', 'pigeon-credential', `${sealed}x`), null); assert.equal(open('secret', 'pigeon-credential', 'v1.a.b'), null); }); + +test('plain tokens are sealed on first read through every way a link is read', async () => { + const id = await createLink(link()); + const plain = (column, value) => + db.prepare(`UPDATE bridge_links SET ${column} = ? WHERE id = ?`).run(value, id); + const stored = (column) => db.prepare(`SELECT ${column} AS v FROM bridge_links WHERE id = ?`).get(id).v; + plain('hook_a_token', 'plain-a'); + plain('hook_b_token', 'plain-b'); + const [found] = await linksOfGuild(D); + assert.deepEqual([found.hookAToken, found.hookBToken], ['plain-a', 'plain-b']); + assert.ok(stored('hook_a_token').startsWith('v1.') && stored('hook_b_token').startsWith('v1.')); + plain('hook_a_token', 'plain-again'); + assert.equal((await linkForChannel(D, CH.d1)).link.hookAToken, 'plain-again'); + assert.ok(stored('hook_a_token').startsWith('v1.')); + plain('hook_b_token', 'plain-hook'); + assert.equal((await linkForHook('hook-b')).hookBToken, 'plain-hook'); + assert.ok(stored('hook_b_token').startsWith('v1.')); +}); + +test('the owners of both servers are recorded with the link, and a code remembers who asked', async () => { + const { setLinkOwner } = await import('../src/db/bridge.js'); + const id = await createLink(link({ ownerA: 'owner-1', ownerB: 'owner-2' })); + let row = await getLink(id); + assert.deepEqual([row.ownerA, row.ownerB], ['owner-1', 'owner-2']); + await setLinkOwner(id, 'b', 'owner-3'); + row = await getLink(id); + assert.deepEqual([row.ownerA, row.ownerB], ['owner-1', 'owner-3']); + const made = await createCode({ guildId: D, channelId: CH.d2, guildOwner: 'owner-1' }); + assert.equal((await peekCode(made.code)).guildOwner, 'owner-1'); + assert.equal((await claimCode(made.code)).guildOwner, 'owner-1'); +}); diff --git a/test/bridge.engine.test.js b/test/bridge.engine.test.js index 24ba7ff1..264c2116 100644 --- a/test/bridge.engine.test.js +++ b/test/bridge.engine.test.js @@ -28,6 +28,7 @@ import { handleJoin, handleMessage, handleUpdate, + sweepOwners, linkView, pauseLink, requestLink, @@ -1571,3 +1572,262 @@ test('a platform that refuses a message gets a plain name, then the text without assert.equal(last.embeds, undefined); assert.equal(last.files, undefined); }); + +// --- round 3: server owners only, and the smaller notes --------------------------------------------------- + +test('the owner of each server is recorded with the link', async () => { + const { link } = await bridged(); + const row = await store.getLink(link.id); + assert.deepEqual([row.ownerA, row.ownerB], [p.dGuild.ownerId, p.fGuild.ownerId]); +}); + +test('a change of owner pauses the link on the next message, until the new owner confirms it', async () => { + const { dCh, fCh, link } = await bridged(); + await relay(fromD(dCh, { content: 'before' }), D); + assert.equal(fakes.transport.sends.length, 1); + + p.fGuild.ownerId = 'new-fluxer-owner'; // the Fluxer server is transferred + await relay(fromD(dCh, { content: 'after the transfer' }), D); + await relay(fromF(fCh, { content: 'and from the new owner side' }), F); + assert.equal(fakes.transport.sends.length, 1, 'nothing crossed after the transfer'); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.pausedSide, row.failReason], ['paused', 'b', 'owner-changed']); + assert.match(say(dCh).at(-1), /Bridge paused:\*\* the owner of Fluxer Home changed/); + assert.match(say(fCh).at(-1), /Bridge paused:\*\* the owner of Fluxer Home changed/); + assert.ok((await listAudit(D)).some((e) => e.action === 'bridge:pause' && /owner/.test(e.detail))); + assert.ok((await listAudit(F)).some((e) => e.action === 'bridge:pause' && /owner/.test(e.detail))); + const view = linkView(row, D); + assert.deepEqual([view.status, view.pausedByThem], ['paused', true]); + assert.match(view.pauseNote, /owner of one of the servers changed/); + assert.deepEqual( + (await bridgeProblems(F)).map((x) => x.code), + ['owner-changed'], + 'a Health item' + ); + + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /only they can resume/, 403); + const resumed = await resumeLink({ guildKey: F, id: link.id, actor: 'B' }); + assert.equal(resumed.active, true); + const after = await store.getLink(link.id); + assert.deepEqual([after.status, after.ownerB], ['active', 'new-fluxer-owner'], 'the new owner is recorded'); + await relay(fromD(dCh, { content: 'confirmed' }), D); + assert.equal(fakes.transport.sends.length, 2); +}); + +test('when both servers changed owner, both owners have to confirm', async () => { + const { dCh, link } = await bridged(); + p.dGuild.ownerId = 'new-discord-owner'; + p.fGuild.ownerId = 'new-fluxer-owner'; + await relay(fromD(dCh, { content: 'x' }), D); + assert.equal((await store.getLink(link.id)).pausedSide, 'a'); + const first = await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.equal(first.active, false); + const waiting = await store.getLink(link.id); + assert.deepEqual( + [waiting.status, waiting.pausedSide], + ['paused', 'b'], + 'now it waits for the Fluxer owner' + ); + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /only they can resume/, 403); + assert.equal((await resumeLink({ guildKey: F, id: link.id, actor: 'B' })).active, true); + await relay(fromD(dCh, { content: 'both confirmed' }), D); + assert.equal(fakes.transport.sends.length, 1); +}); + +test('a quiet link is checked too (the sweep), and so is a join notice; an unknown owner is not a change', async () => { + const { link } = await bridged({ optionsD: { joinMessages: true } }); + p.fGuild.ownerId = undefined; // the platform did not tell: not a change + assert.equal(await sweepOwners(), 0); + p.fGuild.ownerId = 'someone-new'; + assert.equal(await sweepOwners(), 1, 'paused without any message'); + assert.equal((await store.getLink(link.id)).failReason, 'owner-changed'); + assert.equal(await sweepOwners(), 0, 'and only once'); + + await resumeLink({ guildKey: F, id: link.id, actor: 'B' }); + p.dGuild.ownerId = 'discord-owner-2'; + await handleJoin({ displayName: 'Zed', user: { bot: false } }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.sends.length, 0, 'no join notice for a link that just lost its owner'); + assert.equal((await store.getLink(link.id)).status, 'paused'); +}); + +test('a code made by a previous owner is refused', async () => { + const dCh = p.channel(p.dGuild, {}); + const fCh = p.channel(p.fGuild, {}); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + p.dGuild.ownerId = 'the-new-owner'; + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }), + /has a new owner\. Ask for a new code/, + 409 + ); + assert.equal(fakes.transport.hooks.length, 0); + assert.equal((await store.pendingCodes(D)).length, 1, 'nothing was used up'); +}); + +test('a resume by a stopped link also records the owner who confirms', async () => { + const { link } = await bridged(); + await store.setLinkStatus(link.id, { status: 'failed', reason: 'permissions' }); + p.dGuild.ownerId = 'discord-owner-2'; + const out = await resumeLink({ guildKey: D, id: link.id, actor: 'A' }); + assert.equal(out.active, true); + assert.equal((await store.getLink(link.id)).ownerA, 'discord-owner-2'); +}); + +// temporary errors on a delete + +test('a delete that hits a temporary error is tried again, and the map entry stays until the copy is gone', async () => { + const { dCh, link } = await bridged(); + const m = fromD(dCh, { content: 'delete me' }); + await relay(m, D); + fakes.transport.removeFailures.push(apiError(500), apiError(502)); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.equal(fakes.transport.removes.length, 1, 'removed on the third try'); + assert.deepEqual(fakes.sleeps, [1000, 2000], 'with growing pauses'); + assert.equal(await store.mappingOf(link.id, 'a', m.id), null, 'the entry went with the copy'); + + // it keeps failing: the entry stays, so a later delete can still find the copy + const m2 = fromD(dCh, { content: 'stubborn' }); + await relay(m2, D); + for (let i = 0; i < 4; i += 1) fakes.transport.removeFailures.push(apiError(503)); + await handleDelete({ id: m2.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.ok(await store.mappingOf(link.id, 'a', m2.id), 'the copy is still known'); + assert.equal((await store.getLink(link.id)).status, 'active', 'and the link is not stopped for it'); +}); + +test('a delete that cannot be done for good (no webhook, no Manage Messages) keeps the map entry', async () => { + const { dCh, link } = await bridged(); + const m = fromD(dCh, { content: 'cannot be removed' }); + await relay(m, D); + fakes.transport.removeFailures.push(apiError(404, { code: 10015 })); + fakes.transport.botDeleteFailures.push(apiError(403)); + await handleDelete({ id: m.id, channelId: dCh.id }, {}, D); + await bridgeIdle(); + assert.ok(await store.mappingOf(link.id, 'a', m.id)); +}); + +// the download budget is held until the send is over + +function budgetParts(files, gate) { + const sent = []; + const transportSend = fakes.transport.send; + fakes.transport.send = async (key, hook, payload) => { + if (payload.files?.length && gate) await gate.open; + sent.push(payload); + return transportSend(key, hook, payload); + }; + useBridgeParts({ + transport: fakes.transport, + queue: fakes.queue, + budget: { concurrent: 1, bytes: 1024 }, + download: async (url) => { + fakes.downloads.push({ url }); + return { ok: true, status: 200, body: Buffer.from(files[url] ?? 'x') }; + }, + }); + return sent; +} +const pairOfLinks = async (suffix) => { + const one = await bridged(); + const other = p.extra(suffix); + const otherF = `fluxer:${other.fGuild.id}`; + await setGuildModule(other.dGuild.id, 'bridge', { enabled: true }); + await setGuildModule(otherF, 'bridge', { enabled: true }); + const dCh2 = p.channel(other.dGuild, {}); + const fCh2 = p.channel(other.fGuild, {}); + const made = await requestLink({ guildKey: other.dGuild.id, channelId: dCh2.id, actor: 'A' }); + await acceptLink({ guildKey: otherF, channelId: fCh2.id, code: made.code, actor: 'B' }); + return { one, dCh2, otherKey: other.dGuild.id }; +}; +const file = (name) => ({ attachments: new Map([['a', { name, url: `https://cdn/${name}`, size: 4 }]]) }); + +test('the download budget stays held while the copy is being sent, not only while the file downloads', async () => { + const gate = waitFor(); + budgetParts({}, gate); + const { one, dCh2, otherKey } = await pairOfLinks('96'); + await handleMessage(fromD(one.dCh, file('first.png')), {}, D); // downloaded, then the send waits at the gate + await new Promise((r) => setImmediate(r)); + assert.deepEqual( + fakes.downloads.map((d) => d.url), + ['https://cdn/first.png'] + ); + await handleMessage(fromD(dCh2, file('second.png')), {}, otherKey); + await new Promise((r) => setImmediate(r)); + assert.deepEqual( + fakes.downloads.map((d) => d.url), + ['https://cdn/first.png'], + 'the first file is still held: no room' + ); + assert.equal(fakes.transport.sends.at(-1).payload.content, '📎 second.png: https://cdn/second.png'); + gate.release(); + await bridgeIdle(); + // the copy is out: the budget is free again + await relay(fromD(dCh2, file('third.png')), otherKey); + assert.equal(fakes.downloads.at(-1).url, 'https://cdn/third.png'); + assert.equal(fakes.transport.sends.at(-1).payload.files[0].name, 'third.png'); +}); + +test('the download budget is given back when a send fails for good', async () => { + budgetParts({}, null); + const { one, dCh2, otherKey } = await pairOfLinks('97'); + for (let i = 0; i < 4; i += 1) fakes.transport.sendFailures.push(apiError(500)); + await relay(fromD(one.dCh, file('lost.png')), D); // four tries, then given up + assert.equal(fakes.transport.sends.length, 0); + await relay(fromD(dCh2, file('next.png')), otherKey); + assert.equal(fakes.transport.sends.at(-1).payload.files[0].name, 'next.png', 'the budget was free again'); +}); + +// an edit with a token that cannot be read + +test('an edit with an unreadable webhook token makes a new webhook and does not count as a failed delivery', async () => { + const { dCh, link } = await bridged(); + const m = fromD(dCh, { content: 'first' }); + await relay(m, D); + db.prepare('UPDATE bridge_links SET hook_b_token = ? WHERE id = ?').run( + seal('another secret', 'bridge-webhook', 'x'), + link.id + ); + dropBridgeCache(); + const hooks = fakes.transport.hooks.length; + for (let i = 0; i < FAIL_THRESHOLD + 2; i += 1) { + await handleUpdate({ old: m, new: { ...m, content: `edit ${i}` } }, {}, D); + await bridgeIdle(); + } + assert.equal(fakes.transport.hooks.length, hooks + 1, 'one new webhook, once'); + assert.equal(fakes.transport.edits.length, 0, 'the old copy cannot be edited by the new webhook'); + assert.equal((await store.getLink(link.id)).status, 'active', 'never stopped for it'); + await relay(fromD(dCh, { content: 'and new messages cross' }), D); + assert.equal(fakes.transport.sends.at(-1).hook.id, (await store.getLink(link.id)).hookBId); +}); + +// a deleted channel on a link that is not running + +test('a bridged channel deleted while the link is paused is shown as a problem', async () => { + const { dCh, link } = await bridged(); + await pauseLink({ guildKey: D, id: link.id, actor: 'A' }); + await handleChannelDelete(dCh, {}, D); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.pausedSide, row.failReason], ['paused', 'a', 'channel-deleted']); + assert.deepEqual( + (await bridgeProblems(D)).map((x) => [x.linkId, x.code]), + [[link.id, 'channel-deleted']] + ); + assert.deepEqual( + (await bridgeProblems(F)).map((x) => x.code), + ['channel-deleted'], + 'the other server sees it too' + ); + assert.match(linkView(row, D).pauseNote, /channel was deleted/); + p.dGuild.channels.cache.delete(dCh.id); // it is gone on the platform + await refuses(resumeLink({ guildKey: D, id: link.id, actor: 'A' }), /normal text channel/); +}); + +test('a stopped link whose channel is deleted later says so', async () => { + const { dCh, link } = await bridged(); + await failLink(link.id, 'unreachable'); + await handleChannelDelete(dCh, {}, D); + const row = await store.getLink(link.id); + assert.deepEqual([row.status, row.failReason], ['failed', 'channel-deleted']); +}); diff --git a/test/bridgeHealth.test.js b/test/bridgeHealth.test.js index 5c324468..6f9aa214 100644 --- a/test/bridgeHealth.test.js +++ b/test/bridgeHealth.test.js @@ -7,7 +7,17 @@ import { setGuildModule } from '../src/db/modules.js'; import { purgeGuild } from '../src/db/purge.js'; import { buildHealthReport } from '../src/web/lib/permissionHealth.js'; import { keyedGuild } from '../src/platform/platformOf.js'; -import { acceptLink, failLink, requestLink, useBridgeParts, dropBridgeCache } from '../src/modules/bridge.js'; +import { + acceptLink, + dropBridgeCache, + failLink, + handleChannelDelete, + pauseLink, + requestLink, + resumeLink, + sweepOwners, + useBridgeParts, +} from '../src/modules/bridge.js'; import { D, F, installPlatforms, removePlatforms, useFakes } from './helpers/fakeBridge.js'; let p; @@ -71,3 +81,22 @@ test('a server with the module off is not checked', async () => { }); assert.deepEqual(report.checks, []); }); + +test('a paused link whose owner changed, or whose channel was deleted, is a problem too', async () => { + const dCh = p.channel(p.dGuild, { name: 'discord-chat' }); + const fCh = p.channel(p.fGuild, { name: 'fluxer-chat' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'A' }); + const link = await acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'B' }); + p.fGuild.ownerId = 'new owner'; + await sweepOwners(); + const { checks } = await forBridge(keyedGuild(p.dGuild)); + const owner = checks.find((c) => c.code === 'bridge-stopped'); + assert.ok(owner, 'listed although it is paused, not stopped'); + assert.match(owner.message, /owner of one of the servers changed/); + + await resumeLink({ guildKey: F, id: link.id, actor: 'B' }); + await pauseLink({ guildKey: D, id: link.id, actor: 'A' }); + await handleChannelDelete(dCh, {}, D); + const after = await forBridge(keyedGuild(p.dGuild)); + assert.match(after.checks.find((c) => c.code === 'bridge-stopped').message, /channel was deleted/); +}); diff --git a/test/helpers/fakeBridge.js b/test/helpers/fakeBridge.js index 3b1b5d8f..860f36b1 100644 --- a/test/helpers/fakeBridge.js +++ b/test/helpers/fakeBridge.js @@ -49,7 +49,7 @@ function makeGuild(client, { id, name }) { id, name, client, - ownerId: 'owner', + ownerId: `owner-of-${id}`, // each server has its own owner (a platform user id) nsfw: false, // the server's own age restriction (Fluxer) channels: { cache: new Map() }, emojis: { cache: new Map() }, diff --git a/test/v2.accessLevels.test.js b/test/v2.accessLevels.test.js index 68a2c4f7..25d0f348 100644 --- a/test/v2.accessLevels.test.js +++ b/test/v2.accessLevels.test.js @@ -145,8 +145,6 @@ const WRITES = [ ['/modules/afk', { enabled: true }, 200], ['/settings', { botMasterRoleIds: [MASTER], modlogChannelId: '' }, 200], ['/modules/afk/config', {}, 200], - ['/modules/bridge/links/999999/pause', {}, 404], - ['/modules/bridge/links/999999/options', { options: { bots: true } }, 404], ['/moderation/warnings/clear', { userId: '810000000000000001' }, 404], ['/access', { viewRoleIds: [VIEW, OTHER_ROLE] }, 200], ['/setup/apply', { items: [] }, 400], diff --git a/test/v2Bridge.test.js b/test/v2Bridge.test.js index 945561c2..adc0ff66 100644 --- a/test/v2Bridge.test.js +++ b/test/v2Bridge.test.js @@ -23,7 +23,7 @@ const store = await import('../src/db/bridge.js'); const { listAudit } = await import('../src/db/audit.js'); const { purgeGuild } = await import('../src/db/purge.js'); const { ROUTE_ACCESS } = await import('../src/web/lib/routeAccess.js'); -const { dropBridgeCache } = await import('../src/modules/bridge.js'); +const { dropBridgeCache, sweepOwners } = await import('../src/modules/bridge.js'); const { useFakes } = await import('./helpers/fakeBridge.js'); const F_KEY = `fluxer:${F_GUILD}`; @@ -65,6 +65,8 @@ beforeEach(async () => { fGuild.nsfw = false; dGuild.members.cache.clear(); fGuild.members.cache.clear(); + for (const guild of [dGuild, fGuild, runtime.clients.discord.guilds.cache.get(OTHER_D_GUILD)]) + guild.ownerId = 'x'; addChannel(dGuild, CH.d1, 'discord-main'); addChannel(dGuild, CH.d2, 'discord-two'); addChannel(dGuild, CH.dNsfw, 'discord-nsfw', true); @@ -91,6 +93,11 @@ async function person(platform, { guilds, hidden = [], member = true } = {}) { if (guilds) profiles[platform].guilds = guilds; const b = ownBrowser(); await signIn(b, platform); + // The platform reports the person as the owner of every server the profile lists as theirs. + const client = platform === 'discord' ? runtime.clients.discord : runtime.clients.fluxer; + for (const g of profiles[platform].guilds) { + if (g.owner === true) client.guilds.cache.get(g.id).ownerId = profiles[platform].id; + } // The person's member in the server (the channel rights are read from it); `hidden` channels they cannot see. if (member) { const id = profiles[platform].id; @@ -448,3 +455,79 @@ test('a Fluxer channel in an age-restricted category or server is refused for an 'clear: it links' ); }); + +const ALL_POSTS = (key) => + [ + ['/request', { channelId: CH.d1 }], + ['/accept', { code: 'ABCD-EFGH-JKMN', channelId: CH.d1 }], + ['/requests/1/cancel', {}], + ['/links/1/options', { options: { bots: true } }], + ['/links/1/pause', {}], + ['/links/1/resume', {}], + ['/links/1/unlink', {}], + ].map(([path, body]) => [`${base(key)}${path}`, body]); + +test('an Administrator who is not the owner cannot do anything to a bridge, but can see its status', async () => { + // a real link, made by the owners + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + const id = (await store.linksOfGuild(D_GUILD))[0].id; + const before = await store.getLink(id); + + const admin = await person('discord', { guilds: [{ id: D_GUILD, owner: false, permissions: '8' }] }); + const read = await admin.get(`${base(D_GUILD)}/config`); + assert.equal(read.status, 200, 'Administrator reads the status (View)'); + const seen = await json(read); + assert.equal(seen.owner, false); + assert.equal(seen.links.length, 1); + for (const [path, body] of ALL_POSTS(D_GUILD)) { + const res = await admin.post(path.replace('/links/1/', `/links/${id}/`), body); + assert.equal(res.status, 403, path); + assert.match((await json(res)).error, /Only the server owner/, path); + } + assert.deepEqual(await store.getLink(id), before, 'nothing changed'); + assert.equal((await store.pendingCodes(D_GUILD)).length, 0); + + // the same on the Fluxer side + const fAdminOnly = await person('fluxer', { guilds: [{ id: F_GUILD, owner: false, permissions: '8' }] }); + for (const [path, body] of ALL_POSTS(F_KEY)) { + const res = await fAdminOnly.post(path.replace('/links/1/', `/links/${id}/`), body); + assert.equal(res.status, 403, path); + assert.match((await json(res)).error, /Only the server owner/, path); + } + assert.equal((await store.getLink(id)).status, 'active'); + + // the owner of the server reads `owner: true` and can act + assert.equal((await json(await d.get(`${base(D_GUILD)}/config`))).owner, true); + assert.equal((await d.post(`${base(D_GUILD)}/links/${id}/pause`)).status, 200); +}); + +test('a transfer of ownership pauses the link, and only the new owner can resume it', async () => { + const d = await dAdmin(); + const f = await fAdmin(); + const { code } = await json(await d.post(`${base(D_GUILD)}/request`, { channelId: CH.d1 })); + await f.post(`${base(F_KEY)}/accept`, { code, channelId: CH.f1 }); + const id = (await store.linksOfGuild(D_GUILD))[0].id; + + dGuild.ownerId = 'transfer-target'; // the Discord server is handed to someone else + assert.equal(await sweepOwners(), 1); + const paused = await store.getLink(id); + assert.deepEqual([paused.status, paused.pausedSide, paused.failReason], ['paused', 'a', 'owner-changed']); + + // the previous owner is no longer the owner: refused. The Fluxer owner cannot resume Discord's confirmation. + assert.equal((await d.post(`${base(D_GUILD)}/links/${id}/resume`)).status, 403); + const blocked = await f.post(`${base(F_KEY)}/links/${id}/resume`); + assert.equal(blocked.status, 403); + assert.match((await json(blocked)).error, /only they can resume/); + const view = (await json(await f.get(`${base(F_KEY)}/config`))).links[0]; + assert.deepEqual([view.status, view.pausedByThem], ['paused', true]); + assert.match(view.pauseNote, /owner of one of the servers changed/); + + const newOwner = await person('discord'); // becomes the owner the platform reports + const confirmed = await newOwner.post(`${base(D_GUILD)}/links/${id}/resume`); + assert.equal(confirmed.status, 200); + const row = await store.getLink(id); + assert.deepEqual([row.status, row.ownerA], ['active', dGuild.ownerId]); +}); diff --git a/web-v2/src/__tests__/Bridge.test.jsx b/web-v2/src/__tests__/Bridge.test.jsx index a00c489a..afbed9f9 100644 --- a/web-v2/src/__tests__/Bridge.test.jsx +++ b/web-v2/src/__tests__/Bridge.test.jsx @@ -42,6 +42,7 @@ const overview = (over = {}) => ({ platform: 'discord', bothPlatforms: true, enabled: true, + owner: true, limits: { links: 10, requests: 5 }, links: [], requests: [], @@ -88,6 +89,37 @@ describe('Bridge page', () => { expect(screen.getByRole('button', { name: 'Link the channels' }).disabled).toBe(true); }); + it('is read-only for everyone but the server owner, with a note that says so', async () => { + api.getBridge.mockResolvedValue( + overview({ + owner: false, + links: [link({ pauseNote: 'The owner of one of the servers changed.' })], + requests: [ + { + id: 3, + channelId: MAIN, + channel: 'general', + expiresAt: Date.now() + 900_000, + requestedBy: 'Alice', + }, + ], + }) + ); + renderAtGuild(, GUILD); + expect(await screen.findByText(/Only the server owner can ask for a link/)).toBeTruthy(); + expect(screen.getByText(/#general ↔ #chat/)).toBeTruthy(); + expect(screen.getByText('The owner of one of the servers changed.')).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Pause' }).disabled).toBe(true); + expect(screen.queryByRole('button', { name: 'More actions for #general' })).toBeNull(); + expect(screen.queryByRole('button', { name: 'More actions for the request of #general' })).toBeNull(); + expect(screen.getByRole('button', { name: 'Get a code' }).disabled).toBe(true); + expect(screen.getByRole('button', { name: 'Link the channels' }).disabled).toBe(true); + expect( + screen.getByLabelText('Share messages from bots', { selector: '#opt-7-bots' }).matches(':disabled') + ).toBe(true); + expect(screen.getByLabelText('Code').closest('fieldset').disabled).toBe(true); + }); + it('lists a link with the other side, its status, and what each side shares', async () => { api.getBridge.mockResolvedValue(overview({ links: [link({ queued: 3 })] })); renderAtGuild(, GUILD); diff --git a/web-v2/src/moduleForms/Bridge.jsx b/web-v2/src/moduleForms/Bridge.jsx index 62177344..e9988b5e 100644 --- a/web-v2/src/moduleForms/Bridge.jsx +++ b/web-v2/src/moduleForms/Bridge.jsx @@ -10,7 +10,6 @@ import { } from '../api.js'; import { notify } from '../notify.js'; import { useApiData } from '../useApiData.js'; -import { AccessLock } from '../access.jsx'; import LoadError from '../components/LoadError.jsx'; import HowItWorks from '../components/HowItWorks.jsx'; import CopyButton from '../components/CopyButton.jsx'; @@ -115,6 +114,8 @@ export default function Bridge() { const other = data.platform === 'fluxer' ? 'Discord' : 'Fluxer'; const here = PLATFORM[data.platform]; const ready = data.bothPlatforms && data.enabled; + // Only the owner of the server can change a bridge; everyone with View access reads the status. + const owner = data.owner === true; async function run(fn, done) { setBusy(true); @@ -186,9 +187,9 @@ export default function Bridge() {

- Both servers agree. One asks for a link and gets a code that works for 15 minutes; someone with Edit - access in the other server uses it and picks a channel. Sylo has to be in both servers. Either side - can unlink at any time. + Both servers agree. One asks for a link and gets a code that works for 15 minutes; the owner of the + other server uses it and picks a channel. Sylo has to be in both servers. Either side can unlink at + any time.

What crosses: messages, replies (as a short quote), files up to 8 MB (otherwise a link), custom @@ -244,6 +245,7 @@ export default function Bridge() { />

{link.problem ?

{link.problem.text}

: null} + {link.pauseNote ?

{link.pauseNote}

: null} {link.pausedByThem ? (

{link.theirs.server} paused this bridge. Only they can resume it. @@ -251,14 +253,14 @@ export default function Bridge() { ) : null}

What this server shares - +
onOption(link, patch)} /> - +

The other server decides for itself what it shares. Bot messages:{' '} {link.theirOptions.bots ? 'shared' : 'not shared'}. Who joins:{' '} @@ -272,18 +274,22 @@ export default function Bridge() { link.status === 'active' ? { label: 'Pause', - disabled: busy, + disabled: busy || !owner, onClick: () => run(() => bridgeLinkAction(guildId, link.id, 'pause')), } : canResume ? { label: 'Resume', - disabled: busy, + disabled: busy || !owner, onClick: () => run(() => bridgeLinkAction(guildId, link.id, 'resume')), } : null } - items={[{ label: 'Unlink', danger: true, disabled: busy, onClick: () => onUnlink(link) }]} + items={ + owner + ? [{ label: 'Unlink', danger: true, disabled: busy, onClick: () => onUnlink(link) }] + : [] + } /> ); @@ -305,14 +311,18 @@ export default function Bridge() { onCancelRequest(r), - }, - ]} + items={ + owner + ? [ + { + label: 'Cancel request', + danger: true, + disabled: busy, + onClick: () => onCancelRequest(r), + }, + ] + : [] + } /> ))} @@ -320,12 +330,18 @@ export default function Bridge() { ) : null} - + {!owner ? ( +

+ Only the server owner can ask for a link, use a code, change what this server shares, or pause, + resume and unlink a bridge. You can see the status here. +

+ ) : null} +

Ask the other server

- Pick the channel here that should be bridged. You get a one-time code to give to someone with Edit - access in the {other} server. + Pick the channel here that should be bridged. You get a one-time code to give to the owner of the{' '} + {other} server.

@@ -404,7 +420,7 @@ export default function Bridge() {
- +
); }