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

37 lines
1.2 KiB
Ruby
Raw Normal View History

2011-05-23 04:07:54 -04:00
require 'rack/protection'
module Rack
module Protection
2011-05-24 07:23:57 -04:00
##
# 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
2011-05-24 08:32:34 -04:00
# 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.
2011-05-23 04:07:54 -04:00
class SessionHijacking < Base
default_reaction :drop_session
2011-05-29 07:01:47 -04:00
default_options :tracking_key => :tracking, :encrypt_tracking => true,
2011-10-04 16:36:37 -04:00
:track => %w[HTTP_USER_AGENT HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE]
2011-05-29 07:01:47 -04:00
def accepts?(env)
session = session env
key = options[:tracking_key]
if session.include? key
session[key].all? { |k,v| v == encrypt(env[k]) }
2011-05-29 07:01:47 -04:00
else
session[key] = {}
options[:track].each { |k| session[key][k] = encrypt(env[k]) }
2011-05-29 07:01:47 -04:00
end
end
def encrypt(value)
value = value.to_s.downcase
options[:encrypt_tracking] ? super(value) : value
2011-05-29 07:01:47 -04:00
end
2011-05-23 04:07:54 -04:00
end
end
end