From a640c322c19013647cbd19ac907dd18b57c2b1b8 Mon Sep 17 00:00:00 2001 From: David Yip Date: Tue, 5 Jun 2018 02:49:28 -0500 Subject: [PATCH] Escape metacharacters in non-whole-word keyword mutes. Fixes #533. Also addresses #463. --- app/models/glitch/keyword_mute.rb | 2 +- spec/models/glitch/keyword_mute_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/glitch/keyword_mute.rb b/app/models/glitch/keyword_mute.rb index e7cbbe617ff..7d0002afd31 100644 --- a/app/models/glitch/keyword_mute.rb +++ b/app/models/glitch/keyword_mute.rb @@ -50,7 +50,7 @@ class Glitch::KeywordMute < ApplicationRecord end def matches?(str) - str =~ (whole_word ? boundary_regex_for_keyword : /#{keyword}/i) + str =~ (whole_word ? boundary_regex_for_keyword : /#{Regexp.escape(keyword)}/i) end end diff --git a/spec/models/glitch/keyword_mute_spec.rb b/spec/models/glitch/keyword_mute_spec.rb index 79225e3b932..443832ac72a 100644 --- a/spec/models/glitch/keyword_mute_spec.rb +++ b/spec/models/glitch/keyword_mute_spec.rb @@ -79,12 +79,18 @@ RSpec.describe Glitch::KeywordMute, type: :model do expect(matcher.matches?('(hot take)')).to be_truthy end - it 'escapes metacharacters in keywords' do + it 'escapes metacharacters in whole-word keywords' do Glitch::KeywordMute.create!(account: alice, keyword: '(hot take)') expect(matcher.matches?('(hot take)')).to be_truthy end + it 'escapes metacharacters in non-whole-word keywords' do + Glitch::KeywordMute.create!(account: alice, keyword: '(-', whole_word: false) + + expect(matcher.matches?('bad (-)')).to be_truthy + end + it 'uses case-folding rules appropriate for more than just English' do Glitch::KeywordMute.create!(account: alice, keyword: 'großeltern')