diff options
author | Robert Speicher <rspeicher@gmail.com> | 2016-03-14 23:07:51 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-03-15 01:05:01 +0300 |
commit | 41de7b345b0abdaba2f0d7614ebdb1cc7310a5fb (patch) | |
tree | 2d6f9353aeab777469fee6fbcf66c0b8d1ab2cf2 /lib | |
parent | b782e7c9732275175d79b485fcddc85e575d48dd (diff) |
Be more intelligent about sanitizing links with unsafe protocols
This prevents false matches on relative links like
`[database](database.md)`.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/14220
Diffstat (limited to 'lib')
-rw-r--r-- | lib/banzai/filter/sanitization_filter.rb | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/banzai/filter/sanitization_filter.rb b/lib/banzai/filter/sanitization_filter.rb index abd79b329ae..e8011519608 100644 --- a/lib/banzai/filter/sanitization_filter.rb +++ b/lib/banzai/filter/sanitization_filter.rb @@ -7,7 +7,7 @@ module Banzai # # Extends HTML::Pipeline::SanitizationFilter with a custom whitelist. class SanitizationFilter < HTML::Pipeline::SanitizationFilter - UNSAFE_PROTOCOLS = %w(javascript :javascript data vbscript).freeze + UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze def whitelist whitelist = super @@ -64,7 +64,12 @@ module Banzai return unless node.name == 'a' return unless node.has_attribute?('href') - if node['href'].start_with?(*UNSAFE_PROTOCOLS) + begin + uri = Addressable::URI.parse(node['href']) + uri.scheme.strip! if uri.scheme + + node.remove_attribute('href') if UNSAFE_PROTOCOLS.include?(uri.scheme) + rescue Addressable::URI::InvalidURIError node.remove_attribute('href') end end |