Skip to content

Commit

Permalink
Merge pull request #4443 from brave-intl/feat/nightly-ofac-address-ch…
Browse files Browse the repository at this point in the history
…acker

Nightly job to delete banned addresses
  • Loading branch information
tsmartt authored May 29, 2024
2 parents 9343ee2 + c9c18a6 commit 6beb75d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/jobs/banned_address_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BannedAddressJob < ApplicationJob
queue_as :scheduler

def perform
Wallet::DisconnectInvalidP2pAddressService.build.call
end
end
4 changes: 4 additions & 0 deletions app/models/crypto_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def chain_not_changed?
end
end

def banned_address?
!valid? && errors.full_messages.any? { |err| err.include?(BannedAddressValidator::MSG) }
end

def address_not_changed?
if address_changed? && persisted?
errors.add(:address, "can't be changed")
Expand Down
15 changes: 15 additions & 0 deletions app/services/wallet/disconnect_invalid_p2p_address_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# typed: true

class Wallet::DisconnectInvalidP2pAddressService < BuilderBaseService
def self.build
new
end

def call
banned = CryptoAddress.all.select(&:banned_address?)
banned.each do |address|
SlackMessenger.new(message: "A banned address has been detected in creators: Address #{address.address} on chain #{address.chain} for publisher #{address.publisher.id}", channel: "compliance-bot").perform
end
banned&.destroy_all
end
end
3 changes: 2 additions & 1 deletion app/validators/banned_address_validator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class BannedAddressValidator < ActiveModel::EachValidator
MSG = "can't be a banned address"
def validate_each(record, attribute, value)
banned_addresses = OfacAddress.pluck(:address)

banned_addresses.each do |banned_address|
stripped_ba = banned_address.to_s.strip.downcase
stripped_val = value.to_s.strip.downcase
if (stripped_ba.present? && stripped_val.present?) && (stripped_val.include?(stripped_ba) || stripped_ba.include?(stripped_val))
record.errors.add attribute.to_sym, "can't be a banned address"
record.errors.add attribute.to_sym, MSG
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
cron: "0 1 * * *"
description: "Syncs zendesk tickets"
queue: low
BannedAddressJob:
cron: "0 1 * * *"
description: "Scan For Banned Addresses"
Cache::BrowserChannels::Main:
cron: "0 */2 * * *"
description: "Syncs V4 channels list"
Expand Down
4 changes: 3 additions & 1 deletion test/models/crypto_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
address.address = "totally random string"
address.chain = "ETH"
refute address.valid?
refute address.banned_address?

assert_equal "can't be changed", address.errors.messages[:address][0]
assert_equal "can't be changed", address.errors.messages[:chain][0]
Expand All @@ -23,6 +24,7 @@
address = crypto_addresses(:banned_sol_address)
refute address.valid?

assert address.errors.full_messages.first == "Address can't be a banned address"
assert address.banned_address?
assert address.errors.full_messages.first.include?(BannedAddressValidator::MSG)
end
end

0 comments on commit 6beb75d

Please sign in to comment.