A platform for community discussion. Free, open, simple.
SECURITY: Prevent HTML injection in discourse-local-dates rendering
Alan Guo Xiang Tand5d5055fbdf0
diff --git a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.jsindex befd794636a..9a75321f810 100644--- a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js+++ b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js@@ -43,8 +43,12 @@ export function applyLocalDates(dates, siteSettings, timezone) {element.insertAdjacentHTML("beforeend",`${iconHTML("earth-americas")}- <span class="relative-time">${localDateBuilder.formatted}</span>`+ `);+ const relativeTime = document.createElement("span");+ relativeTime.classList.add("relative-time");+ relativeTime.textContent = localDateBuilder.formatted;+ element.appendChild(relativeTime);element.setAttribute("aria-label", localDateBuilder.textPreview);const classes = ["cooked-date"];diff --git a/plugins/discourse-local-dates/spec/system/local_dates_spec.rb b/plugins/discourse-local-dates/spec/system/local_dates_spec.rbindex bd7430a0b1a..629c56925c8 100644--- a/plugins/discourse-local-dates/spec/system/local_dates_spec.rb+++ b/plugins/discourse-local-dates/spec/system/local_dates_spec.rb@@ -19,6 +19,8 @@ describe "Local dates" doendlet(:topic_page) { PageObjects::Pages::Topic.new }+ let(:local_date) { PageObjects::Components::LocalDate.new }+ let(:markup) { "<img src=x>" }def formatted_date_for_year(month, day)Date.parse("#{year}-#{month}-#{day}").strftime("%A, %B %-d, %Y")@@ -75,6 +77,20 @@ describe "Local dates" doendend+ it "renders a custom date format containing markup as plain text" do+ create_post(+ user: current_user,+ topic: topic,+ raw:+ %(<span class="discourse-local-date" data-date="2025-01-01" data-time="05:00:00" data-format="[#{CGI.escapeHTML(markup)}]"></span>),+ )+ sign_in(current_user)+ topic_page.visit_topic(topic)++ expect(local_date).to have_rendered_date+ expect(local_date).to have_text_in_date(markup)+ end+describe "insert modal" dolet(:timezone) { "Australia/Brisbane" }diff --git a/plugins/discourse-local-dates/spec/system/page_objects/components/local_date.rb b/plugins/discourse-local-dates/spec/system/page_objects/components/local_date.rbnew file mode 100644index 00000000000..085863ea903--- /dev/null+++ b/plugins/discourse-local-dates/spec/system/page_objects/components/local_date.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true++module PageObjects+ module Components+ class LocalDate < PageObjects::Components::Base+ def has_rendered_date?+ has_css?(".discourse-local-date.cooked-date")+ end++ def has_text_in_date?(text)+ has_css?(".discourse-local-date .relative-time", text: text)+ end+ end+ end+enddiff --git a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.jsindex dcef2ef90b5..d910fa0b3ad 100644--- a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js+++ b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js@@ -122,4 +122,95 @@ module("Unit | discourse-local-dates", function (hooks) {});});++ test("applyLocalDates renders a crafted data-format as literal text without injecting HTML", function (assert) {+ const element = createElementFromHTML(+ "<span " ++ 'data-date="2022-10-06" ' ++ 'data-time="17:21:00" ' ++ 'class="discourse-local-date" ' ++ 'data-timezone="Asia/Singapore" ' ++ 'data-format="[<img src=x class=xss-probe>]">' ++ "</span>"+ );++ freezeTime(+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },+ () => {+ applyLocalDates([element], { discourse_local_dates_enabled: true });++ assert.dom("img", element).doesNotExist();+ assert+ .dom(".relative-time", element)+ .includesText("<img src=x class=xss-probe>");+ }+ );+ });++ test("applyLocalDates renders a crafted data-displayed-timezone as literal text without injecting HTML", function (assert) {+ const element = createElementFromHTML(+ "<span " ++ 'data-date="2022-10-06" ' ++ 'data-time="17:21:00" ' ++ 'class="discourse-local-date" ' ++ 'data-timezone="Asia/Singapore" ' ++ 'data-displayed-timezone="<img src=x class=xss-probe>">' ++ "</span>"+ );++ freezeTime(+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },+ () => {+ applyLocalDates([element], { discourse_local_dates_enabled: true });++ assert.dom("img", element).doesNotExist();+ assert+ .dom(".relative-time", element)+ .includesText("<img src=x class=xss-probe>");+ }+ );+ });++ test("applyLocalDates renders a crafted data-timezone as literal text without injecting HTML", function (assert) {+ const element = createElementFromHTML(+ "<span " ++ 'data-date="2022-10-06" ' ++ 'class="discourse-local-date" ' ++ 'data-timezone="<img src=x class=xss-probe>">' ++ "</span>"+ );++ freezeTime(+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },+ () => {+ applyLocalDates([element], { discourse_local_dates_enabled: true });++ assert.dom("img", element).doesNotExist();+ assert+ .dom(".relative-time", element)+ .includesText("<img src=x class=xss-probe>");+ }+ );+ });++ test("applyLocalDates preserves legitimate bracketed moment literals as text", function (assert) {+ const element = createElementFromHTML(+ "<span " ++ 'data-date="2022-10-06" ' ++ 'data-time="17:21:00" ' ++ 'class="discourse-local-date" ' ++ 'data-timezone="Asia/Singapore" ' ++ 'data-format="[at] LL">' ++ "</span>"+ );++ freezeTime(+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },+ () => {+ applyLocalDates([element], { discourse_local_dates_enabled: true });++ assert.dom(".relative-time", element).hasText("at October 6, 2022");+ }+ );+ });});