Hardening

This commit is contained in:
Claire committed 2026-10-29 14:56:19 +01:00
1 parent 4123b61923
commit 125b3b6f0a
5 files changed
+18 -5

No files matched your search

+8
View File
@@ -19,6 +19,7 @@
class Keypair < ApplicationRecord
include Expireable
include JsonLdHelper
self.inheritance_column = nil
@@ -34,6 +35,7 @@ class Keypair < ApplicationRecord
validates :uri, presence: true, uniqueness: true, if: -> { account.remote? }
validates :uri, absence: true, if: -> { account.local? }
validate :validate_uri_origin, if: -> { account.remote? }
validates :local_fragment, presence: true, uniqueness: { scope: :account_id }, format: { with: /\A#[A-Z0-9-]+\Z/i }, if: -> { account.local? }
validates :local_fragment, absence: true, if: -> { account.remote? }
@@ -130,4 +132,10 @@ class Keypair < ApplicationRecord
)
end
end
private
def validate_uri_origin
errors.add(:uri, :cross_origin) if non_matching_uri_hosts?(uri, account.uri)
end
end
@@ -45,6 +45,8 @@ class ActivityPub::FetchRemoteKeyService < BaseService
end
def keypair_from_actor_json(actor_uri, actor_json)
return if non_matching_uri_hosts?(actor_uri, @uri)
actor = find_actor(actor_uri, actor_json)
return if actor.nil?
@@ -397,6 +397,7 @@ class ActivityPub::ProcessAccountService < BaseService
end
key_id = value
next if non_matching_uri_hosts?(@account.uri, key_id)
# Key is fetched without ID validation because of a GoToSocial bug
value = fetch_resource_without_id_validation(key_id)
@@ -405,7 +406,7 @@ class ActivityPub::ProcessAccountService < BaseService
# Special handling for GoToSocial which returns the whole actor for the key ID
value = first_of_value(value['publicKey']) if value.is_a?(Hash) && value.key?('publicKey')
next unless value['owner'] == @account.uri
next unless value.present? && value['owner'] == @account.uri
key = value['publicKeyPem']
next if key.nil?
@@ -431,10 +432,11 @@ class ActivityPub::ProcessAccountService < BaseService
end
key_id = value
next if non_matching_uri_hosts?(@account.uri, key_id)
value = fetch_resource(key_id)
next unless value['type'] == 'Multikey' && value['controller'] == @account.uri
next unless value.present? && value['type'] == 'Multikey' && value['controller'] == @account.uri
key_type, key = key_from_multikey(value['publicKeyMultibase'])
next if key_type.nil? || key.nil?
+3 -2
View File
@@ -17,8 +17,9 @@ Fabricator(:account) do
indexable true
# This is not strictly needed but this avoids generating multiple keys
# and, when `store_private_key` is passed, stores private keys for use in request specs
keypairs do |attrs|
uri = attrs[:uri] ? "#{attrs[:uri]}#main-key" : "https://#{attrs[:domain]}/users/#{attrs[:username]}#main-key" if attrs[:domain].present?
if attrs[:legacy_keypair] || attrs[:public_key].present?
[]
else
@@ -27,7 +28,7 @@ Fabricator(:account) do
type: :rsa,
public_key: SigningKeysHelpers::PUBLIC_RSA_TEST_KEY,
private_key: attrs[:domain].blank? ? SigningKeysHelpers::PRIVATE_RSA_TEST_KEY : nil,
uri: attrs[:domain].nil? ? nil : "https://#{attrs[:domain]}/users/#{attrs[:username]}#main-key",
uri: uri,
local_fragment: attrs[:domain].nil? ? '#main-key' : nil
),
]
+1 -1
View File
@@ -704,7 +704,7 @@ RSpec.describe 'signature verification concern' do
end
context 'with a known account' do
let!(:actor) { Fabricate(:account, domain: 'remote.domain', uri: 'https://remote.domain/users/bob') }
let!(:actor) { Fabricate(:account, domain: 'remote.domain', uri: 'https://remote.domain/users/bob', keypairs: []) }
before do
Fabricate(:keypair, account: actor, type: :ed25519, public_key: actor_keypair.public_to_pem, uri: 'https://remote.domain/users/bob#main-key')