<commit_before># frozen_string_literal: true

BLOCK_USER_AGENTS = %w(
  BLEXBot
).freeze

BLOCK_IPS = %w(
  192.168.3.11/16
  192.168.3.11/24
  172.16.17.32/16
).freeze

def remote_ip(req)
  @remote_ip ||= (req.env["action_dispatch.remote_ip"] || req.ip).to_s
end

def include_ua?(req)
  BLOCK_USER_AGENTS.any? { |ua| req.user_agent.include?(ua) }
end

def include_ip?(req)
  BLOCK_IPS.any? { |ip| IPAddr.new(ip).include?(remote_ip(req)) }
end

Rack::Attack.blocklist("block all access from specific user agents and ips") do |req|
  include_ua?(req) || include_ip?(req)
end

Rack::Attack.throttle("requests by ip", limit: 5, period: 2.seconds) do |req|
  remote_ip(req)
end

ActiveSupport::Notifications.subscribe(/rack_attack/) do |_name, _start, _finish, _request_id, payload|
  req = payload[:request]

  next unless %i(throttle blacklist).include? req.env["rack.attack.match_type"]

  Rails.logger.info("Rate limit hit (#{req.env['rack.attack.match_type']}): #{remote_ip(req)} #{req.request_method} #{req.fullpath}")
end<commit_msg>Fix undefined method error<commit_after># frozen_string_literal: true

BLOCK_USER_AGENTS = %w(
  BLEXBot
).freeze

BLOCK_IPS = %w(
  192.168.3.11/16
  192.168.3.11/24
  172.16.17.32/16
).freeze

def remote_ip(req)
  @remote_ip ||= (req.env["action_dispatch.remote_ip"] || req.ip).to_s
end

def include_ua?(req)
  BLOCK_USER_AGENTS.any? { |ua| req.user_agent&.include?(ua) }
end

def include_ip?(req)
  BLOCK_IPS.any? { |ip| IPAddr.new(ip).include?(remote_ip(req)) }
end

Rack::Attack.blocklist("block all access from specific user agents and ips") do |req|
  include_ua?(req) || include_ip?(req)
end

Rack::Attack.throttle("requests by ip", limit: 5, period: 2.seconds) do |req|
  remote_ip(req)
end

ActiveSupport::Notifications.subscribe(/rack_attack/) do |_name, _start, _finish, _request_id, payload|
  req = payload[:request]

  next unless %i(throttle blacklist).include? req.env["rack.attack.match_type"]

  Rails.logger.info("Rate limit hit (#{req.env['rack.attack.match_type']}): #{remote_ip(req)} #{req.request_method} #{req.fullpath}")
end
{'fraction_non_alphanumeric': 0.1644295302013423, 'fraction_numerical': 0.03355704697986577, 'mean_word_length': 5.362804878048781, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '909422', 'n_tokens_mistral': 869, 'n_tokens_neox': 814, 'n_words': 158}