Skip to content

Commit bd7af4a

Browse files
committed
Merge pull request gjtorikian#247 from oreoshake/filter-email-addresses
Optionally filter email addresses
2 parents 1bfbd69 + 19ad2b2 commit bd7af4a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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+
if context[:hide_quoted_email_addresses]
51+
pieces.map! do |piece|
52+
piece.gsub!(EMAIL_REGEX, HIDDEN_EMAIL_PATTERN)
53+
end
54+
end
4855
pieces.unshift EMAIL_QUOTED_HEADER
4956
pieces << EMAIL_HEADER_END
5057
elsif fragment.signature?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "test_helper"
2+
3+
EmailReplyFilter = HTML::Pipeline::EmailReplyFilter
4+
5+
class HTML::Pipeline::EmailReplyFilterTest < Minitest::Test
6+
def setup
7+
@body = <<-EMAIL
8+
Hey, don't send email addresses in comments. They aren't filtered.
9+
10+
> On Mar 5, 2016, at 08:05, Boaty McBoatface <boatymcboatface@example.com> wrote:
11+
>
12+
> Sup. alreadyleaked@example.com
13+
>
14+
> —
15+
> Reply to this email directly or view it on GitHub.
16+
EMAIL
17+
end
18+
19+
def test_doesnt_hide_by_default
20+
filter = EmailReplyFilter.new(@body)
21+
doc = filter.call.to_s
22+
assert_match %r(alreadyleaked@example.com), doc
23+
assert_match %r(boatymcboatface@example.com), doc
24+
end
25+
26+
def test_hides_email_addresses_when_configured
27+
filter = EmailReplyFilter.new(@body, :hide_quoted_email_addresses => true)
28+
doc = filter.call.to_s
29+
refute_match %r(boatymcboatface@example.com), doc
30+
refute_match %r(alreadyleaked@example.com), doc
31+
end
32+
end

0 commit comments

Comments
 (0)