Skip to content

Commit

Permalink
Fix security context sometimes not being added in LD-Signed activities (
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored and atsu1125 committed Dec 2, 2024
1 parent a4ed734 commit 9ec8c05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 8 additions & 1 deletion app/lib/activitypub/linked_data_signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ActivityPub::LinkedDataSignature
include JsonLdHelper

CONTEXT = 'https://w3id.org/identity/v1'
SIGNATURE_CONTEXT = 'https://w3id.org/security/v1'

def initialize(json)
@json = json.with_indifferent_access
Expand Down Expand Up @@ -46,7 +47,13 @@ def sign!(creator, sign_with: nil)

signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), to_be_signed))

@json.merge('signature' => options.merge('signatureValue' => signature))
# Mastodon's context is either an array or a single URL
context_with_security = Array(@json['@context'])
context_with_security << 'https://w3id.org/security/v1'
context_with_security.uniq!
context_with_security = context_with_security.first if context_with_security.size == 1

@json.merge('signature' => options.merge('signatureValue' => signature), '@context' => context_with_security)
end

private
Expand Down
9 changes: 2 additions & 7 deletions spec/lib/activitypub/linked_data_signature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,11 @@
describe '#sign!' do
subject { described_class.new(raw_json).sign!(sender) }

it 'returns a hash' do
it 'returns a hash with a signature, the expected context, and the signature can be verified', :aggregate_failures do
expect(subject).to be_a Hash
end

it 'contains signature' do
expect(subject['signature']).to be_a Hash
expect(subject['signature']['signatureValue']).to be_present
end

it 'can be verified again' do
expect(Array(subject['@context'])).to include('https://w3id.org/security/v1')
expect(described_class.new(subject).verify_account!).to eq sender
end
end
Expand Down

0 comments on commit 9ec8c05

Please sign in to comment.