vpndetection 5.21.1 → 5.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bcfc8be80ffb743237b0d1c75aee757d77b5dd9cb9279b7c5a595d5c7957e3c
4
- data.tar.gz: 652db98f92868e0e871cf7e5a1cdc876c80c998682506aa04d4449b01274f24e
3
+ metadata.gz: 93b336a33a0b11e6b05b319a3a0acfa80a4db7e99fb26bc7c8b761ab0d854585
4
+ data.tar.gz: f563eaba0e2128d8a58a132bbb87f40de81d9ae15ee05cc69d39ed708ccd02fc
5
5
  SHA512:
6
- metadata.gz: 454249f5a1a7d1f8004ad84fb914a4cab054d688e76937f330e3777a2ee1070bfdd59f57b8be7d6742e06b1fd401cf3e6ac654e0700cda5be094416d0f0e6a75
7
- data.tar.gz: 0bdf23dbcbc3fca339bbbcc9c187e44cd440059ec687d6bce5e9b03504638d59c86a70a439b412d5a6d1f176e30129c697d273a0542a49025e52577539bd6bb1
6
+ metadata.gz: 727dd2f5e5c98b15752658ccf6d6cbd4062dade648d8031ebd333cf49adb21a7ca6a201bbcc98b2bc8f31c256addf6bec18d2b4f10c622e18578fb3f625d0461
7
+ data.tar.gz: acf92ab25d763f6abe8f993a118a97d503a2ea60cae040902efcd2c35267e2a736a331abb8c4897d1d37e097b6fc7a607143651558f8d5fdb1c77a5349d79630
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'digest'
4
+ require 'lru_redux'
5
+
3
6
  require_relative 'condition'
7
+ require_relative 'crawlers'
4
8
 
5
9
  module VPNDetection
6
10
  # The framework-agnostic half of a web middleware: resolve a client address,
@@ -27,6 +31,25 @@ module VPNDetection
27
31
  DEFAULT_TIMEOUT = 2.5
28
32
  DEFAULT_RETRIES = 0
29
33
 
34
+ # The most crawler agents one address is looked up with while it is remembered.
35
+ MAX_AGENTS_PER_ADDRESS = 4
36
+
37
+ # How long an address's crawler agents are remembered, from its first one: the
38
+ # client's default cache TTL.
39
+ AGENTS_TTL = 60 * 60
40
+
41
+ # How many addresses' agents are remembered at once.
42
+ AGENTS_MAX_ADDRESSES = 10_000
43
+ private_constant :AGENTS_TTL, :AGENTS_MAX_ADDRESSES
44
+
45
+ # Whether a `User-Agent` names a crawler, the only kind of agent whose verdict
46
+ # can be `verified` or `spoofed`. It errs toward a match, which costs a lookup.
47
+ # Case is folded for A to Z alone, as the corpus's terms are written.
48
+ def self.names_crawler?(agent)
49
+ lower = agent.b.downcase(:ascii)
50
+ Crawlers::TERMS.any? { |term| lower.include?(term) }
51
+ end
52
+
30
53
  # Reads a request's `User-Agent` where the adapter names no reader: Rack's
31
54
  # request has `user_agent`, and so do Sinatra's and Hanami's.
32
55
  DEFAULT_USER_AGENT = ->(request) { request.user_agent if request.respond_to?(:user_agent) }
@@ -38,18 +61,23 @@ module VPNDetection
38
61
  # @param user_agent_of [#call] reads the request's own `User-Agent`, which
39
62
  # `allow_bots: true` sends with the lookup.
40
63
  # @option options [Boolean] :allow_bots (false) send the request's own
41
- # `User-Agent` with its lookup, and never refuse a request whose answer
42
- # says `user_agent.verdict` is `verified`, whatever the condition
43
- # matches; the answer is still attached. An absent or empty header sends
44
- # none. A keyless client, or a free key past its trial, never gets a
45
- # verdict, so nothing is let through there, and that is warned of once
46
- # under `on_missing_field`.
64
+ # `User-Agent` with its lookup when it names a crawler, and never refuse
65
+ # a request whose answer says `user_agent.verdict` is `verified`,
66
+ # whatever the condition matches; the answer is still attached. Any other
67
+ # agent, an absent or an empty header sends none, since the verdict could
68
+ # only be `not_bot`. One address is looked up with at most
69
+ # {MAX_AGENTS_PER_ADDRESS} crawler agents an hour: past those, the lookup
70
+ # sends none, and the request passes as a crawler only where the answer's
71
+ # `bot.verified` is true. A keyless client, or a free key past its trial,
72
+ # never gets a verdict, so nothing is let through there, and that is
73
+ # warned of once under `on_missing_field`.
47
74
  def initialize(default_ip_selector, user_agent_of = DEFAULT_USER_AGENT, **options)
