Crystal: HTTP::Server crash with self-signed certificate

Created on 20 Aug 2018  路  3Comments  路  Source: crystal-lang/crystal

It is possible in the latest version to crash without rescuable warnings an HTTP::Server that uses openssl

Example code:

require "openssl"
require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world!"
end

context = OpenSSL::SSL::Context::Server.new
context.certificate_chain = "cert.pem"
context.private_key = "key.pem"
server.bind_ssl "127.0.0.1", 443, context

server.listen

Using a curl https://127.0.0.1 will crash the application.

When using a self-signed certificate, or when the connecting client will generate an OpenSSL "SSL certificate problem: unable to get local issuer certificate" the ssl server process will exit.

This is problematic as basically every exposed crystal server that uses SSL can be crashed using a simple ssl request that has an empty cert store.

Most helpful comment

Closing the connection with some minimum http response like 495 sounds good enough to me in this case.

The whole listen could be guarded to avoid crashing the server. Logging of that can be deferd for now IMO.

All 3 comments

The handling of an HTTP request by a handler is guarded against crashing the entire application. All exceptions are rescued and generate an appropriate HTTP request.

But there is no proper error handling while establishing a client connection in HTTP::Server.listen and setting up the HTTP protocol processing in HTTP::Server::RequestProcessor.process.

The question is, how should the server respond to such an error? Just printing it to STDOUT could be a quick fix but it's not a good solution. Having some sort of error handling with structured concurrency like proposed in #6468 would be great for this.

Closing the connection with some minimum http response like 495 sounds good enough to me in this case.

The whole listen could be guarded to avoid crashing the server. Logging of that can be deferd for now IMO.

definitely having multiple layers of protection is good for security, guarding the whole listen is a very good idea

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArthurZ picture ArthurZ  路  3Comments

oprypin picture oprypin  路  3Comments

grosser picture grosser  路  3Comments

lgphp picture lgphp  路  3Comments

pbrusco picture pbrusco  路  3Comments