Skip to content

Commit

Permalink
Add HTTP signature keyId to request log (mastodon#11591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and hiyuki2578 committed Oct 2, 2019
1 parent 8076457 commit 863128b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/controllers/concerns/signature_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def signature_verification_failure_code
@signature_verification_failure_code || 401
end

def signature_key_id
raw_signature = request.headers['Signature']
signature_params = {}

raw_signature.split(',').each do |part|
parsed_parts = part.match(/([a-z]+)="([^"]+)"/i)
next if parsed_parts.nil? || parsed_parts.size != 3
signature_params[parsed_parts[1]] = parsed_parts[2]
end

signature_params['keyId']
end

def signed_request_account
return @signed_request_account if defined?(@signed_request_account)

Expand Down Expand Up @@ -154,7 +167,7 @@ def stoplight_wrap_request(&block)
.with_fallback { nil }
.with_threshold(1)
.with_cool_off_time(5.minutes.seconds)
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) ? handle.call(error) : raise(error) }
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
.run
end

Expand Down
6 changes: 6 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
# Better log formatting
config.lograge.enabled = true

config.lograge.custom_payload do |controller|
if controller.respond_to?(:signed_request?) && controller.signed_request?
{ key: controller.signature_key_id }
end
end

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

Expand Down

0 comments on commit 863128b

Please sign in to comment.