Use config_for for VAPID key storage (#34845)
				
					
				
			This commit is contained in:
		@@ -40,7 +40,6 @@ Style/FetchEnvVar:
 | 
				
			|||||||
    - 'config/initializers/cache_buster.rb'
 | 
					    - 'config/initializers/cache_buster.rb'
 | 
				
			||||||
    - 'config/initializers/devise.rb'
 | 
					    - 'config/initializers/devise.rb'
 | 
				
			||||||
    - 'config/initializers/paperclip.rb'
 | 
					    - 'config/initializers/paperclip.rb'
 | 
				
			||||||
    - 'config/initializers/vapid.rb'
 | 
					 | 
				
			||||||
    - 'lib/tasks/repo.rake'
 | 
					    - 'lib/tasks/repo.rake'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# This cop supports safe autocorrection (--autocorrect).
 | 
					# This cop supports safe autocorrection (--autocorrect).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,8 +79,8 @@ class WebPushRequest
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  def vapid_key
 | 
					  def vapid_key
 | 
				
			||||||
    @vapid_key ||= Webpush::VapidKey.from_keys(
 | 
					    @vapid_key ||= Webpush::VapidKey.from_keys(
 | 
				
			||||||
      Rails.configuration.x.vapid_public_key,
 | 
					      Rails.configuration.x.vapid.public_key,
 | 
				
			||||||
      Rails.configuration.x.vapid_private_key
 | 
					      Rails.configuration.x.vapid.private_key
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,6 @@ class REST::ApplicationSerializer < ActiveModel::Serializer
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def vapid_key
 | 
					  def vapid_key
 | 
				
			||||||
    Rails.configuration.x.vapid_public_key
 | 
					    Rails.configuration.x.vapid.public_key
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -65,7 +65,7 @@ class REST::InstanceSerializer < ActiveModel::Serializer
 | 
				
			|||||||
      },
 | 
					      },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      vapid: {
 | 
					      vapid: {
 | 
				
			||||||
        public_key: Rails.configuration.x.vapid_public_key,
 | 
					        public_key: Rails.configuration.x.vapid.public_key,
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      accounts: {
 | 
					      accounts: {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ class REST::WebPushSubscriptionSerializer < ActiveModel::Serializer
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def server_key
 | 
					  def server_key
 | 
				
			||||||
    Rails.configuration.x.vapid_public_key
 | 
					    Rails.configuration.x.vapid.public_key
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def policy
 | 
					  def policy
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@
 | 
				
			|||||||
  - if user_signed_in?
 | 
					  - if user_signed_in?
 | 
				
			||||||
    %meta{ name: 'initialPath', content: request.path }
 | 
					    %meta{ name: 'initialPath', content: request.path }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  %meta{ name: 'applicationServerKey', content: Rails.configuration.x.vapid_public_key }
 | 
					  %meta{ name: 'applicationServerKey', content: Rails.configuration.x.vapid.public_key }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  = render_initial_state
 | 
					  = render_initial_state
 | 
				
			||||||
  = vite_typescript_tag 'application.ts', crossorigin: 'anonymous'
 | 
					  = vite_typescript_tag 'application.ts', crossorigin: 'anonymous'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,6 +106,7 @@ module Mastodon
 | 
				
			|||||||
    config.x.captcha = config_for(:captcha)
 | 
					    config.x.captcha = config_for(:captcha)
 | 
				
			||||||
    config.x.mastodon = config_for(:mastodon)
 | 
					    config.x.mastodon = config_for(:mastodon)
 | 
				
			||||||
    config.x.translation = config_for(:translation)
 | 
					    config.x.translation = config_for(:translation)
 | 
				
			||||||
 | 
					    config.x.vapid = config_for(:vapid)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ENV.fetch('QUERY_LOG_TAGS_ENABLED', 'false') == 'true'
 | 
					    if ENV.fetch('QUERY_LOG_TAGS_ENABLED', 'false') == 'true'
 | 
				
			||||||
      config.active_record.query_log_tags_enabled = ENV.fetch('QUERY_LOG_TAGS_ENABLED', 'false') == 'true'
 | 
					      config.active_record.query_log_tags_enabled = ENV.fetch('QUERY_LOG_TAGS_ENABLED', 'false') == 'true'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,10 +40,10 @@ Rails.application.configure do
 | 
				
			|||||||
  # Override default file logging in favor of STDOUT logging in dev environment
 | 
					  # Override default file logging in favor of STDOUT logging in dev environment
 | 
				
			||||||
  config.logger = ActiveSupport::TaggedLogging.logger($stdout, formatter: config.log_formatter)
 | 
					  config.logger = ActiveSupport::TaggedLogging.logger($stdout, formatter: config.log_formatter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Generate random VAPID keys
 | 
					  # Generate random VAPID keys when needed
 | 
				
			||||||
  Webpush.generate_key.tap do |vapid_key|
 | 
					  Webpush.generate_key.tap do |vapid_key|
 | 
				
			||||||
    config.x.vapid_private_key = vapid_key.private_key
 | 
					    config.x.vapid.private_key ||= vapid_key.private_key
 | 
				
			||||||
    config.x.vapid_public_key = vapid_key.public_key
 | 
					    config.x.vapid.public_key ||= vapid_key.public_key
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Don't care if the mailer can't send.
 | 
					  # Don't care if the mailer can't send.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,10 +48,11 @@ Rails.application.configure do
 | 
				
			|||||||
  # Print deprecation notices to the stderr.
 | 
					  # Print deprecation notices to the stderr.
 | 
				
			||||||
  config.active_support.deprecation = :stderr
 | 
					  config.active_support.deprecation = :stderr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Generate random VAPID keys
 | 
					  # Generate random VAPID keys when needed
 | 
				
			||||||
  vapid_key = Webpush.generate_key
 | 
					  Webpush.generate_key.tap do |vapid_key|
 | 
				
			||||||
  config.x.vapid_private_key = vapid_key.private_key
 | 
					    config.x.vapid.private_key ||= vapid_key.private_key
 | 
				
			||||||
  config.x.vapid_public_key = vapid_key.public_key
 | 
					    config.x.vapid.public_key ||= vapid_key.public_key
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Raise exceptions when a reorder occurs in in_batches
 | 
					  # Raise exceptions when a reorder occurs in in_batches
 | 
				
			||||||
  config.active_record.error_on_ignored_order = true
 | 
					  config.active_record.error_on_ignored_order = true
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,16 +0,0 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Rails.application.configure do
 | 
					 | 
				
			||||||
  # You can generate the keys using the following command (first is the private key, second is the public one)
 | 
					 | 
				
			||||||
  # You should only generate this once per instance. If you later decide to change it, all push subscription will
 | 
					 | 
				
			||||||
  # be invalidated, requiring the users to access the website again to resubscribe.
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # Generate with `bundle exec rails mastodon:webpush:generate_vapid_key` task (`docker-compose run --rm web bundle exec rails mastodon:webpush:generate_vapid_key` if you use docker compose)
 | 
					 | 
				
			||||||
  #
 | 
					 | 
				
			||||||
  # For more information visit https://rossta.net/blog/using-the-web-push-api-with-vapid.html
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if Rails.env.production?
 | 
					 | 
				
			||||||
    config.x.vapid_private_key = ENV['VAPID_PRIVATE_KEY']
 | 
					 | 
				
			||||||
    config.x.vapid_public_key = ENV['VAPID_PUBLIC_KEY']
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
							
								
								
									
										17
									
								
								config/vapid.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								config/vapid.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					# You can generate the private and public keys using the following task. You
 | 
				
			||||||
 | 
					# should only generate this once per instance. If you later decide to change it,
 | 
				
			||||||
 | 
					# all push subscriptions will be invalidated, requiring users to access the
 | 
				
			||||||
 | 
					# website again to resubscribe.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Generate on the CLI:
 | 
				
			||||||
 | 
					# `bundle exec rails mastodon:webpush:generate_vapid_key`
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Generate via Docker Compose:
 | 
				
			||||||
 | 
					# `docker-compose run --rm web bundle exec rails mastodon:webpush:generate_vapid_key`
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# For more information visit
 | 
				
			||||||
 | 
					# https://rossta.net/blog/using-the-web-push-api-with-vapid.html
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					shared:
 | 
				
			||||||
 | 
					  private_key: <%= ENV.fetch('VAPID_PRIVATE_KEY', nil) %>
 | 
				
			||||||
 | 
					  public_key: <%= ENV.fetch('VAPID_PUBLIC_KEY', nil) %>
 | 
				
			||||||
@@ -29,7 +29,7 @@ RSpec.describe 'Credentials' do
 | 
				
			|||||||
            redirect_uris: token.application.redirect_uris,
 | 
					            redirect_uris: token.application.redirect_uris,
 | 
				
			||||||
            # Deprecated properties as of 4.3:
 | 
					            # Deprecated properties as of 4.3:
 | 
				
			||||||
            redirect_uri: token.application.redirect_uri.split.first,
 | 
					            redirect_uri: token.application.redirect_uri.split.first,
 | 
				
			||||||
            vapid_key: Rails.configuration.x.vapid_public_key
 | 
					            vapid_key: Rails.configuration.x.vapid.public_key
 | 
				
			||||||
          )
 | 
					          )
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
@@ -69,7 +69,7 @@ RSpec.describe 'Credentials' do
 | 
				
			|||||||
            redirect_uris: token.application.redirect_uris,
 | 
					            redirect_uris: token.application.redirect_uris,
 | 
				
			||||||
            # Deprecated properties as of 4.3:
 | 
					            # Deprecated properties as of 4.3:
 | 
				
			||||||
            redirect_uri: token.application.redirect_uri.split.first,
 | 
					            redirect_uri: token.application.redirect_uri.split.first,
 | 
				
			||||||
            vapid_key: Rails.configuration.x.vapid_public_key
 | 
					            vapid_key: Rails.configuration.x.vapid.public_key
 | 
				
			||||||
          )
 | 
					          )
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,7 +49,7 @@ RSpec.describe 'Apps' do
 | 
				
			|||||||
            redirect_uris: redirect_uris,
 | 
					            redirect_uris: redirect_uris,
 | 
				
			||||||
            # Deprecated properties as of 4.3:
 | 
					            # Deprecated properties as of 4.3:
 | 
				
			||||||
            redirect_uri: redirect_uri,
 | 
					            redirect_uri: redirect_uri,
 | 
				
			||||||
            vapid_key: Rails.configuration.x.vapid_public_key
 | 
					            vapid_key: Rails.configuration.x.vapid.public_key
 | 
				
			||||||
          )
 | 
					          )
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,7 @@ RSpec.describe REST::InstanceSerializer do
 | 
				
			|||||||
  describe 'configuration' do
 | 
					  describe 'configuration' do
 | 
				
			||||||
    it 'returns the VAPID public key' do
 | 
					    it 'returns the VAPID public key' do
 | 
				
			||||||
      expect(serialization['configuration']['vapid']).to eq({
 | 
					      expect(serialization['configuration']['vapid']).to eq({
 | 
				
			||||||
        'public_key' => Rails.configuration.x.vapid_public_key,
 | 
					        'public_key' => Rails.configuration.x.vapid.public_key,
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,13 +38,13 @@ RSpec.describe Web::PushNotificationWorker do
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  describe 'perform' do
 | 
					  describe 'perform' do
 | 
				
			||||||
    around do |example|
 | 
					    around do |example|
 | 
				
			||||||
      original_private = Rails.configuration.x.vapid_private_key
 | 
					      original_private = Rails.configuration.x.vapid.private_key
 | 
				
			||||||
      original_public = Rails.configuration.x.vapid_public_key
 | 
					      original_public = Rails.configuration.x.vapid.public_key
 | 
				
			||||||
      Rails.configuration.x.vapid_private_key = vapid_private_key
 | 
					      Rails.configuration.x.vapid.private_key = vapid_private_key
 | 
				
			||||||
      Rails.configuration.x.vapid_public_key = vapid_public_key
 | 
					      Rails.configuration.x.vapid.public_key = vapid_public_key
 | 
				
			||||||
      example.run
 | 
					      example.run
 | 
				
			||||||
      Rails.configuration.x.vapid_private_key = original_private
 | 
					      Rails.configuration.x.vapid.private_key = original_private
 | 
				
			||||||
      Rails.configuration.x.vapid_public_key = original_public
 | 
					      Rails.configuration.x.vapid.public_key = original_public
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    before do
 | 
					    before do
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user