Skip to content

Commit

Permalink
Merge pull request #14 from DmitryBochkarev/SERVICES-906
Browse files Browse the repository at this point in the history
fix(sanitizers): подменять невалидные ссылки своим содержимым
  • Loading branch information
DmitryBochkarev committed Mar 10, 2016
2 parents 184963b + c23456a commit 5c79160
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ bundler/install:

appraisal/install:
${BUNDLE} exec ${APPRAISAL} install

clear:
rm -rf gemfiles
rm -f Gemfile.lock
2 changes: 2 additions & 0 deletions lib/string_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def call(env)
def normalize_link(node, attr_name)
return unless node[attr_name]
node[attr_name] = Addressable::URI.parse(node[attr_name]).normalize.to_s
rescue Addressable::URI::InvalidURIError
node.swap node.children
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/string_tools/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def call(node)
elsif !whitelisted?(SimpleIDN.to_unicode(uri.host))
replace_with_content node
end
rescue Addressable::URI::InvalidURIError
replace_with_content node
end

def whitelisted?(domain)
Expand Down
8 changes: 8 additions & 0 deletions spec/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
MARKUP
end
end

context 'content with invalid links' do
let(:html) { '<a href="http://"><span>a</span>www.фермаежей.рф<span>z</span></a>' }

it 'should return content without links' do
is_expected.to eq '<span>a</span>www.фермаежей.рф<span>z</span>'
end
end
end

context 'when whitelist passed' do
Expand Down
12 changes: 12 additions & 0 deletions spec/string_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
sanitized_string = described_class.sanitize(origin_str)
expect(sanitized_string).to eq '<a href="http://www.xn--80ajbaetq5a8a.xn--p1ai/">www.фермаежей.рф</a>'
end

it 'should delete links with invalid href but keep content' do
origin_str = '<a href="http://"><span>a</span>www.фермаежей.рф<span>z</span></a>'
sanitized_string = described_class.sanitize(origin_str)
expect(sanitized_string).to eq '<span>a</span>www.фермаежей.рф<span>z</span>'
end

it 'should delete images with invalid src' do
origin_str = '<span>a</span><img src="http://"/><span>z</span>'
sanitized_string = described_class.sanitize(origin_str)
expect(sanitized_string).to eq '<span>a</span><span>z</span>'
end
end

describe '#strip_all_tags_and_entities' do
Expand Down
4 changes: 2 additions & 2 deletions string_tools.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '>= 2.14.0'
spec.add_development_dependency 'rspec-rails', '>= 2.14.0'
spec.add_development_dependency 'rspec', '>= 3.4'
spec.add_development_dependency 'rspec-rails', '>= 3.4'
spec.add_development_dependency 'rspec-given', '~> 3.5'
spec.add_development_dependency 'shoulda-matchers', '~> 2.0'
spec.add_development_dependency 'appraisal', '>= 1.0.2'
Expand Down

0 comments on commit 5c79160

Please sign in to comment.