1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/rack-protection/lib/rack/protection/session_hijacking.rb
2011-06-19 14:54:29 +02:00

35 lines
1.1 KiB
Ruby

require 'rack/protection'
module Rack
module Protection
##
# Prevented attack:: Session Hijacking
# Supported browsers:: all
# More infos:: http://en.wikipedia.org/wiki/Session_hijacking
#
# Tracks request properties like the user agent in the session and empties
# the session if those properties change. This essentially prevents attacks
# from Firesheep. Since all headers taken into consideration might be
# spoofed, too, this will not prevent all hijacking attempts.
class SessionHijacking < Base
default_options :tracking_key => :tracking, :encrypt_tracking => true,
:track => %w[HTTP_USER_AGENT HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE
HTTP_VERSION]
def accepts?(env)
session = session env
key = options[:tracking_key]
if session.include? key
session[key].all? { |k,v| env[k] == encrypt(v) }
else
session[key] = {}
options[:track].each { |k| session[k] = encrypt(env[k]) }
end
end
def encrypt(value)
options[:encrypt_tracking] ? super(value) : value.to_s
end
end
end
end