vpndetection 5.21.1 → 5.22.0

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: 1c158a3f311984225a01173fe299a0d24a410e857ce63c1bbb0537aaaa97c7e6
4
+ data.tar.gz: febea3b0b3665774985825d2012eefe3b1940f881c978e80d354c21c6d30878a
5
5
  SHA512:
6
- metadata.gz: 454249f5a1a7d1f8004ad84fb914a4cab054d688e76937f330e3777a2ee1070bfdd59f57b8be7d6742e06b1fd401cf3e6ac654e0700cda5be094416d0f0e6a75
7
- data.tar.gz: 0bdf23dbcbc3fca339bbbcc9c187e44cd440059ec687d6bce5e9b03504638d59c86a70a439b412d5a6d1f176e30129c697d273a0542a49025e52577539bd6bb1
6
+ metadata.gz: 593d2fc397c7edb94ac7b8df2aae18adb462eb338d9f390139560635ff7e097a8b2a6009124e05cafdaa29c592dd0710b7e1038cc9bafdcf11b0a00eaa88b8f5
7
+ data.tar.gz: 6cc95f71d88da71662ccd04ed0fb2cf10e46be2c069574d1432b1544d17ac5a280e09b05a34485ac21c4cd105432fd5ad105cf9d786d2f99e1cea021a541890d
@@ -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,28 +143,36 @@ 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
160
  report_missing(result)
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
178
  def report_missing(result)
@@ -183,6 +220,42 @@ module VPNDetection
183
220
  end
184
221
  end
185
222
 
223
+ # A crawler's agent as one request makes it: `agent` is what the lookup sends,
224
+ # nil once the address is past its bound.
225
+ Claim = Struct.new(:agent)
226
+ private_constant :Claim
227
+
228
+ # The crawler agents each address was looked up with. An address claiming more
229
+ # than MAX_AGENTS_PER_ADDRESS of them in an hour sends none past those, so a
230
+ # visitor inventing an agent per request spends at most that many lookups.
231
+ #
232
+ # An agent is kept as its digest, so what an address costs in memory is
233
+ # bounded whatever length of header it sends. Adding one changes the stored
234
+ # list in place, which leaves the address's hour running from its first.
235
+ class CrawlerAgents
236
+ def initialize
237
+ @seen = LruRedux::TTL::Cache.new(AGENTS_MAX_ADDRESSES, AGENTS_TTL)
238
+ @lock = Mutex.new
239
+ end
240
+
241
+ # The agent to send for `ip`, or nil once the address has its bound.
242
+ def admit(ip, agent)
243
+ digest = Digest::SHA256.digest(agent)
244
+ @lock.synchronize do
245
+ known = @seen[ip]
246
+ if known.nil?
247
+ @seen[ip] = [digest]
248
+ elsif !known.include?(digest)
249
+ return nil if known.length >= MAX_AGENTS_PER_ADDRESS
250
+
251
+ known << digest
252
+ end
253
+ agent
254
+ end
255
+ end
256
+ end
257
+ private_constant :CrawlerAgents
258
+
186
259
  # The shared client-address selectors, bound to one framework's request type.
187
260
  #
188
261
  # 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.0'
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.0
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