Skip to content

Commit

Permalink
Update IP mapping in User spam scorer
Browse files Browse the repository at this point in the history
Mapping values from the config in the initializer isn't tested. Lets map
values to `IPAddr` instances when we use them.
  • Loading branch information
gbp committed Sep 28, 2023
1 parent 40ce318 commit 2178f0f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 9 deletions.
7 changes: 0 additions & 7 deletions config/initializers/user_spam_scorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
if File.exist?(path)
settings = YAML.load(File.read(path))['user_spam_scorer']
settings.each do |key, value|
case key
when 'ip_ranges'
value = value.map { |v| IPAddr.new(v) }
when /_format/
raise "UserSpamScorer: Can't load Regexp from YAML file"
end

UserSpamScorer.public_send("#{key}=", value)
end
end
2 changes: 1 addition & 1 deletion lib/user_spam_scorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def user_agent_is_suspicious?(user)

def ip_range_is_suspicious?(user)
return false unless user.respond_to?(:ip)
suspicious_ip_ranges.any? { |range| range.include?(user.ip) }
suspicious_ip_ranges.any? { |range| IPAddr.new(range).include?(user.ip) }
end

# TODO: Akismet thinks user is spam
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/user_spam_scorer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@

describe '#ip_range_is_suspicious?' do

before { UserSpamScorer.suspicious_ip_ranges = [IPAddr.new('127.0.0.0/8')] }
before { UserSpamScorer.suspicious_ip_ranges = ['127.0.0.0/8'] }
after { UserSpamScorer.reset }

it 'is true if the IP address is within a suspicious range' do
Expand Down

0 comments on commit 2178f0f

Please sign in to comment.