Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally filter email addresses #247

Merged
merged 7 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/html/pipeline/email_reply_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class EmailReplyFilter < TextFilter
EMAIL_SIGNATURE_HEADER = %(<div class="email-signature-reply">).freeze
EMAIL_FRAGMENT_HEADER = %(<div class="email-fragment">).freeze
EMAIL_HEADER_END = "</div>".freeze
EMAIL_REGEX = /[^@\s.][^@\s]*@\[?[a-z0-9.-]+\]?/
HIDDEN_EMAIL_PATTERN = "***@***.***"

# Scans an email body to determine which bits are quoted and which should
# be hidden. EmailReplyParser is used to split the comment into an Array
Expand All @@ -45,6 +47,11 @@ def call
paragraphs = EmailReplyParser.read(text.dup).fragments.map do |fragment|
pieces = [escape_html(fragment.to_s.strip).gsub(/^\s*(>|&gt;)/, '')]
if fragment.quoted?
if context[:hide_quoted_email_addresses]
pieces.map! do |piece|
piece.gsub!(EMAIL_REGEX, HIDDEN_EMAIL_PATTERN)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug 😢 gsub! can return nil.

end
end
pieces.unshift EMAIL_QUOTED_HEADER
pieces << EMAIL_HEADER_END
elsif fragment.signature?
Expand Down
32 changes: 32 additions & 0 deletions test/html/pipeline/email_reply_filter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "test_helper"

EmailReplyFilter = HTML::Pipeline::EmailReplyFilter

class HTML::Pipeline::EmailReplyFilterTest < Minitest::Test
def setup
@body = <<-EMAIL
Hey, don't send email addresses in comments. They aren't filtered.

> On Mar 5, 2016, at 08:05, Boaty McBoatface <[email protected]> wrote:
>
> Sup. [email protected]
>
> —
> Reply to this email directly or view it on GitHub.
EMAIL
end

def test_doesnt_hide_by_default
filter = EmailReplyFilter.new(@body)
doc = filter.call.to_s
assert_match %r([email protected]), doc
assert_match %r([email protected]), doc
end

def test_hides_email_addresses_when_configured
filter = EmailReplyFilter.new(@body, :hide_quoted_email_addresses => true)
doc = filter.call.to_s
refute_match %r([email protected]), doc
refute_match %r([email protected]), doc
end
end