Change form-action Content-Security-Policy directive to be more restrictive (#26897)
This commit is contained in:
		@@ -8,6 +8,16 @@ module WebAppControllerConcern
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    before_action :redirect_unauthenticated_to_permalinks!
 | 
					    before_action :redirect_unauthenticated_to_permalinks!
 | 
				
			||||||
    before_action :set_app_body_class
 | 
					    before_action :set_app_body_class
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    content_security_policy do |p|
 | 
				
			||||||
 | 
					      policy = ContentSecurityPolicy.new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if policy.sso_host.present?
 | 
				
			||||||
 | 
					        p.form_action policy.sso_host
 | 
				
			||||||
 | 
					      else
 | 
				
			||||||
 | 
					        p.form_action :none
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def skip_csrf_meta_tags?
 | 
					  def skip_csrf_meta_tags?
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,6 +13,22 @@ class ContentSecurityPolicy
 | 
				
			|||||||
    [assets_host, cdn_host_value, paperclip_root_url].compact
 | 
					    [assets_host, cdn_host_value, paperclip_root_url].compact
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def sso_host
 | 
				
			||||||
 | 
					    return unless ENV['ONE_CLICK_SSO_LOGIN'] == 'true' && ENV['OMNIAUTH_ONLY'] == 'true' && Devise.omniauth_providers.length == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    provider = Devise.omniauth_configs[Devise.omniauth_providers[0]]
 | 
				
			||||||
 | 
					    @sso_host ||= begin
 | 
				
			||||||
 | 
					      case provider.provider
 | 
				
			||||||
 | 
					      when :cas
 | 
				
			||||||
 | 
					        provider.cas_url
 | 
				
			||||||
 | 
					      when :saml
 | 
				
			||||||
 | 
					        provider.options[:idp_sso_target_url]
 | 
				
			||||||
 | 
					      when :openid_connect
 | 
				
			||||||
 | 
					        provider.options.dig(:client_options, :authorization_endpoint) || OpenIDConnect::Discovery::Provider::Config.discover!(provider.options[:issuer]).authorization_endpoint
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def url_from_configured_asset_host
 | 
					  def url_from_configured_asset_host
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,24 +12,6 @@ policy = ContentSecurityPolicy.new
 | 
				
			|||||||
assets_host = policy.assets_host
 | 
					assets_host = policy.assets_host
 | 
				
			||||||
media_hosts = policy.media_hosts
 | 
					media_hosts = policy.media_hosts
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def sso_host
 | 
					 | 
				
			||||||
  return unless ENV['ONE_CLICK_SSO_LOGIN'] == 'true'
 | 
					 | 
				
			||||||
  return unless ENV['OMNIAUTH_ONLY'] == 'true'
 | 
					 | 
				
			||||||
  return unless Devise.omniauth_providers.length == 1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  provider = Devise.omniauth_configs[Devise.omniauth_providers[0]]
 | 
					 | 
				
			||||||
  @sso_host ||= begin
 | 
					 | 
				
			||||||
    case provider.provider
 | 
					 | 
				
			||||||
    when :cas
 | 
					 | 
				
			||||||
      provider.cas_url
 | 
					 | 
				
			||||||
    when :saml
 | 
					 | 
				
			||||||
      provider.options[:idp_sso_target_url]
 | 
					 | 
				
			||||||
    when :openid_connect
 | 
					 | 
				
			||||||
      provider.options.dig(:client_options, :authorization_endpoint) || OpenIDConnect::Discovery::Provider::Config.discover!(provider.options[:issuer]).authorization_endpoint
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Rails.application.config.content_security_policy do |p|
 | 
					Rails.application.config.content_security_policy do |p|
 | 
				
			||||||
  p.base_uri        :none
 | 
					  p.base_uri        :none
 | 
				
			||||||
  p.default_src     :none
 | 
					  p.default_src     :none
 | 
				
			||||||
@@ -40,8 +22,8 @@ Rails.application.config.content_security_policy do |p|
 | 
				
			|||||||
  p.media_src       :self, :data, *media_hosts
 | 
					  p.media_src       :self, :data, *media_hosts
 | 
				
			||||||
  p.manifest_src    :self, assets_host
 | 
					  p.manifest_src    :self, assets_host
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if sso_host.present?
 | 
					  if policy.sso_host.present?
 | 
				
			||||||
    p.form_action :self, sso_host
 | 
					    p.form_action :self, policy.sso_host
 | 
				
			||||||
  else
 | 
					  else
 | 
				
			||||||
    p.form_action :self
 | 
					    p.form_action :self
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,7 +26,7 @@ RSpec.describe 'Content-Security-Policy' do
 | 
				
			|||||||
      connect-src 'self' data: blob: https://cb6e6126.ngrok.io #{Rails.configuration.x.streaming_api_base_url}
 | 
					      connect-src 'self' data: blob: https://cb6e6126.ngrok.io #{Rails.configuration.x.streaming_api_base_url}
 | 
				
			||||||
      default-src 'none'
 | 
					      default-src 'none'
 | 
				
			||||||
      font-src 'self' https://cb6e6126.ngrok.io
 | 
					      font-src 'self' https://cb6e6126.ngrok.io
 | 
				
			||||||
      form-action 'self'
 | 
					      form-action 'none'
 | 
				
			||||||
      frame-ancestors 'none'
 | 
					      frame-ancestors 'none'
 | 
				
			||||||
      frame-src 'self' https:
 | 
					      frame-src 'self' https:
 | 
				
			||||||
      img-src 'self' data: blob: https://cb6e6126.ngrok.io
 | 
					      img-src 'self' data: blob: https://cb6e6126.ngrok.io
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user