IRB History and colors with Wirble

For a nicer irb experience there is wirble, a gem that adds history and colorization, the only thing missing so far was actual support and bugfixes, which i finally found on blackwinters branch, now history scrolling is fun again 🙂

A small excerpt from my dotfiles irbrc

def require_without_bundler(gem, file)
  return require(file) unless defined?(::Bundler)
  if gem_path = Dir.glob("{#{Gem.path.join(',')}}/gems/#{gem}*").first
    $LOAD_PATH << "#{gem_path}/lib"
    require file
  else
    raise LoadError, "Gem #{gem} not found via require_without_bundler"
  end
end

begin
  # nice history and color
  require_without_bundler 'blackwinter-wirble', 'wirble' # blackwinter branch has history_uniq fixes
  Wirble::History::DEFAULTS[:history_uniq] = 'reverse'

  Wirble.init
  Wirble.colorize
rescue LoadError
  # No wirble use defaults to get at least history
  require 'irb/ext/save-history'
  IRB.conf[:USE_READLINE] = true
  IRB.conf[:SAVE_HISTORY] = 10000
  IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
end

Empty session id leads to shared session + Fix

We recently encountered some users that had an empty sessionid and therefore where logged in as other users, to fix this (the reason why the ids where empty is still unknown….) we now forbid blank session ids.

# users with blank session id get logged in as other users / share session 
#-> forbid empty session ids
# TEST: set _session_id cookie to "" it should be removed/replaced
class ActionController::Session::AbstractStore
  def load_session_with_blank_id_protection(*args)
    id, data = load_session_without_blank_id_protection(*args)
    return [nil, {}] if id.blank?
    [id, data]
  end
  alias_method_chain :load_session, :blank_id_protection
end

Reliable timeout for Ruby 1.8.x with fallback

We want reliable timeouts, but dont want to hard-code SystemTimer everywhere, so atm we are using this:

# lib/safe_timeout.rb
# stolen from klarlack -- http://github.com/schoefmax/klarlack
# to get an reliable timeout that wont fail on other platforms
# or if sytem_timer is missing
SafeTimeout = begin
  # Try to use the SystemTimer gem instead of Ruby's timeout library
  # when running on something that looks like Ruby 1.8.x. See:
  # http://ph7spot.com/articles/system_timer
  # We don't want to bother trying to load SystemTimer on jruby and
  # ruby 1.9+.
  if RUBY_VERSION =~ /^1\.8\./ and RUBY_PLATFORM !~ /java/
    require 'system_timer'
    SystemTimer
  else
    require 'timeout'
    Timeout
  end
rescue LoadError => e
  $stderr.puts "Could not load SystemTimer gem, falling back to Ruby's slower/unsafe timeout library: #{e.message}"
  require 'timeout'
  Timeout
end