From e365675e0a8d2b01bab397e6462e301ef57280e5 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 17:28:30 -0600 Subject: [PATCH 1/6] Strip HTML from keyword mute input. #234. --- app/lib/feed_manager.rb | 17 +---------------- app/models/glitch/filter_helper.rb | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 app/models/glitch/filter_helper.rb diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index fe5ebfc36f..7507b37d24 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -178,22 +178,7 @@ class FeedManager end def keyword_filter?(status, receiver_id) - text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) - tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) - - should_filter = text_matcher.matches?(status.text) - should_filter ||= text_matcher.matches?(status.spoiler_text) - should_filter ||= tag_matcher.matches?(status.tags) - - if status.reblog? - reblog = status.reblog - - should_filter ||= text_matcher.matches?(reblog.text) - should_filter ||= text_matcher.matches?(reblog.spoiler_text) - should_filter ||= tag_matcher.matches?(status.tags) - end - - should_filter + Glitch::FilterHelper.new(receiver_id).matches?(status) end def filter_from_mentions?(status, receiver_id) diff --git a/app/models/glitch/filter_helper.rb b/app/models/glitch/filter_helper.rb new file mode 100644 index 0000000000..0dfd695265 --- /dev/null +++ b/app/models/glitch/filter_helper.rb @@ -0,0 +1,23 @@ +class Glitch::FilterHelper + include ActionView::Helpers::SanitizeHelper + + attr_reader :text_matcher + attr_reader :tag_matcher + + def initialize(receiver_id) + @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) + @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) + end + + def matches?(status) + matchers_match?(status) || (status.reblog? && matchers_match?(status.reblog)) + end + + private + + def matchers_match?(status) + text_matcher.matches?(strip_tags(status.text)) || + text_matcher.matches?(strip_tags(status.spoiler_text)) || + tag_matcher.matches?(status.tags) + end +end From a5f1c1f4693e895f326b32927d6f05e11331e288 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 21:40:28 -0600 Subject: [PATCH 2/6] Fill out some examples for Glitch::FilterHelper. #234. Also add HTML entity decoding to Glitch::FilterHelper, which is needed to e.g. match "<" to the tag-stripped version of "

<3

" or "

<3

". --- app/models/glitch/filter_helper.rb | 16 ++++++--- spec/models/glitch/filter_helper_spec.rb | 43 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 spec/models/glitch/filter_helper_spec.rb diff --git a/app/models/glitch/filter_helper.rb b/app/models/glitch/filter_helper.rb index 0dfd695265..11be877c18 100644 --- a/app/models/glitch/filter_helper.rb +++ b/app/models/glitch/filter_helper.rb @@ -1,12 +1,16 @@ +require 'htmlentities' + class Glitch::FilterHelper include ActionView::Helpers::SanitizeHelper attr_reader :text_matcher attr_reader :tag_matcher + attr_reader :entity_decoder def initialize(receiver_id) - @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) - @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) + @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) + @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) + @entity_decoder = HTMLEntities.new end def matches?(status) @@ -16,8 +20,12 @@ class Glitch::FilterHelper private def matchers_match?(status) - text_matcher.matches?(strip_tags(status.text)) || - text_matcher.matches?(strip_tags(status.spoiler_text)) || + text_matcher.matches?(prepare_text(status.text)) || + text_matcher.matches?(prepare_text(status.spoiler_text)) || tag_matcher.matches?(status.tags) end + + def prepare_text(text) + entity_decoder.decode(strip_tags(text)) + end end diff --git a/spec/models/glitch/filter_helper_spec.rb b/spec/models/glitch/filter_helper_spec.rb new file mode 100644 index 0000000000..9a808667de --- /dev/null +++ b/spec/models/glitch/filter_helper_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe Glitch::FilterHelper do + describe '#matches?' do + let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } + let(:helper) { Glitch::FilterHelper.new(alice) } + + it 'ignores names of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'addr') + + expect(helper.matches?(status)).to be false + end + + it 'ignores properties of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'href') + + expect(helper.matches?(status)).to be false + end + + it 'matches text inside HTML tags' do + status = Fabricate(:status, text: '

