Fix Webfinger endpoint not handling new AP ID scheme (#38391)

This commit is contained in:
Claire 2026-03-26 10:58:47 +01:00 committed by GitHub
parent 19806be8ec
commit 806e2a993a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 90 additions and 109 deletions

View File

@ -18,23 +18,7 @@ module WellKnown
private private
def set_account def set_account
username = username_from_resource @account = WebfingerResource.new(resource_param).account
@account = begin
if username == Rails.configuration.x.local_domain || username == Rails.configuration.x.web_domain
Account.representative
else
Account.find_local!(username)
end
end
end
def username_from_resource
resource_user = resource_param
username, domain = resource_user.split('@')
resource_user = "#{username}@#{Rails.configuration.x.local_domain}" if Rails.configuration.x.alternate_domains.include?(domain)
WebfingerResource.new(resource_user).username
end end
def resource_param def resource_param

View File

@ -9,14 +9,14 @@ class WebfingerResource
@resource = resource @resource = resource
end end
def username def account
case resource case resource
when %r{\A(https?://)?#{instance_actor_regexp}/?\Z} when %r{\A(https?://)?#{instance_actor_regexp}/?\Z}
Rails.configuration.x.local_domain Account.representative
when /\Ahttps?/i when /\Ahttps?/i
username_from_url account_from_url
when /@/ when /@/
username_from_acct account_from_acct
else else
raise InvalidRequest raise InvalidRequest
end end
@ -31,11 +31,11 @@ class WebfingerResource
Regexp.union(hosts) Regexp.union(hosts)
end end
def username_from_url def account_from_url
if account_show_page? if account_show_page?
path_params[:username] path_params.key?(:username) ? Account.find_local!(path_params[:username]) : Account.local.find(path_params[:id])
elsif instance_actor_page? elsif instance_actor_page?
Rails.configuration.x.local_domain Account.representative
else else
raise ActiveRecord::RecordNotFound raise ActiveRecord::RecordNotFound
end end
@ -53,10 +53,13 @@ class WebfingerResource
Rails.application.routes.recognize_path(resource) Rails.application.routes.recognize_path(resource)
end end
def username_from_acct def account_from_acct
raise ActiveRecord::RecordNotFound unless domain_matches_local? raise ActiveRecord::RecordNotFound unless domain_matches_local?
local_username username = local_username
return Account.representative if username == Rails.configuration.x.local_domain || username == Rails.configuration.x.web_domain
Account.find_local!(username)
end end
def split_acct def split_acct
@ -76,6 +79,6 @@ class WebfingerResource
end end
def domain_matches_local? def domain_matches_local?
TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain) TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain) || Rails.configuration.x.alternate_domains.include?(local_domain)
end end
end end

View File

@ -11,133 +11,127 @@ RSpec.describe WebfingerResource do
Rails.configuration.x.web_domain = before_web Rails.configuration.x.web_domain = before_web
end end
describe '#username' do describe '#account' do
subject { described_class.new(resource).account }
describe 'with a URL value' do describe 'with a URL value' do
it 'raises with a route whose controller is not AccountsController' do context 'with a route whose controller is not AccountsController' do
resource = 'https://example.com/users/alice/other' let(:resource) { 'https://example.com/users/alice/other' }
expect do it 'raises an error' do
described_class.new(resource).username expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end.to raise_error(ActiveRecord::RecordNotFound) end
end end
it 'raises with a route whose action is not show' do context 'with a string that does not start with an URL' do
resource = 'https://example.com/users/alice' let(:resource) { 'website for http://example.com/users/alice.other' }
recognized = Rails.application.routes.recognize_path(resource) it 'raises an error' do
allow(recognized).to receive(:[]).with(:controller).and_return('accounts') expect { subject }.to raise_error(described_class::InvalidRequest)
allow(recognized).to receive(:[]).with(:username).and_return('alice') end
allow(recognized).to receive(:[]).with(:action).and_return('create')
allow(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized)
expect do
described_class.new(resource).username
end.to raise_error(ActiveRecord::RecordNotFound)
expect(recognized).to have_received(:[]).exactly(3).times
expect(Rails.application.routes).to have_received(:recognize_path)
.with(resource)
.at_least(:once)
end end
it 'raises with a string that doesnt start with URL' do context 'with a valid HTTPS route to an existing user' do
resource = 'website for http://example.com/users/alice/other' let(:account) { Fabricate(:account) }
let(:resource) { "https://example.com/users/#{account.username}" }
expect do it { is_expected.to eq(account) }
described_class.new(resource).username
end.to raise_error(described_class::InvalidRequest)
end end
it 'finds the username in a valid https route' do context 'with a valid HTTPS route to an existing user using the new API scheme' do
resource = 'https://example.com/users/alice' let(:account) { Fabricate(:account) }
let(:resource) { "https://example.com/ap/users/#{account.id}" }
result = described_class.new(resource).username it { is_expected.to eq(account) }
expect(result).to eq 'alice'
end end
it 'finds the username in a mixed case http route' do context 'with a valid HTTPS route to a non-existing user' do
resource = 'HTTp://exAMPLe.com/users/alice' let(:account) { Fabricate(:account) }
let(:resource) { 'https://example.com/users/alice' }
result = described_class.new(resource).username it 'raises an error' do
expect(result).to eq 'alice' expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end end
it 'finds the username in a valid http route' do context 'with a mixed case HTTP but valid route to an existing user' do
resource = 'http://example.com/users/alice' let(:account) { Fabricate(:account) }
let(:resource) { "HTTp://example.com/users/#{account.username}" }
result = described_class.new(resource).username it { is_expected.to eq(account) }
expect(result).to eq 'alice' end
context 'with a valid HTTP route to an existing user' do
let(:account) { Fabricate(:account) }
let(:resource) { "http://example.com/users/#{account.username}" }
it { is_expected.to eq(account) }
end end
end end
describe 'with a username and hostname value' do describe 'with a username and hostname value' do
it 'raises on a non-local domain' do context 'with a non-local domain' do
resource = 'user@remote-host.com' let(:account) { Fabricate(:account) }
let(:resource) { "#{account.username}@remote-host.com" }
expect do it 'raises an error' do
described_class.new(resource).username expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end.to raise_error(ActiveRecord::RecordNotFound) end
end end
it 'finds username for a local domain' do context 'with a valid handle for a local user with local domain' do
Rails.configuration.x.local_domain = 'example.com' let(:account) { Fabricate(:account) }
resource = 'alice@example.com' let(:resource) { "#{account.username}@example.com" }
result = described_class.new(resource).username before { Rails.configuration.x.local_domain = 'example.com' }
expect(result).to eq 'alice'
it { is_expected.to eq(account) }
end end
it 'finds username for a web domain' do context 'with a valid handle for a local user with web domain' do
Rails.configuration.x.web_domain = 'example.com' let(:account) { Fabricate(:account) }
resource = 'alice@example.com' let(:resource) { "#{account.username}@example.com" }
result = described_class.new(resource).username before { Rails.configuration.x.web_domain = 'example.com' }
expect(result).to eq 'alice'
it { is_expected.to eq(account) }
end end
end end
describe 'with an acct value' do describe 'with an acct value' do
it 'raises on a non-local domain' do context 'with a non-local domain' do
resource = 'acct:user@remote-host.com' let(:account) { Fabricate(:account) }
let(:resource) { "acct:#{account.username}@remote-host.com" }
expect do it 'raises an error' do
described_class.new(resource).username expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end.to raise_error(ActiveRecord::RecordNotFound) end
end end
it 'raises on a nonsense domain' do context 'with a valid handle for a local user with local domain' do
resource = 'acct:user@remote-host@remote-hostess.remote.local@remote' let(:account) { Fabricate(:account) }
let(:resource) { "acct:#{account.username}@example.com" }
expect do before { Rails.configuration.x.local_domain = 'example.com' }
described_class.new(resource).username
end.to raise_error(ActiveRecord::RecordNotFound) it { is_expected.to eq(account) }
end end
it 'finds the username for a local account if the domain is the local one' do context 'with a valid handle for a local user with web domain' do
Rails.configuration.x.local_domain = 'example.com' let(:account) { Fabricate(:account) }
resource = 'acct:alice@example.com' let(:resource) { "acct:#{account.username}@example.com" }
result = described_class.new(resource).username before { Rails.configuration.x.web_domain = 'example.com' }
expect(result).to eq 'alice'
end
it 'finds the username for a local account if the domain is the Web one' do it { is_expected.to eq(account) }
Rails.configuration.x.web_domain = 'example.com'
resource = 'acct:alice@example.com'
result = described_class.new(resource).username
expect(result).to eq 'alice'
end end
end end
describe 'with a nonsense resource' do describe 'with a nonsense resource' do
it 'raises InvalidRequest' do let(:resource) { 'df/:dfkj' }
resource = 'df/:dfkj'
expect do it 'raises an error' do
described_class.new(resource).username expect { subject }.to raise_error(described_class::InvalidRequest)
end.to raise_error(described_class::InvalidRequest)
end end
end end
end end