From 279ebe6512f0a03f5951c705b009171a8aee67c5 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Sat, 25 Feb 2023 12:23:57 -0800 Subject: [PATCH 1/2] Rescue RuntimeError when urandom is not available. --- lib/sinatra/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 9a2cd954c9..d6b7beac7a 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1848,8 +1848,8 @@ def force_encoding(*args) begin require 'securerandom' set :session_secret, SecureRandom.hex(64) - rescue LoadError, NotImplementedError - # SecureRandom raises a NotImplementedError if no random device is available + rescue LoadError, NotImplementedError, RuntimeError + # SecureRandom raises a NotImplementedError if no random device is available, or RuntimeError if urandom is not available set :session_secret, format('%064x', Kernel.rand((2**256) - 1)) end From ff4d68e62ce1f9b415481b93627759185509bb9e Mon Sep 17 00:00:00 2001 From: Patrik Ragnarsson Date: Sun, 26 Feb 2023 21:47:57 +0100 Subject: [PATCH 2/2] Update comment about SecureRandom --- lib/sinatra/base.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index d6b7beac7a..1d37910df2 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1849,7 +1849,8 @@ def force_encoding(*args) require 'securerandom' set :session_secret, SecureRandom.hex(64) rescue LoadError, NotImplementedError, RuntimeError - # SecureRandom raises a NotImplementedError if no random device is available, or RuntimeError if urandom is not available + # SecureRandom raises a NotImplementedError if no random device is available + # RuntimeError raised due to broken openssl backend: https://bugs.ruby-lang.org/issues/19230 set :session_secret, format('%064x', Kernel.rand((2**256) - 1)) end