HEY THIS IS SOMETHING ANNOYING

') + Glitch::KeywordMute.create!(account: alice, keyword: 'annoying') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML-stripped text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + end +end From 00cb76389350839b455321deff681c6fdff65f38 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 21:49:55 -0600 Subject: [PATCH 3/6] Glitch::FilterHelper -> Glitch::KeywordMuteHelper. #234. The class helps out with keyword mutes, not just some general concept of "filtering". --- app/lib/feed_manager.rb | 2 +- .../glitch/{filter_helper.rb => keyword_mute_helper.rb} | 4 ++-- .../{filter_helper_spec.rb => keyword_mute_helper_spec.rb} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename app/models/glitch/{filter_helper.rb => keyword_mute_helper.rb} (88%) rename spec/models/glitch/{filter_helper_spec.rb => keyword_mute_helper_spec.rb} (93%) diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 7507b37d24..c16b256355 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -178,7 +178,7 @@ class FeedManager end def keyword_filter?(status, receiver_id) - Glitch::FilterHelper.new(receiver_id).matches?(status) + Glitch::KeywordMuteHelper.new(receiver_id).matches?(status) end def filter_from_mentions?(status, receiver_id) diff --git a/app/models/glitch/filter_helper.rb b/app/models/glitch/keyword_mute_helper.rb similarity index 88% rename from app/models/glitch/filter_helper.rb rename to app/models/glitch/keyword_mute_helper.rb index 11be877c18..1b8c64e4ed 100644 --- a/app/models/glitch/filter_helper.rb +++ b/app/models/glitch/keyword_mute_helper.rb @@ -1,6 +1,6 @@ require 'htmlentities' -class Glitch::FilterHelper +class Glitch::KeywordMuteHelper include ActionView::Helpers::SanitizeHelper attr_reader :text_matcher @@ -26,6 +26,6 @@ class Glitch::FilterHelper end def prepare_text(text) - entity_decoder.decode(strip_tags(text)) + entity_decoder.decode(strip_tags(text)).tap { |x| puts x } end end diff --git a/spec/models/glitch/filter_helper_spec.rb b/spec/models/glitch/keyword_mute_helper_spec.rb similarity index 93% rename from spec/models/glitch/filter_helper_spec.rb rename to spec/models/glitch/keyword_mute_helper_spec.rb index 9a808667de..9d09e58da8 100644 --- a/spec/models/glitch/filter_helper_spec.rb +++ b/spec/models/glitch/keyword_mute_helper_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe Glitch::FilterHelper do +RSpec.describe Glitch::KeywordMuteHelper do describe '#matches?' do let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } - let(:helper) { Glitch::FilterHelper.new(alice) } + let(:helper) { Glitch::KeywordMuteHelper.new(alice) } it 'ignores names of HTML tags in status text' do status = Fabricate(:status, text: 'uh example') From 79f594fcbae88a5cd8c23d97ddc202367fbbcafb Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 10 Feb 2018 02:32:39 -0600 Subject: [PATCH 4/6] Introduce html2text for extracting plaintext from statuses. #236. Unlike strip_tags, html2text will preserve text present in other nodes, e.g. anchor tags: [1] pry(main)> str = 'A link' => "A link" [2] pry(main)> Html2Text.convert(str) => "[A link](http://www.example.com)" [3] pry(main)> include ActionView::Helpers::SanitizeHelper => Object [4] pry(main)> strip_tags(str) => "A link" Preserving the href of an anchor allows keyword mutes to also match on URLs, which is something that the frontend regex filter can currently do. --- Gemfile | 1 + Gemfile.lock | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 1d128d6573..d2cd3b42db 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ gem 'fast_blank', '~> 1.0' gem 'goldfinger', '~> 2.1' gem 'hiredis', '~> 0.6' gem 'redis-namespace', '~> 1.5' +gem 'html2text' gem 'htmlentities', '~> 4.3' gem 'http', '~> 3.0' gem 'http_accept_language', '~> 2.1' diff --git a/Gemfile.lock b/Gemfile.lock index 3a65f35a53..3400b1a0fd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -205,6 +205,8 @@ GEM highline (1.7.10) hiredis (0.6.1) hkdf (0.3.0) + html2text (0.2.1) + nokogiri (~> 1.6) htmlentities (4.3.4) http (3.0.0) addressable (~> 2.3) @@ -601,6 +603,7 @@ DEPENDENCIES goldfinger (~> 2.1) hamlit-rails (~> 0.2) hiredis (~> 0.6) + html2text htmlentities (~> 4.3) http (~> 3.0) http_accept_language (~> 2.1) From 062f72c593d0f3304e43dae0bc714800a7a03c01 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 10 Feb 2018 10:32:14 -0600 Subject: [PATCH 5/6] Use Html2Text to generate plaintext for keyword mutes. #236. This allows us to match URLs inside link hrefs. --- app/models/glitch/keyword_mute_helper.rb | 8 ++------ spec/models/glitch/keyword_mute_helper_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/glitch/keyword_mute_helper.rb b/app/models/glitch/keyword_mute_helper.rb index 1b8c64e4ed..6d067947f1 100644 --- a/app/models/glitch/keyword_mute_helper.rb +++ b/app/models/glitch/keyword_mute_helper.rb @@ -1,16 +1,12 @@ -require 'htmlentities' +require 'html2text' class Glitch::KeywordMuteHelper - include ActionView::Helpers::SanitizeHelper - attr_reader :text_matcher attr_reader :tag_matcher - attr_reader :entity_decoder def initialize(receiver_id) @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) - @entity_decoder = HTMLEntities.new end def matches?(status) @@ -26,6 +22,6 @@ class Glitch::KeywordMuteHelper end def prepare_text(text) - entity_decoder.decode(strip_tags(text)).tap { |x| puts x } + Html2Text.convert(text) end end diff --git a/spec/models/glitch/keyword_mute_helper_spec.rb b/spec/models/glitch/keyword_mute_helper_spec.rb index 9d09e58da8..b3f991d5b8 100644 --- a/spec/models/glitch/keyword_mute_helper_spec.rb +++ b/spec/models/glitch/keyword_mute_helper_spec.rb @@ -39,5 +39,12 @@ RSpec.describe Glitch::KeywordMuteHelper do expect(helper.matches?(status)).to be true end + + it 'matches link hrefs in HTML text' do + status = Fabricate(:status, text: '

yep

') + Glitch::KeywordMute.create!(account: alice, keyword: 'milk') + + expect(helper.matches?(status)).to be true + end end end From e16e51d341270a1fd51416d2e472eb59ab063638 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 10 Feb 2018 11:01:49 -0600 Subject: [PATCH 6/6] Remove NOKOGIRI_USE_SYSTEM_LIBRARIES from Travis. #236. Nokogiri linked to the version of libxml2 shipped in Travis' Trusty image exhibits incorrect behavior when parsing strings resembling

I <3 oats

In particular, Html2Text.convert (which uses nokogiri) will return "I" for the above string when it ought to return "I <3 oats". The version of libxml2 shipped with nokogiri 1.8.1+ exhibits correct parse behavior for this string, as does the libxml2 present in Ubuntu Xenial. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c9a442aedd..505d8683e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,6 @@ env: - LOCAL_DOMAIN=cb6e6126.ngrok.io - LOCAL_HTTPS=true - RAILS_ENV=test - - NOKOGIRI_USE_SYSTEM_LIBRARIES=true - PARALLEL_TEST_PROCESSORS=2 - "PATH=$HOME:$PATH"