Skip to content

A platform for community discussion. Free, open, simple.

FIX: BBCode attribute values with quotation marks produce malformed output (#40016)

Juan David Martínez Cubillosefb59140a6c5
diff --git a/frontend/discourse-markdown-it/src/features/bbcode-block.js b/frontend/discourse-markdown-it/src/features/bbcode-block.js
index 631bc0e4adc..462a7ef8d2f 100644
--- a/frontend/discourse-markdown-it/src/features/bbcode-block.js
+++ b/frontend/discourse-markdown-it/src/features/bbcode-block.js
@@ -34,7 +34,18 @@ function trailingSpaceOnly(src, start, max) {
// Most common quotation marks.
// More can be found at https://en.wikipedia.org/wiki/Quotation_mark
-const QUOTATION_MARKS = [`""`, `''`, `“”`, `””`, `‘’`, `„“`, `‚’`, `«»`, `‹›`];
+// Keep in sync with plugins/chat/lib/chat/transcript_service.rb
+export const QUOTATION_MARKS = [
+ `""`,
+ `''`,
+ `«»`,
+ `“”`,
+ `””`,
+ `‘’`,
+ `„“`,
+ `‚’`,
+ `‹›`,
+];
const QUOTATION_MARKS_PATTERN = new RegExp(
`[${QUOTATION_MARKS.join("")}]`,
diff --git a/frontend/discourse/app/lib/text.js b/frontend/discourse/app/lib/text.js
index 9d19761219d..faefe061d43 100644
--- a/frontend/discourse/app/lib/text.js
+++ b/frontend/discourse/app/lib/text.js
@@ -6,6 +6,7 @@ import deprecated from "discourse/lib/deprecated";
import { getURLWithCDN } from "discourse/lib/get-url";
import { helperContext } from "discourse/lib/helpers";
import { i18n } from "discourse-i18n";
+import { QUOTATION_MARKS } from "discourse-markdown-it/features/bbcode-block";
async function withEngine(name, ...args) {
const engine = await waitForPromise(import("discourse/static/markdown-it"));
@@ -162,6 +163,8 @@ const BBCODE_REQUIRES_QUOTES_PATTERN = /[\s\]]/;
*
* Automatically determines whether quotes are needed based on the value content.
* Quotes are required when the value contains whitespace or `]` characters.
+ * When the value contains quotation marks, cycles through supported quote pairs
+ * to find one that doesn't conflict with the value's content.
*
* @param {string|null|undefined} value - The attribute value to serialize
* @param {string} name - The attribute name
@@ -170,6 +173,8 @@ const BBCODE_REQUIRES_QUOTES_PATTERN = /[\s\]]/;
* @example
* serializeBBCodeAttr("12:00:00", "time") // returns ' time=12:00:00'
* serializeBBCodeAttr("YYYY-MM-DD HH:mm", "format") // returns ' format="YYYY-MM-DD HH:mm"'
+ * serializeBBCodeAttr('Design "Gems"', "channel") // returns " channel='Design \"Gems\"'"
+ * serializeBBCodeAttr("Sam's \"Release\"", "title") // returns ' title=«Sam's "Release"»'
* serializeBBCodeAttr(null, "time") // returns ''
*/
export function serializeBBCodeAttr(value, name) {
@@ -180,7 +185,19 @@ export function serializeBBCodeAttr(value, name) {
const stringValue = String(value);
const needsQuotes = BBCODE_REQUIRES_QUOTES_PATTERN.test(stringValue);
- return needsQuotes ? ` ${name}="${stringValue}"` : ` ${name}=${stringValue}`;
+ if (!needsQuotes) {
+ return ` ${name}=${stringValue}`;
+ }
+
+ for (const pair of QUOTATION_MARKS) {
+ const [open, close] = pair;
+ if (!stringValue.includes(open) && !stringValue.includes(close)) {
+ return ` ${name}=${open}${stringValue}${close}`;
+ }
+ }
+
+ // Unreachable in practice - would require all 18 quote characters in value
+ return ` ${name}="${stringValue.replaceAll('"', "")}"`;
}
/**
diff --git a/frontend/discourse/tests/unit/lib/pretty-text-test.js b/frontend/discourse/tests/unit/lib/pretty-text-test.js
index 8f7a5a272ab..49466cd2548 100644
--- a/frontend/discourse/tests/unit/lib/pretty-text-test.js
+++ b/frontend/discourse/tests/unit/lib/pretty-text-test.js
@@ -9,6 +9,7 @@ import QUnit, { module, test } from "qunit";
import { deepMerge } from "discourse/lib/object";
import DiscourseMarkdownIt from "discourse-markdown-it";
import { extractDataAttribute } from "discourse-markdown-it/engine";
+import { QUOTATION_MARKS } from "discourse-markdown-it/features/bbcode-block";
const rawOpts = {
siteSettings: {
@@ -966,6 +967,29 @@ eviltrout</p>
'<p><a href="https://discourse.org/path" data-bbcode="true"><span class="bbcode-b">discourse.org/path</span></a></p>',
"paths are correctly handled"
);
+
+ assert.cooked(
+ `[url='http://example.com/path?a=1']single quoted[/url]`,
+ '<p><a href="http://example.com/path?a=1" data-bbcode="true">single quoted</a></p>',
+ "single-quoted attributes are parsed correctly"
+ );
+
+ assert.cooked(
+ `[url='http://example.com/"test"']url with double quotes[/url]`,
+ '<p><a href="http://example.com/&quot;test&quot;" data-bbcode="true">url with double quotes</a></p>',
+ "single-quoted attributes can contain double quotes"
+ );
+
+ QUOTATION_MARKS.forEach((pair, index) => {
+ const [open, close] = pair;
+
+ // Test that each delimiter can wrap a URL
+ assert.cooked(
+ `[url=${open}http://example.com/test${close}]test[/url]`,
+ `<p><a href="http://example.com/test" data-bbcode="true">test</a></p>`,
+ `delimiter ${index} (${open}${close}) parses correctly`
+ );
+ });
});
test("images", function (assert) {
diff --git a/frontend/discourse/tests/unit/lib/text-test.js b/frontend/discourse/tests/unit/lib/text-test.js
index 7783a1b48c2..7078cdec6d8 100644
--- a/frontend/discourse/tests/unit/lib/text-test.js
+++ b/frontend/discourse/tests/unit/lib/text-test.js
@@ -1,7 +1,15 @@
import { setupTest } from "ember-qunit";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import { module, test } from "qunit";
-import { cook, excerpt, parseAsync, parseMentions } from "discourse/lib/text";
+import {
+ buildBBCodeAttrs,
+ cook,
+ excerpt,
+ parseAsync,
+ parseMentions,
+ serializeBBCodeAttr,
+} from "discourse/lib/text";
+import { QUOTATION_MARKS } from "discourse-markdown-it/features/bbcode-block";
module("Unit | Utility | text", function (hooks) {
setupTest(hooks);
@@ -74,3 +82,124 @@ module("Unit | Utility | text | parseMentions", function (hooks) {
assert.strictEqual(mentions.length, 0);
});
});
+
+module("Unit | Utility | text | serializeBBCodeAttr", function () {
+ test("returns empty string for falsy values", function (assert) {
+ assert.strictEqual(serializeBBCodeAttr(null, "name"), "");
+ assert.strictEqual(serializeBBCodeAttr(undefined, "name"), "");
+ assert.strictEqual(serializeBBCodeAttr("", "name"), "");
+ });
+
+ test("serializes simple values without quotes", function (assert) {
+ assert.strictEqual(serializeBBCodeAttr("value", "name"), " name=value");
+ assert.strictEqual(
+ serializeBBCodeAttr("12:00:00", "time"),
+ " time=12:00:00"
+ );
+ });
+
+ test("serializes values with whitespace using double quotes", function (assert) {
+ assert.strictEqual(
+ serializeBBCodeAttr("hello world", "name"),
+ ' name="hello world"'
+ );
+ });
+
+ test("serializes values with ] using quotes", function (assert) {
+ assert.strictEqual(
+ serializeBBCodeAttr("value]test", "name"),
+ ' name="value]test"'
+ );
+ });
+
+ test("uses single quotes when value contains double quotes", function (assert) {
+ assert.strictEqual(
+ serializeBBCodeAttr('Design "Gems" Discussion', "channel"),
+ " channel='Design \"Gems\" Discussion'"
+ );
+ });
+
+ test("uses double quotes when value contains single quotes", function (assert) {
+ assert.strictEqual(
+ serializeBBCodeAttr("it's great", "title"),
+ ' title="it\'s great"'
+ );
+ });
+
+ test("uses guillemets when both ASCII quote types present", function (assert) {
+ assert.strictEqual(
+ serializeBBCodeAttr(`Sam's "Release" discussion`, "title"),
+ ` title=«Sam's "Release" discussion»`
+ );
+ });
+
+ test("uses non-conflicting delimiter and preserves value", function (assert) {
+ QUOTATION_MARKS.forEach((pair, index) => {
+ const [open, close] = pair;
+
+ // Create a value containing all delimiter characters except the one being tested
+ let conflictingChars = Array.from(QUOTATION_MARKS);
+ conflictingChars = conflictingChars.filter(
+ (p) => !p.includes(open) && !p.includes(close)
+ );
+ conflictingChars = conflictingChars.join("");
+ const value = `test ${conflictingChars} value`;
+ const result = serializeBBCodeAttr(value, "attr");
+
+ // Find which delimiter was used
+ const usedPair = QUOTATION_MARKS.find(
+ ([o, c]) => result.startsWith(` attr=${o}`) && result.endsWith(c)
+ );
+
+ // Assert a non-conflicting delimiter was used
+ const hasConflict =
+ usedPair &&
+ !value.includes(usedPair[0]) &&
+ !value.includes(usedPair[1]);
+
+ assert.true(
+ hasConflict,
+ `delimiter ${index} (${open}${close}): a non-conflicting delimiter is used`
+ );
+
+ // Assert the value is preserved
+ assert.true(
+ result.includes(value),
+ `delimiter ${index} (${open}${close}): value is preserved`
+ );
+ });
+ });
+});
+
+module("Unit | Utility | text | buildBBCodeAttrs", function () {
+ test("builds attributes string from object", function (assert) {
+ const attrs = { foo: "bar", baz: "qux" };
+ assert.strictEqual(buildBBCodeAttrs(attrs), "foo=bar baz=qux");
+ });
+
+ test("skips null and undefined values", function (assert) {
+ const attrs = { foo: "bar", skip: null, also: undefined, baz: "qux" };
+ assert.strictEqual(buildBBCodeAttrs(attrs), "foo=bar baz=qux");
+ });
+
+ test("skips specified attributes", function (assert) {
+ const attrs = { foo: "bar", skip: "this", baz: "qux" };
+ assert.strictEqual(
+ buildBBCodeAttrs(attrs, { skipAttrs: ["skip"] }),
+ "foo=bar baz=qux"
+ );
+ });
+
+ test("quotes values with whitespace", function (assert) {
+ const attrs = { name: "hello world" };
+ assert.strictEqual(buildBBCodeAttrs(attrs), 'name="hello world"');
+ });
+
+ test("switches to single quotes for values with double quotes", function (assert) {
+ const attrs = { channel: 'Design "Gems" :tada:' };
+ assert.strictEqual(
+ buildBBCodeAttrs(attrs),
+ "channel='Design \"Gems\" :tada:'"
+ );
+ });
+});
diff --git a/plugins/chat/lib/chat/transcript_service.rb b/plugins/chat/lib/chat/transcript_service.rb
index bba48e058c1..be08f6dd8bf 100644
--- a/plugins/chat/lib/chat/transcript_service.rb
+++ b/plugins/chat/lib/chat/transcript_service.rb
@@ -20,6 +20,9 @@ module Chat
MULTIQUOTE_ATTR = "multiQuote=\"true\""
NO_LINK_ATTR = "noLink=\"true\""
+ # Keep in sync with frontend/discourse-markdown-it/src/features/bbcode-block.js
+ QUOTATION_MARKS = ["\"\"", "''", "«»", "“”", "””", "‘’", "„“", "‚’", "‹›"].freeze
+
class TranscriptBBCode
attr_reader :channel,
:multiquote,
@@ -128,7 +131,7 @@ module Chat
end
def channel_attr
- "channel=\"#{channel.title(@acting_user)}\""
+ serialize_attr("channel", channel.title(@acting_user))
end
def channel_id_attr
@@ -144,7 +147,17 @@ module Chat
thread_title = thread.title.presence || I18n.t("chat.transcript.default_thread_title")
thread_title += " (#{range})" if range.present?
- "threadTitle=\"#{thread_title}\""
+ serialize_attr("threadTitle", thread_title)
+ end
+
+ def serialize_attr(name, value)
+ QUOTATION_MARKS.each do |pair|
+ open, close = pair.chars
+ next if value.include?(open) || value.include?(close)
+ return "#{name}=#{open}#{value}#{close}"
+ end
+
+ "#{name}=\"#{value.delete('"')}\""
end
end
diff --git a/plugins/chat/spec/system/rich_editor_extension_transcript_spec.rb b/plugins/chat/spec/system/rich_editor_extension_transcript_spec.rb
index f5b8556652d..e394dd315b4 100644
--- a/plugins/chat/spec/system/rich_editor_extension_transcript_spec.rb
+++ b/plugins/chat/spec/system/rich_editor_extension_transcript_spec.rb
@@ -9,11 +9,23 @@ describe "chat transcripts in rich editor" do
)
end
fab!(:channel, :chat_channel)
+ fab!(:channel_with_quotation_marks) do
+ Fabricate(:chat_channel, name: 'Channel with a "special" name')
+ end
fab!(:message_1) do
Fabricate(:chat_message, user: current_user, chat_channel: channel, created_at: 2.days.ago)
end
fab!(:message_2) { Fabricate(:chat_message, chat_channel: channel, created_at: 1.day.ago) }
+ fab!(:message_3) do
+ Fabricate(
+ :chat_message,
+ user: current_user,
+ chat_channel: channel_with_quotation_marks,
+ created_at: 2.days.ago,
+ )
+ end
+
let(:cdp) { PageObjects::CDP.new }
let(:composer) { PageObjects::Components::Composer.new }
let(:rich) { composer.rich_editor }
@@ -48,6 +60,31 @@ describe "chat transcripts in rich editor" do
expect(rich).to have_css(".chat-transcript .chat-transcript-messages", text: message_1.message)
end
+ it "works for channel name with quotation marks in its name" do
+ page.visit "/new-topic"
+ expect(composer).to be_opened
+ composer.focus
+
+ markdown =
+ Chat::TranscriptService.new(
+ channel_with_quotation_marks,
+ current_user,
+ messages_or_ids: [message_3],
+ ).generate_markdown
+
+ cdp.copy_paste(
+ markdown,
+ css_selector: PageObjects::Components::Composer.new.composer_input_selector,
+ )
+
+ expect(rich).to have_css(".chat-transcript", text: channel_with_quotation_marks.name)
+ expect(rich).to have_css(
+ ".chat-transcript .chat-transcript-user .chat-transcript-username",
+ text: message_3.user.username,
+ )
+ expect(rich).to have_css(".chat-transcript .chat-transcript-messages", text: message_3.message)
+ end
+
it "works for multiple messages" do
page.visit "/new-topic"
expect(composer).to be_opened