diff --git a/lib/tag_sanitizer.rb b/lib/tag_sanitizer.rb new file mode 100644 index 000000000..f44b06adf --- /dev/null +++ b/lib/tag_sanitizer.rb @@ -0,0 +1,17 @@ +class TagSanitizer + + DISALLOWED_STRINGS = %w(? < > = /) + + def sanitize_tag(tag) + tag = tag.dup + DISALLOWED_STRINGS.each do |s| + tag.gsub!(s, '') + end + tag + end + + def sanitize_tag_list(tag_list) + tag_list.map { |tag| sanitize_tag(tag) } + end + +end diff --git a/spec/lib/tag_sanitizer_spec.rb b/spec/lib/tag_sanitizer_spec.rb new file mode 100644 index 000000000..e1fd6499b --- /dev/null +++ b/spec/lib/tag_sanitizer_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe TagSanitizer do + + subject { described_class.new } + + describe '#sanitize_tag' do + it 'allows regular text, even spaces' do + expect(subject.sanitize_tag('hello there')).to eq('hello there') + end + + it 'filters out dangerous strings' do + expect(subject.sanitize_tag('user_id=1')).to eq('user_id1') + end + end + + describe '#sanitize_tag_list' do + it 'returns a new tag list with sanitized tags' do + expect(subject.sanitize_tag_list(%w{x=1 y?z})).to eq(%w(x1 yz)) + end + end + +end