forked from gsamokovarov/web-console
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from gsamokovarov/whitelisted-ips
Always whitelist localhost and inform users why no console is displayed
- Loading branch information
Showing
13 changed files
with
235 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module WebConsole | ||
# Noisy wrapper around +Request+. | ||
# | ||
# If any calls to +from_whitelisted_ip?+ and +acceptable_content_type?+ | ||
# return false, an info log message will be displayed in users' logs. | ||
class WhinyRequest < SimpleDelegator | ||
def from_whitelited_ip? | ||
whine_unless request.from_whitelited_ip? do | ||
"Cannot render console from #{request.remote_ip}! " \ | ||
"Allowed networks: #{request.whitelisted_ips}" | ||
end | ||
end | ||
|
||
def acceptable_content_type? | ||
whine_unless request.acceptable_content_type? do | ||
"Cannot render console with content type #{request.content_type}" \ | ||
"Allowed content types: #{request.acceptable_content_types}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def whine_unless(condition) | ||
unless condition | ||
logger.info { yield } | ||
end | ||
condition | ||
end | ||
|
||
def logger | ||
env['action_dispatch.logger'] || WebConsole.logger | ||
end | ||
|
||
def request | ||
__getobj__ | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module WebConsole | ||
# Whitelist of allowed networks that can access Web Console. | ||
# | ||
# Networks are represented by standard IPAddr and can be either IPv4 or IPv6 | ||
# networks. | ||
class Whitelist | ||
# IPv4 and IPv6 localhost should be always whitelisted. | ||
ALWAYS_WHITELISTED_NETWORKS = %w( 127.0.0.0/8 ::1 ) | ||
|
||
def initialize(networks = nil) | ||
@networks = normalize_networks(networks).map(&method(:coerce_network_to_ipaddr)).uniq | ||
end | ||
|
||
def include?(network) | ||
@networks.any? { |whitelist| whitelist.include?(network.to_s) } | ||
end | ||
|
||
def to_s | ||
@networks.map(&method(:human_readable_ipaddr)).join(', ') | ||
end | ||
|
||
private | ||
|
||
def normalize_networks(networks) | ||
Array(networks).concat(ALWAYS_WHITELISTED_NETWORKS) | ||
end | ||
|
||
def coerce_network_to_ipaddr(network) | ||
if network.is_a?(IPAddr) | ||
network | ||
else | ||
IPAddr.new(network) | ||
end | ||
end | ||
|
||
def human_readable_ipaddr(ipaddr) | ||
ipaddr.to_range.to_s.split('..').uniq.join('/') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.