A platform for community discussion. Free, open, simple.
FIX: Properly escape ICS feed content per RFC 5545 (#38761)
Rafael dos Santos Silva32c0b7c434e6
diff --git a/app/views/users/bookmarks.ics.erb b/app/views/users/bookmarks.ics.erbindex 2c80cc951b8..b0c328e274d 100644--- a/app/views/users/bookmarks.ics.erb+++ b/app/views/users/bookmarks.ics.erb@@ -7,7 +7,7 @@ UID:bookmark_reminder_#<%= bookmark.id %>@<%= Discourse.current_hostname %>DTSTAMP:<%= bookmark.updated_at.strftime(I18n.t("datetime_formats.formats.calendar_ics")) %>DTSTART:<%= bookmark.reminder_at_ics_start %>DTEND:<%= bookmark.reminder_at_ics_end %>-SUMMARY:<%= bookmark.name.presence || bookmark.title %>+SUMMARY:<%= IcalEncoder.encode(bookmark.name.presence || bookmark.title) %>DESCRIPTION:<%= bookmark.bookmarkable_url %>URL:<%= bookmark.bookmarkable_url %>END:VEVENTdiff --git a/lib/ical_encoder.rb b/lib/ical_encoder.rbnew file mode 100644index 00000000000..8a44e6d89dd--- /dev/null+++ b/lib/ical_encoder.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++module IcalEncoder+ SANITIZER = Rails::Html::FullSanitizer.new++ # Encodes a string for use in iCalendar text fields (SUMMARY, DESCRIPTION, LOCATION).+ # Strips HTML tags, decodes HTML entities, and escapes special characters per RFC 5545.+ def self.encode(text)+ return "" if text.blank?+ text = SANITIZER.sanitize(text)+ text = CGI.unescapeHTML(text)+ text+ .gsub("\\", "\\\\\\\\")+ .gsub(",", "\\,")+ .gsub(";", "\\;")+ .gsub("\r\n", "\\n")+ .gsub("\n", "\\n")+ end+enddiff --git a/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erbindex dc609bf2b30..9dc26c110a9 100644--- a/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb+++ b/plugins/discourse-calendar/app/views/discourse_post_event/events/index.ics.erb@@ -8,9 +8,10 @@ UID:calendar_event_#<%= event.id %>@<%= Discourse.current_hostname %>DTSTAMP:<%= Time.now.utc.strftime("%Y%m%dT%H%M%SZ") %>DTSTART:<%= event.starts_at.strftime("%Y%m%dT%H%M%SZ") %>DTEND:<%= event.ends_at.presence ? event.ends_at.strftime("%Y%m%dT%H%M%SZ") : (event.starts_at + 1.hour).strftime("%Y%m%dT%H%M%SZ") %>-SUMMARY:<%= event.name.presence || event.post.topic.title %>-<%- if event.location.present? -%> LOCATION:<%= event.location %><%- end -%>-DESCRIPTION:<%= event.description.presence || PrettyText.format_for_email(event.post.excerpt, event.post).html_safe.gsub(/\r?\n/, '\n') %>+SUMMARY:<%= IcalEncoder.encode(event.name.presence || event.post.topic.title) %>+<%- if event.location.present? -%>LOCATION:<%= IcalEncoder.encode(event.location) %>+<%- end -%>+DESCRIPTION:<%= IcalEncoder.encode(event.description.presence || event.post.excerpt) %>URL:<%= event.url.presence || event.post.topic.url(event.post.post_number) %>END:VEVENT<%- end -%>diff --git a/spec/lib/ical_encoder_spec.rb b/spec/lib/ical_encoder_spec.rbnew file mode 100644index 00000000000..5604f469357--- /dev/null+++ b/spec/lib/ical_encoder_spec.rb@@ -0,0 +1,60 @@+# frozen_string_literal: true++RSpec.describe IcalEncoder do+ describe ".encode" do+ it "returns empty string for nil" do+ expect(described_class.encode(nil)).to eq("")+ end++ it "returns empty string for blank string" do+ expect(described_class.encode("")).to eq("")+ end++ it "passes through plain text" do+ expect(described_class.encode("Hello World")).to eq("Hello World")+ end++ it "strips HTML tags" do+ expect(described_class.encode("<b>Bold</b> and <i>italic</i>")).to eq("Bold and italic")+ end++ it "decodes HTML entities" do+ expect(described_class.encode("Tom & Jerry")).to eq("Tom & Jerry")+ expect(described_class.encode("1 < 2 > 0")).to eq("1 < 2 > 0")+ expect(described_class.encode(""quoted"")).to eq('"quoted"')+ end++ it "strips HTML tags then decodes entities" do+ expect(+ described_class.encode("<a href="https://example.com">link</a>"),+ ).to eq('<a href="https://example.com">link</a>')+ end++ it "escapes commas" do+ expect(described_class.encode("New York, NY")).to eq("New York\\, NY")+ end++ it "escapes semicolons" do+ expect(described_class.encode("one; two")).to eq("one\\; two")+ end++ it "escapes backslashes" do+ expect(described_class.encode("path\\to\\file")).to eq("path\\\\to\\\\file")+ end++ it "escapes newlines" do+ expect(described_class.encode("line one\nline two")).to eq("line one\\nline two")+ expect(described_class.encode("line one\r\nline two")).to eq("line one\\nline two")+ end++ it "handles complex HTML content from Discourse posts" do+ html =+ '<a class="lightbox" href="https://example.com/image.jpg">[Image]</a> \nSome text with & special chars'+ result = described_class.encode(html)+ expect(result).not_to include("<")+ expect(result).not_to include("&")+ expect(result).not_to include(""")+ expect(result).to include("& special chars")+ end+ end+end