2021-05-26 17:41:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-06 21:19:41 +08:00
|
|
|
module Onebox
|
|
|
|
module SanitizeConfig
|
2021-05-26 17:41:35 +08:00
|
|
|
HTTP_PROTOCOLS ||= ['http', 'https', :relative].freeze
|
|
|
|
|
2022-04-06 21:19:41 +08:00
|
|
|
ONEBOX ||= Sanitize::Config.freeze_config(Sanitize::Config.merge(Sanitize::Config::RELAXED,
|
|
|
|
elements: Sanitize::Config::RELAXED[:elements] + %w[audio details embed iframe source video svg path],
|
2021-05-26 17:41:35 +08:00
|
|
|
|
|
|
|
attributes: {
|
2022-04-06 21:19:41 +08:00
|
|
|
'a' => Sanitize::Config::RELAXED[:attributes]['a'] + %w(target),
|
2021-05-26 17:41:35 +08:00
|
|
|
'audio' => %w[controls controlslist],
|
|
|
|
'embed' => %w[height src type width],
|
|
|
|
'iframe' => %w[allowfullscreen frameborder height scrolling src width data-original-href data-unsanitized-src],
|
|
|
|
'source' => %w[src type],
|
|
|
|
'video' => %w[controls height loop width autoplay muted poster controlslist playsinline],
|
|
|
|
'path' => %w[d],
|
|
|
|
'svg' => ['aria-hidden', 'width', 'height', 'viewbox'],
|
|
|
|
'div' => [:data], # any data-* attributes,
|
|
|
|
'span' => [:data], # any data-* attributes
|
|
|
|
},
|
|
|
|
|
|
|
|
add_attributes: {
|
|
|
|
'iframe' => {
|
|
|
|
'seamless' => 'seamless',
|
|
|
|
'sandbox' => 'allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox' \
|
|
|
|
' allow-presentation',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-06 21:19:41 +08:00
|
|
|
transformers: (Sanitize::Config::RELAXED[:transformers] || []) + [
|
2021-05-26 17:41:35 +08:00
|
|
|
lambda do |env|
|
|
|
|
next unless env[:node_name] == 'a'
|
|
|
|
a_tag = env[:node]
|
|
|
|
a_tag['href'] ||= '#'
|
|
|
|
if a_tag['href'] =~ %r{^(?:[a-z]+:)?//}
|
|
|
|
a_tag['rel'] = 'nofollow ugc noopener'
|
|
|
|
else
|
|
|
|
a_tag.remove_attribute('target')
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
lambda do |env|
|
|
|
|
next unless env[:node_name] == 'iframe'
|
|
|
|
|
|
|
|
iframe = env[:node]
|
|
|
|
allowed_regexes = env[:config][:allowed_iframe_regexes] || [/.*/]
|
|
|
|
|
|
|
|
allowed = allowed_regexes.any? { |r| iframe["src"] =~ r }
|
|
|
|
|
|
|
|
if !allowed
|
|
|
|
# add a data attribute with the blocked src. This is not required
|
|
|
|
# but makes it much easier to troubleshoot onebox issues
|
|
|
|
iframe["data-unsanitized-src"] = iframe["src"]
|
|
|
|
iframe.remove_attribute("src")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
],
|
|
|
|
|
|
|
|
protocols: {
|
|
|
|
'embed' => { 'src' => HTTP_PROTOCOLS },
|
|
|
|
'iframe' => { 'src' => HTTP_PROTOCOLS },
|
|
|
|
'source' => { 'src' => HTTP_PROTOCOLS },
|
|
|
|
},
|
|
|
|
|
|
|
|
css: {
|
2022-04-06 21:19:41 +08:00
|
|
|
properties: Sanitize::Config::RELAXED[:css][:properties] + %w[--aspect-ratio]
|
2021-05-26 17:41:35 +08:00
|
|
|
}
|
2022-04-06 21:19:41 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
DISCOURSE_ONEBOX ||=
|
|
|
|
Sanitize::Config.freeze_config(
|
|
|
|
Sanitize::Config.merge(
|
|
|
|
ONEBOX,
|
|
|
|
attributes: Sanitize::Config.merge(
|
|
|
|
ONEBOX[:attributes],
|
|
|
|
'aside' => [:data]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
end
|