Add support for outgoing HTTP Message Signatures (#39756)

This commit is contained in:
Claire authored and GitHub committed 2026-07-14 14:33:51 +00:00
1 parent dde6c28e97
commit 770042e43d
4 files changed
+78 -6

No files matched your search

+27
View File
@@ -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)
+1 -6
View File
@@ -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
+22
View File
@@ -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
+28
View File
@@ -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) }