Add restricted access check to account's watchlist

pull/6544/head
Никита Поздняков 2 years ago
parent a5bd6b82d7
commit ff6914ba91
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 25
      apps/block_scout_web/lib/block_scout_web/views/access_helpers.ex
  2. 31
      apps/explorer/lib/explorer/access_helpers.ex
  3. 8
      apps/explorer/lib/explorer/account/notifier/forbidden_address.ex
  4. 6
      config/runtime.exs

@ -8,35 +8,14 @@ defmodule BlockScoutWeb.AccessHelpers do
alias BlockScoutWeb.API.APILogger alias BlockScoutWeb.API.APILogger
alias BlockScoutWeb.API.RPC.RPCView alias BlockScoutWeb.API.RPC.RPCView
alias BlockScoutWeb.WebRouter.Helpers alias BlockScoutWeb.WebRouter.Helpers
alias Explorer.AccessHelpers
alias Explorer.Account.Api.Key, as: ApiKey alias Explorer.Account.Api.Key, as: ApiKey
alias Plug.Conn alias Plug.Conn
alias RemoteIp alias RemoteIp
def restricted_access?(address_hash, params) do def restricted_access?(address_hash, params) do
restricted_list_var = Application.get_env(:block_scout_web, :restricted_list) AccessHelpers.restricted_access?(address_hash, params)
restricted_list = (restricted_list_var && String.split(restricted_list_var, ",")) || []
if Enum.count(restricted_list) > 0 do
formatted_restricted_list =
restricted_list
|> Enum.map(fn addr ->
String.downcase(addr)
end)
formatted_address_hash = String.downcase(address_hash)
address_restricted =
formatted_restricted_list
|> Enum.member?(formatted_address_hash)
key = if params && Map.has_key?(params, "key"), do: Map.get(params, "key"), else: nil
correct_key = key && key == Application.get_env(:block_scout_web, :restricted_list_key)
if address_restricted && !correct_key, do: {:restricted_access, true}, else: {:ok, false}
else
{:ok, false}
end
end end
def get_path(conn, path, template, address_hash) do def get_path(conn, path, template, address_hash) do

@ -0,0 +1,31 @@
defmodule Explorer.AccessHelpers do
@moduledoc """
Helpers to restrict access to some pages filtering by address
"""
def restricted_access?(address_hash, params) do
restricted_list_var = Application.get_env(:explorer, :restricted_list)
restricted_list = (restricted_list_var && String.split(restricted_list_var, ",")) || []
if Enum.count(restricted_list) > 0 do
formatted_restricted_list =
restricted_list
|> Enum.map(fn addr ->
String.downcase(addr)
end)
formatted_address_hash = String.downcase(address_hash)
address_restricted =
formatted_restricted_list
|> Enum.member?(formatted_address_hash)
key = if params && Map.has_key?(params, "key"), do: Map.get(params, "key"), else: nil
correct_key = key && key == Application.get_env(:explorer, :restricted_list_key)
if address_restricted && !correct_key, do: {:restricted_access, true}, else: {:ok, false}
else
{:ok, false}
end
end
end

@ -8,6 +8,7 @@ defmodule Explorer.Account.Notifier.ForbiddenAddress do
"0x000000000000000000000000000000000000dEaD" "0x000000000000000000000000000000000000dEaD"
] ]
alias Explorer.AccessHelpers
alias Explorer.Chain.Token alias Explorer.Chain.Token
alias Explorer.Repo alias Explorer.Repo
@ -20,11 +21,11 @@ defmodule Explorer.Account.Notifier.ForbiddenAddress do
{:error, message} {:error, message}
address_hash -> address_hash ->
check(address_hash) check(address_hash, address_string)
end end
end end
def check(%Explorer.Chain.Hash{} = address_hash) do def check(%Explorer.Chain.Hash{} = address_hash, address_hash_string) do
cond do cond do
address_hash in blacklist() -> address_hash in blacklist() ->
{:error, "This address is blacklisted"} {:error, "This address is blacklisted"}
@ -32,6 +33,9 @@ defmodule Explorer.Account.Notifier.ForbiddenAddress do
is_contract(address_hash) -> is_contract(address_hash) ->
{:error, "This address isn't personal"} {:error, "This address isn't personal"}
match?({:restricted_access, true}, AccessHelpers.restricted_access?(address_hash_string, %{})) ->
{:error, "This address has restricted access"}
address_hash -> address_hash ->
{:ok, address_hash} {:ok, address_hash}
end end

@ -89,8 +89,6 @@ config :block_scout_web,
apps_menu: if(System.get_env("APPS_MENU", "false") == "true", do: true, else: false), apps_menu: if(System.get_env("APPS_MENU", "false") == "true", do: true, else: false),
apps: System.get_env("APPS") || System.get_env("EXTERNAL_APPS"), apps: System.get_env("APPS") || System.get_env("EXTERNAL_APPS"),
gas_price: System.get_env("GAS_PRICE", nil), gas_price: System.get_env("GAS_PRICE", nil),
restricted_list: System.get_env("RESTRICTED_LIST", nil),
restricted_list_key: System.get_env("RESTRICTED_LIST_KEY", nil),
dark_forest_addresses: System.get_env("CUSTOM_CONTRACT_ADDRESSES_DARK_FOREST"), dark_forest_addresses: System.get_env("CUSTOM_CONTRACT_ADDRESSES_DARK_FOREST"),
dark_forest_addresses_v_0_5: System.get_env("CUSTOM_CONTRACT_ADDRESSES_DARK_FOREST_V_0_5"), dark_forest_addresses_v_0_5: System.get_env("CUSTOM_CONTRACT_ADDRESSES_DARK_FOREST_V_0_5"),
circles_addresses: System.get_env("CUSTOM_CONTRACT_ADDRESSES_CIRCLES"), circles_addresses: System.get_env("CUSTOM_CONTRACT_ADDRESSES_CIRCLES"),
@ -217,7 +215,9 @@ config :explorer,
enable_caching_implementation_data_of_proxy: true, enable_caching_implementation_data_of_proxy: true,
avg_block_time_as_ttl_cached_implementation_data_of_proxy: true, avg_block_time_as_ttl_cached_implementation_data_of_proxy: true,
fallback_ttl_cached_implementation_data_of_proxy: :timer.seconds(4), fallback_ttl_cached_implementation_data_of_proxy: :timer.seconds(4),
implementation_data_fetching_timeout: :timer.seconds(2) implementation_data_fetching_timeout: :timer.seconds(2),
restricted_list: System.get_env("RESTRICTED_LIST", nil),
restricted_list_key: System.get_env("RESTRICTED_LIST_KEY", nil)
config :explorer, Explorer.Visualize.Sol2uml, config :explorer, Explorer.Visualize.Sol2uml,
service_url: System.get_env("VISUALIZE_SOL2UML_SERVICE_URL"), service_url: System.get_env("VISUALIZE_SOL2UML_SERVICE_URL"),

Loading…
Cancel
Save