From c51a5ce2788f34d377d988a68087c348c007890a Mon Sep 17 00:00:00 2001 From: l1s5 Date: Mon, 2 Nov 2026 11:48:21 +0900 Subject: [PATCH] Fix points of HTML injection in d2m converter This commit contains property tests to check all messages in `data.js` for unescaped HTML passing through the d2m converter. It also contains fixes for the instances that were discovered. --- src/d2m/converters/message-to-event.js | 18 +-- src/d2m/converters/message-to-event.test.js | 141 +++++++++++++++++++- 2 files changed, 149 insertions(+), 10 deletions(-) diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index a3555f2..012f646 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -46,9 +46,9 @@ function getDiscordParseCallbacks(message, guild, useHTML, spoilers = []) { || (message.author?.id === node.id ? message.author.username : null) || "unknown-user" if (mxid && useHTML) { - return `@${username}` + return tag`@${username}` } else { - return `@${username}:` + return useHTML ? tag`@${username}:` : `@${username}:` } }, /** @param {{id: string, type: "discordChannel", row: {room_id: string, name: string, nick: string?}?, via: string}} node */ @@ -56,12 +56,12 @@ function getDiscordParseCallbacks(message, guild, useHTML, spoilers = []) { if (!node.row) { // fallback for when this channel is not bridged const channel = discord.channels.get(node.id) if (channel) { - return `#${channel.name} [channel not bridged]` + return useHTML ? tag`#${channel.name} [channel not bridged]` : `#${channel.name} [channel not bridged]` } else { return `#unknown-channel [channel from an unbridged server]` } } else if (useHTML) { - return `#${node.row.nick || node.row.name}` + return tag`#${node.row.nick || node.row.name}` } else { return `#${node.row.nick || node.row.name}` } @@ -99,9 +99,9 @@ function getDiscordParseCallbacks(message, guild, useHTML, spoilers = []) { // If the cache breaks, fix discord-packets.js to store role info properly. return "@&" + node.id } else if (useHTML && role.color) { - return `@${role.name}` + return tag`@${role.name}` } else if (useHTML) { - return `@${role.name}` + return tag`@${role.name}` } else { return `@${role.name}:` } @@ -706,7 +706,7 @@ async function messageToEvent(message, guild, options = {}, di) { if (((message.flags || 0) & DiscordTypes.MessageFlags.IsCrosspost) && message.author?.username) { body = `[↷ ${message.author.username}]\n` + body - html = `↷ ${message.author.username}
` + html + html = tag`↷ ${message.author.username}
` + html } // Fallback body/formatted_body for replies @@ -737,7 +737,7 @@ async function messageToEvent(message, guild, options = {}, di) { repliedToUserHtml = tag`${repliedToDisplayName}` } else { repliedToDisplayName = referenced.author.global_name || referenced.author.username || "a Discord user" - repliedToUserHtml = repliedToDisplayName + repliedToUserHtml = tag`${repliedToDisplayName}` } // Content @@ -767,7 +767,7 @@ async function messageToEvent(message, guild, options = {}, di) { body = `> ${repliedToDisplayName} joined the room\n\n` + body } else { // repliedToUnknownEvent const dateDisplay = dUtils.howOldUnbridgedMessage(referenced.timestamp, message.timestamp) - html = `
In reply to ${dateDisplay} from ${repliedToDisplayName}:` + html = tag`
In reply to ${dateDisplay} from ${repliedToDisplayName}:` + `
${repliedToHtml}
` + html body = `In reply to ${dateDisplay}:\n${repliedToDisplayName}: ${repliedToBody}`.split("\n").map(line => "> " + line).join("\n") diff --git a/src/d2m/converters/message-to-event.test.js b/src/d2m/converters/message-to-event.test.js index 3f806e8..cd69c18 100644 --- a/src/d2m/converters/message-to-event.test.js +++ b/src/d2m/converters/message-to-event.test.js @@ -1,10 +1,11 @@ const {test} = require("supertape") +const domino = require("domino") const {messageToEvent} = require("./message-to-event") const {MatrixServerError} = require("../../matrix/mreq") const data = require("../../../test/data") const {mockGetEffectivePower} = require("../../matrix/utils.test") const Ty = require("../../types") -const {db} = require("../../passthrough") +const {db, discord} = require("../../passthrough") /** * @param {string} roomID @@ -2033,3 +2034,141 @@ test("message2event: smalltext from other webhook is intact", async t => { formatted_body: "hmm" }]) }) + +// Property tests: +// A canary is appended to potentially user-controlled fields in a discord event +// and should never come out as real HTML. + +const CANARY = `"'>` + +const DISCORD_GENERATED_FIELDS = new Set([ + "application_id", + "avatar", + "channel_id", + "discriminator", + "edited_timestamp", + "guild_id", + "id", + "identity_guild_id", + "message_id", + "proxy_icon_url", + "proxy_url", + "reference_id", + "target_message_id", + "timestamp", + "webhook_id", +]) + +/** + * Paths to every field in a discord event that a user could have written. + * @returns {Generator} + */ +function* userControlledFields(value, path = []) { + for (const [k, v] of Object.entries(value)) { + if (typeof v === "string") { + if (!DISCORD_GENERATED_FIELDS.has(k)) yield [...path, k] + } else if (v && typeof v === "object") { + yield* userControlledFields(v, [...path, k]) + } + } +} + +/** + * Return a deep copy of `value` with a canary appended to the string at `path`. + * @param {Object} value + * @param {string[]} path + */ +function withCanary(value, path) { + const copy = structuredClone(value) + let parent = copy + for (const k of path.slice(0, -1)) { + parent = parent[k] + } + parent[path.at(-1)] += CANARY + return copy +} + +// This covers most of the API calls that messageToEvent makes. +const PROPERTY_TEST_DI = { + api: { + getEffectivePower: mockGetEffectivePower(), + getJoinedMembers: async () => ({ joined: {} }), + // Used for discord replies to matrix events + getEvent: async (roomID, eventID) => ({ + type: "m.room.message", + sender: "@cadence:cadence.moe", + content: { msgtype: "m.text", body: "hi" }, + event_id: eventID, + room_id: roomID, + origin_server_ts: 0, + unsigned: {}, + }), + // Used for historical events + getEventForTimestamp: async () => ({ + event_id: "$canary", + origin_server_ts: 0, + }), + }, +} + +async function assertCanaryEscaped(t, message, guild) { + let events + try { + events = await messageToEvent(message, guild, {}, PROPERTY_TEST_DI) + } catch (e) { + // The file upload guard in test.js may throw. + return t.pass(`not checked, conversion threw: ${e.message.split("\n")[0]}`) + } + const leaked = events.find(event => event.formatted_body && domino.createDocument(`${event.formatted_body}`).querySelector("x-canary")) + t.equal(leaked?.formatted_body, undefined, "canary should stay escaped") +} + +const MESSAGE_CATEGORIES = [ + "message", + "pk_message", + "message_mention_everyone", + "message_with_embeds", + "message_with_components", + "special_message", + "interaction_message", +] + +for (const category of MESSAGE_CATEGORIES) { + for (const [name, message] of Object.entries(data[category])) { + for (const path of userControlledFields(message)) { + test(`message2event: ${category}.${name}.${path.join(".")} never becomes HTML`, async t => { + await assertCanaryEscaped(t, withCanary(message, path), data.guild.general) + }) + } + } +} + +// Channel and role names need to be edited in the database/discord objects. +for (const category of MESSAGE_CATEGORIES) { + for (const [name, message] of Object.entries(data[category])) { + test(`message2event: channel/role names in ${category}.${name} never become HTML`, async t => { + const guild = structuredClone(data.guild.general) + for (const role of guild.roles) { + role.name += CANARY + } + + const channelNames = new Map([...discord.channels].map(([id, channel]) => [id, channel.name])) + for (const channel of discord.channels.values()) { + if (channel.name) { + channel.name += CANARY + } + } + + db.prepare("UPDATE channel_room SET name = name || ?, nick = nick || ?").run(CANARY, CANARY) + + try { + await assertCanaryEscaped(t, message, guild) + } finally { + db.prepare("UPDATE channel_room SET name = replace(name, ?, ''), nick = replace(nick, ?, '')").run(CANARY, CANARY) + for (const [id, name] of channelNames) { + discord.channels.get(id).name = name + } + } + }) + } +} -- 2.34.1