From 770042e43d9f000255d3001da6b52fd9798b7920 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 14 Jul 2026 16:33:51 +0200 Subject: [PATCH] Add support for outgoing HTTP Message Signatures (#39756) --- app/lib/request.rb | 27 +++++++++++++++++++++++++++ app/lib/signed_request.rb | 7 +------ app/models/keypair.rb | 22 ++++++++++++++++++++++ spec/lib/request_spec.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/app/lib/request.rb b/app/lib/request.rb index ee4c486dfc..aa482c7d53 100644 --- a/app/lib/request.rb +++ b/app/lib/request.rb @@ -131,7 +131,16 @@ class Request def perform begin + # Try with HTTP Signatures (draft-cavage-http-signatures-12) first response = perform_cavage_signed_request + + # Then if it fails, double-knock using RFC 9421: HTTP Message Signatures + if @signing_keypair.present? && [400, 401].include?(response.code) + # Consume the body of the first response first + response.truncated_body if http_client.persistent? && !response.connection.finished_request? + + response = perform_rfc9421_signed_request + end rescue => e raise e.class, "#{e.message} on #{@url}", e.backtrace[0] end @@ -180,6 +189,24 @@ class Request client.request(@verb, @url.to_s, @options.merge(headers:)) end + def perform_rfc9421_signed_request + headers = @headers + headers = headers.merge('content-digest' => "sha-256=:#{OpenSSL::Digest.base64digest('sha256', @options[:body])}:") if @options[:body] + + client = http_client + + if @signing_keypair.present? + client = client.use( + http_signature: { + key: @signing_keypair.linzer_private_key, + covered_components: @options.key?(:body) ? %w(@method @target-uri content-digest) : %w(@method @target-uri), + } + ) + end + + client.request(@verb, @url.to_s, @options.merge(headers:)) + end + # Using code from https://github.com/sporkmonger/addressable/blob/3450895887d0a1770660d8831d1b6fcfed9bd9d6/lib/addressable/uri.rb#L1609-L1635 # to preserve some URL Encodings while normalizing def normalize_preserving_url_encodings(url, preserved_chars = SAFE_PRESERVED_CHARS, *flags) diff --git a/app/lib/signed_request.rb b/app/lib/signed_request.rb index 5b6bc7eb70..dedb26d006 100644 --- a/app/lib/signed_request.rb +++ b/app/lib/signed_request.rb @@ -171,12 +171,7 @@ class SignedRequest end def verified?(keypair) - case keypair.type - when 'rsa' - key = Linzer.new_rsa_v1_5_sha256_public_key(keypair.public_key) - when 'ed25519' - key = Linzer.new_ed25519_public_key(keypair.public_key) - end + key = keypair.linzer_public_key Linzer.verify(key, @message, @signature) rescue Linzer::VerifyError diff --git a/app/models/keypair.rb b/app/models/keypair.rb index f3ab49637a..48dbc636b5 100644 --- a/app/models/keypair.rb +++ b/app/models/keypair.rb @@ -64,6 +64,28 @@ class Keypair < ApplicationRecord end end + def linzer_public_key + @linzer_public_key ||= begin + case type + when 'rsa' + Linzer.new_rsa_v1_5_sha256_public_key(public_key) + when 'ed25519' + Linzer.new_ed25519_public_key(public_key) + end + end + end + + def linzer_private_key + @linzer_private_key ||= begin + case type + when 'rsa' + Linzer.new_rsa_v1_5_sha256_key(private_key, full_uri) + when 'ed25519' + Linzer.new_ed25519_key(private_key, full_uri) + end + end + end + def usable? !revoked? && !expired? end diff --git a/spec/lib/request_spec.rb b/spec/lib/request_spec.rb index f8c2de203a..b937cf1433 100644 --- a/spec/lib/request_spec.rb +++ b/spec/lib/request_spec.rb @@ -98,6 +98,34 @@ RSpec.describe Request do end end + context 'with server that only supports RFC 9421' do + let(:account) { Fabricate(:account) } + + before do + # cavage-12 + stub_request(:get, 'http://example.com') + .with { |request| request.headers.key?('Signature') && !request.headers.key?('Signature-Input') } + .to_return(status: 401) + + # RFC 9421 + stub_request(:get, 'http://example.com') + .with { |request| request.headers.key?('Signature') && request.headers.key?('Signature-Input') } + .to_return(status: 200) + end + + it 'makes two valid requests with the specific signatures' do + expect { |block| subject.on_behalf_of(account).perform(&block) }.to yield_control + + # Makes a valid request using cavage-12 + expect(a_request(:get, 'http://example.com').with { |request| verified_signed_mocked_request?(request, account.keypair) && !request.headers.key?('Signature-Input') }) + .to have_been_made.once + + # Makes a valid request using RFC 9421 + expect(a_request(:get, 'http://example.com').with { |request| verified_signed_mocked_request?(request, account.keypair) && request.headers.key?('Signature-Input') }) + .to have_been_made.once + end + end + context 'with a redirect and HTTP signatures' do let(:account) { Fabricate(:account) }