Adds TagSanitizer

This commit is contained in:
kikito
2015-08-03 19:30:06 +02:00
parent 8fa6bab454
commit 99c09afb02
2 changed files with 40 additions and 0 deletions

17
lib/tag_sanitizer.rb Normal file
View File

@@ -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

View File

@@ -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