2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
require "screening_model"
|
2013-10-21 14:49:51 -04:00
|
|
|
|
|
|
|
# A ScreenedIpAddress record represents an IP address or subnet that is being watched,
|
|
|
|
# and possibly blocked from creating accounts.
|
|
|
|
class ScreenedIpAddress < ActiveRecord::Base
|
|
|
|
include ScreeningModel
|
|
|
|
|
|
|
|
default_action :block
|
|
|
|
|
2013-10-22 16:30:30 -04:00
|
|
|
validates :ip_address, ip_address_format: true, presence: true
|
2022-12-06 06:09:38 -05:00
|
|
|
after_validation :check_for_match, if: :will_save_change_to_ip_address?
|
2013-10-21 14:49:51 -04:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
ROLLED_UP_BLOCKS = [
|
|
|
|
# IPv4
|
|
|
|
[4, 32, 24],
|
|
|
|
# IPv6
|
|
|
|
[6, (65..128).to_a, 64],
|
|
|
|
[6, 64, 60],
|
|
|
|
[6, 60, 56],
|
|
|
|
[6, 56, 52],
|
|
|
|
[6, 52, 48],
|
|
|
|
]
|
|
|
|
|
2013-10-21 14:49:51 -04:00
|
|
|
def self.watch(ip_address, opts = {})
|
|
|
|
match_for_ip_address(ip_address) ||
|
|
|
|
create(opts.slice(:action_type).merge(ip_address: ip_address))
|
|
|
|
end
|
|
|
|
|
2018-03-19 14:34:43 -04:00
|
|
|
def check_for_match
|
2024-05-27 06:27:13 -04:00
|
|
|
if self.errors[:ip_address].blank?
|
2018-03-19 14:34:43 -04:00
|
|
|
matched = self.class.match_for_ip_address(self.ip_address)
|
|
|
|
if matched && matched.action_type == self.action_type
|
|
|
|
self.errors.add(:ip_address, :ip_address_already_screened)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-05 11:24:13 -05:00
|
|
|
# In Rails 4.0.0, validators are run to handle invalid assignments to inet columns (as they should).
|
|
|
|
# In Rails 4.0.1, an exception is raised before validation happens, so we need this hack for
|
|
|
|
# inet/cidr columns:
|
2013-11-04 18:32:35 -05:00
|
|
|
def ip_address=(val)
|
2014-02-18 13:00:46 -05:00
|
|
|
if val.nil?
|
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-02-10 13:38:59 -05:00
|
|
|
if val.is_a?(IPAddr)
|
2014-02-18 13:00:46 -05:00
|
|
|
write_attribute(:ip_address, val)
|
2015-02-10 13:38:59 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
v = IPAddr.handle_wildcards(val)
|
|
|
|
|
|
|
|
if v.nil?
|
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
return
|
2014-02-18 13:00:46 -05:00
|
|
|
end
|
2013-11-17 17:31:44 -05:00
|
|
|
|
2015-02-10 13:38:59 -05:00
|
|
|
write_attribute(:ip_address, v)
|
|
|
|
|
2013-11-17 17:31:44 -05:00
|
|
|
# this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0
|
|
|
|
# handle both exceptions
|
|
|
|
rescue ArgumentError, IPAddr::InvalidAddressError
|
2013-11-04 18:32:35 -05:00
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
end
|
|
|
|
|
2014-02-18 10:33:08 -05:00
|
|
|
# Return a string with the ip address and mask in standard format. e.g., "127.0.0.0/8".
|
|
|
|
def ip_address_with_mask
|
2014-11-21 12:16:06 -05:00
|
|
|
ip_address.try(:to_cidr_s)
|
2014-02-18 10:33:08 -05:00
|
|
|
end
|
|
|
|
|
2013-10-21 14:49:51 -04:00
|
|
|
def self.match_for_ip_address(ip_address)
|
|
|
|
# The <<= operator on inet columns means "is contained within or equal to".
|
|
|
|
#
|
|
|
|
# Read more about PostgreSQL's inet data type here:
|
|
|
|
#
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/datatype-net-types.html
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/functions-net.html
|
2022-04-12 14:07:37 -04:00
|
|
|
ip_address = IPAddr === ip_address ? ip_address.to_cidr_s : ip_address.to_s
|
|
|
|
order("masklen(ip_address) DESC").find_by("? <<= ip_address", ip_address)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.should_block?(ip_address)
|
2013-10-23 17:11:11 -04:00
|
|
|
exists_for_ip_address_and_action?(ip_address, actions[:block])
|
|
|
|
end
|
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
def self.is_allowed?(ip_address)
|
2013-10-23 17:11:11 -04:00
|
|
|
exists_for_ip_address_and_action?(ip_address, actions[:do_nothing])
|
|
|
|
end
|
|
|
|
|
2014-09-04 18:50:27 -04:00
|
|
|
def self.exists_for_ip_address_and_action?(ip_address, action_type, opts = {})
|
2013-10-21 14:49:51 -04:00
|
|
|
b = match_for_ip_address(ip_address)
|
2022-01-11 02:16:51 -05:00
|
|
|
found = !!b && b.action_type == action_type
|
|
|
|
b.record_match! if found && opts[:record_match] != false
|
2014-09-04 18:50:27 -04:00
|
|
|
found
|
|
|
|
end
|
|
|
|
|
2015-03-02 12:13:10 -05:00
|
|
|
def self.block_admin_login?(user, ip_address)
|
2020-07-26 20:23:54 -04:00
|
|
|
return false unless SiteSetting.use_admin_ip_allowlist
|
2014-09-04 18:50:27 -04:00
|
|
|
return false if user.nil?
|
|
|
|
return false if !user.admin?
|
|
|
|
return false if ScreenedIpAddress.where(action_type: actions[:allow_admin]).count == 0
|
|
|
|
return true if ip_address.nil?
|
|
|
|
!exists_for_ip_address_and_action?(ip_address, actions[:allow_admin], record_match: false)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
2015-03-09 13:55:17 -04:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
def self.subnets(family, from_masklen, to_masklen)
|
|
|
|
sql = <<~SQL
|
|
|
|
WITH ips_and_subnets AS (
|
|
|
|
SELECT ip_address,
|
|
|
|
network(inet(host(ip_address) || '/' || :to_masklen))::text subnet
|
2015-03-09 13:55:17 -04:00
|
|
|
FROM screened_ip_addresses
|
2022-04-12 14:07:37 -04:00
|
|
|
WHERE family(ip_address) = :family AND
|
|
|
|
masklen(ip_address) IN (:from_masklen) AND
|
|
|
|
action_type = :blocked
|
2015-03-09 13:55:17 -04:00
|
|
|
)
|
2022-04-12 14:07:37 -04:00
|
|
|
SELECT subnet
|
|
|
|
FROM ips_and_subnets
|
|
|
|
GROUP BY subnet
|
|
|
|
HAVING COUNT(*) >= :min_ban_entries_for_roll_up
|
2015-03-09 13:55:17 -04:00
|
|
|
SQL
|
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
DB.query_single(
|
|
|
|
sql,
|
|
|
|
family: family,
|
|
|
|
from_masklen: from_masklen,
|
|
|
|
to_masklen: to_masklen,
|
|
|
|
blocked: ScreenedIpAddress.actions[:block],
|
|
|
|
min_ban_entries_for_roll_up: SiteSetting.min_ban_entries_for_roll_up,
|
|
|
|
)
|
2015-03-09 13:55:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.roll_up(current_user = Discourse.system_user)
|
2022-04-12 14:07:37 -04:00
|
|
|
ROLLED_UP_BLOCKS.each do |family, from_masklen, to_masklen|
|
|
|
|
ScreenedIpAddress
|
|
|
|
.subnets(family, from_masklen, to_masklen)
|
|
|
|
.map do |subnet|
|
|
|
|
next if ScreenedIpAddress.where("? <<= ip_address", subnet).exists?
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
old_ips =
|
|
|
|
ScreenedIpAddress
|
|
|
|
.where(action_type: ScreenedIpAddress.actions[:block])
|
|
|
|
.where("ip_address << ?", subnet)
|
|
|
|
.where("family(ip_address) = ?", family)
|
|
|
|
.where("masklen(ip_address) IN (?)", from_masklen)
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
sum_match_count, max_last_match_at, min_created_at =
|
2023-02-12 23:39:45 -05:00
|
|
|
old_ips.pick("SUM(match_count), MAX(last_match_at), MIN(created_at)")
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
ScreenedIpAddress.create!(
|
|
|
|
ip_address: subnet,
|
|
|
|
match_count: sum_match_count,
|
|
|
|
last_match_at: max_last_match_at,
|
|
|
|
created_at: min_created_at,
|
|
|
|
)
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
StaffActionLogger.new(current_user).log_roll_up(subnet, old_ips.map(&:ip_address))
|
|
|
|
old_ips.delete_all
|
|
|
|
end
|
2015-03-09 13:55:17 -04:00
|
|
|
end
|
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
2013-12-05 01:40:35 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: screened_ip_addresses
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# ip_address :inet not null
|
|
|
|
# action_type :integer not null
|
|
|
|
# match_count :integer default(0), not null
|
|
|
|
# last_match_at :datetime
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-12-05 01:40:35 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_screened_ip_addresses_on_ip_address (ip_address) UNIQUE
|
|
|
|
# index_screened_ip_addresses_on_last_match_at (last_match_at)
|
|
|
|
#
|