Skip to content

Commit 288f98a

Browse files
authored
Merge pull request #2152 from codidact/0valt/general-fixes
Safer subscription mailer view post body handling
2 parents 49017ca + 8fddaf5 commit 288f98a

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

app/mailers/subscription_mailer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class SubscriptionMailer < ApplicationMailer
2+
helper PostsHelper
23
helper UsersHelper
34

45
def subscription

app/views/subscription_mailer/subscription.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
<%= link_to question.title, post_url(question, host: @subscription.community.host) %>
1616
</h3>
1717
<p>
18-
<%= question.body.first(150).gsub(/<\/?[^>]+>/, '') %>
19-
<%= question.body.length > 150 ? '...' : '' %>
18+
<%= sanitize(strip_tags(question.body).truncate(150), scrubber: scrubber) %>
2019
</p>
2120
<p>
2221
&mdash; <%= user_link question.user, { host: @subscription.community.host } %>

test/fixtures/posts.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,17 @@ without_new_thread_followers:
621621
community: sample
622622
category: main
623623
license: cc_by_sa
624+
625+
with_sanitized_html:
626+
post_type: question
627+
title: This post contains HTML in body that is sanitized away
628+
body: |
629+
This is the body of the post used to test HTML sanitization
630+
<del>oops</del>
631+
body_markdown: |
632+
<p>
633+
This is the body of the post used to test HTML sanitization
634+
<del>oops</del>
635+
</p>
636+
community: sample
637+
user: standard_user
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
require 'test_helper'
22

33
class SubscriptionMailerTest < ActionMailer::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
test 'should correctly send subscription emails' do
5+
all_sub = subscriptions(:all)
6+
post_with_html = posts(:with_sanitized_html)
7+
8+
mailer = SubscriptionMailer.with(subscription: all_sub)
9+
email = mailer.subscription
10+
11+
assert(all_sub.questions&.any? { |q| q.id == post_with_html.id })
12+
13+
assert_emails 1 do
14+
email.deliver_later
15+
end
16+
17+
assert email.from.include?(SiteSetting['SubscriptionSenderEmail'])
18+
assert email.to.include?(all_sub.user.email)
19+
assert email.subject.start_with?('Latest questions from your')
20+
21+
assert_dom_email do
22+
assert_not_dom 'del'
23+
assert_dom 'p', /oops/
24+
end
25+
end
726
end

0 commit comments

Comments
 (0)