diff --git a/app/controllers/concerns/web_app_controller_concern.rb b/app/controllers/concerns/web_app_controller_concern.rb index 5a540d17b44..ef08779fd85 100644 --- a/app/controllers/concerns/web_app_controller_concern.rb +++ b/app/controllers/concerns/web_app_controller_concern.rb @@ -27,10 +27,7 @@ module WebAppControllerConcern end def redirect_unauthenticated_to_permalinks! - return if user_signed_in? && current_account.moved_to_account_id.nil? - - permalink_redirector = PermalinkRedirector.new(request.original_fullpath) - return if permalink_redirector.redirect_path.blank? + return if (user_signed_in? && current_account.moved_to_account_id.nil?) || permalink_redirector.redirect_path.blank? expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.day) unless user_signed_in? @@ -72,4 +69,8 @@ module WebAppControllerConcern def set_referer_header response.set_header('Referrer-Policy', Setting.allow_referrer_origin ? 'strict-origin-when-cross-origin' : 'same-origin') end + + def permalink_redirector + @permalink_redirector ||= PermalinkRedirector.new(request.original_fullpath) + end end diff --git a/app/controllers/remote_accounts_controller.rb b/app/controllers/remote_accounts_controller.rb new file mode 100644 index 00000000000..40f820a3872 --- /dev/null +++ b/app/controllers/remote_accounts_controller.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class RemoteAccountsController < HomeController + before_action :require_resource_exists! + + private + + def require_resource_exists! + # The page should still render normally, but the HTTP code should be modified + response.status = 404 if permalink_redirector.object.nil? + end +end diff --git a/app/services/resolve_url_service.rb b/app/services/resolve_url_service.rb index 5c27121acd3..61f4855996a 100644 --- a/app/services/resolve_url_service.rb +++ b/app/services/resolve_url_service.rb @@ -103,7 +103,7 @@ class ResolveURLService < BaseService return unless recognized_params[:action] == 'show' Account.find_local(recognized_params[:username]) - when 'home' + when 'home', 'remote_accounts' return unless recognized_params[:action] == 'index' && recognized_params[:username_with_domain].present? if recognized_params[:any]&.match?(/\A[0-9]+\Z/) diff --git a/config/routes.rb b/config/routes.rb index ee040a30728..03f6d9d21d9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -176,7 +176,7 @@ Rails.application.routes.draw do get '/@:account_username/wrapstodon/:year/:share_key', to: 'wrapstodon#show', as: :public_wrapstodon end - get '/@:username_with_domain/(*any)', to: 'home#index', constraints: { username_with_domain: %r{([^/])+?} }, as: :account_with_domain, format: false + get '/@:username_with_domain/(*any)', to: 'remote_accounts#index', constraints: { username_with_domain: %r{([^/])+?} }, as: :account_with_domain, format: false get '/settings', to: redirect('/settings/profile') draw(:settings) diff --git a/spec/routing/accounts_routing_spec.rb b/spec/routing/accounts_routing_spec.rb index bb0bf082bde..64822f0299c 100644 --- a/spec/routing/accounts_routing_spec.rb +++ b/spec/routing/accounts_routing_spec.rb @@ -107,35 +107,35 @@ RSpec.describe 'Routes under accounts/' do let(:username) { 'alice@example.com' } it 'routes /@:username' do - expect(get("/@#{username}")).to route_to('home#index', username_with_domain: username) + expect(get("/@#{username}")).to route_to('remote_accounts#index', username_with_domain: username) end it 'routes /@:username/:id' do - expect(get("/@#{username}/123")).to route_to('home#index', username_with_domain: username, any: '123') + expect(get("/@#{username}/123")).to route_to('remote_accounts#index', username_with_domain: username, any: '123') end it 'routes /@:username/:id/embed' do - expect(get("/@#{username}/123/embed")).to route_to('home#index', username_with_domain: username, any: '123/embed') + expect(get("/@#{username}/123/embed")).to route_to('remote_accounts#index', username_with_domain: username, any: '123/embed') end it 'routes /@:username/following' do - expect(get("/@#{username}/following")).to route_to('home#index', username_with_domain: username, any: 'following') + expect(get("/@#{username}/following")).to route_to('remote_accounts#index', username_with_domain: username, any: 'following') end it 'routes /@:username/followers' do - expect(get("/@#{username}/followers")).to route_to('home#index', username_with_domain: username, any: 'followers') + expect(get("/@#{username}/followers")).to route_to('remote_accounts#index', username_with_domain: username, any: 'followers') end it 'routes /@:username/with_replies' do - expect(get("/@#{username}/with_replies")).to route_to('home#index', username_with_domain: username, any: 'with_replies') + expect(get("/@#{username}/with_replies")).to route_to('remote_accounts#index', username_with_domain: username, any: 'with_replies') end it 'routes /@:username/media' do - expect(get("/@#{username}/media")).to route_to('home#index', username_with_domain: username, any: 'media') + expect(get("/@#{username}/media")).to route_to('remote_accounts#index', username_with_domain: username, any: 'media') end it 'routes /@:username/tagged/:tag' do - expect(get("/@#{username}/tagged/foo")).to route_to('home#index', username_with_domain: username, any: 'tagged/foo') + expect(get("/@#{username}/tagged/foo")).to route_to('remote_accounts#index', username_with_domain: username, any: 'tagged/foo') end end