2019-07-03 08:53:00 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# Sanitize HTML produced by markup languages (Markdown, AsciiDoc...).
|
|
|
|
# Specific rules are implemented in dedicated filters:
|
|
|
|
#
|
|
|
|
# - Banzai::Filter::SanitizationFilter (Markdown)
|
|
|
|
# - Banzai::Filter::AsciiDocSanitizationFilter (AsciiDoc/Asciidoctor)
|
2019-12-16 18:08:22 +00:00
|
|
|
# - Banzai::Filter::BroadcastMessageSanitizationFilter (Markdown with styled links and line breaks)
|
2019-07-03 08:53:00 +00:00
|
|
|
#
|
|
|
|
# Extends HTML::Pipeline::SanitizationFilter with common rules.
|
|
|
|
class BaseSanitizationFilter < HTML::Pipeline::SanitizationFilter
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 13:41:11 +00:00
|
|
|
extend Gitlab::Utils::SanitizeNodeLink
|
2019-07-03 08:53:00 +00:00
|
|
|
|
|
|
|
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
|
|
|
|
|
|
|
|
def whitelist
|
|
|
|
strong_memoize(:whitelist) do
|
|
|
|
whitelist = super.deep_dup
|
|
|
|
|
|
|
|
# Allow span elements
|
|
|
|
whitelist[:elements].push('span')
|
|
|
|
|
|
|
|
# Allow data-math-style attribute in order to support LaTeX formatting
|
|
|
|
whitelist[:attributes]['code'] = %w(data-math-style)
|
|
|
|
whitelist[:attributes]['pre'] = %w(data-math-style)
|
|
|
|
|
|
|
|
# Allow html5 details/summary elements
|
|
|
|
whitelist[:elements].push('details')
|
|
|
|
whitelist[:elements].push('summary')
|
|
|
|
|
|
|
|
# Allow abbr elements with title attribute
|
|
|
|
whitelist[:elements].push('abbr')
|
|
|
|
whitelist[:attributes]['abbr'] = %w(title)
|
|
|
|
|
|
|
|
# Disallow `name` attribute globally, allow on `a`
|
|
|
|
whitelist[:attributes][:all].delete('name')
|
|
|
|
whitelist[:attributes]['a'].push('name')
|
|
|
|
|
|
|
|
# Allow any protocol in `a` elements
|
|
|
|
# and then remove links with unsafe protocols
|
|
|
|
whitelist[:protocols].delete('a')
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 13:41:11 +00:00
|
|
|
whitelist[:transformers].push(self.class.method(:remove_unsafe_links))
|
2019-07-03 08:53:00 +00:00
|
|
|
|
|
|
|
# Remove `rel` attribute from `a` elements
|
|
|
|
whitelist[:transformers].push(self.class.remove_rel)
|
|
|
|
|
|
|
|
customize_whitelist(whitelist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def customize_whitelist(whitelist)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def remove_rel
|
|
|
|
lambda do |env|
|
|
|
|
if env[:node_name] == 'a'
|
|
|
|
env[:node].remove_attribute('rel')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|