A platform for community discussion. Free, open, simple.
FIX: Escape markdown characters in upload filenames (#39133)
Régis HANOL13280c102312
diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rbindex ca58c977701..7b3c35b182f 100644--- a/app/services/inline_uploads.rb+++ b/app/services/inline_uploads.rb@@ -199,7 +199,7 @@ class InlineUploadshas_attachment = node.attributes["class"]&.valueindex = $~.offset(0)[0]text = match[2].strip.gsub("\n", "").gsub(/ +/, " ")- text = "#{text}|attachment" if has_attachment+ text = "#{UploadMarkdown.escape_markdown(text)}|attachment" if has_attachmentyield(match[0], href, +"[#{text}](#{PLACEHOLDER})", index) if block_given?end@@ -255,9 +255,9 @@ class InlineUploadsraw =raw.gsub(%r{^(https?://\S+)(\s?)$}) do |match|if upload = blk.call(match)- filename_modified = upload.original_filename&.gsub(/[\[\]\|]/, "").to_s+ filename_modified = upload.original_filename.to_sfilename_modified = File.basename(filename_modified, File.extname(filename_modified))- ""+ ""elsematchenddiff --git a/frontend/discourse-markdown-it/src/engine.js b/frontend/discourse-markdown-it/src/engine.jsindex 21a1ddf4d97..c8e6e5df7e8 100644--- a/frontend/discourse-markdown-it/src/engine.js+++ b/frontend/discourse-markdown-it/src/engine.js@@ -173,16 +173,35 @@ function audioHTML(token) {const IMG_SIZE_REGEX =/^([1-9]+[0-9]*)x([1-9]+[0-9]*)(\s*,\s*(x?)([1-9][0-9]{0,2}?)([%x]?))?$/;+function isKnownImageSuffix(segment) {+ return (+ segment === "video" ||+ segment === "audio" ||+ segment === "thumbnail" ||+ IMG_SIZE_REGEX.test(segment) ||+ !!extractDataAttribute(segment)+ );+}+function renderImageOrPlayableMedia(tokens, idx, options, env, slf) {const token = tokens[idx];const alt = slf.renderInlineAsText(token.children, options, env);const split = alt.split("|");- const altSplit = [split[0]];++ // Find where structured suffixes begin by scanning from the right.+ // Everything before that boundary is alt text (which may contain pipes).+ let suffixStart = split.length;+ while (suffixStart > 1 && isKnownImageSuffix(split[suffixStart - 1])) {+ suffixStart--;+ }++ const altText = split.slice(0, suffixStart).join("|");+ const suffixes = split.slice(suffixStart);// markdown-it supports returning HTML instead of continuing to render the current token// see https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer// handles |video and |audio alt transformations for image tags- if (split[1] === "video") {+ if (suffixes[0] === "video") {if (options.discourse.previewing &&!options.discourse.limitedSiteSettings.enableDiffhtmlPreview@@ -197,13 +216,13 @@ function renderImageOrPlayableMedia(tokens, idx, options, env, slf) {} else {return videoHTML(token);}- } else if (split[1] === "audio") {+ } else if (suffixes[0] === "audio") {return audioHTML(token);}// parsing ![myimage|500x300]() or ![myimage|75%]() or ![myimage|500x300, 75%]- for (let i = 1, match, data; i < split.length; ++i) {- if ((match = split[i].match(IMG_SIZE_REGEX)) && match[1] && match[2]) {+ for (let i = 0, match, data; i < suffixes.length; ++i) {+ if ((match = suffixes[i].match(IMG_SIZE_REGEX)) && match[1] && match[2]) {let width = match[1];let height = match[2];@@ -243,20 +262,17 @@ function renderImageOrPlayableMedia(tokens, idx, options, env, slf) {) {token.attrs.push(["class", "resizable"]);}- } else if ((data = extractDataAttribute(split[i]))) {+ } else if ((data = extractDataAttribute(suffixes[i]))) {token.attrs.push(data);- } else if (split[i] === "thumbnail") {+ } else if (suffixes[i] === "thumbnail") {token.attrs.push(["data-thumbnail", "true"]);- } else {- altSplit.push(split[i]);}}- const altValue = altSplit.join("|").trim();- if (altValue === "") {+ if (altText === "") {token.attrSet("role", "presentation");} else {- token.attrSet("alt", altValue);+ token.attrSet("alt", altText);}return slf.renderToken(tokens, idx, options);@@ -274,23 +290,36 @@ export const ATTACHMENT_CSS_CLASS = "attachment";function renderAttachment(tokens, idx, options, env, slf) {const linkToken = tokens[idx];- const textToken = tokens[idx + 1];-- const split = textToken.content.split("|");- const contentSplit = [];- for (let i = 0, data; i < split.length; ++i) {- if (split[i] === ATTACHMENT_CSS_CLASS) {- linkToken.attrs.unshift(["class", split[i]]);- } else if ((data = extractDataAttribute(split[i]))) {- linkToken.attrs.push(data);- } else {- contentSplit.push(split[i]);+ // Link text may span multiple tokens when it contains inline formatting.+ // Scan forward to find the last text token which holds the |attachment marker.+ let lastTextToken = null;+ for (let i = idx + 1; i < tokens.length; i++) {+ if (tokens[i].type === "link_close") {+ break;+ }+ if (tokens[i].type === "text" && tokens[i].content) {+ lastTextToken = tokens[i];}}- if (contentSplit.length > 0) {- textToken.content = contentSplit.join("|");+ if (lastTextToken) {+ const split = lastTextToken.content.split("|");+ const contentSplit = [];++ for (let i = 0, data; i < split.length; ++i) {+ if (split[i] === ATTACHMENT_CSS_CLASS) {+ linkToken.attrs.unshift(["class", split[i]]);+ } else if ((data = extractDataAttribute(split[i]))) {+ linkToken.attrs.push(data);+ } else {+ contentSplit.push(split[i]);+ }+ }++ if (contentSplit.length > 0) {+ lastTextToken.content = contentSplit.join("|");+ }}return slf.renderToken(tokens, idx, options);diff --git a/frontend/discourse/app/lib/markdown-image-builder.js b/frontend/discourse/app/lib/markdown-image-builder.jsindex 1e578033ba2..18e15b15f78 100644--- a/frontend/discourse/app/lib/markdown-image-builder.js+++ b/frontend/discourse/app/lib/markdown-image-builder.js@@ -1,3 +1,7 @@+export function escapeMarkdownCharacters(text) {+ return text.replace(/[\\*_~`\[\]|]/g, "\\$&");+}+export function sanitizeAlt(text, options = {}) {const fallback = options.fallback ?? "";@@ -10,7 +14,7 @@ export function sanitizeAlt(text, options = {}) {return fallback;}- return trimmed.replace(/\|/g, "|").replace(/([\\\[\]])/g, "\\$1");+ return escapeMarkdownCharacters(trimmed);}/**diff --git a/frontend/discourse/app/lib/to-markdown.js b/frontend/discourse/app/lib/to-markdown.jsindex 440b392b2ec..5c43217e190 100644--- a/frontend/discourse/app/lib/to-markdown.js+++ b/frontend/discourse/app/lib/to-markdown.js@@ -1,5 +1,6 @@import {buildImageMarkdown,+ escapeMarkdownCharacters,extensionFromUrl,} from "discourse/lib/markdown-image-builder";@@ -396,6 +397,7 @@ export class Tag {let linkModifier = "";if (attr.class?.includes("attachment")) {+ text = escapeMarkdownCharacters(text);linkModifier = "|attachment";}diff --git a/frontend/discourse/app/lib/uploads.js b/frontend/discourse/app/lib/uploads.jsindex 052c530aeed..7b5b1f77fdf 100644--- a/frontend/discourse/app/lib/uploads.js+++ b/frontend/discourse/app/lib/uploads.js@@ -1,5 +1,6 @@import deprecated from "discourse/lib/deprecated";import { getOwnerWithFallback } from "discourse/lib/get-owner";+import { escapeMarkdownCharacters } from "discourse/lib/markdown-image-builder";import { humanizeList } from "discourse/lib/text";import { capabilities } from "discourse/services/capabilities";import I18n, { i18n } from "discourse-i18n";@@ -284,7 +285,7 @@ function markdownNameFromFileName(fileName) {name = i18n("upload_selector.default_image_alt_text");}- return name.replace(/\[|\]|\|/g, "");+ return escapeMarkdownCharacters(name);}function imageMarkdown(upload) {@@ -300,7 +301,7 @@ function playableMediaMarkdown(upload, type) {}function attachmentMarkdown(upload) {- return `[${upload.original_filename}|attachment](${+ return `[${escapeMarkdownCharacters(upload.original_filename)}|attachment](${upload.short_url}) (${I18n.toHumanSize(upload.filesize)})`;}diff --git a/frontend/discourse/app/static/prosemirror/extensions/link.js b/frontend/discourse/app/static/prosemirror/extensions/link.jsindex 01013bf36fc..ff3fa9d750c 100644--- a/frontend/discourse/app/static/prosemirror/extensions/link.js+++ b/frontend/discourse/app/static/prosemirror/extensions/link.js@@ -53,10 +53,20 @@ const extension = {link: {mark: "link",getAttrs(tok, tokens, i) {- const nextContent = tokens[i + 1].content;- const attachment = nextContent.endsWith("|attachment");- if (attachment) {- tokens[i + 1].content = nextContent.slice(0, -11);+ // Link text may span multiple tokens when it contains inline formatting.+ let attachment = false;+ for (let j = i + 1; j < tokens.length; j++) {+ if (tokens[j].type === "link_close") {+ break;+ }+ if (+ tokens[j].type === "text" &&+ tokens[j].content.endsWith("|attachment")+ ) {+ tokens[j].content = tokens[j].content.slice(0, -11);+ attachment = true;+ break;+ }}return {diff --git a/frontend/discourse/tests/unit/lib/markdown-image-builder-test.js b/frontend/discourse/tests/unit/lib/markdown-image-builder-test.jsindex 7d1933da750..872e0e75e1e 100644--- a/frontend/discourse/tests/unit/lib/markdown-image-builder-test.js+++ b/frontend/discourse/tests/unit/lib/markdown-image-builder-test.js@@ -23,14 +23,11 @@ module("Unit | Lib | markdown-image-builder", function () {assert.strictEqual(sanitizeAlt(" ", { fallback: "image" }), "image");});- test("escapes pipes for markdown", function (assert) {+ test("escapes markdown special characters", function (assert) {assert.strictEqual(sanitizeAlt("alt|text|with|pipes"),- "alt|text|with|pipes"+ "alt\\|text\\|with\\|pipes");- });-- test("escapes backslashes, brackets", function (assert) {assert.strictEqual(sanitizeAlt("text\\with\\slashes"),"text\\\\with\\\\slashes"@@ -39,6 +36,14 @@ module("Unit | Lib | markdown-image-builder", function () {sanitizeAlt("text[with]brackets"),"text\\[with\\]brackets");+ assert.strictEqual(+ sanitizeAlt("_underscores_ and *stars*"),+ "\\_underscores\\_ and \\*stars\\*"+ );+ assert.strictEqual(+ sanitizeAlt("~~strike~~ and `code`"),+ "\\~\\~strike\\~\\~ and \\`code\\`"+ );});test("trims whitespace", function (assert) {@@ -137,7 +142,7 @@ module("Unit | Lib | markdown-image-builder", function () {src: "/uploads/image.png",alt: "text|with|pipes",}),- ""+ "");});diff --git a/frontend/discourse/tests/unit/lib/uploads-test.js b/frontend/discourse/tests/unit/lib/uploads-test.jsindex ff3d421fe59..666900802d7 100644--- a/frontend/discourse/tests/unit/lib/uploads-test.js+++ b/frontend/discourse/tests/unit/lib/uploads-test.js@@ -321,7 +321,7 @@ module("Unit | Utility | uploads", function (hooks) {);assert.strictEqual(testUploadMarkdown("[foo|bar].png"),- ""+ "![\\[foo\\|bar\\]|100x200](/uploads/123/abcdef.ext)");assert.strictEqual(testUploadMarkdown("file name with space.png"),@@ -339,6 +339,11 @@ module("Unit | Utility | uploads", function (hooks) {testUploadMarkdown("important.txt", { short_url }),`[important.txt|attachment](${short_url}) (42 Bytes)`);++ assert.strictEqual(+ testUploadMarkdown("_test_file_.txt", { short_url }),+ `[\\_test\\_file\\_.txt|attachment](${short_url}) (42 Bytes)`+ );});test("getUploadMarkdown - replaces GUID in image alt text on iOS", function (assert) {diff --git a/lib/upload_markdown.rb b/lib/upload_markdown.rbindex 5fdecb14b52..4d83fccc339 100644--- a/lib/upload_markdown.rb+++ b/lib/upload_markdown.rb@@ -17,14 +17,14 @@ class UploadMarkdowndef image_markdown(display_name: nil)display_name ||= @upload.original_filename- ""+ ""enddef attachment_markdown(display_name: nil, with_filesize: true)human_filesize = with_filesize ? " (#{@upload.human_filesize})" : ""display_name ||= @upload.original_filename- "[#{display_name}|attachment](#{@upload.short_url})#{human_filesize}"+ "[#{self.class.escape_markdown(display_name)}|attachment](#{@upload.short_url})#{human_filesize}"enddef playable_media_markdown(display_name: nil)@@ -36,6 +36,10 @@ class UploadMarkdownendreturn attachment_markdown if !typedisplay_name ||= @upload.original_filename- ""+ ""+ end++ def self.escape_markdown(text)+ text.gsub(/[\\*_~`\[\]|]/) { |c| "\\#{c}" }endenddiff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rbindex 50573e7bb18..4d23aa01864 100644--- a/plugins/chat/spec/models/chat/message_spec.rb+++ b/plugins/chat/spec/models/chat/message_spec.rb@@ -661,7 +661,7 @@ describe Chat::Message doexpect(message.to_markdown).to eq(<<~MSG.chomp)hey friend, what's up?!- + MSGenddiff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rbindex ce24c2c65d7..d9e150c1516 100644--- a/spec/jobs/export_csv_file_spec.rb+++ b/spec/jobs/export_csv_file_spec.rb@@ -34,8 +34,7 @@ RSpec.describe Jobs::ExportCsvFile doexpect(system_message.first_post.raw).to eq(I18n.t("system_messages.csv_export_succeeded.text_body_template",- download_link:- "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.filesize} Bytes)",+ download_link: UploadMarkdown.new(upload).attachment_markdown,).chomp,)diff --git a/spec/jobs/export_user_archive_spec.rb b/spec/jobs/export_user_archive_spec.rbindex 54055141c9a..455a7889255 100644--- a/spec/jobs/export_user_archive_spec.rb+++ b/spec/jobs/export_user_archive_spec.rb@@ -85,8 +85,7 @@ RSpec.describe Jobs::ExportUserArchive doexpect(system_message.first_post.raw).to eq(I18n.t("system_messages.csv_export_succeeded.text_body_template",- download_link:- "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.human_filesize})",+ download_link: UploadMarkdown.new(upload).attachment_markdown,).chomp,)@@ -145,8 +144,7 @@ RSpec.describe Jobs::ExportUserArchive doexpect(system_message.first_post.raw).to eq(I18n.t("system_messages.csv_export_succeeded.text_body_template",- download_link:- "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.human_filesize})",+ download_link: UploadMarkdown.new(upload).attachment_markdown,).chomp,)enddiff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rbindex 59ab4a2f8c5..72580a0a5f6 100644--- a/spec/lib/pretty_text_spec.rb+++ b/spec/lib/pretty_text_spec.rb@@ -2593,6 +2593,27 @@ HTMLexpect(PrettyText.cook(raw)).to eq(cooked.strip)end+ it "handles attachment filenames with markdown characters" do+ SiteSetting.authorized_extensions = "txt"++ {+ "_test_file_.txt" => "<em>",+ "*test*.txt" => "<em>",+ "**bold**.txt" => "<strong>",+ "~~strike~~.txt" => "<s>",+ "`code`.txt" => "<code>",+ }.each do |filename, bad_tag|+ upload = Fabricate(:upload, original_filename: filename, extension: "txt")+ markdown = UploadMarkdown.new(upload).to_markdown+ cooked = PrettyText.cook(markdown)++ expect(cooked).to include('class="attachment"'),+ "expected attachment class for filename: #{filename}\nmarkdown: #{markdown}\ncooked: #{cooked}"+ expect(cooked).not_to include(bad_tag),+ "unexpected #{bad_tag} for filename: #{filename}\nmarkdown: #{markdown}\ncooked: #{cooked}"+ end+ end+it "can place a blank image if we can not find the upload" doraw = <<~MDdiff --git a/spec/lib/upload_markdown_spec.rb b/spec/lib/upload_markdown_spec.rbindex 5166a10bb36..6b910fd1833 100644--- a/spec/lib/upload_markdown_spec.rb+++ b/spec/lib/upload_markdown_spec.rb@@ -16,21 +16,44 @@ RSpec.describe UploadMarkdown do)expect(UploadMarkdown.new(video).to_markdown).to eq(<<~MD.chomp)- + MDexpect(UploadMarkdown.new(audio).to_markdown).to eq(<<~MD.chomp)- + MDexpect(UploadMarkdown.new(attachment).to_markdown).to eq(<<~MD.chomp)- [test_file.pdf|attachment](#{attachment.short_url}) (#{attachment.human_filesize})+ [test\\_file.pdf|attachment](#{attachment.short_url}) (#{attachment.human_filesize})MDexpect(UploadMarkdown.new(image).to_markdown).to eq(<<~MD.chomp)- + MDunknown = Fabricate(:upload, original_filename: "test_video.mmmppp444", extension: "mmmppp444")expect(UploadMarkdown.new(unknown).playable_media_markdown).to eq(<<~MD.chomp)- [test_video.mmmppp444|attachment](#{unknown.short_url}) (#{unknown.human_filesize})+ [test\\_video.mmmppp444|attachment](#{unknown.short_url}) (#{unknown.human_filesize})MDend++ it "escapes markdown characters in attachment filenames" do+ SiteSetting.authorized_extensions = "txt"++ {+ "_test_file_.txt" => "<em>",+ "*test*.txt" => "<em>",+ "**bold**.txt" => "<strong>",+ "~~strike~~.txt" => "<s>",+ "`code`.txt" => "<code>",+ }.each do |filename, bad_tag|+ upload = Fabricate(:upload, original_filename: filename, extension: "txt")+ markdown = UploadMarkdown.new(upload).attachment_markdown+ cooked = PrettyText.cook(markdown)++ expect(cooked).to include('class="attachment"'),+ "expected attachment class for filename: #{filename}\ncooked: #{cooked}"+ expect(cooked).not_to include(bad_tag),+ "unexpected #{bad_tag} in cooked output for filename: #{filename}\ncooked: #{cooked}"+ expect(cooked).to include(filename.gsub("\\", "")),+ "expected display name for filename: #{filename}\ncooked: #{cooked}"+ end+ endenddiff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rbindex 50e878d8f40..fbbe7ff9842 100644--- a/spec/services/inline_uploads_spec.rb+++ b/spec/services/inline_uploads_spec.rb@@ -613,7 +613,7 @@ RSpec.describe InlineUploads do[test3|attachment](#{upload.short_url})[test3|attachment](#{upload2.short_url})[test3|attachment](#{upload3.short_url})- [This is some _test_ here|attachment](#{upload3.short_url})+ [This is some \\_test\\_ here|attachment](#{upload3.short_url})MDend@@ -762,7 +762,7 @@ RSpec.describe InlineUploads doimage_uploadend- expect(raw).to eq("look at this:\n")+ expect(raw).to eq("look at this:\n![image\\]1](#{image_upload.short_url})")endendend