48
75
  Condition.validate!(options[:block_condition])
49
76
  @condition = options[:block_condition]
50
77
  @selector = options[:ip_selector] || default_ip_selector
51
78
  @allow_bots = options.fetch(:allow_bots, false) == true
52
79
  @user_agent_of = user_agent_of
80
+ @agents = @allow_bots ? CrawlerAgents.new : nil
53
81
  @fail_closed = options.fetch(:fail_closed, false)
54
82
  @on_missing_field = options.fetch(:on_missing_field, :warn)
55
83
  @skip = options[:skip]
@@ -92,13 +120,14 @@ module VPNDetection
92
120
  end
93
121
 
94
122
  # A bogon is answered locally, so nothing is sent for it.
95
- agent = @allow_bots && !Bogon.bogon?(ip) ? user_agent(request) : nil
123
+ claim = @allow_bots && !Bogon.bogon?(ip) ? claim_agent(ip, request) : nil
124
+ agent = claim&.agent
96
125
  begin
97
126
  result = @client.lookup(ip, retries: @retries, **(agent ? { user_agent: agent } : {}))
98
127
  rescue VPNDetection::Error => e
99
128
  return Lookup.new(blocked: @fail_closed, ip: ip, error: e)
100
129
  end
101
- decide(ip, result, agent)
130
+ decide(ip, result, claim)
102
131
  end
103
132
 
104
133
  private
@@ -114,37 +143,47 @@ module VPNDetection
114
143
  )
115
144
  end
116
145
 
117
- # The request's own `User-Agent`, or nil when it sent none or an empty one.
118
- def user_agent(request)
146
+ # What this request's agent comes to: nil when it names no crawler (or sent
147
+ # none), else a claim whose agent is the one to send, or nil past the
148
+ # address's bound.
149
+ def claim_agent(ip, request)
119
150
  agent = @user_agent_of.call(request)
120
- agent.is_a?(String) && !agent.empty? ? agent : nil
151
+ return nil unless agent.is_a?(String) && !agent.empty? && Middleware.names_crawler?(agent)
152
+
153
+ Claim.new(@agents.admit(ip, agent))
121
154
  end
122
155
 
123
- def decide(ip, result, agent)
124
- report_unanswered_agent(result) unless agent.nil?
156
+ def decide(ip, result, claim)
157
+ report_unanswered_agent(result) unless claim&.agent.nil?
125
158
  return Lookup.new(blocked: false, ip: ip, result: result) if @condition.nil?
126
159
 
127
- report_missing(result)
160
+ report_missing(result, sent: !claim&.agent.nil?)
128
161
  Lookup.new(
129
- blocked: Condition.matches?(@condition, result) && !verified?(result),
162
+ blocked: Condition.matches?(@condition, result) && !verified?(result, claim),
130
163
  ip: ip,
131
164
  result: result
132
165
  )
133
166
  end
134
167
 
135
168
  # Whether `allow_bots` lets this answer through: its agent's operator
136
- # vouches for the address.
137
- def verified?(result)
138
- @allow_bots && result.user_agent.is_a?(Hash) && result.user_agent['verdict'] == 'verified'
169
+ # vouches for the address, or, past the bound, the answer says a crawler's
170
+ # operator does.
171
+ def verified?(result, claim)
172
+ return false if claim.nil?
173
+ return result.bot.is_a?(Hash) && result.bot['verified'] == true if claim.agent.nil?
174
+
175
+ result.user_agent.is_a?(Hash) && result.user_agent['verdict'] == 'verified'
139
176
  end
140
177
 
141
- def report_missing(result)
178
+ def report_missing(result, sent:)
142
179
  return if @on_missing_field == :ignore
143
180
  # A bogon is answered locally in the widest shape, which carries every
144
181
  # member but `user_agent`, and no agent is sent for it.
145
182
  return if result.bogon?
146
183
 
147
184
  missing = Condition.missing_members(@condition, result)
185
+ # A request that sent no agent has no verdict to lack, whatever the plan serves.
186
+ missing -= ['user_agent'] if @allow_bots && !sent
148
187
  return if missing.empty?
149
188
 
150
189
  message = "block_condition names #{missing.join(', ')}, which your plan does not " \
@@ -183,6 +222,42 @@ module VPNDetection
183
222
  end
184
223
  end
185
224
 
