summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-07-30 11:10:46 +0200
committerGitHub <noreply@github.com>2019-07-30 11:10:46 +0200
commit24552b5160a5090e7d6056fb69a209aa48fe4fce (patch)
tree57ab47f71d7f589c9da4dd959d3dec6f0902409a
parent85b7b565def2594b6ad791731802eb4c8a803a69 (diff)
Add whitelist mode (#11291)
-rw-r--r--app/controllers/about_controller.rb5
-rw-r--r--app/controllers/activitypub/base_controller.rb2
-rw-r--r--app/controllers/activitypub/inboxes_controller.rb2
-rw-r--r--app/controllers/admin/domain_allows_controller.rb40
-rw-r--r--app/controllers/admin/instances_controller.rb28
-rw-r--r--app/controllers/api/base_controller.rb9
-rw-r--r--app/controllers/api/v1/accounts_controller.rb2
-rw-r--r--app/controllers/api/v1/apps_controller.rb2
-rw-r--r--app/controllers/api/v1/instances/activity_controller.rb3
-rw-r--r--app/controllers/api/v1/instances/peers_controller.rb3
-rw-r--r--app/controllers/api/v1/instances_controller.rb1
-rw-r--r--app/controllers/application_controller.rb4
-rw-r--r--app/controllers/concerns/account_owned_concern.rb1
-rw-r--r--app/controllers/directories_controller.rb5
-rw-r--r--app/controllers/home_controller.rb2
-rw-r--r--app/controllers/media_controller.rb1
-rw-r--r--app/controllers/media_proxy_controller.rb2
-rw-r--r--app/controllers/public_timelines_controller.rb5
-rw-r--r--app/controllers/remote_interaction_controller.rb1
-rw-r--r--app/controllers/tags_controller.rb1
-rw-r--r--app/helpers/domain_control_helper.rb10
-rw-r--r--app/models/domain_allow.rb33
-rw-r--r--app/models/instance.rb3
-rw-r--r--app/models/instance_filter.rb4
-rw-r--r--app/policies/domain_allow_policy.rb11
-rw-r--r--app/services/concerns/payloadable.rb2
-rw-r--r--app/services/unallow_domain_service.rb11
-rw-r--r--app/views/admin/domain_allows/new.html.haml14
-rw-r--r--app/views/admin/instances/index.html.haml35
-rw-r--r--app/views/admin/instances/show.html.haml4
-rw-r--r--app/views/admin/settings/edit.html.haml28
-rw-r--r--app/views/auth/registrations/new.html.haml2
-rw-r--r--app/views/layouts/public.html.haml9
-rw-r--r--config/initializers/2_whitelist_mode.rb5
-rw-r--r--config/locales/en.yml7
-rw-r--r--config/locales/simple_form.en.yml2
-rw-r--r--config/navigation.rb2
-rw-r--r--config/routes.rb1
-rw-r--r--db/migrate/20190705002136_create_domain_allows.rb9
-rw-r--r--db/schema.rb9
-rw-r--r--lib/mastodon/domains_cli.rb22
-rw-r--r--spec/fabricators/domain_allow_fabricator.rb3
-rw-r--r--spec/models/domain_allow_spec.rb5
-rw-r--r--streaming/index.js5
44 files changed, 302 insertions, 53 deletions
diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb
index 31cf177105b..d276e8fe5fa 100644
--- a/app/controllers/about_controller.rb
+++ b/app/controllers/about_controller.rb
@@ -3,6 +3,7 @@
class AboutController < ApplicationController
layout 'public'
+ before_action :require_open_federation!, only: [:show, :more]
before_action :set_body_classes, only: :show
before_action :set_instance_presenter
before_action :set_expires_in
@@ -19,6 +20,10 @@ class AboutController < ApplicationController
private
+ def require_open_federation!
+ not_found if whitelist_mode?
+ end
+
def new_user
User.new.tap do |user|
user.build_account
diff --git a/app/controllers/activitypub/base_controller.rb b/app/controllers/activitypub/base_controller.rb
index a3b5c4dfa52..0c2591e9743 100644
--- a/app/controllers/activitypub/base_controller.rb
+++ b/app/controllers/activitypub/base_controller.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
class ActivityPub::BaseController < Api::BaseController
+ skip_before_action :require_authenticated_user!
+
private
def set_cache_headers
diff --git a/app/controllers/activitypub/inboxes_controller.rb b/app/controllers/activitypub/inboxes_controller.rb
index 7cfd9a25e18..bcfc1e6d423 100644
--- a/app/controllers/activitypub/inboxes_controller.rb
+++ b/app/controllers/activitypub/inboxes_controller.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-class ActivityPub::InboxesController < Api::BaseController
+class ActivityPub::InboxesController < ActivityPub::BaseController
include SignatureVerification
include JsonLdHelper
include AccountOwnedConcern
diff --git a/app/controllers/admin/domain_allows_controller.rb b/app/controllers/admin/domain_allows_controller.rb
new file mode 100644
index 00000000000..31be1978bbb
--- /dev/null
+++ b/app/controllers/admin/domain_allows_controller.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class Admin::DomainAllowsController < Admin::BaseController
+ before_action :set_domain_allow, only: [:destroy]
+
+ def new
+ authorize :domain_allow, :create?
+
+ @domain_allow = DomainAllow.new(domain: params[:_domain])
+ end
+
+ def create
+ authorize :domain_allow, :create?
+
+ @domain_allow = DomainAllow.new(resource_params)
+
+ if @domain_allow.save
+ log_action :create, @domain_allow
+ redirect_to admin_instances_path, notice: I18n.t('admin.domain_allows.created_msg')
+ else
+ render :new
+ end
+ end
+
+ def destroy
+ authorize @domain_allow, :destroy?
+ UnallowDomainService.new.call(@domain_allow)
+ redirect_to admin_instances_path, notice: I18n.t('admin.domain_allows.destroyed_msg')
+ end
+
+ private
+
+ def set_domain_allow
+ @domain_allow = DomainAllow.find(params[:id])
+ end
+
+ def resource_params
+ params.require(:domain_allow).permit(:domain)
+ end
+end
diff --git a/app/controllers/admin/instances_controller.rb b/app/controllers/admin/instances_controller.rb
index 7888e844fb5..d4f2018079e 100644
--- a/app/controllers/admin/instances_controller.rb
+++ b/app/controllers/admin/instances_controller.rb
@@ -2,6 +2,10 @@
module Admin
class InstancesController < BaseController
+ before_action :set_domain_block, only: :show
+ before_action :set_domain_allow, only: :show
+ before_action :set_instance, only: :show
+
def index
authorize :instance, :index?
@@ -11,20 +15,38 @@ module Admin
def show
authorize :instance, :show?
- @instance = Instance.new(Account.by_domain_accounts.find_by(domain: params[:id]) || DomainBlock.find_by!(domain: params[:id]))
@following_count = Follow.where(account: Account.where(domain: params[:id])).count
@followers_count = Follow.where(target_account: Account.where(domain: params[:id])).count
@reports_count = Report.where(target_account: Account.where(domain: params[:id])).count
@blocks_count = Block.where(target_account: Account.where(domain: params[:id])).count
@available = DeliveryFailureTracker.available?(Account.select(:shared_inbox_url).where(domain: params[:id]).first&.shared_inbox_url)
@media_storage = MediaAttachment.where(account: Account.where(domain: params[:id])).sum(:file_file_size)
- @domain_block = DomainBlock.rule_for(params[:id])
end
private
+ def set_domain_block
+ @domain_block = DomainBlock.rule_for(params[:id])
+ end
+
+ def set_domain_allow
+ @domain_allow = DomainAllow.rule_for(params[:id])
+ end
+
+ def set_instance
+ resource = Account.by_domain_accounts.find_by(domain: params[:id])
+ resource ||= @domain_block
+ resource ||= @domain_allow
+
+ if resource
+ @instance = Instance.new(resource)
+ else
+ not_found
+ end
+ end
+
def filtered_instances
- InstanceFilter.new(filter_params).results
+ InstanceFilter.new(whitelist_mode? ? { allowed: true } : filter_params).results
end
def paginated_instances
diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb
index 6f33a1ea994..109e38ffabb 100644
--- a/app/controllers/api/base_controller.rb
+++ b/app/controllers/api/base_controller.rb
@@ -9,6 +9,7 @@ class Api::BaseController < ApplicationController
skip_before_action :store_current_location
skip_before_action :require_functional!
+ before_action :require_authenticated_user!, if: :disallow_unauthenticated_api_access?
before_action :set_cache_headers
protect_from_forgery with: :null_session
@@ -69,6 +70,10 @@ class Api::BaseController < ApplicationController
nil
end
+ def require_authenticated_user!
+ render json: { error: 'This API requires an authenticated user' }, status: 401 unless current_user
+ end
+
def require_user!
if !current_user
render json: { error: 'This method requires an authenticated user' }, status: 422
@@ -94,4 +99,8 @@ class Api::BaseController < ApplicationController
def set_cache_headers
response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
end
+
+ def disallow_unauthenticated_api_access?
+ authorized_fetch_mode?
+ end
end
diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb
index b0c62778e65..b306e8e8cce 100644
--- a/app/controllers/api/v1/accounts_controller.rb
+++ b/app/controllers/api/v1/accounts_controller.rb
@@ -12,6 +12,8 @@ class Api::V1::AccountsController < Api::BaseController
before_action :check_account_suspension, only: [:show]
before_action :check_enabled_registrations, only: [:create]
+ skip_before_action :require_authenticated_user!, only: :create
+
respond_to :json
def show
diff --git a/app/controllers/api/v1/apps_controller.rb b/app/controllers/api/v1/apps_controller.rb
index e9f7a7291c1..97177547a2b 100644
--- a/app/controllers/api/v1/apps_controller.rb
+++ b/app/controllers/api/v1/apps_controller.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
class Api::V1::AppsController < Api::BaseController
+ skip_before_action :require_authenticated_user!
+
def create
@app = Doorkeeper::Application.create!(application_options)
render json: @app, serializer: REST::ApplicationSerializer
diff --git a/app/controllers/api/v1/instances/activity_controller.rb b/app/controllers/api/v1/instances/activity_controller.rb
index d0080c5c2e1..4fb5a69d8b9 100644
--- a/app/controllers/api/v1/instances/activity_controller.rb
+++ b/app/controllers/api/v1/instances/activity_controller.rb
@@ -2,6 +2,7 @@
class Api::V1::Instances::ActivityController < Api::BaseController
before_action :require_enabled_api!
+
skip_before_action :set_cache_headers
respond_to :json
@@ -33,6 +34,6 @@ class Api::V1::Instances::ActivityController < Api::BaseController
end
def require_enabled_api!
- head 404 unless Setting.activity_api_enabled
+ head 404 unless Setting.activity_api_enabled && !whitelist_mode?
end
end
diff --git a/app/controllers/api/v1/instances/peers_controller.rb b/app/controllers/api/v1/instances/peers_controller.rb
index 450e6502f54..75c3cb4ba81 100644
--- a/app/controllers/api/v1/instances/peers_controller.rb
+++ b/app/controllers/api/v1/instances/peers_controller.rb
@@ -2,6 +2,7 @@
class Api::V1::Instances::PeersController < Api::BaseController
before_action :require_enabled_api!
+
skip_before_action :set_cache_headers
respond_to :json
@@ -14,6 +15,6 @@ class Api::V1::Instances::PeersController < Api::BaseController
private
def require_enabled_api!
- head 404 unless Setting.peers_api_enabled
+ head 404 unless Setting.peers_api_enabled && !whitelist_mode?
end
end
diff --git a/app/controllers/api/v1/instances_controller.rb b/app/controllers/api/v1/instances_controller.rb
index 93e4f000307..8d8231423ec 100644
--- a/app/controllers/api/v1/instances_controller.rb
+++ b/app/controllers/api/v1/instances_controller.rb
@@ -2,6 +2,7 @@
class Api::V1::InstancesController < Api::BaseController
respond_to :json
+
skip_before_action :set_cache_headers
def show
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 41ce1a0ca22..0d3913ee07d 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -11,12 +11,14 @@ class ApplicationController < ActionController::Base
include UserTrackingConcern
include SessionTrackingConcern
include CacheConcern
+ include DomainControlHelper
helper_method :current_account
helper_method :current_session
helper_method :current_theme
helper_method :single_user_mode?
helper_method :use_seamless_external_login?
+ helper_method :whitelist_mode?
rescue_from ActionController::RoutingError, with: :not_found
rescue_from ActiveRecord::RecordNotFound, with: :not_found
@@ -38,7 +40,7 @@ class ApplicationController < ActionController::Base
end
def authorized_fetch_mode?
- ENV['AUTHORIZED_FETCH'] == 'true'
+ ENV['AUTHORIZED_FETCH'] == 'true' || Rails.configuration.x.whitelist_mode
end
def public_fetch_mode?
diff --git a/app/controllers/concerns/account_owned_concern.rb b/app/controllers/concerns/account_owned_concern.rb
index 99c240fe986..460f71f65fa 100644
--- a/app/controllers/concerns/account_owned_concern.rb
+++ b/app/controllers/concerns/account_owned_concern.rb
@@ -4,6 +4,7 @@ module AccountOwnedConcern
extend ActiveSupport::Concern
included do
+ before_action :authenticate_user!, if: -> { whitelist_mode? && request.format != :json }
before_action :set_account, if: :account_required?
before_action :check_account_approval, if: :account_required?
before_action :check_account_suspension, if: :account_required?
diff --git a/app/controllers/directories_controller.rb b/app/controllers/directories_controller.rb
index 5949076740f..d2ef76f0691 100644
--- a/app/controllers/directories_controller.rb
+++ b/app/controllers/directories_controller.rb
@@ -3,7 +3,8 @@
class DirectoriesController < ApplicationController
layout 'public'
- before_action :check_enabled
+ before_action :authenticate_user!, if: :whitelist_mode?
+ before_action :require_enabled!
before_action :set_instance_presenter
before_action :set_tag, only: :show
before_action :set_tags
@@ -19,7 +20,7 @@ class DirectoriesController < ApplicationController
private
- def check_enabled
+ def require_enabled!
return not_found unless Setting.profile_directory
end
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 42493cd7825..22d507e7797 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -55,7 +55,7 @@ class HomeController < ApplicationController
end
def default_redirect_path
- if request.path.start_with?('/web')
+ if request.path.start_with?('/web') || whitelist_mode?
new_user_session_path
elsif single_user_mode?
short_account_path(Account.local.without_suspended.where('id > 0').first)
diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb
index b3b7519a1c2..1f693de3204 100644
--- a/app/controllers/media_controller.rb
+++ b/app/controllers/media_controller.rb
@@ -5,6 +5,7 @@ class MediaController < ApplicationController
skip_before_action :store_current_location
+ before_action :authenticate_user!, if: :whitelist_mode?
before_action :set_media_attachment
before_action :verify_permitted_status!
before_action :check_playable, only: :player
diff --git a/app/controllers/media_proxy_controller.rb b/app/controllers/media_proxy_controller.rb
index 8fc18dd0602..8da6c6fe0c9 100644
--- a/app/controllers/media_proxy_controller.rb