Skip to content

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

FIX: Prevent HTML-encoding of special characters in ICS feeds (#39152)

Régis HANOLcfd5b466b9c0
diff --git a/app/views/users/bookmarks.ics.erb b/app/views/users/bookmarks.ics.erb
index b0c328e274d..f28cece62d1 100644
--- a/app/views/users/bookmarks.ics.erb
+++ b/app/views/users/bookmarks.ics.erb
@@ -8,8 +8,8 @@ DTSTAMP:<%= bookmark.updated_at.strftime(I18n.t("datetime_formats.formats.calend
DTSTART:<%= bookmark.reminder_at_ics_start %>
DTEND:<%= bookmark.reminder_at_ics_end %>
SUMMARY:<%= IcalEncoder.encode(bookmark.name.presence || bookmark.title) %>
-DESCRIPTION:<%= bookmark.bookmarkable_url %>
-URL:<%= bookmark.bookmarkable_url %>
+DESCRIPTION:<%= IcalEncoder.encode(bookmark.bookmarkable_url) %>
+URL:<%= IcalEncoder.encode(bookmark.bookmarkable_url) %>
END:VEVENT
<% end %>
END:VCALENDAR
diff --git a/lib/ical_encoder.rb b/lib/ical_encoder.rb
index 8a44e6d89dd..eab87e346c4 100644
--- a/lib/ical_encoder.rb
+++ b/lib/ical_encoder.rb
@@ -15,5 +15,6 @@ module IcalEncoder
.gsub(";", "\\;")
.gsub("\r\n", "\\n")
.gsub("\n", "\\n")
+ .html_safe
end
end
diff --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.erb
index c7f99dc7b44..495afce6a1a 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
@@ -15,7 +15,7 @@ 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), event.post.full_url].compact.join("\n")) %>
-URL:<%= event.url.presence || event.post.topic.url(event.post.post_number) %>
+URL:<%= IcalEncoder.encode(event.url.presence || event.post.topic.url(event.post.post_number)) %>
END:VEVENT
<%- end -%>
END:VCALENDAR
diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
index 8e598a7e010..22b00c16256 100644
--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
@@ -106,6 +106,31 @@ module DiscoursePostEvent
expect(body).to include("URL:https://example.com/event-info")
end
+ it "should not HTML-encode ampersands in ics format" do
+ Fabricate(
+ :event,
+ original_starts_at: 1.day.from_now,
+ name: "Tom & Jerry",
+ location: "Room A & B",
+ description: "Q & A session",
+ url: "https://example.com/event?a=1&b=2",
+ )
+
+ get "/discourse-post-event/events.ics"
+
+ expect(response.body).to include("SUMMARY:Tom & Jerry")
+ expect(response.body).not_to include("&amp;")
+ end
+
+ it "should not HTML-encode topic title used as ics summary" do
+ event = Fabricate(:event, original_starts_at: 1.day.from_now)
+ event.post.topic.update!(title: "Rock & Roll Music Festival 2026")
+
+ get "/discourse-post-event/events.ics"
+
+ expect(response.body).to include("SUMMARY:Rock & Roll Music Festival 2026")
+ end
+
it "should handle events without location and description in ics format" do
event = Fabricate(:event, original_starts_at: 1.day.from_now, name: "Simple Event")
diff --git a/spec/lib/ical_encoder_spec.rb b/spec/lib/ical_encoder_spec.rb
index 5604f469357..138664a39a0 100644
--- a/spec/lib/ical_encoder_spec.rb
+++ b/spec/lib/ical_encoder_spec.rb
@@ -47,6 +47,10 @@ RSpec.describe IcalEncoder do
expect(described_class.encode("line one\r\nline two")).to eq("line one\\nline two")
end
+ it "returns html_safe strings to prevent double-escaping in ERB templates" do
+ expect(described_class.encode("Tom & Jerry")).to be_html_safe
+ end
+
it "handles complex HTML content from Discourse posts" do
html =
'&lt;a class=&quot;lightbox&quot; href=&quot;https://example.com/image.jpg&quot;&gt;[Image]&lt;/a&gt; \nSome text with &amp; special chars'
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 55c14661729..d25dd21fe0b 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -7383,6 +7383,18 @@ RSpec.describe UsersController do
expect(body).to include("bookmark_reminder_##{bookmark3.id}")
end
+ it "does not HTML-encode special characters in .ics feed" do
+ bookmark1.update!(name: "Tom & Jerry", reminder_at: 1.day.from_now)
+
+ sign_in(user1)
+ get "/u/#{user1.username}/bookmarks.ics"
+
+ expect(response.status).to eq(200)
+ body = response.body
+ expect(body).not_to include("&amp;")
+ expect(body).to include("SUMMARY:Tom & Jerry")
+ end
+
it "does not show another user's bookmarks" do
sign_in(Fabricate(:user))
get "/u/#{bookmark3.user.username}/bookmarks.json"