mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
move stuff around, add remote_token protection
This commit is contained in:
parent
ab177702bb
commit
3588ba5d33
6 changed files with 22 additions and 2 deletions
|
@ -22,7 +22,6 @@ module Rack
|
|||
use EscapedParams, options unless except.include? :escaped_params
|
||||
use FrameOptions, options unless except.include? :frame_options
|
||||
use PathTraversal, options unless except.include? :path_traversal
|
||||
use RemoteReferrer, options unless except.include? :remote_referrer
|
||||
use RemoteToken, options unless except.include? :remote_token
|
||||
use SessionHijacking, options unless except.include? :session_hijacking
|
||||
use XSSHeader, options unless except.include? :xss_header
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'rack/protection'
|
||||
require 'logger'
|
||||
require 'uri'
|
||||
|
||||
module Rack
|
||||
module Protection
|
||||
|
@ -58,6 +59,11 @@ module Rack
|
|||
env['rack.session'] ||= {}
|
||||
end
|
||||
|
||||
def referrer(env)
|
||||
ref = env['HTTP_REFERER']
|
||||
URI.parse(ref).host || Request.new(env).host if ref and not ref.empty?
|
||||
end
|
||||
|
||||
def random_string(secure = defined? SecureRandom)
|
||||
secure ? SecureRandom.hex(32) : "%032x" % rand(2**128-1)
|
||||
rescue NotImpelentedError
|
||||
|
|
|
@ -18,7 +18,7 @@ module Rack
|
|||
default_reaction :deny
|
||||
|
||||
def accepts?(env)
|
||||
safe?(env) or (env['HTTP_REFERER'] and not env['HTTP_REFERER'].empty?)
|
||||
safe?(env) or referrer(env)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,6 +15,11 @@ module Rack
|
|||
#
|
||||
# Not Yet Implemented!
|
||||
class RemoteReferrer < Base
|
||||
default_reaction :deny
|
||||
|
||||
def accepts?(env)
|
||||
safe?(env) or referrer(env) == Request.new(env).host
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,6 +14,11 @@ module Rack
|
|||
#
|
||||
# Not Yet Implemented!
|
||||
class RemoteToken < AuthenticityToken
|
||||
default_reaction :deny
|
||||
|
||||
def accepts?(env)
|
||||
super or referrer(env) == Request.new(env).host
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
5
rack-protection/spec/protection_spec.rb
Normal file
5
rack-protection/spec/protection_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../spec_helper.rb', __FILE__)
|
||||
|
||||
describe Rack::Protection do
|
||||
it_behaves_like "any rack application"
|
||||
end
|
Loading…
Add table
Reference in a new issue