Skip to content

Commit 8a8e7b3

Browse files
committed
remove email addresses from first line of quote email replies
1 parent 1bfbd69 commit 8a8e7b3

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gemspec
66
group :development do
77
gem "bundler"
88
gem "rake"
9+
gem "pry"
910
end
1011

1112
group :test do

lib/html/pipeline/email_reply_filter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class EmailReplyFilter < TextFilter
2727
EMAIL_SIGNATURE_HEADER = %(<div class="email-signature-reply">).freeze
2828
EMAIL_FRAGMENT_HEADER = %(<div class="email-fragment">).freeze
2929
EMAIL_HEADER_END = "</div>".freeze
30+
EMAIL_REGEX = /[^@\s.][^@\s]*@\[?[a-z0-9.-]+\]?/
31+
HIDDEN_EMAIL_PATTERN = "***@***.***"
3032

3133
# Scans an email body to determine which bits are quoted and which should
3234
# be hidden. EmailReplyParser is used to split the comment into an Array
@@ -45,6 +47,11 @@ def call
4547
paragraphs = EmailReplyParser.read(text.dup).fragments.map do |fragment|
4648
pieces = [escape_html(fragment.to_s.strip).gsub(/^\s*(>|&gt;)/, '')]
4749
if fragment.quoted?
50+
pieces.map! do |piece|
51+
lines = piece.lines
52+
lines.first.gsub!(EMAIL_REGEX, HIDDEN_EMAIL_PATTERN)
53+
lines
54+
end
4855
pieces.unshift EMAIL_QUOTED_HEADER
4956
pieces << EMAIL_HEADER_END
5057
elsif fragment.signature?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "test_helper"
2+
3+
EmailReplyFilter = HTML::Pipeline::EmailReplyFilter
4+
5+
class HTML::Pipeline::EmailReplyFilterTest < Minitest::Test
6+
def test_hides_email_addresses
7+
filter = EmailReplyFilter.new(<<-EMAIL, :highlight => "coffeescript")
8+
Hey, don't send email addresses in comments. They aren't filtered.
9+
10+
> On Mar 5, 2016, at 08:05, Hacker J. Hackerson <example@example.com> wrote:
11+
>
12+
> Sup. alreadyleaked@example.com
13+
>
14+
> —
15+
> Reply to this email directly or view it on GitHub.
16+
>
17+
EMAIL
18+
19+
doc = filter.call
20+
refute_match %r(example@example.com), doc.to_s
21+
assert_match %r(alreadyleaked@example.com), doc.to_s
22+
end
23+
end

0 commit comments

Comments
 (0)