Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add domain specification #328

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/better_errors/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ class Middleware
#
# Set to `{ "127.0.0.1/8", "::1/128" }` by default.
ALLOWED_IPS = Set.new
ALLOWED_DOMAINS = Set.new

# Adds an address to the set of IP addresses allowed to access Better
# Errors.
def self.allow_ip!(addr)
ALLOWED_IPS << IPAddr.new(addr)
end

def self.allow_domain!(addr)
ALLOWED_DOMAINS << addr
end


allow_ip! "127.0.0.0/8"
allow_ip! "::1/128" rescue nil # windows ruby doesn't have ipv6 support

Expand All @@ -53,7 +59,7 @@ def initialize(app, handler = ErrorPage)
# @param [Hash] env
# @return [Array]
def call(env)
if allow_ip? env
if allow_domain?(env) || allow_ip?(env)
better_errors_call env
else
@app.call env
Expand All @@ -69,6 +75,11 @@ def allow_ip?(env)
ALLOWED_IPS.any? { |subnet| subnet.include? ip }
end

def allow_domain?(env)
request = Rack::Request.new(env)
ALLOWED_DOMAINS.include? request.host
end

def better_errors_call(env)
case env["PATH_INFO"]
when %r{/__better_errors/(?<id>.+?)/(?<method>\w+)\z}
Expand Down