From 5e1d0f339bfb6f74e71a84c1d7a33f6818187c0a Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 8 Jul 2026 20:07:45 +0200 Subject: [PATCH] Refactor Multibase error handling and clean up ObjectIntegrityProof (#39754) --- app/lib/activitypub/object_integrity_proof.rb | 8 ++------ app/lib/multibase.rb | 16 ++++++++++------ .../activitypub/process_account_service.rb | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/lib/activitypub/object_integrity_proof.rb b/app/lib/activitypub/object_integrity_proof.rb index ffdea61b7db..e6d84c2b937 100644 --- a/app/lib/activitypub/object_integrity_proof.rb +++ b/app/lib/activitypub/object_integrity_proof.rb @@ -4,9 +4,6 @@ class ActivityPub::ObjectIntegrityProof include JsonLdHelper - CONTEXT = 'https://w3id.org/identity/v1' - SIGNATURE_CONTEXT = 'https://w3id.org/security/v1' - def initialize(json) @json = json end @@ -15,9 +12,8 @@ class ActivityPub::ObjectIntegrityProof return unless @json.is_a?(Hash) && @json['proof'].is_a?(Hash) proof = @json['proof'] - return unless proof['type'].present? && proof['verificationMethod'].present? && proof['proofPurpose'].present? + return unless proof['type'] == 'DataIntegrityProof' && proof['verificationMethod'].present? && proof['proofPurpose'] == proof_purpose - return if proof_purpose != proof['proofPurpose'] || proof['type'] != 'DataIntegrityProof' return if proof['expires']&.to_datetime&.past? cryptosuite = proof['cryptosuite'] @@ -30,7 +26,7 @@ class ActivityPub::ObjectIntegrityProof return if keypair.nil? || !keypair.usable? || keypair.type != 'ed25519' keypair.actor if ActivityPub::ObjectIntegrityProof.verify_eddsa_jcs_2022(@json, keypair.keypair) - rescue OpenSSL::PKey::RSAError + rescue Multibase::Error, OpenSSL::PKey::PKeyError false end diff --git a/app/lib/multibase.rb b/app/lib/multibase.rb index f1c08eccb52..d8f54bd949d 100644 --- a/app/lib/multibase.rb +++ b/app/lib/multibase.rb @@ -22,8 +22,10 @@ class Multibase ED25519_PUB_DER_HEADER = ['302a300506032b6570032100'].pack('H*').freeze ML_DSA_44_PUB_DER_HEADER = ['30820532300b06096086480165030403110382052100'].pack('H*').freeze + class Error < StandardError; end + def self.decode(string) - raise ArgumentError if string.nil? + raise Error, 'Multibase string is null' if string.nil? case string[0] when 'u' @@ -31,7 +33,7 @@ class Multibase when 'z' Base58.base58_to_binary(string[1...], :bitcoin) else - raise ArgumentError + raise Error, 'Invalid Multibase base' end end @@ -42,7 +44,7 @@ class Multibase return [tag, binary[prefix.length..]] if binary.starts_with?(prefix) end - raise ArgumentError + raise Error, 'Unknown Multicodec tag' end def self.decode_key_to_pem(string) @@ -52,17 +54,19 @@ class Multibase when :'rsa-pub' [:rsa, OpenSSL::PKey::RSA.new(binary).to_pem] when :'ed25519-pub' - raise ArgumentError unless binary.size == 32 + raise Error, 'Invalid data size for ed25519-pub' unless binary.size == 32 der = ED25519_PUB_DER_HEADER + binary [:ed25519, "-----BEGIN PUBLIC KEY-----\n#{Base64.strict_encode64(der)}\n-----END PUBLIC KEY-----\n"] when :'mldsa-44-pub' - raise ArgumentError unless binary.size == 1312 + raise Error, 'Invalid data size for mldsa-44-pub' unless binary.size == 1312 der = ML_DSA_44_PUB_DER_HEADER + binary [:'ml-dsa-44', "-----BEGIN PUBLIC KEY-----\n#{Base64.strict_encode64(der).scan(/.{,64}/).join("\n")}-----END PUBLIC KEY-----\n"] else - raise ArgumentError + raise Error, 'Unsupported key type' end + rescue OpenSSL::PKey::RSAError => e + raise Error, e end end diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index df8bb4e3e57..db18bdfc091 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -335,7 +335,7 @@ class ActivityPub::ProcessAccountService < BaseService def key_from_multikey(value) Multibase.decode_key_to_pem(value) - rescue ArgumentError + rescue Multibase::Error nil end