From 7b86e122e156bd47246d85d8d03d13567597ccc9 Mon Sep 17 00:00:00 2001 From: manafire Date: Sun, 20 Dec 2015 19:43:43 -0500 Subject: [PATCH 1/2] Add domain specification --- lib/better_errors/middleware.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/better_errors/middleware.rb b/lib/better_errors/middleware.rb index 5362f3cc..87ad0f23 100644 --- a/lib/better_errors/middleware.rb +++ b/lib/better_errors/middleware.rb @@ -29,6 +29,7 @@ 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. @@ -36,6 +37,11 @@ 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 @@ -53,7 +59,7 @@ def initialize(app, handler = ErrorPage) # @param [Hash] env # @return [Array] def call(env) - if allow_ip? env + if allow_ip?(env) || allow_domain?(env) better_errors_call env else @app.call env @@ -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/(?.+?)/(?\w+)\z} From bff72c44c3e040b1b016cccdfe128896835d88b0 Mon Sep 17 00:00:00 2001 From: manafire Date: Sat, 2 Jan 2016 08:55:06 -0500 Subject: [PATCH 2/2] swap order of domain and ip checks to fix socket issue (https://github.com/charliesome/better_errors/pull/328#issuecomment-167981977). --- lib/better_errors/middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/better_errors/middleware.rb b/lib/better_errors/middleware.rb index 87ad0f23..7589e4e9 100644 --- a/lib/better_errors/middleware.rb +++ b/lib/better_errors/middleware.rb @@ -59,7 +59,7 @@ def initialize(app, handler = ErrorPage) # @param [Hash] env # @return [Array] def call(env) - if allow_ip?(env) || allow_domain?(env) + if allow_domain?(env) || allow_ip?(env) better_errors_call env else @app.call env