Skip to content

Commit

Permalink
Change Events#ssl_error signature from (error, peeraddr, peercert) to…
Browse files Browse the repository at this point in the history
… (error, ssl_socket)

The method signature should include the socket, so it determines what socket info is logged

Co-authored-by: ocowchun <ocowchun@gmail.com>
  • Loading branch information
MSP-Greg and ocowchun committed Sep 22, 2020
1 parent a734909 commit bed5b24
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
3 changes: 3 additions & 0 deletions History.md
Expand Up @@ -6,6 +6,9 @@
* Bugfixes
* Your bugfix goes here (#Github Number)

* Refactor
* Change Events#ssl_error signature from (error, peeraddr, peercert) to (error, ssl_socket) (#2375)

## 5.0.0

* Features
Expand Down
8 changes: 5 additions & 3 deletions lib/puma/events.rb
Expand Up @@ -106,10 +106,12 @@ def parse_error(error, req)
end

# An SSL error has occurred.
# +error+ an exception object, +peeraddr+ peer address,
# and +peercert+ any peer certificate (if present).
# @param error <Puma::MiniSSL::SSLError>
# @param ssl_socket <Puma::MiniSSL::Socket>
#
def ssl_error(error, peeraddr, peercert)
def ssl_error(error, ssl_socket)
peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
peercert = ssl_socket.peercert
subject = peercert ? peercert.subject : nil
@error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
end
Expand Down
15 changes: 2 additions & 13 deletions lib/puma/reactor.rb
Expand Up @@ -237,23 +237,12 @@ def run_internal

# SSL handshake failure
rescue MiniSSL::SSLError => e
@server.lowlevel_error(e, c.env)

ssl_socket = c.io
begin
addr = ssl_socket.peeraddr.last
# EINVAL can happen when browser closes socket w/security exception
rescue IOError, Errno::EINVAL
addr = "<unknown>"
end

cert = ssl_socket.peercert
@server.lowlevel_error e, c.env
@events.ssl_error e, c.io

c.close
clear_monitor mon

@events.ssl_error e, addr, cert

# The client doesn't know HTTP well
rescue HttpParserError => e
@server.lowlevel_error(e, c.env)
Expand Down
16 changes: 3 additions & 13 deletions lib/puma/server.rb
Expand Up @@ -220,13 +220,9 @@ def run(background=true)
process_now = true
end
rescue MiniSSL::SSLError => e
ssl_socket = client.io
addr = ssl_socket.peeraddr.last
cert = ssl_socket.peercert

@events.ssl_error e, client.io
client.close

@events.ssl_error e, addr, cert
rescue HttpParserError => e
client.write_error(400)
client.close
Expand Down Expand Up @@ -423,16 +419,10 @@ def process_client(client, buffer)

# SSL handshake error
rescue MiniSSL::SSLError => e
lowlevel_error(e, client.env)

ssl_socket = client.io
addr = ssl_socket.peeraddr.last
cert = ssl_socket.peercert

lowlevel_error e, client.env
@events.ssl_error e, client.io
close_socket = true

@events.ssl_error e, addr, cert

# The client doesn't know HTTP well
rescue HttpParserError => e
lowlevel_error(e, client.env)
Expand Down
6 changes: 3 additions & 3 deletions test/test_puma_server_ssl.rb
Expand Up @@ -12,10 +12,10 @@
class SSLEventsHelper < ::Puma::Events
attr_accessor :addr, :cert, :error

def ssl_error(error, peeraddr, peercert)
def ssl_error(error, ssl_socket)
self.error = error
self.addr = peeraddr
self.cert = peercert
self.addr = ssl_socket.peeraddr.last rescue "<unknown>"
self.cert = ssl_socket.peercert
end
end

Expand Down

0 comments on commit bed5b24

Please sign in to comment.