mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-16 09:18:46 +00:00
Extract constants for captcha directives/sources (#35439)
This commit is contained in:
@@ -5,6 +5,18 @@ module Auth::CaptchaConcern
|
|||||||
|
|
||||||
include Hcaptcha::Adapters::ViewMethods
|
include Hcaptcha::Adapters::ViewMethods
|
||||||
|
|
||||||
|
CAPTCHA_DIRECTIVES = %w(
|
||||||
|
connect_src
|
||||||
|
frame_src
|
||||||
|
script_src
|
||||||
|
style_src
|
||||||
|
).freeze
|
||||||
|
|
||||||
|
CAPTCHA_SOURCES = %w(
|
||||||
|
https://*.hcaptcha.com
|
||||||
|
https://hcaptcha.com
|
||||||
|
).freeze
|
||||||
|
|
||||||
included do
|
included do
|
||||||
helper_method :render_captcha
|
helper_method :render_captcha
|
||||||
end
|
end
|
||||||
@@ -42,20 +54,9 @@ module Auth::CaptchaConcern
|
|||||||
end
|
end
|
||||||
|
|
||||||
def extend_csp_for_captcha!
|
def extend_csp_for_captcha!
|
||||||
policy = request.content_security_policy&.clone
|
return unless captcha_required? && request.content_security_policy.present?
|
||||||
|
|
||||||
return unless captcha_required? && policy.present?
|
request.content_security_policy = captcha_adjusted_policy
|
||||||
|
|
||||||
%w(script_src frame_src style_src connect_src).each do |directive|
|
|
||||||
values = policy.send(directive)
|
|
||||||
|
|
||||||
values << 'https://hcaptcha.com' unless values.include?('https://hcaptcha.com') || values.include?('https:')
|
|
||||||
values << 'https://*.hcaptcha.com' unless values.include?('https://*.hcaptcha.com') || values.include?('https:')
|
|
||||||
|
|
||||||
policy.send(directive, *values)
|
|
||||||
end
|
|
||||||
|
|
||||||
request.content_security_policy = policy
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_captcha
|
def render_captcha
|
||||||
@@ -63,4 +64,24 @@ module Auth::CaptchaConcern
|
|||||||
|
|
||||||
hcaptcha_tags
|
hcaptcha_tags
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def captcha_adjusted_policy
|
||||||
|
request.content_security_policy.clone.tap do |policy|
|
||||||
|
populate_captcha_policy(policy)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def populate_captcha_policy(policy)
|
||||||
|
CAPTCHA_DIRECTIVES.each do |directive|
|
||||||
|
values = policy.send(directive)
|
||||||
|
|
||||||
|
CAPTCHA_SOURCES.each do |source|
|
||||||
|
values << source unless values.include?(source) || values.include?('https:')
|
||||||
|
end
|
||||||
|
|
||||||
|
policy.send(directive, *values)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user