forked from brynary/rack-bug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug.rb
88 lines (65 loc) · 1.93 KB
/
bug.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require "ipaddr"
require "digest"
require "rack"
require "digest/sha1"
require "rack/bug/autoloading"
class Rack::Bug
include Options
VERSION = "0.3.0"
class SecurityError < StandardError
end
def self.enable
Thread.current["rack-bug.enabled"] = true
end
def self.disable
Thread.current["rack-bug.enabled"] = false
end
def self.enabled?
Thread.current["rack-bug.enabled"] == true
end
def initialize(app, options = {}, &block)
@app = asset_server(app)
initialize_options options
instance_eval(&block) if block_given?
@toolbar = Toolbar.new(RedirectInterceptor.new(@app))
end
def call(env)
env.replace @default_options.merge(env)
@env = env
@original_request = Rack::Request.new(@env)
if ((toolbar_requested? && toolbar_xhr?) || railsbug_enabled?) && ip_authorized? && password_authorized?
@toolbar.railsbug_enabled = railsbug_enabled?
@toolbar.call(env)
else
@app.call(env)
end
end
private
def toolbar_xhr?
!@original_request.xhr? || @original_request.path =~ /^\/__rack_bug__/
end
def asset_server(app)
RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__rack_bug__"], :root => public_path))
end
def public_path
::File.expand_path(::File.dirname(__FILE__) + "/bug/public")
end
def toolbar_requested?
@original_request.cookies["rack_bug_enabled"]
end
def railsbug_enabled?
!!options['HTTP_X_RAILSBUG_ENABLED']
end
def ip_authorized?
return true unless options["rack-bug.ip_masks"]
options["rack-bug.ip_masks"].any? do |ip_mask|
ip_mask.include?(IPAddr.new(@original_request.ip))
end
end
def password_authorized?
return true unless options["rack-bug.password"]
expected_sha = Digest::SHA1.hexdigest ["rack_bug", options["rack-bug.password"]].join(":")
actual_sha = @original_request.cookies["rack_bug_password"]
actual_sha == expected_sha
end
end