Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

sanitize and properly truncate iCalendar attributes, translate description keys

Jonas Jabarib16d77f5b819
diff --git a/modules/calendar/app/services/calendar/create_ical_service.rb b/modules/calendar/app/services/calendar/create_ical_service.rb
index 0b622c57a46..14e957b795a 100644
--- a/modules/calendar/app/services/calendar/create_ical_service.rb
+++ b/modules/calendar/app/services/calendar/create_ical_service.rb
@@ -27,9 +27,12 @@
#++
require 'icalendar'
+require 'rails-html-sanitizer'
module Calendar
class CreateICalService < ::BaseServices::BaseCallable
+ include TextFormattingHelper
+
def perform(work_packages:, calendar_name: "OpenProject Calendar")
ical_string = create_ical_string(work_packages, calendar_name)
@@ -115,32 +118,54 @@ module Calendar
end
def description_value(work_package)
- # TODO: translate keys
- project = "Project: #{work_package.project.name}"
- type = "Type: #{type_emoji(work_package)}#{work_package.type&.name}"
- status = "Status: #{work_package.status&.name}"
- assignee = "Assignee: #{work_package.assigned_to&.name}"
- priority = "Priority: #{priority_emoji(work_package)}#{work_package.priority&.name}"
- description = truncated_work_package_description_value(work_package)
+ map = map_description_values(work_package)
[
- project, type, status, assignee, priority, description
+ map[:project], map[:type], map[:status],
+ map[:assignee], map[:priority], map[:description]
].join("\n")
end
- def type_emoji(work_package)
- # TODO: Differentiate emoji based on type
- # "🟩"
+ def map_description_values(work_package)
+ map = {}
+ map[:project] = translated_project_name(work_package)
+ map[:type] = translated_type_name(work_package)
+ map[:status] = translated_status_name(work_package)
+ map[:assignee] = translated_assigne_name(work_package)
+ map[:priority] = translated_priority_name(work_package)
+ map[:description] = truncated_work_package_description_value(work_package)
+
+ map
+ end
+
+ def translated_project_name(work_package)
+ "#{I18n::t('activerecord.models.project')}: #{work_package.project.name}"
+ end
+
+ def translated_type_name(work_package)
+ "#{I18n::t('activerecord.models.type')}: #{work_package.type&.name}"
+ end
+
+ def translated_status_name(work_package)
+ "#{I18n::t('attributes.status')}: #{work_package.status&.name}"
+ end
+
+ def translated_assigne_name(work_package)
+ "#{I18n::t('attributes.assignee')}: #{work_package.assigned_to&.name}"
+ end
+
+ def translated_priority_name(work_package)
+ "#{I18n::t('activerecord.attributes.work_package.priority')}: #{work_package.priority&.name}"
end
- def priority_emoji(work_package)
- # TODO: Differentiate emoji based on priority
- # "🟢"
+ def translated_description_name
+ I18n::t("attributes.description")
end
def truncated_work_package_description_value(work_package)
if work_package.description.present?
- "\nDescription:\n #{work_package.description&.truncate(250)}"
+ stripped_text = truncate_formatted_text(work_package.description.to_s, length: 250)
+ "\n#{translated_description_name}:\n #{stripped_text}"
end
end
@@ -150,5 +175,37 @@ module Calendar
event
end
+
+ # override from text_formatting_helper
+ # didn't work in this context -> got error "undefined method `full_sanitizer`"
+ # furthermore the replacement of \n with <br> is not desired in the context of iCalendar files
+ def truncate_formatted_text(text, length: 120)
+ # rubocop:disable Rails/OutputSafety
+ stripped_text = sanitizer_instance.sanitize(format_text(text)).html_safe
+
+ if length
+ truncate_multiline(stripped_text, length)
+ else
+ stripped_text
+ end
+ .strip
+ .html_safe
+ # rubocop:enable Rails/OutputSafety
+ end
+
+ # override from text_formatting_helper
+ # the original method was statically truncating at 120 characters
+ def truncate_multiline(string, length)
+ if string.to_s =~ /\A(.{#{length}}).*?$/m
+ "#{$1}..."
+ else
+ string
+ end
+ end
+
+ # got error "undefined method `full_sanitizer`" without this when calling `strip_tags`
+ def sanitizer_instance
+ @sanitizer_instance ||= Rails::Html::FullSanitizer.new
+ end
end
end
diff --git a/modules/calendar/spec/services/create_ical_service_spec.rb b/modules/calendar/spec/services/create_ical_service_spec.rb
index 0b176e5ae32..b73537974e7 100644
--- a/modules/calendar/spec/services/create_ical_service_spec.rb
+++ b/modules/calendar/spec/services/create_ical_service_spec.rb
@@ -139,4 +139,89 @@ describe Calendar::CreateICalService, type: :model do
expect(subject)
.to be_success
end
+
+ describe 'stripped and truncated workpackage description' do
+ let(:rich_text_description) do
+ <<-STRING
+ test **description**\n\n1. **foo**\n2. bar\n3. **baz**\n\nLorem ipsum#{' '}
+ dolor<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/4/content">sit#{' '}
+ amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore#{' '}
+ magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.#{' '}
+ Stet clita kasd gubergren, no sea tak
+ STRING
+ end
+ let(:work_package_with_rich_text_description) do
+ create(:work_package, project:,
+ due_date: Time.zone.today + 7.days, assigned_to: user,
+ description: "test **description**\n\n1. **foo**\n2. bar\n3. **baz**")
+ end
+ let(:work_package_with_image) do
+ create(:work_package, project:,
+ due_date: Time.zone.today + 7.days, assigned_to: user,
+ description: "test <img class=\"op-uc-image op-uc-image_inline\"
+ src=\"/api/v3/attachments/3/content\">image")
+ end
+ let(:work_package_with_long_text) do
+ create(:work_package, project:,
+ due_date: Time.zone.today + 7.days, assigned_to: user,
+ description: "sit amet, consetetur sadipscing elitr, sed diam nonumy
+ eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
+ voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
+ Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum
+ dolor sit amet.")
+ end
+ let(:work_packages) do
+ [
+ work_package_with_rich_text_description,
+ work_package_with_image,
+ work_package_with_long_text
+ ]
+ end
+
+ it 'strips html tags from the description' do
+ expect(subject.result).to include("test description") # no <b> tags
+ expect(subject.result).to include("\\nfoo\\nbar\\nbaz") # no <br> tags or <ol> tags
+ end
+
+ it 'strips images from the description' do
+ expect(subject.result).to include("test image") # no <img> tags
+ end
+
+ it 'truncates the description at 250 chars' do
+ expect(subject.result).to include("sanctus est ...")
+ end
+ end
+
+ describe 'sanitized attributes' do
+ # the iCalendar gem takes care of escaping malicious values
+ # following specs double check on this behaviour
+ let(:work_package_with_malicious_subject) do
+ create(:work_package, subject: "<script>alert('Subject');</script>", project:,
+ due_date: Time.zone.today + 7.days, assigned_to: user)
+ end
+ let(:work_package_with_malicious_description) do
+ create(:work_package, project:,
+ due_date: Time.zone.today + 7.days, assigned_to: user,
+ description: "<script>alert('Description');</script>")
+ end
+ let(:work_packages) do
+ [
+ work_package_with_malicious_subject,
+ work_package_with_malicious_description
+ ]
+ end
+ let(:formatted_result) do
+ subject.result.gsub("\r\n ", "").gsub("\r", "").gsub("\\n", "\n")
+ end
+
+ it 'escapes malicious workpackage subject values' do
+ expect(formatted_result).not_to include("<script>alert('Subject');</script>")
+ expect(formatted_result).to include("&lt\\;script&gt\\;alert('Subject')\\;&lt\\;/script&gt\\;")
+ end
+
+ it 'escapes malicious workpackage description values' do
+ expect(formatted_result).not_to include("<script>alert('Description');</script>")
+ expect(formatted_result).to include("&lt\\;script&gt\\;alert('Description')\\;&lt\\;/script&gt\\;")
+ end
+ end
end