225
+ # A crawler's agent as one request makes it: `agent` is what the lookup sends,
226
+ # nil once the address is past its bound.
227
+ Claim = Struct.new(:agent)
228
+ private_constant :Claim
229
+
230
+ # The crawler agents each address was looked up with. An address claiming more
231
+ # than MAX_AGENTS_PER_ADDRESS of them in an hour sends none past those, so a
232
+ # visitor inventing an agent per request spends at most that many lookups.
233
+ #
234
+ # An agent is kept as its digest, so what an address costs in memory is
235
+ # bounded whatever length of header it sends. Adding one changes the stored
236
+ # list in place, which leaves the address's hour running from its first.
237
+ class CrawlerAgents
238
+ def initialize
239
+ @seen = LruRedux::TTL::Cache.new(AGENTS_MAX_ADDRESSES, AGENTS_TTL)
240
+ @lock = Mutex.new
241
+ end
242
+
243
+ # The agent to send for `ip`, or nil once the address has its bound.
244
+ def admit(ip, agent)
245
+ digest = Digest::SHA256.digest(agent)
246
+ @lock.synchronize do
247
+ known = @seen[ip]
248
+ if known.nil?
249
+ @seen[ip] = [digest]
250
+ elsif !known.include?(digest)
251
+ return nil if known.length >= MAX_AGENTS_PER_ADDRESS
252
+
253
+ known << digest
254
+ end
255
+ agent
256
+ end
257
+ end
258
+ end
259
+ private_constant :CrawlerAgents
260
+
186
261
  # The shared client-address selectors, bound to one framework's request type.
187
262
  #
188
263
  # There is no portable default: a framework's own accessor may return the
@@ -0,0 +1,74 @@
1
+ # Generated. Do not edit: a regeneration overwrites it.
2
+ # Lowercase substrings, one of which a crawler's ASCII-lowercased User-Agent contains.
3
+
4
+ module VPNDetection
5
+ module Middleware
6
+ module Crawlers
7
+ TERMS = [
8
+ '+http',
9
+ 'ahrefssiteaudit',
10
+ 'amzn-user',
11
+ 'anthropic-ai',
12
+ 'barkrowler',
13
+ 'better stack',
14
+ 'betterstack',
15
+ 'bingpreview',
16
+ 'bot',
17
+ 'censysinspect',
18
+ 'chatgpt-user',
19
+ 'claude-user',
20
+ 'claude-web',
21
+ 'cohere-ai',
22
+ 'crawl',
23
+ 'datadogsynthetics',
24
+ 'embedly',
25
+ 'expanse',
26
+ 'facebookcatalog',
27
+ 'facebookexternalhit',
28
+ 'flipboardproxy',
29
+ 'flipboardrss',
30
+ 'github-hookshot/',
31
+ 'google-agent',
32
+ 'google-extended',
33
+ 'google-inspectiontool',
34
+ 'google-read-aloud',
35
+ 'google-site-verification',
36
+ 'googleother',
37
+ 'iframely',
38
+ 'internetmeasurement',
39
+ 'keenable-user',
40
+ 'lumar',
41
+ 'masscan',
42
+ 'mediapartners-google',
43
+ 'meta-externalagent',
44
+ 'meta-externalfetcher',
45
+ 'meta-webindexer',
46
+ 'mistralai-user',
47
+ 'nmap',
48
+ 'notebooklm',
49
+ 'nuclei',
50
+ 'paloaltonetworks.com',
51
+ 'parse.ly',
52
+ 'parsely.com',
53
+ 'perplexity-user',
54
+ 'pingdom',
55
+ 'pinterest',
56
+ 'rssapi/',
57
+ 'screaming frog',
58
+ 'site24x7',
59
+ 'skypeuripreview',
60
+ 'slack-imgproxy',
61
+ 'slurp',
62
+ 'sogou',
63
+ 'spider',
64
+ 'statuscake',
65
+ 'stripe/1.0',
66
+ 'ttd-content',
67
+ 'whatsapp',
68
+ 'yandex',
69
+ 'yeti/',
70
+ 'zgrab',
71
+ ].freeze
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VPNDetection
4
- VERSION = '5.21.1'
4
+ VERSION = '5.22.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vpndetection
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.21.1
4
+ version: 5.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mslm Dev
@@ -68,6 +68,7 @@ files:
68
68
  - lib/vpndetection/middleware.rb
69
69
  - lib/vpndetection/middleware/condition.rb
70
70
  - lib/vpndetection/middleware/core.rb
71
+ - lib/vpndetection/middleware/crawlers.rb
71
72
  - lib/vpndetection/models/abuse_contact.rb
72
73
  - lib/vpndetection/models/account_create_apikey_request.rb
73
74
  - lib/vpndetection/models/account_created_apikey.rb